aboutsummaryrefslogtreecommitdiff
path: root/tests/websockets_select_server_closes_connection.c
blob: 6ce6d311c5e3cc54b9caecd5cd1505c0aea73e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <assert.h>
#if EMSCRIPTEN
#include <emscripten.h>
#endif

#define EXPECTED_BYTES 5

int SocketFD;

int done = 0;

void iter(void *arg) {  
  static char readbuf[1024];
  static int readPos = 0;
  
  fd_set sett;
  FD_ZERO(&sett);
  FD_SET(SocketFD, &sett);
  
  if( readPos < 7 ){
    // still reading
    int selectRes = select(64, &sett, NULL, NULL, NULL);
    
    if( selectRes == 0 )
      return;
    
    if( selectRes == -1 ){
      perror( "Connection to websocket server failed" );
      exit(EXIT_FAILURE);
    }
    if( selectRes > 0 ){
      assert(FD_ISSET(SocketFD, &sett));
      
      int bytesRead = recv( SocketFD, readbuf+readPos, 7-readPos, 0 );
      readPos += bytesRead;
    }
  } else {
    // here the server should have closed the connection
    int selectRes = select(64, &sett, NULL, NULL, NULL);
    
    if( selectRes == 0 )
      return;
    
    if( selectRes == -1 ){
      perror( "Connection to websocket server failed as expected" );
      int result = 266;
      REPORT_RESULT();
      emscripten_cancel_main_loop();
      done = 1;
    } 
    
    if( selectRes > 0 ){
      printf( "Error: socket should not show up on select call anymore.\n" );
      exit(EXIT_FAILURE);
    }     
  }
  
  return;
}

// Scenario: the server sends data and closes the connection after 7 bytes. 
// This test should provoke the situation in which the underlying 
// tcp connection has been torn down already but there is still data 
// in the inQueue. The select call has to succeed as long the queue
// still contains data and only then start to throw errors.
int main(void)
{
  struct sockaddr_in stSockAddr;
  int Res;
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (-1 == SocketFD)
  {
    perror("cannot create socket");
    exit(EXIT_FAILURE);
  }

  memset(&stSockAddr, 0, sizeof(stSockAddr));

  stSockAddr.sin_family = AF_INET;
  stSockAddr.sin_port = htons(
#if EMSCRIPTEN
    8995
#else
    8994
#endif
  );
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);

  if (0 > Res) {
    perror("error: first parameter is not a valid address family");
    close(SocketFD);
    exit(EXIT_FAILURE);
  } else if (0 == Res) {
    perror("char string (second parameter does not contain valid ipaddress)");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }

  // This call should succeed (even if the server port is closed)
  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
    perror("connect failed");
    close(SocketFD);
    exit(EXIT_FAILURE);

  }

#if EMSCRIPTEN
  emscripten_set_main_loop(iter, 0, 0);
#else
  while (!done) iter(NULL);
#endif

  return EXIT_SUCCESS;
}