~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/sockets/test_sockets_select_server_down_client.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <errno.h>
 
2
#include <sys/types.h>
 
3
#include <sys/socket.h>
 
4
#include <netinet/in.h>
 
5
#include <arpa/inet.h>
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
#include <unistd.h>
 
10
#include <fcntl.h>
 
11
#include <sys/ioctl.h>
 
12
#include <assert.h>
 
13
#if EMSCRIPTEN
 
14
#include <emscripten.h>
 
15
#endif
 
16
 
 
17
#define EXPECTED_BYTES 5
 
18
 
 
19
int sockfd = -1;
 
20
 
 
21
void finish(int result) {
 
22
  close(sockfd);
 
23
#if EMSCRIPTEN
 
24
  REPORT_RESULT();
 
25
#endif
 
26
  exit(result);
 
27
}
 
28
 
 
29
void iter(void *arg) {
 
30
  static int retries = 0;
 
31
 
 
32
  fd_set sett;
 
33
  FD_ZERO(&sett);
 
34
  FD_SET(sockfd, &sett);
 
35
  
 
36
  // currently, we've connected to a closed server port.
 
37
  // the initial async connect "succeeded" and select
 
38
  // should say that the socket is ready for a non-blocking
 
39
  // read, however, the read should be 0 sized signalling
 
40
  // that the remote end has closed.
 
41
  int handles = select(64, &sett, NULL, NULL, NULL);
 
42
  if (handles == -1) {
 
43
    perror("select failed");
 
44
    finish(EXIT_FAILURE);
 
45
  }
 
46
 
 
47
  if (FD_ISSET(sockfd, &sett)) {
 
48
    char buffer[1024];
 
49
    int n = recv(sockfd, buffer, sizeof(buffer), 0);
 
50
    if (n == -1 && retries++ > 10) {
 
51
      perror("recv failed");
 
52
      finish(EXIT_FAILURE);
 
53
    } else if (!n) {
 
54
      perror("Connection to websocket server failed as expected.");
 
55
      finish(266);
 
56
    }
 
57
  }
 
58
}
 
59
 
 
60
// This is for testing a websocket connection to a closed server port.
 
61
// The connect call will succeed (due to the asynchronous websocket
 
62
// behavior) but once the underlying websocket system realized that 
 
63
// the connection cannot be established, the next select call will fail.
 
64
int main() {
 
65
  struct sockaddr_in addr;
 
66
  int res;
 
67
 
 
68
  sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
69
  if (sockfd == -1) {
 
70
    perror("cannot create socket");
 
71
    finish(EXIT_FAILURE);
 
72
  }
 
73
  fcntl(sockfd, F_SETFL, O_NONBLOCK);
 
74
 
 
75
  memset(&addr, 0, sizeof(addr));
 
76
  addr.sin_family = AF_INET;
 
77
  addr.sin_port = htons(SOCKK);
 
78
  if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) {
 
79
    perror("inet_pton failed");
 
80
    finish(EXIT_FAILURE);
 
81
  }
 
82
 
 
83
  // This call should succeed (even if the server port is closed)
 
84
  res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
 
85
  if (res == -1 && errno != EINPROGRESS) {
 
86
    perror("connect failed");
 
87
    finish(EXIT_FAILURE);
 
88
  }
 
89
 
 
90
#if EMSCRIPTEN
 
91
  emscripten_set_main_loop(iter, 0, 0);
 
92
#else
 
93
  while (1) iter(NULL);
 
94
#endif
 
95
  
 
96
  return EXIT_FAILURE;
 
97
}
 
98