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
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
// -*- pike -*- TEST: RUN-AS-PIKE-SCRIPT 
// 
// WebSocket testsuite 
// 
 
constant loopback = "127.0.0.1"; 
 
int successes; 
int fails; 
int skips; 
 
Thread.Mutex mux = Thread.Mutex(); 
 
Thread.Condition cond = Thread.Condition(); 
 
enum brokeness { 
  NOT_WEBSOCKET, 
  BAD_HDR_VERSION, 
  BAD_HDR_KEY_ACCEPT, 
  BAD_HDR_CONNECTION, 
  MAX_BROKENESS, 
}; 
 
class BrokenPort 
{ 
  constant is_broken = 1; 
 
  inherit Protocols.WebSocket.Port; 
 
  protected int brokeness_state; 
  int next_brokeness() 
  { 
    brokeness_state++; 
    if (brokeness_state < MAX_BROKENESS) return 1; 
    return 0; 
  } 
 
  class BrokenRequest 
  { 
    inherit Protocols.WebSocket.Request; 
 
    protected array(mapping(string:string)|array) 
      low_websocket_accept(string protocol, 
                           void|array extensions, 
                           void|mapping extra_headers) 
    { 
      [mapping heads, array _extensions] = 
        ::low_websocket_accept(protocol, extensions, extra_headers); 
 
      switch(brokeness_state) { 
      case NOT_WEBSOCKET: 
        Tools.Testsuite.log_status("Refusing WebSocket attempt.\n"); 
        m_delete(heads, "Sec-WebSocket-Accept"); 
        break; 
      case BAD_HDR_VERSION: 
        Tools.Testsuite.log_status("Testing bad WebSocket Version header.\n"); 
        heads["Sec-WebSocket-Version"] = "999"; 
        break; 
      case BAD_HDR_KEY_ACCEPT: 
        Tools.Testsuite.log_status("Testing bad WebSocket Accept header.\n"); 
        heads["Sec-WebSocket-Accept"] = "INVALID"; 
        break; 
      case BAD_HDR_CONNECTION: 
        Tools.Testsuite.log_status("Testing bad Connection header.\n"); 
        heads["Connection"] = "Downgrade"; 
        break; 
      } 
      object key = mux->lock(); 
      done |= 4;        // Forced server failure. 
      return ({ heads, _extensions }); 
    } 
  } 
 
  protected void create(function(Protocols.HTTP.Server.Request:void) http_cb, 
                        function(array(string), BrokenRequest:void)|void ws_cb, 
                        void|int portno, void|string interface) { 
 
    ::create(http_cb, UNDEFINED, portno, interface); 
 
    if (ws_cb) 
      request_program = Function.curry(BrokenRequest)(ws_cb); 
  } 
 
  protected string _sprintf(int c) 
  { 
    return sprintf("%O(), brokeness: %d", this_program, brokeness_state); 
  } 
} 
 
class BrokenConnection 
{ 
  constant is_broken = 1; 
 
  inherit Protocols.WebSocket.Connection; 
 
  protected int brokeness_state; 
  int next_brokeness() 
  { 
    brokeness_state++; 
    if (brokeness_state < MAX_BROKENESS) return 1; 
    return 0; 
  } 
 
  protected array(mapping) low_connect(Standards.URI endpoint, 
                                       mapping(string:string) extra_headers, 
                                       void|array extensions) 
  { 
    [mapping heads, mapping rext] = 
      ::low_connect(endpoint, extra_headers, extensions); 
 
    switch(brokeness_state) { 
    case NOT_WEBSOCKET: 
      Tools.Testsuite.log_status("Testing without WebSocket Key header.\n"); 
      m_delete(heads, "Sec-WebSocket-Key"); 
      break; 
    case BAD_HDR_VERSION: 
      Tools.Testsuite.log_status("Testing bad WebSocket Version header.\n"); 
      heads["Sec-WebSocket-Version"] = "999"; 
      break; 
    case BAD_HDR_KEY_ACCEPT: 
      Tools.Testsuite.log_status("Testing with empty WebSocket Key header.\n"); 
      heads["Sec-WebSocket-Key"] = ""; 
      break; 
    case BAD_HDR_CONNECTION: 
      Tools.Testsuite.log_status("Testing bad Connection header.\n"); 
      heads["Connection"] = "Downgrade"; 
      break; 
    } 
 
    return ({ heads, rext }); 
  } 
 
  protected string _sprintf(int c) 
  { 
    return sprintf("%s, brokeness: %d", ::_sprintf(c), brokeness_state); 
  } 
} 
 
int done; 
 
void got_server_fail() 
{ 
  object key = mux->lock(); 
  done |= 5; 
  cond->broadcast(); 
} 
 
void got_server_ok() 
{ 
  object key = mux->lock(); 
  done |= 1; 
  cond->broadcast(); 
} 
 
void got_client_fail() 
{ 
  object key = mux->lock(); 
  done |= 10; 
  cond->broadcast(); 
} 
 
