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

« back to all changes in this revision

Viewing changes to tests/websockets_select_server_closes_connection.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 <sys/ioctl.h>
11
 
#include <assert.h>
12
 
#if EMSCRIPTEN
13
 
#include <emscripten.h>
14
 
#endif
15
 
 
16
 
#define EXPECTED_BYTES 5
17
 
 
18
 
int SocketFD;
19
 
 
20
 
int done = 0;
21
 
 
22
 
void iter(void *arg) {  
23
 
  static char readbuf[1024];
24
 
  static int readPos = 0;
25
 
  
26
 
  fd_set sett;
27
 
  FD_ZERO(&sett);
28
 
  FD_SET(SocketFD, &sett);
29
 
  
30
 
  if( readPos < 7 ){
31
 
    // still reading
32
 
    int selectRes = select(64, &sett, NULL, NULL, NULL);
33
 
    
34
 
    if( selectRes == 0 )
35
 
      return;
36
 
    
37
 
    if( selectRes == -1 ){
38
 
      perror( "Connection to websocket server failed" );
39
 
      exit(EXIT_FAILURE);
40
 
    }
41
 
    if( selectRes > 0 ){
42
 
      assert(FD_ISSET(SocketFD, &sett));
43
 
      
44
 
      int bytesRead = recv( SocketFD, readbuf+readPos, 7-readPos, 0 );
45
 
      readPos += bytesRead;
46
 
    }
47
 
  } else {
48
 
    // here the server should have closed the connection
49
 
    int selectRes = select(64, &sett, NULL, NULL, NULL);
50
 
    
51
 
    if( selectRes == 0 )
52
 
      return;
53
 
    
54
 
    if( selectRes == -1 ){
55
 
      perror( "Connection to websocket server failed as expected" );
56
 
      int result = 266;
57
 
      REPORT_RESULT();
58
 
      emscripten_cancel_main_loop();
59
 
      done = 1;
60
 
    } 
61
 
    
62
 
    if( selectRes > 0 ){
63
 
      printf( "Error: socket should not show up on select call anymore.\n" );
64
 
      exit(EXIT_FAILURE);
65
 
    }     
66
 
  }
67
 
  
68
 
  return;
69
 
}
70
 
 
71
 
// Scenario: the server sends data and closes the connection after 7 bytes. 
72
 
// This test should provoke the situation in which the underlying 
73
 
// tcp connection has been torn down already but there is still data 
74
 
// in the inQueue. The select call has to succeed as long the queue
75
 
// still contains data and only then start to throw errors.
76
 
int main(void)
77
 
{
78
 
  struct sockaddr_in stSockAddr;
79
 
  int Res;
80
 
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
81
 
 
82
 
  if (-1 == SocketFD)
83
 
  {
84
 
    perror("cannot create socket");
85
 
    exit(EXIT_FAILURE);
86
 
  }
87
 
 
88
 
  memset(&stSockAddr, 0, sizeof(stSockAddr));
89
 
 
90
 
  stSockAddr.sin_family = AF_INET;
91
 
  stSockAddr.sin_port = htons(
92
 
#if EMSCRIPTEN
93
 
    8995
94
 
#else
95
 
    8994
96
 
#endif
97
 
  );
98
 
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
99
 
 
100
 
  if (0 > Res) {
101
 
    perror("error: first parameter is not a valid address family");
102
 
    close(SocketFD);
103
 
    exit(EXIT_FAILURE);
104
 
  } else if (0 == Res) {
105
 
    perror("char string (second parameter does not contain valid ipaddress)");
106
 
    close(SocketFD);
107
 
    exit(EXIT_FAILURE);
108
 
  }
109
 
 
110
 
  // This call should succeed (even if the server port is closed)
111
 
  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
112
 
    perror("connect failed");
113
 
    close(SocketFD);
114
 
    exit(EXIT_FAILURE);
115
 
 
116
 
  }
117
 
 
118
 
#if EMSCRIPTEN
119
 
  emscripten_set_main_loop(iter, 0, 0);
120
 
#else
121
 
  while (!done) iter(NULL);
122
 
#endif
123
 
 
124
 
  return EXIT_SUCCESS;
125
 
}
126