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

« back to all changes in this revision

Viewing changes to tests/sockets/test_sockets_select_server_closes_connection_client_rw.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
#include "test_sockets_msg.h"
 
18
 
 
19
#define MESSAGE "0123456789"
 
20
 
 
21
int sockfd;
 
22
msg_t readmsg;
 
23
msg_t writemsg;
 
24
 
 
25
void finish(int result) {
 
26
  close(sockfd);
 
27
#if EMSCRIPTEN
 
28
  REPORT_RESULT();
 
29
#endif
 
30
  exit(result);
 
31
}
 
32
 
 
33
void main_loop(void *arg) {
 
34
  static int state = 0;
 
35
  static int readPos = 0;
 
36
  static int writePos = 0;
 
37
  int selectRes;
 
38
  ssize_t transferAmount;
 
39
  fd_set sett;  
 
40
  
 
41
  switch (state) {
 
42
    case 0:
 
43
      // writing 10 bytes to the server
 
44
      
 
45
      // since the socket in the read file descriptors has no available data,
 
46
      // select should tell us 0 handles are ready
 
47
      FD_ZERO(&sett);
 
48
      FD_SET(sockfd, &sett);
 
49
      selectRes = select(64, &sett, NULL, NULL, NULL);
 
50
      if (selectRes != 0) {
 
51
        printf("case 0: read select != 0 (%d)\n", selectRes);
 
52
        finish(EXIT_FAILURE);
 
53
      }
 
54
      
 
55
      // the socket in the write file descriptors has to result in either a 0 or 1
 
56
      // the connection either is setting up or is established and writing is possible
 
57
      FD_ZERO(&sett);
 
58
      FD_SET(sockfd, &sett);
 
59
      selectRes = select(64, NULL, &sett, NULL, NULL);
 
60
      if (selectRes == -1) {
 
61
        printf("case 0: write select == -1\n");
 
62
        finish(EXIT_FAILURE);
 
63
      } else if (selectRes == 0) {
 
64
        return;
 
65
      }
 
66
      
 
67
      // send a single byte
 
68
      transferAmount = do_msg_write(sockfd, &writemsg, writePos, 1, NULL, 0);
 
69
      if (transferAmount != -1) writePos += transferAmount;
 
70
   
 
71
      // after 10 bytes switch to next state
 
72
      if (writePos >= writemsg.length) {
 
73
        state = 1;
 
74
      }
 
75
      break;
 
76
      
 
77
    case 1:
 
78
      // wait until we can read one byte to make sure the server
 
79
      // has sent the data and then closed the connection
 
80
      FD_ZERO(&sett);
 
81
      FD_SET(sockfd, &sett);
 
82
      selectRes = select(64, &sett, NULL, NULL, NULL);
 
83
      if (selectRes == -1) {
 
84
        printf("case 1: read selectRes == -1\n");
 
85
        finish(EXIT_FAILURE);
 
86
      } else if (selectRes == 0) {
 
87
        return;
 
88
      }
 
89
 
 
90
      // read a single byte
 
91
      transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
 
92
      if (transferAmount == 0) {
 
93
        perror("server closed");
 
94
        finish(EXIT_FAILURE);
 
95
      } else if (transferAmount != -1) {
 
96
        readPos += transferAmount;
 
97
      }
 
98
   
 
99
      // if successfully reading 1 byte, switch to next state
 
100
      if (readPos >= 1) {
 
101
        state = 2;
 
102
      }
 
103
      break;
 
104
    
 
105
    case 2:
 
106
      // calling select with the socket in the write file descriptors should
 
107
      // succeed, but the socket should not set in the set.
 
108
      FD_ZERO(&sett);
 
109
      FD_SET(sockfd, &sett);
 
110
      selectRes = select(64, NULL, &sett, NULL, NULL);
 
111
      if (selectRes != 0 || FD_ISSET(sockfd, &sett)) {
 
112
        printf("case 2: write selectRes != 0 || FD_ISSET(sockfd, &sett)\n");
 
113
        finish(EXIT_FAILURE);
 
114
      }
 
115
 
 
116
      // calling select with the socket in the read file descriptors 
 
117
      // has to succeed because there is still data in the inQueue
 
118
      FD_ZERO(&sett);
 
119
      FD_SET(sockfd, &sett);
 
120
      selectRes = select(64, &sett, NULL, NULL, NULL);
 
121
      if (selectRes != 1) {
 
122
        printf("case 2: read selectRes != 1\n");
 
123
        finish(EXIT_FAILURE);
 
124
      } else if (selectRes == 0) {
 
125
        return;
 
126
      }
 
127
      
 
128
      // read a single byte
 
129
      transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
 
130
      if (transferAmount == 0) {
 
131
        perror("server closed");
 
132
        finish(EXIT_FAILURE);
 
133
      } else if (transferAmount != -1) {
 
134
        readPos += transferAmount;
 
135
      }
 
136
      
 
137
      // with 10 bytes read the inQueue is empty => switch state
 
138
      if (readPos >= readmsg.length) {
 
139
        state = 3;
 
140
      }
 
141
      break;
 
142
      
 
143
    case 3:
 
144
      // calling select with the socket in the read file descriptors 
 
145
      // should succeed
 
146
      FD_ZERO(&sett);
 
147
      FD_SET(sockfd, &sett);
 
148
      selectRes = select(64, &sett, NULL, NULL, NULL);
 
149
      if (selectRes != 1) {
 
150
        printf("case 3: read selectRes != 1\n");
 
151
        finish(EXIT_FAILURE);
 
152
      }
 
153
 
 
154
      // but recv should return 0 signaling the remote
 
155
      // end has closed the connection.
 
156
      transferAmount = do_msg_read(sockfd, &readmsg, readPos, 0, NULL, NULL);
 
157
      if (transferAmount) {
 
158
        printf("case 3: read != 0\n");
 
159
        finish(EXIT_FAILURE);
 
160
      }
 
161
      
 
162
      // report back success, the 266 is just an arbitrary value without 
 
163
      // deeper meaning
 
164
      finish(266);
 
165
      break;
 
166
      
 
167
    default:
 
168
      printf("Impossible state!\n");
 
169
      finish(EXIT_FAILURE);
 
170
      break;
 
171
  }
 
172
  
 
173
  return;
 
174
}
 