void got_client_ok() 
{ 
  object key = mux->lock(); 
  done |= 2; 
  cond->broadcast(); 
} 
 
void got_server_http(Protocols.WebSocket.Request req) 
{ 
  req->response_and_finish(([ "error": Protocols.HTTP.HTTP_BAD, 
                              "data": "Bad request\n", 
                              "type": "text/plain" ])); 
  got_server_fail(); 
} 
 
void got_client_close(Protocols.WebSocket.CLOSE_STATUS cause) 
{ 
  Tools.Testsuite.log_status("Got close: %d\n", cause); 
  if (cause != Protocols.WebSocket.CLOSE_NORMAL) got_client_fail(); 
} 
 
void got_server_ws(array(string) protocols, Protocols.WebSocket.Request wsr) 
{ 
  Tools.Testsuite.log_status("Got web socket\n"); 
  wsr->websocket_accept(sizeof(protocols)?protocols[0]:UNDEFINED); 
  got_server_ok(); 
} 
 
void wait_for_completion(int|void expect_failure) 
{ 
  object key = mux->lock(); 
  while ((done & 3) != 3) { 
    Tools.Testsuite.log_status("Main: Waiting for completion (done: %d).\n", 
                               done); 
    cond->wait(key); 
  } 
 
  int expected = expect_failure?15:3; 
 
  if (done == expected) { 
    Tools.Testsuite.log_status("Ok: Done: %d\n", done); 
    successes++; 
  } else { 
    Tools.Testsuite.log_msg("Failure: Done: %d (expected: %d)\n", 
                            done, expected); 
    fails++; 
  } 
} 
 
void run_tests() 
{ 
  foreach(({ Protocols.WebSocket.Port, Protocols.WebSocket.SSLPort, 
             BrokenPort, 
          }), program(Protocols.WebSocket.Port) port_prog) { 
    Tools.Testsuite.log_status("Attempting to open a %O port.\n", port_prog); 
    // NB: The API doesn't allow for the ANY port. 
    int port_no; 
    Protocols.WebSocket.Port port; 
    mixed err; 
    for (port_no = 16384; port_no < 65536; port_no++) { 
      err = catch { 
          port = port_prog(got_server_http, 
                           got_server_ws, 
                           port_no, loopback); 
        }; 
      if (port) break; 
    } 
    if (!port) { 
      fails++; 
      Tools.Testsuite.log_msg("Failed to open %O port: %s\n", 
                              port_prog, describe_backtrace(err)); 
      continue; 
    } 
    successes++; 
    Tools.Testsuite.log_status("%O port opened on %s:%d\n", 
                               port_prog, loopback, port_no); 
 
    Protocols.WebSocket.Connection client = 
      Protocols.WebSocket.Connection(); 
 
    client->onopen = got_client_ok; 
    client->onclose = got_client_close; 
 
    Standards.URI ws_uri = 
      Standards.URI(sprintf("ws://%s:%d/path", loopback, port_no)); 
    if(port->ctx) { 
      ws_uri->scheme = "wss"; 
    } 
 
    if (!port->is_broken) { 
      Tools.Testsuite.log_status("Connecting to %O...\n", ws_uri); 
      done = 0; 
      if (!client->connect(ws_uri)) { 
        fails++; 
        Tools.Testsuite.log_msg("Failed to connect to %O.\n", ws_uri); 
        continue; 
      } 
      successes++; 
 
      wait_for_completion(); 
 
      if (!port->ctx) { 
        client = BrokenConnection(); 
        client->onopen = got_client_ok; 
        do { 
          // NB: onclose is zapped after calling. 
          client->onclose = got_client_close; 
 
          Tools.Testsuite.log_status("Connecting to %O with broken client...\n", ws_uri); 
          done = 0; 
          if (!client->connect(ws_uri)) { 
            fails++; 
            Tools.Testsuite.log_msg("Failed to connect with %O.\n", client); 
            continue; 
          } 
          successes++; 
 
          wait_for_completion(1); 
        } while (client->next_brokeness()); 
      } 
    } else { 
      // BrokenPort. 
      do { 
        // NB: onclose is zapped after calling. 
        client->onclose = got_client_close; 
 
        Tools.Testsuite.log_status("Connecting to %O...\n", ws_uri); 
        done = 0; 
        if (!client->connect(ws_uri)) { 
          fails++; 
          Tools.Testsuite.log_msg("Failed to connect to %O.\n", port); 
          continue; 
        } 
        successes++; 
 
        wait_for_completion(1); 
      } while (port->next_brokeness()); 
    } 
  } 
 
  Tools.Testsuite.report_result(successes, fails, skips); 
 
  // NB: Paranoia: Call exit(0) AFTER our thread has terminated. 
  call_out(exit, 1, 0); 
} 
 
int main(int argc, array(string) argv) 
{ 
  Tools.Testsuite.log_status("Testing Protocols.WebSocket...\n"); 
 
  Thread.thread_create(run_tests); 
  return -1; 
}