175
 
 
176
// This test checks for an intended asymmetry in the behavior of the select function.
 
177
// Scenario: the client sends data to the server. After 10 received bytes the 
 
178
// server sends 10 bytes on its own and immediately afterwards closes the connection.
 
179
// This mimics a typical connect-request-response-disconnect situation.
 
180
// After the server closed the connection select calls with the socket in the write file 
 
181
// descriptors have to fail as the tcp connection is already down and there is no way 
 
182
// anymore to send data. 
 
183
// Select calls with the socket in the read file descriptor list still have to succeed 
 
184
// as there are still 10 bytes to read from the inQueue. So, for the same socket the 
 
185
// select call behaves differently depending on whether the socket is listed in the
 
186
// read or write file descriptors.
 
187
int main() {
 
188
  struct sockaddr_in addr;
 
189
  int res;
 
190
 
 
191
  memset(&readmsg, 0, sizeof(msg_t));
 
192
  memset(&writemsg, 0, sizeof(msg_t));
 
193
  writemsg.length = strlen(MESSAGE) + 1;
 
194
  writemsg.buffer = malloc(writemsg.length);
 
195
  strncpy(writemsg.buffer, MESSAGE, writemsg.length);
 
196
 
 
197
  sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
198
  if (sockfd == -1) {
 
199
    perror("cannot create socket");
 
200
    finish(EXIT_FAILURE);
 
201
  }
 
202
  fcntl(sockfd, F_SETFL, O_NONBLOCK);
 
203
 
 
204
  memset(&addr, 0, sizeof(addr));
 
205
  addr.sin_family = AF_INET;
 
206
  addr.sin_port = htons(SOCKK);
 
207
  if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) {
 
208
    perror("inet_pton failed");
 
209
    finish(EXIT_FAILURE);
 
210
  }
 
211
 
 
212
  // This call should succeed (even if the server port is closed)
 
213
  res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
 
214
  if (res == -1 && errno != EINPROGRESS) {
 
215
    perror("connect failed");
 
216
    finish(EXIT_FAILURE);
 
217
  }
 
218
 
 
219
#if EMSCRIPTEN
 
220
  emscripten_set_main_loop(main_loop, 0, 0);
 
221
#else
 
222
  while (1) main_loop(NULL);
 
223
#endif
 
224
 
 
225
  return EXIT_SUCCESS;
 
226
}
 
227