GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_header.c Lines: 1447 1447 100.0 %
Date: 2020-12-10 21:44:00 Branches: 573 1910 30.0 %

Line Branch Exec Source
1
#include <signal.h>
2
#include <stddef.h>
3
#include <stdio.h>
4
#include <regex.h>
5
#include <criterion/criterion.h>
6
7
#include "alloc.h"
8
#include "server.h"
9
#include "http.h"
10
#include "header.h"
11
#include "config.h"
12
#include "subprotocols.h"
13
#include "extensions.h"
14
#include "error.h"
15
16
#define HEADER_CARRIAGE_RETURN "\r"
17
#define HEADER_BAD_NEWLINE "\r\a\r\a"
18
#define HEADER_BAD_NEWLINE_PAYLOAD "GET / HTTP/1.1\r\n"\
19
                                   "test: test\r\a\r\n"
20
#define HEADER_BAD_NEWLINE_PAYLOAD_MULTIPLE_HEADERS "GET / HTTP/1.1\r\n"\
21
                                   "test: test\r\n"\
22
                                   "test2: test2\r\a\r\n"
23
#define HEADER_NO_BODY_NEWLINE "GET / HTTP/1.1\r\n"
24
#define HEADER_NEWLINE "\r\n\r\n"
25
#define HEADER_SPACE_NEWLINE " \r\n\r\n"
26
#define HEADER_INVALID_HTTP_PATH "GET * HTTP/1.1\r\n\r\n"
27
#define HEADER_NO_PATH "GET\r\n\r\n"
28
#define HEADER_NO_VERSION "GET /another/test\r\n\r\n"
29
#define HEADER_PATH_BIG "GET /another/test/path/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/?access_token=ACCESS&csrf_token=CSRF\r\n\r\n"
30
#define HEADER_WRONG_HTTP_METHOD "POST / HTTP/1.1\r\n\r\n"
31
#define HEADER_WRONG_HTTP_PATH "GET /invalid HTTP/1.1\r\n\r\n"
32
#define HEADER_HTTPS_HTTP_PATH "GET https://mortz.dk HTTP/1.1\r\n\r\n"
33
#define HEADER_WRONG_HTTP_PATH_QUERY "GET /test/path?access_token=test&invalid=yes HTTP/1.1\r\n\r\n"
34
#define HEADER_WRONG_HTTP_VERSION "GET / HTTP/0.8\r\n\r\n"
35
#define HEADER_HTTP_PATH "GET /test/path HTTP/1.1\r\n\r\n"
36
#define HEADER_HTTP_PATH_QUERY "GET /another/test/path?access_token=ACCESS&csrf_token=CSRF HTTP/1.1\r\n\r\n"
37
#define HEADER_HTTP "GET / HTTP/1.1\r\n\r\n"
38
#define HEADER_DUPLICATE_VERSION "GET / HTTP/1.1\r\n"\
39
                                 "Sec-WebSocket-Version: 13\r\n"\
40
                                 "Sec-WebSocket-Version: 13\r\n\r\n"
41
#define HEADER_DUPLICATE_KEY "GET / HTTP/1.1\r\n"\
42
                             "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
43
                             "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n"
44
#define HEADER_BIG "GET / HTTP/1.1\r\n"\
45
                   "Lorem: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac nisi risus. Fusce ante mauris, placerat vel ex eget, ultrices varius arcu. Fusce nec tincidunt mi. Curabitur gravida orci a odio vulputate, eu molestie enim auctor. Fusce viverra viverra est vel sagittis. Ut lacinia lacinia enim sit amet porta. Proin sit amet elementum tellus, a egestas ipsum. Nulla eu interdum odio. Nulla sodales enim eu urna vestibulum, id mattis justo euismod. Etiam mattis diam non ante ullamcorper, ut facilisis tortor interdum. Nunc sit amet auctor lacus, eget dictum diam. Fusce vel tellus consequat, malesuada elit ac, posuere erat. Nullam vel congue nisi, non imperdiet massa. Praesent sollicitudin massa ex, sollicitudin suscipit neque fringilla ac. Proin congue erat in semper ultrices. Praesent at odio sit amet metus condimentum elementum. Vivamus ullamcorper malesuada facilisis. Cras interdum ante egestas magna scelerisque, vel consectetur lectus dapibus. Ut vitae semper eros. Vivamus in augue vel ligula blandit tempor eu. This is tooo large\r\n\r\n"
46
#define HEADER_PAYLOAD_BIG "GET / HTTP/1.1\r\n"\
47
                           "\r\n"\
48
                           "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac nisi risus. Fusce ante mauris, placerat vel ex eget, ultrices varius arcu. Fusce nec tincidunt mi. Curabitur gravida orci a odio vulputate, eu molestie enim auctor. Fusce viverra viverra est vel sagittis. Ut lacinia lacinia enim sit amet porta. Proin sit amet elementum tellus, a egestas ipsum. Nulla eu interdum odio. Nulla sodales enim eu urna vestibulum, id mattis justo euismod. Etiam mattis diam non ante ullamcorper, ut facilisis tortor interdum. Nunc sit amet auctor lacus, eget dictum diam. Fusce vel tellus consequat, malesuada elit ac, posuere erat. Nullam vel congue nisi, non imperdiet massa. Praesent sollicitudin massa ex, sollicitudin suscipit neque fringilla ac. Proin congue erat in semper ultrices. Praesent at odio sit amet metus condimentum elementum. Vivamus ullamcorper malesuada facilisis. Cras interdum ante egestas magna scelerisque, vel consectetur lectus dapibus. Ut vitae semper eros. Vivamus in augue vel ligula blandit tempor eu. This is tooo large."
49
#define HEADER_PAYLOAD_BIG_WITH_HEADERS "GET / HTTP/1.1\r\n"\
50
                           "Test: test\r\n"\
51
                           "\r\n"\
52
                           "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac nisi risus. Fusce ante mauris, placerat vel ex eget, ultrices varius arcu. Fusce nec tincidunt mi. Curabitur gravida orci a odio vulputate, eu molestie enim auctor. Fusce viverra viverra est vel sagittis. Ut lacinia lacinia enim sit amet porta. Proin sit amet elementum tellus, a egestas ipsum. Nulla eu interdum odio. Nulla sodales enim eu urna vestibulum, id mattis justo euismod. Etiam mattis diam non ante ullamcorper, ut facilisis tortor interdum. Nunc sit amet auctor lacus, eget dictum diam. Fusce vel tellus consequat, malesuada elit ac, posuere erat. Nullam vel congue nisi, non imperdiet massa. Praesent sollicitudin massa ex, sollicitudin suscipit neque fringilla ac. Proin congue erat in semper ultrices. Praesent at odio sit amet metus condimentum elementum. Vivamus ullamcorper malesuada facilisis. Cras interdum ante egestas magna scelerisque, vel consectetur lectus dapibus. Ut vitae semper eros. Vivamus in augue vel ligula blandit tempor eu. This is tooo large."
53
#define HEADER_HTTP "GET / HTTP/1.1\r\n\r\n"
54
#define HEADER_RFC6455 "GET /test/path HTTP/1.1\r\n"\
55
                       "Host: 127.0.0.1\r\n"\
56
                       "Upgrade: websocket\r\n"\
57
                       "Connection: Upgrade\r\n"\
58
                       "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
59
                       "Origin: 127.0.0.1\r\n"\
60
                       "Sec-WebSocket-Protocol: chat, broadcast\r\n"\
61
                       "Cookie: test=test;\r\n"\
62
                       "Sec-WebSocket-Extensions: imaginary-extension ,\r\n"\
63
                       "Sec-WebSocket-Extensions: permessage-deflate;\r\n"\
64
                       "Sec-WebSocket-Extensions: client_max_window_bits=15 ,\r\n"\
65
                       "Sec-WebSocket-Extensions: permessage-deflate\r\n"\
66
                       "Sec-WebSocket-Version: 13\r\n\r\n"
67
68
#define HEADER_HYBI10 "GET /test/path HTTP/1.1\r\n"\
69
                      "Host: 127.0.0.1\r\n"\
70
                      "Upgrade: websocket\r\n"\
71
                      "Connection: Upgrade\r\n"\
72
                      "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
73
                      "Origin: 127.0.0.1\r\n"\
74
                      "Sec-WebSocket-Protocol: broadcast, superchat\r\n"\
75
                      "Sec-WebSocket-Version: 8\r\n\r\n"
76
77
#define HEADER_HYBI07 "GET /test/path HTTP/1.1\r\n"\
78
                      "Host: 127.0.0.1\r\n"\
79
                      "Upgrade: websocket\r\n"\
80
                      "Connection: Upgrade\r\n"\
81
                      "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
82
                      "Origin: 127.0.0.1\r\n"\
83
                      "Sec-WebSocket-Protocol: chat, superchat, echo\r\n"\
84
                      "Sec-WebSocket-Version: 7\r\n\r\n"
85
86
#define HEADER_HYBI06 "GET /test/path HTTP/1.1\r\n"\
87
                      "Host: 127.0.0.1\r\n"\
88
                      "Upgrade: websocket\r\n"\
89
                      "Connection: Upgrade\r\n"\
90
                      "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
91
                      "Sec-WebSocket-Origin: 127.0.0.1\r\n"\
92
                      "Sec-WebSocket-Protocol: chat, echo, superchat\r\n"\
93
                      "Sec-WebSocket-Version: 6\r\n\r\n"
94
95
#define HEADER_HYBI05 "GET /test/path HTTP/1.1\r\n"\
96
                      "Host: 127.0.0.1\r\n"\
97
                      "Upgrade: websocket\r\n"\
98
                      "Connection: Upgrade\r\n"\
99
                      "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
100
                      "Sec-WebSocket-Origin: 127.0.0.1\r\n"\
101
                      "Sec-WebSocket-Protocol: chat, superchat\r\n"\
102
                      "Sec-WebSocket-Version: 5\r\n\r\n"
103
104
#define HEADER_HYBI04 "GET /test/path HTTP/1.1\r\n"\
105
                      "Host: 127.0.0.1\r\n"\
106
                      "Upgrade: websocket\r\n"\
107
                      "Connection: Upgrade\r\n"\
108
                      "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"\
109
                      "Sec-WebSocket-Origin: 127.0.0.1\r\n"\
110
                      "Sec-WebSocket-Protocol: chat, superchat\r\n"\
111
                      "Sec-WebSocket-Version: 4\r\n\r\n"
112
113
#define HEADER_HIXIE76 "GET /test/path HTTP/1.1\r\n"\
114
                       "Host: 127.0.0.1\r\n"\
115
                       "Connection: Upgrade\r\n"\
116
                       "Sec-WebSocket-Key2: 12998 5 Y3 1  .P00\r\n"\
117
                       "Sec-WebSocket-Protocol: sample\r\n"\
118
                       "Upgrade: WebSocket\r\n"\
119
                       "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n"\
120
                       "Origin: 127.0.0.1\r\n"\
121
                       "\r\n"\
122
                       "^n:ds[4U"
123
124
#define HEADER_HIXIE75 "GET /test/path HTTP/1.1\r\n"\
125
                       "Upgrade: WebSocket\r\n"\
126
                       "Connection: Upgrade\r\n"\
127
                       "Host: 127.0.0.1\r\n"\
128
                       "Origin: 127.0.0.1\r\n"\
129
                       "WebSocket-Protocol: sample, echo\r\n\r\n"
130
131
#define HEADER_HIXIE75_REVERSE_PROTOCOL "GET /test/path HTTP/1.1\r\n"\
132
                                        "Upgrade: WebSocket\r\n"\
133
                                        "Connection: Upgrade\r\n"\
134
                                        "Host: 127.0.0.1\r\n"\
135
                                        "Origin: 127.0.0.1\r\n"\
136
                                        "WebSocket-Protocol: echo, sample\r\n\r\n"
137
138
139
#define HEADER_HIXIE75_NO_PROTOCOL "GET /test/path HTTP/1.1\r\n"\
140
                                   "Upgrade: WebSocket\r\n"\
141
                                   "Connection: Upgrade\r\n"\
142
                                   "Host: 127.0.0.1\r\n"\
143
                                   "Origin: 127.0.0.1\r\n\r\n"
144
145
60
static void setup(void) {
146
#ifdef USE_RPMALLOC
147
60
    rpmalloc_initialize();
148
#endif
149
60
}
150
151
60
static void teardown(void) {
152
#ifdef USE_RPMALLOC
153
60
    rpmalloc_finalize();
154
#endif
155
60
}
156
157
TestSuite(WSS_parse_header, .init = setup, .fini = teardown);
158
159
4
Test(WSS_parse_header, no_header_content_data) {
160
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
161
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
162
2
    enum HttpStatus_Code code;
163
2
    int fd = -1;
164
165


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
166
167
2
    header->content = NULL;
168
2
    header->length = 0;
169
170
2
    code = WSS_parse_header(fd, header, conf);
171
172


2
    cr_assert(code == HttpStatus_BadRequest);
173
174
2
    WSS_config_free(conf);
175
176
2
    WSS_free((void**) &conf);
177
2
    WSS_free_header(header);
178
2
}
179
180
4
Test(WSS_parse_header, bad_carriage_return) {
181
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
182
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
183
2
    size_t l = strlen(HEADER_CARRIAGE_RETURN);
184
2
    char *h = WSS_malloc(l*sizeof(char)+1);
185
2
    enum HttpStatus_Code code;
186
2
    int fd = -1;
187
188
2
    sprintf(h, "%s", HEADER_CARRIAGE_RETURN);
189
190


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
191
192
2
    header->content = h;
193
2
    header->length = l;
194
195
2
    code = WSS_parse_header(fd, header, conf);
196
197


2
    cr_assert(code == HttpStatus_BadRequest);
198
199
2
    WSS_config_free(conf);
200
2
    WSS_free((void**) &conf);
201
2
    WSS_free_header(header);
202
2
}
203
204
4
Test(WSS_parse_header, bad_newline) {
205
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
206
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
207
2
    size_t l = strlen(HEADER_BAD_NEWLINE);
208
2
    char *h = WSS_malloc(l*sizeof(char)+1);
209
2
    enum HttpStatus_Code code;
210
2
    int fd = -1;
211
212
2
    sprintf(h, "%s", HEADER_BAD_NEWLINE);
213
214


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
215
216
2
    header->content = h;
217
2
    header->length = l;
218
219
2
    code = WSS_parse_header(fd, header, conf);
220
221


2
    cr_assert(code == HttpStatus_BadRequest);
222
223
2
    WSS_config_free(conf);
224
2
    WSS_free((void**) &conf);
225
2
    WSS_free_header(header);
226
2
}
227
228
4
Test(WSS_parse_header, bad_newline_payload) {
229
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
230
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
231
2
    size_t l = strlen(HEADER_BAD_NEWLINE_PAYLOAD);
232
2
    char *h = WSS_malloc(l*sizeof(char)+1);
233
2
    enum HttpStatus_Code code;
234
2
    int fd = -1;
235
236
2
    sprintf(h, "%s", HEADER_BAD_NEWLINE_PAYLOAD);
237
238


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
239
240
2
    header->content = h;
241
2
    header->length = l;
242
243
2
    code = WSS_parse_header(fd, header, conf);
244
245


2
    cr_assert(code == HttpStatus_BadRequest);
246
247
2
    WSS_config_free(conf);
248
2
    WSS_free((void**) &conf);
249
2
    WSS_free_header(header);
250
2
}
251
252
4
Test(WSS_parse_header, bad_newline_payload_multiple_headers) {
253
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
254
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
255
2
    size_t l = strlen(HEADER_BAD_NEWLINE_PAYLOAD_MULTIPLE_HEADERS);
256
2
    char *h = WSS_malloc(l*sizeof(char)+1);
257
2
    enum HttpStatus_Code code;
258
2
    int fd = -1;
259
260
2
    sprintf(h, "%s", HEADER_BAD_NEWLINE_PAYLOAD_MULTIPLE_HEADERS);
261
262


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
263
264
2
    header->content = h;
265
2
    header->length = l;
266
267
2
    code = WSS_parse_header(fd, header, conf);
268
269


2
    cr_assert(code == HttpStatus_BadRequest);
270
271
2
    WSS_config_free(conf);
272
2
    WSS_free((void**) &conf);
273
2
    WSS_free_header(header);
274
2
}
275
276
4
Test(WSS_parse_header, only_newline) {
277
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
278
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
279
2
    size_t l = strlen(HEADER_NEWLINE);
280
2
    char *h = WSS_malloc(l*sizeof(char)+1);
281
2
    enum HttpStatus_Code code;
282
2
    int fd = -1;
283
284
2
    sprintf(h, "%s", HEADER_NEWLINE);
285
286


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
287
288
2
    header->content = h;
289
2
    header->length = l;
290
291
2
    code = WSS_parse_header(fd, header, conf);
292
293


2
    cr_assert(code == HttpStatus_MethodNotAllowed);
294
295
2
    WSS_config_free(conf);
296
2
    WSS_free((void**) &conf);
297
2
    WSS_free_header(header);
298
2
}
299
300
4
Test(WSS_parse_header, space_and_newline) {
301
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
302
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
303
2
    size_t l = strlen(HEADER_SPACE_NEWLINE);
304
2
    char *h = WSS_malloc(l*sizeof(char)+1);
305
2
    enum HttpStatus_Code code;
306
2
    int fd = -1;
307
308
2
    sprintf(h, "%s", HEADER_SPACE_NEWLINE);
309
310


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
311
312
2
    header->content = h;
313
2
    header->length = l;
314
315
2
    code = WSS_parse_header(fd, header, conf);
316
317


2
    cr_assert(code == HttpStatus_BadRequest);
318
319
2
    WSS_config_free(conf);
320
2
    WSS_free((void**) &conf);
321
2
    WSS_free_header(header);
322
2
}
323
324
4
Test(WSS_parse_header, no_body_newline) {
325
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
326
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
327
2
    size_t l = strlen(HEADER_NO_BODY_NEWLINE);
328
2
    char *h = WSS_malloc(l*sizeof(char)+1);
329
2
    enum HttpStatus_Code code;
330
2
    int fd = -1;
331
332


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
333
334
2
    sprintf(h, "%s", HEADER_NO_BODY_NEWLINE);
335
336
2
    header->content = h;
337
2
    header->length = l;
338
339
2
    code = WSS_parse_header(fd, header, conf);
340
341
2
    cr_log_info("Code: %d", code);
342


2
    cr_assert(code == HttpStatus_BadRequest);
343
344
2
    WSS_config_free(conf);
345
2
    WSS_free((void**) &conf);
346
2
    WSS_free_header(header);
347
2
}
348
349
4
Test(WSS_parse_header, unsupported_http_method) {
350
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
351
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
352
2
    size_t l = strlen(HEADER_WRONG_HTTP_METHOD);
353
2
    char *h = WSS_malloc(l*sizeof(char)+1);
354
2
    enum HttpStatus_Code code;
355
2
    int fd = -1;
356
357
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_METHOD);
358
359


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
360
361
2
    header->content = h;
362
2
    header->length = l;
363
364
2
    code = WSS_parse_header(fd, header, conf);
365
366


2
    cr_assert(code == HttpStatus_MethodNotAllowed);
367
368
2
    WSS_config_free(conf);
369
370
2
    WSS_free((void**) &conf);
371
2
    WSS_free_header(header);
372
2
}
373
374
4
Test(WSS_parse_header, no_http_path) {
375
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
376
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
377
2
    size_t l = strlen(HEADER_NO_PATH);
378
2
    char *h = WSS_malloc(l*sizeof(char)+1);
379
2
    enum HttpStatus_Code code;
380
2
    int fd = -1;
381
382
2
    sprintf(h, "%s", HEADER_NO_PATH);
383
384


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
385
386
2
    header->content = h;
387
2
    header->length = l;
388
389
2
    code = WSS_parse_header(fd, header, conf);
390
391


2
    cr_assert(code == HttpStatus_BadRequest);
392
393
2
    WSS_config_free(conf);
394
395
2
    WSS_free((void**) &conf);
396
2
    WSS_free_header(header);
397
2
}
398
399
4
Test(WSS_parse_header, too_big_path) {
400
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
401
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
402
2
    size_t l = strlen(HEADER_PATH_BIG);
403
2
    char *h = WSS_malloc(l*sizeof(char)+1);
404
2
    enum HttpStatus_Code code;
405
2
    int fd = -1;
406
407
2
    sprintf(h, "%s", HEADER_PATH_BIG);
408
409


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
410
411
2
    header->content = h;
412
2
    header->length = l;
413
414
2
    code = WSS_parse_header(fd, header, conf);
415
416


2
    cr_assert(code == HttpStatus_URITooLong);
417
418
2
    WSS_config_free(conf);
419
420
2
    WSS_free((void**) &conf);
421
2
    WSS_free_header(header);
422
2
}
423
424
4
Test(WSS_parse_header, invalid_http_path) {
425
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
426
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
427
2
    size_t l = strlen(HEADER_INVALID_HTTP_PATH);
428
2
    char *h = WSS_malloc(l*sizeof(char)+1);
429
2
    enum HttpStatus_Code code;
430
2
    int fd = -1;
431
432
2
    sprintf(h, "%s", HEADER_INVALID_HTTP_PATH);
433
434


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
435
436
2
    header->content = h;
437
2
    header->length = l;
438
439
2
    code = WSS_parse_header(fd, header, conf);
440
441


2
    cr_assert(code == HttpStatus_BadRequest);
442
443
2
    WSS_config_free(conf);
444
445
2
    WSS_free((void**) &conf);
446
2
    WSS_free_header(header);
447
2
}
448
449
4
Test(WSS_parse_header, unsupported_http_path) {
450
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
451
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
452
2
    size_t l = strlen(HEADER_WRONG_HTTP_PATH);
453
2
    char *h = WSS_malloc(l*sizeof(char)+1);
454
2
    enum HttpStatus_Code code;
455
2
    int fd = -1;
456
457
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_PATH);
458
459


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
460
461
2
    header->content = h;
462
2
    header->length = l;
463
464
2
    code = WSS_parse_header(fd, header, conf);
465
466


2
    cr_assert(code == HttpStatus_OK);
467
468
2
    WSS_config_free(conf);
469
470
2
    WSS_free((void**) &conf);
471
2
    WSS_free_header(header);
472
2
}
473
474
4
Test(WSS_parse_header, unsupported_http_path_query) {
475
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
476
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
477
2
    size_t l = strlen(HEADER_WRONG_HTTP_PATH_QUERY);
478
2
    char *h = WSS_malloc(l*sizeof(char)+1);
479
2
    enum HttpStatus_Code code;
480
2
    int fd = -1;
481
482
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_PATH_QUERY);
483
484


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
485
486
2
    header->content = h;
487
2
    header->length = l;
488
489
2
    code = WSS_parse_header(fd, header, conf);
490
491


2
    cr_assert(code == HttpStatus_OK);
492
493
2
    WSS_config_free(conf);
494
495
2
    WSS_free((void**) &conf);
496
2
    WSS_free_header(header);
497
2
}
498
499
4
Test(WSS_parse_header, no_http_version) {
500
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
501
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
502
2
    size_t l = strlen(HEADER_NO_VERSION);
503
2
    char *h = WSS_malloc(l*sizeof(char)+1);
504
2
    enum HttpStatus_Code code;
505
2
    int fd = -1;
506
507
2
    sprintf(h, "%s", HEADER_NO_VERSION);
508
509


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
510
511
2
    header->content = h;
512
2
    header->length = l;
513
514
2
    code = WSS_parse_header(fd, header, conf);
515
516


2
    cr_assert(code == HttpStatus_BadRequest);
517
518
2
    WSS_config_free(conf);
519
520
2
    WSS_free((void**) &conf);
521
2
    WSS_free_header(header);
522
2
}
523
524
4
Test(WSS_parse_header, unsupported_http_version) {
525
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
526
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
527
2
    size_t l = strlen(HEADER_WRONG_HTTP_VERSION);
528
2
    char *h = WSS_malloc(l*sizeof(char)+1);
529
2
    enum HttpStatus_Code code;
530
2
    int fd = -1;
531
532
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_VERSION);
533
534


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
535
536
2
    header->content = h;
537
2
    header->length = l;
538
539
2
    code = WSS_parse_header(fd, header, conf);
540
541


2
    cr_assert(code == HttpStatus_HTTPVersionNotSupported);
542
543
2
    WSS_config_free(conf);
544
545
2
    WSS_free((void**) &conf);
546
2
    WSS_free_header(header);
547
2
}
548
549
4
Test(WSS_parse_header, payload_too_big) {
550
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
551
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
552
2
    size_t l = strlen(HEADER_PAYLOAD_BIG);
553
2
    char *h = WSS_malloc(l*sizeof(char)+1);
554
2
    enum HttpStatus_Code code;
555
2
    int fd = -1;
556
557
2
    sprintf(h, "%s", HEADER_PAYLOAD_BIG);
558
559


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
560
561
2
    header->content = h;
562
2
    header->length = l;
563
564
2
    code = WSS_parse_header(fd, header, conf);
565
566


2
    cr_assert(code == HttpStatus_PayloadTooLarge);
567
568
2
    WSS_config_free(conf);
569
570
2
    WSS_free((void**) &conf);
571
2
    WSS_free_header(header);
572
2
}
573
574
4
Test(WSS_parse_header, payload_too_big_with_headers) {
575
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
576
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
577
2
    size_t l = strlen(HEADER_PAYLOAD_BIG_WITH_HEADERS);
578
2
    char *h = WSS_malloc(l*sizeof(char)+1);
579
2
    enum HttpStatus_Code code;
580
2
    int fd = -1;
581
582
2
    sprintf(h, "%s", HEADER_PAYLOAD_BIG_WITH_HEADERS);
583
584


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
585
586
2
    header->content = h;
587
2
    header->length = l;
588
589
2
    code = WSS_parse_header(fd, header, conf);
590
591


2
    cr_assert(code == HttpStatus_PayloadTooLarge);
592
593
2
    WSS_config_free(conf);
594
595
2
    WSS_free((void**) &conf);
596
2
    WSS_free_header(header);
597
2
}
598
599
4
Test(WSS_parse_header, duplicate_version) {
600
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
601
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
602
2
    size_t l = strlen(HEADER_DUPLICATE_VERSION);
603
2
    char *h = WSS_malloc(l*sizeof(char)+1);
604
2
    enum HttpStatus_Code code;
605
2
    int fd = -1;
606
607
2
    sprintf(h, "%s", HEADER_DUPLICATE_VERSION);
608
609


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
610
611
2
    header->content = h;
612
2
    header->length = l;
613
614
2
    code = WSS_parse_header(fd, header, conf);
615
616


2
    cr_assert(code == HttpStatus_BadRequest);
617
618
2
    WSS_config_free(conf);
619
620
2
    WSS_free((void**) &conf);
621
2
    WSS_free_header(header);
622
2
}
623
624
4
Test(WSS_parse_header, duplicate_key) {
625
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
626
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
627
2
    size_t l = strlen(HEADER_DUPLICATE_KEY);
628
2
    char *h = WSS_malloc(l*sizeof(char)+1);
629
2
    enum HttpStatus_Code code;
630
2
    int fd = -1;
631
632
2
    sprintf(h, "%s", HEADER_DUPLICATE_KEY);
633
634


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
635
636
2
    header->content = h;
637
2
    header->length = l;
638
639
2
    code = WSS_parse_header(fd, header, conf);
640
641


2
    cr_assert(code == HttpStatus_BadRequest);
642
643
2
    WSS_config_free(conf);
644
645
2
    WSS_free((void**) &conf);
646
2
    WSS_free_header(header);
647
2
}
648
649
4
Test(WSS_parse_header, header_too_big) {
650
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
651
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
652
2
    size_t l = strlen(HEADER_BIG);
653
2
    char *h = WSS_malloc(l*sizeof(char)+1);
654
2
    enum HttpStatus_Code code;
655
2
    int fd = -1;
656
657
2
    sprintf(h, "%s", HEADER_BIG);
658
659


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
660
661
2
    header->content = h;
662
2
    header->length = l;
663
664
2
    code = WSS_parse_header(fd, header, conf);
665
666


2
    cr_assert(code == HttpStatus_RequestHeaderFieldsTooLarge);
667
668
2
    WSS_config_free(conf);
669
670
2
    WSS_free((void**) &conf);
671
2
    WSS_free_header(header);
672
2
}
673
674
4
Test(WSS_parse_header, valid_rfc6455) {
675
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
676
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
677
2
    size_t l = strlen(HEADER_RFC6455);
678
2
    char *h = WSS_malloc(l*sizeof(char)+1);
679
2
    enum HttpStatus_Code code;
680
2
    int fd = -1;
681
682
2
    sprintf(h, "%s", HEADER_RFC6455);
683
684


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
685
686
2
    WSS_load_subprotocols(conf);
687
2
    WSS_load_extensions(conf);
688
689
2
    header->content = h;
690
2
    header->length = l;
691
692
2
    code = WSS_parse_header(fd, header, conf);
693
694


2
    cr_assert(code == HttpStatus_OK);
695


2
    cr_assert(NULL != header->ws_extensions);
696


2
    cr_assert(NULL != header->ws_protocol);
697
698
2
    WSS_destroy_subprotocols();
699
2
    WSS_destroy_extensions();
700
2
    WSS_config_free(conf);
701
702
2
    WSS_free((void**) &conf);
703
2
    WSS_free_header(header);
704
2
}
705
706
4
Test(WSS_parse_header, valid_hybi10) {
707
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
708
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
709
2
    size_t l = strlen(HEADER_HYBI10);
710
2
    char *h = WSS_malloc(l*sizeof(char)+1);
711
2
    enum HttpStatus_Code code;
712
2
    int fd = -1;
713
714
2
    sprintf(h, "%s", HEADER_HYBI10);
715
716


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
717
718
2
    WSS_load_subprotocols(conf);
719
720
2
    header->content = h;
721
2
    header->length = l;
722
723
2
    code = WSS_parse_header(fd, header, conf);
724
725


2
    cr_assert(code == HttpStatus_OK);
726


2
    cr_assert(NULL != header->ws_protocol);
727
728
2
    WSS_destroy_subprotocols();
729
2
    WSS_config_free(conf);
730
731
2
    WSS_free((void**) &conf);
732
2
    WSS_free_header(header);
733
2
}
734
735
4
Test(WSS_parse_header, valid_hybi07) {
736
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
737
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
738
2
    size_t l = strlen(HEADER_HYBI07);
739
2
    char *h = WSS_malloc(l*sizeof(char)+1);
740
2
    enum HttpStatus_Code code;
741
2
    int fd = -1;
742
743
2
    sprintf(h, "%s", HEADER_HYBI07);
744
745


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
746
747
2
    WSS_load_subprotocols(conf);
748
749
2
    header->content = h;
750
2
    header->length = l;
751
752
2
    code = WSS_parse_header(fd, header, conf);
753
754


2
    cr_assert(code == HttpStatus_OK);
755


2
    cr_assert(NULL != header->ws_protocol);
756
757
2
    WSS_destroy_subprotocols();
758
2
    WSS_config_free(conf);
759
760
2
    WSS_free((void**) &conf);
761
2
    WSS_free_header(header);
762
2
}
763
764
4
Test(WSS_parse_header, valid_hybi06) {
765
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
766
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
767
2
    size_t l = strlen(HEADER_HYBI06);
768
2
    char *h = WSS_malloc(l*sizeof(char)+1);
769
2
    enum HttpStatus_Code code;
770
2
    int fd = -1;
771
772
2
    sprintf(h, "%s", HEADER_HYBI06);
773
774


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
775
776
2
    WSS_load_subprotocols(conf);
777
778
2
    header->content = h;
779
2
    header->length = l;
780
781
2
    code = WSS_parse_header(fd, header, conf);
782
783


2
    cr_assert(code == HttpStatus_OK);
784


2
    cr_assert(NULL != header->ws_protocol);
785
786
2
    WSS_destroy_subprotocols();
787
2
    WSS_config_free(conf);
788
789
2
    WSS_free((void**) &conf);
790
2
    WSS_free_header(header);
791
2
}
792
793
4
Test(WSS_parse_header, valid_hybi05) {
794
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
795
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
796
2
    size_t l = strlen(HEADER_HYBI05);
797
2
    char *h = WSS_malloc(l*sizeof(char)+1);
798
2
    enum HttpStatus_Code code;
799
2
    int fd = -1;
800
801
2
    sprintf(h, "%s", HEADER_HYBI05);
802
803


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
804
805
2
    header->content = h;
806
2
    header->length = l;
807
808
2
    code = WSS_parse_header(fd, header, conf);
809
810


2
    cr_assert(code == HttpStatus_OK);
811
812
2
    WSS_config_free(conf);
813
814
2
    WSS_free((void**) &conf);
815
2
    WSS_free_header(header);
816
2
}
817
818
4
Test(WSS_parse_header, valid_hybi04) {
819
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
820
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
821
2
    size_t l = strlen(HEADER_HYBI04);
822
2
    char *h = WSS_malloc(l*sizeof(char)+1);
823
2
    enum HttpStatus_Code code;
824
2
    int fd = -1;
825
826
2
    sprintf(h, "%s", HEADER_HYBI04);
827
828


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
829
830
2
    header->content = h;
831
2
    header->length = l;
832
833
2
    code = WSS_parse_header(fd, header, conf);
834
835


2
    cr_assert(code == HttpStatus_OK);
836
837
2
    WSS_config_free(conf);
838
839
2
    WSS_free((void**) &conf);
840
2
    WSS_free_header(header);
841
2
}
842
843
4
Test(WSS_parse_header, valid_hixie76) {
844
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
845
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
846
2
    size_t l = strlen(HEADER_HIXIE76);
847
2
    char *h = WSS_malloc(l*sizeof(char)+1);
848
2
    enum HttpStatus_Code code;
849
2
    int fd = -1;
850
851
2
    sprintf(h, "%s", HEADER_HIXIE76);
852
853


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
854
855
2
    header->content = h;
856
2
    header->length = l;
857
858
2
    code = WSS_parse_header(fd, header, conf);
859
860


2
    cr_assert(code == HttpStatus_OK);
861
862
2
    WSS_config_free(conf);
863
864
2
    WSS_free((void**) &conf);
865
2
    WSS_free_header(header);
866
2
}
867
868
4
Test(WSS_parse_header, valid_hixie75) {
869
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
870
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
871
2
    size_t l = strlen(HEADER_HIXIE75);
872
2
    char *h = WSS_malloc(l*sizeof(char)+1);
873
2
    enum HttpStatus_Code code;
874
2
    int fd = -1;
875
876
2
    sprintf(h, "%s", HEADER_HIXIE75);
877
878


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
879
880
2
    WSS_load_subprotocols(conf);
881
882
2
    header->content = h;
883
2
    header->length = l;
884
885
2
    code = WSS_parse_header(fd, header, conf);
886
887


2
    cr_assert(code == HttpStatus_OK);
888


2
    cr_assert(NULL != header->ws_protocol);
889
890
2
    WSS_config_free(conf);
891
892
2
    WSS_destroy_subprotocols();
893
2
    WSS_free((void**) &conf);
894
2
    WSS_free_header(header);
895
2
}
896
897
4
Test(WSS_parse_header, valid_hixie75_reverse_protocol) {
898
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
899
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
900
2
    size_t l = strlen(HEADER_HIXIE75_REVERSE_PROTOCOL);
901
2
    char *h = WSS_malloc(l*sizeof(char)+1);
902
2
    enum HttpStatus_Code code;
903
2
    int fd = -1;
904
905
2
    sprintf(h, "%s", HEADER_HIXIE75_REVERSE_PROTOCOL);
906
907


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
908
909
2
    WSS_load_subprotocols(conf);
910
911
2
    header->content = h;
912
2
    header->length = l;
913
914
2
    code = WSS_parse_header(fd, header, conf);
915
916


2
    cr_assert(code == HttpStatus_OK);
917
918
2
    WSS_destroy_subprotocols();
919
2
    WSS_config_free(conf);
920
921
2
    WSS_free((void**) &conf);
922
2
    WSS_free_header(header);
923
2
}
924
925
4
Test(WSS_parse_header, valid_hixie75_with_no_protocol) {
926
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
927
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
928
2
    size_t l = strlen(HEADER_HIXIE75_NO_PROTOCOL);
929
2
    char *h = WSS_malloc(l*sizeof(char)+1);
930
2
    enum HttpStatus_Code code;
931
2
    int fd = -1;
932
933
2
    sprintf(h, "%s", HEADER_HIXIE75_NO_PROTOCOL);
934
935


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
936
937
2
    header->content = h;
938
2
    header->length = l;
939
940
2
    code = WSS_parse_header(fd, header, conf);
941
942


2
    cr_assert(code == HttpStatus_OK);
943
944
2
    WSS_config_free(conf);
945
946
2
    WSS_free((void**) &conf);
947
2
    WSS_free_header(header);
948
2
}
949
950
TestSuite(WSS_upgrade_header, .init = setup, .fini = teardown);
951
952
4
Test(WSS_upgrade_header, upgrade_required_for_http_path) {
953
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
954
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
955
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
956
2
    size_t l = strlen(HEADER_HTTPS_HTTP_PATH);
957
2
    char *h = WSS_malloc(l*sizeof(char)+1);
958
2
    enum HttpStatus_Code code;
959
2
    int fd = -1;
960
961
2
    sprintf(h, "%s", HEADER_HTTPS_HTTP_PATH);
962
963


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
964
965
2
    header->content = h;
966
2
    header->length = l;
967
968
2
    code = WSS_parse_header(fd, header, conf);
969
970


2
    cr_assert(code == HttpStatus_OK);
971
972
2
    server->config = conf;
973
2
    server->port = conf->port_http;
974
2
    WSS_http_regex_init(server);
975
2
    code = WSS_upgrade_header(header, conf, server->re);
976


2
    cr_assert(code == HttpStatus_UpgradeRequired);
977
978
2
    regfree(server->re);
979
2
    WSS_free((void **)&server->re);
980
2
    WSS_free((void **)&server);
981
982
2
    WSS_config_free(conf);
983
984
2
    WSS_free((void**) &conf);
985
2
    WSS_free_header(header);
986
2
}
987
988
4
Test(WSS_upgrade_header, unsupported_http_path) {
989
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
990
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
991
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
992
2
    size_t l = strlen(HEADER_WRONG_HTTP_PATH);
993
2
    char *h = WSS_malloc(l*sizeof(char)+1);
994
2
    enum HttpStatus_Code code;
995
2
    int fd = -1;
996
997
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_PATH);
998
999


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1000
1001
2
    header->content = h;
1002
2
    header->length = l;
1003
1004
2
    code = WSS_parse_header(fd, header, conf);
1005
1006


2
    cr_assert(code == HttpStatus_OK);
1007
1008
2
    server->config = conf;
1009
2
    server->port = conf->port_http;
1010
2
    WSS_http_regex_init(server);
1011
2
    code = WSS_upgrade_header(header, conf, server->re);
1012


2
    cr_assert(code == HttpStatus_NotFound);
1013
1014
2
    code = WSS_upgrade_header(header, conf, server->re);
1015


2
    cr_assert(code == HttpStatus_NotFound);
1016
1017
2
    regfree(server->re);
1018
2
    WSS_free((void **)&server->re);
1019
2
    WSS_free((void **)&server);
1020
1021
2
    WSS_config_free(conf);
1022
1023
2
    WSS_free((void**) &conf);
1024
2
    WSS_free_header(header);
1025
2
}
1026
1027
4
Test(WSS_upgrade_header, unsupported_http_path_query) {
1028
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1029
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1030
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1031
2
    size_t l = strlen(HEADER_WRONG_HTTP_PATH_QUERY);
1032
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1033
2
    enum HttpStatus_Code code;
1034
2
    int fd = -1;
1035
1036
2
    sprintf(h, "%s", HEADER_WRONG_HTTP_PATH_QUERY);
1037
1038


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1039
1040
2
    header->content = h;
1041
2
    header->length = l;
1042
1043
2
    code = WSS_parse_header(fd, header, conf);
1044
1045


2
    cr_assert(code == HttpStatus_OK);
1046
1047
2
    server->config = conf;
1048
2
    server->port = conf->port_http;
1049
2
    WSS_http_regex_init(server);
1050
2
    code = WSS_upgrade_header(header, conf, server->re);
1051


2
    cr_assert(code == HttpStatus_NotFound);
1052
1053
2
    code = WSS_upgrade_header(header, conf, server->re);
1054


2
    cr_assert(code == HttpStatus_NotFound);
1055
1056
2
    regfree(server->re);
1057
2
    WSS_free((void **)&server->re);
1058
2
    WSS_free((void **)&server);
1059
2
    WSS_config_free(conf);
1060
1061
2
    WSS_free((void**) &conf);
1062
2
    WSS_free_header(header);
1063
2
}
1064
1065
4
Test(WSS_upgrade_header, supported_http_path_missing_host) {
1066
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1067
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1068
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1069
2
    size_t l = strlen(HEADER_HTTP_PATH);
1070
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1071
2
    enum HttpStatus_Code code;
1072
2
    int fd = -1;
1073
1074
2
    sprintf(h, "%s", HEADER_HTTP_PATH);
1075
1076


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1077
1078
2
    header->content = h;
1079
2
    header->length = l;
1080
1081
2
    code = WSS_parse_header(fd, header, conf);
1082
1083


2
    cr_assert(code == HttpStatus_OK);
1084
1085
2
    server->config = conf;
1086
2
    server->port = conf->port_http;
1087
2
    WSS_http_regex_init(server);
1088
2
    code = WSS_upgrade_header(header, conf, server->re);
1089


2
    cr_assert(code == HttpStatus_BadRequest);
1090
1091
2
    code = WSS_upgrade_header(header, conf, server->re);
1092


2
    cr_assert(code == HttpStatus_BadRequest);
1093
1094
2
    regfree(server->re);
1095
2
    WSS_free((void **)&server->re);
1096
2
    WSS_free((void **)&server);
1097
2
    WSS_config_free(conf);
1098
1099
2
    WSS_free((void**) &conf);
1100
2
    WSS_free_header(header);
1101
2
}
1102
1103
4
Test(WSS_upgrade_header, supported_http_path_query_missing_host) {
1104
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1105
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1106
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1107
2
    size_t l = strlen(HEADER_HTTP_PATH_QUERY);
1108
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1109
2
    enum HttpStatus_Code code;
1110
2
    int fd = -1;
1111
1112
2
    sprintf(h, "%s", HEADER_HTTP_PATH_QUERY);
1113
1114


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1115
1116
2
    header->content = h;
1117
2
    header->length = l;
1118
1119
2
    code = WSS_parse_header(fd, header, conf);
1120
1121


2
    cr_assert(code == HttpStatus_OK);
1122
1123
2
    server->config = conf;
1124
2
    server->port = conf->port_http;
1125
2
    WSS_http_regex_init(server);
1126
2
    code = WSS_upgrade_header(header, conf, server->re);
1127


2
    cr_assert(code == HttpStatus_BadRequest);
1128
1129
2
    code = WSS_upgrade_header(header, conf, server->re);
1130


2
    cr_assert(code == HttpStatus_BadRequest);
1131
1132
2
    regfree(server->re);
1133
2
    WSS_free((void **)&server->re);
1134
2
    WSS_free((void **)&server);
1135
2
    WSS_config_free(conf);
1136
1137
2
    WSS_free((void**) &conf);
1138
2
    WSS_free_header(header);
1139
2
}
1140
1141
4
Test(WSS_upgrade_header, wrong_host) {
1142
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1143
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1144
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1145
2
    size_t l = strlen(HEADER_HTTP);
1146
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1147
2
    enum HttpStatus_Code code;
1148
2
    int fd = -1;
1149
1150
2
    sprintf(h, "%s", HEADER_HTTP);
1151
1152


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1153
1154
2
    header->content = h;
1155
2
    header->length = l;
1156
2
    header->host = "mortz.dk";
1157
1158
2
    code = WSS_parse_header(fd, header, conf);
1159
1160


2
    cr_assert(code == HttpStatus_OK);
1161
1162
2
    server->config = conf;
1163
2
    server->port = conf->port_http;
1164
2
    WSS_http_regex_init(server);
1165
2
    code = WSS_upgrade_header(header, conf, server->re);
1166


2
    cr_assert(code == HttpStatus_BadRequest);
1167
1168
2
    code = WSS_upgrade_header(header, conf, server->re);
1169


2
    cr_assert(code == HttpStatus_BadRequest);
1170
1171
2
    regfree(server->re);
1172
2
    WSS_free((void **)&server->re);
1173
2
    WSS_free((void **)&server);
1174
2
    WSS_config_free(conf);
1175
1176
2
    WSS_free((void**) &conf);
1177
2
    WSS_free_header(header);
1178
2
}
1179
1180
4
Test(WSS_upgrade_header, supported_host_missing_upgrade) {
1181
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1182
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1183
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1184
2
    size_t l = strlen(HEADER_HTTP);
1185
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1186
2
    enum HttpStatus_Code code;
1187
2
    int fd = -1;
1188
1189
2
    sprintf(h, "%s", HEADER_HTTP);
1190
1191


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1192
1193
2
    header->content = h;
1194
2
    header->length = l;
1195
2
    header->host = "127.0.0.1";
1196
1197
2
    code = WSS_parse_header(fd, header, conf);
1198
1199


2
    cr_assert(code == HttpStatus_OK);
1200
1201
2
    server->config = conf;
1202
2
    server->port = conf->port_http;
1203
2
    WSS_http_regex_init(server);
1204
2
    code = WSS_upgrade_header(header, conf, server->re);
1205


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1206
1207
2
    code = WSS_upgrade_header(header, conf, server->re);
1208


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1209
1210
2
    regfree(server->re);
1211
2
    WSS_free((void **)&server->re);
1212
2
    WSS_free((void **)&server);
1213
2
    WSS_config_free(conf);
1214
1215
2
    WSS_free((void**) &conf);
1216
2
    WSS_free_header(header);
1217
2
}
1218
1219
4
Test(WSS_upgrade_header, too_short_upgrade) {
1220
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1221
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1222
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1223
2
    size_t l = strlen(HEADER_HTTP);
1224
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1225
2
    enum HttpStatus_Code code;
1226
2
    int fd = -1;
1227
1228
2
    sprintf(h, "%s", HEADER_HTTP);
1229
1230


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1231
1232
2
    header->content = h;
1233
2
    header->length = l;
1234
2
    header->host = "127.0.0.1";
1235
2
    header->ws_upgrade = "short";
1236
1237
2
    code = WSS_parse_header(fd, header, conf);
1238
1239


2
    cr_assert(code == HttpStatus_OK);
1240
1241
2
    server->config = conf;
1242
2
    server->port = conf->port_http;
1243
2
    WSS_http_regex_init(server);
1244
2
    code = WSS_upgrade_header(header, conf, server->re);
1245


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1246
1247
2
    code = WSS_upgrade_header(header, conf, server->re);
1248


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1249
1250
2
    regfree(server->re);
1251
2
    WSS_free((void **)&server->re);
1252
2
    WSS_free((void **)&server);
1253
2
    WSS_config_free(conf);
1254
1255
2
    WSS_free((void**) &conf);
1256
2
    WSS_free_header(header);
1257
2
}
1258
1259
4
Test(WSS_upgrade_header, supported_upgrade_missing_connection) {
1260
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1261
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1262
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1263
2
    size_t l = strlen(HEADER_HTTP);
1264
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1265
2
    enum HttpStatus_Code code;
1266
2
    int fd = -1;
1267
1268
2
    sprintf(h, "%s", HEADER_HTTP);
1269
1270


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1271
1272
2
    header->content = h;
1273
2
    header->length = l;
1274
2
    header->host = "127.0.0.1";
1275
2
    header->ws_upgrade = "websocket";
1276
1277
2
    code = WSS_parse_header(fd, header, conf);
1278
1279


2
    cr_assert(code == HttpStatus_OK);
1280
1281
2
    server->config = conf;
1282
2
    server->port = conf->port_http;
1283
2
    WSS_http_regex_init(server);
1284
2
    code = WSS_upgrade_header(header, conf, server->re);
1285


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1286
1287
2
    code = WSS_upgrade_header(header, conf, server->re);
1288


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1289
1290
2
    regfree(server->re);
1291
2
    WSS_free((void **)&server->re);
1292
2
    WSS_free((void **)&server);
1293
2
    WSS_config_free(conf);
1294
1295
2
    WSS_free((void**) &conf);
1296
2
    WSS_free_header(header);
1297
2
}
1298
1299
4
Test(WSS_upgrade_header, too_short_connection) {
1300
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1301
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1302
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1303
2
    size_t l = strlen(HEADER_HTTP);
1304
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1305
2
    enum HttpStatus_Code code;
1306
2
    int fd = -1;
1307
1308
2
    sprintf(h, "%s", HEADER_HTTP);
1309
1310


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1311
1312
2
    header->content = h;
1313
2
    header->length = l;
1314
2
    header->host = "127.0.0.1";
1315
2
    header->ws_upgrade = "websocket";
1316
2
    header->ws_connection = "short";
1317
1318
2
    code = WSS_parse_header(fd, header, conf);
1319
1320


2
    cr_assert(code == HttpStatus_OK);
1321
1322
2
    server->config = conf;
1323
2
    server->port = conf->port_http;
1324
2
    WSS_http_regex_init(server);
1325
2
    code = WSS_upgrade_header(header, conf, server->re);
1326


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1327
1328
2
    code = WSS_upgrade_header(header, conf, server->re);
1329


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1330
1331
2
    regfree(server->re);
1332
2
    WSS_free((void **)&server->re);
1333
2
    WSS_free((void **)&server);
1334
2
    WSS_config_free(conf);
1335
1336
2
    WSS_free((void**) &conf);
1337
2
    WSS_free_header(header);
1338
2
}
1339
1340
4
Test(WSS_upgrade_header, connection_comma) {
1341
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1342
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1343
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1344
2
    size_t l = strlen(HEADER_HTTP);
1345
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1346
2
    enum HttpStatus_Code code;
1347
2
    int fd = -1;
1348
1349
2
    sprintf(h, "%s", HEADER_HTTP);
1350
1351


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1352
1353
2
    header->content = h;
1354
2
    header->length = l;
1355
2
    header->host = "127.0.0.1";
1356
2
    header->ws_upgrade = "websocket";
1357
2
    header->ws_connection = ",,,,,,,,,,";
1358
1359
2
    code = WSS_parse_header(fd, header, conf);
1360
1361


2
    cr_assert(code == HttpStatus_OK);
1362
1363
2
    server->config = conf;
1364
2
    server->port = conf->port_http;
1365
2
    WSS_http_regex_init(server);
1366
2
    code = WSS_upgrade_header(header, conf, server->re);
1367


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1368
1369
2
    code = WSS_upgrade_header(header, conf, server->re);
1370


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1371
1372
2
    regfree(server->re);
1373
2
    WSS_free((void **)&server->re);
1374
2
    WSS_free((void **)&server);
1375
2
    WSS_config_free(conf);
1376
1377
2
    WSS_free((void**) &conf);
1378
2
    WSS_free_header(header);
1379
2
}
1380
1381
4
Test(WSS_upgrade_header, wrong_connection) {
1382
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1383
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1384
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1385
2
    size_t l = strlen(HEADER_HTTP);
1386
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1387
2
    enum HttpStatus_Code code;
1388
2
    int fd = -1;
1389
1390
2
    sprintf(h, "%s", HEADER_HTTP);
1391
1392


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1393
1394
2
    header->content = h;
1395
2
    header->length = l;
1396
2
    header->host = "127.0.0.1";
1397
2
    header->ws_upgrade = "websocket";
1398
2
    header->ws_connection = (char *) WSS_malloc(20);
1399
2
    sprintf(header->ws_connection, "%s", "tralalala, 42, test");
1400
1401
2
    code = WSS_parse_header(fd, header, conf);
1402
1403


2
    cr_assert(code == HttpStatus_OK);
1404
1405
2
    server->config = conf;
1406
2
    server->port = conf->port_http;
1407
2
    WSS_http_regex_init(server);
1408
2
    code = WSS_upgrade_header(header, conf, server->re);
1409


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1410
1411
2
    code = WSS_upgrade_header(header, conf, server->re);
1412


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1413
1414
2
    regfree(server->re);
1415
2
    WSS_free((void **)&server->re);
1416
2
    WSS_free((void **)&server);
1417
2
    WSS_config_free(conf);
1418
1419
2
    WSS_free((void**) &conf);
1420
2
    WSS_free((void**) &header->ws_connection);
1421
2
    WSS_free_header(header);
1422
2
}
1423
1424
4
Test(WSS_upgrade_header, supported_connection_missing_origin) {
1425
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1426
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1427
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1428
2
    size_t l = strlen(HEADER_HTTP);
1429
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1430
2
    enum HttpStatus_Code code;
1431
2
    int fd = -1;
1432
1433
2
    sprintf(h, "%s", HEADER_HTTP);
1434
1435


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1436
1437
2
    header->content = h;
1438
2
    header->length = l;
1439
2
    header->host = "127.0.0.1";
1440
2
    header->ws_upgrade = "websocket";
1441
2
    header->ws_connection = (char *) WSS_malloc(8);
1442
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1443
1444
2
    code = WSS_parse_header(fd, header, conf);
1445
1446


2
    cr_assert(code == HttpStatus_OK);
1447
1448
2
    server->config = conf;
1449
2
    server->port = conf->port_http;
1450
2
    WSS_http_regex_init(server);
1451
2
    code = WSS_upgrade_header(header, conf, server->re);
1452


2
    cr_assert(code == HttpStatus_Forbidden);
1453
1454
2
    code = WSS_upgrade_header(header, conf, server->re);
1455


2
    cr_assert(code == HttpStatus_Forbidden);
1456
1457
2
    regfree(server->re);
1458
2
    WSS_free((void **)&server->re);
1459
2
    WSS_free((void **)&server);
1460
2
    WSS_config_free(conf);
1461
1462
2
    WSS_free((void**) &conf);
1463
2
    WSS_free((void**) &header->ws_connection);
1464
2
    WSS_free_header(header);
1465
2
}
1466
1467
4
Test(WSS_upgrade_header, wrong_origin) {
1468
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1469
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1470
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1471
2
    size_t l = strlen(HEADER_HTTP);
1472
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1473
2
    enum HttpStatus_Code code;
1474
2
    int fd = -1;
1475
1476
2
    sprintf(h, "%s", HEADER_HTTP);
1477
1478


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1479
1480
2
    header->content = h;
1481
2
    header->length = l;
1482
2
    header->host = "127.0.0.1";
1483
2
    header->ws_upgrade = "websocket";
1484
2
    header->ws_connection = (char *) WSS_malloc(8);
1485
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1486
2
    header->ws_origin = "127.1.1.1";
1487
1488
2
    code = WSS_parse_header(fd, header, conf);
1489
1490


2
    cr_assert(code == HttpStatus_OK);
1491
1492
2
    server->config = conf;
1493
2
    server->port = conf->port_http;
1494
2
    WSS_http_regex_init(server);
1495
2
    code = WSS_upgrade_header(header, conf, server->re);
1496


2
    cr_assert(code == HttpStatus_Forbidden);
1497
1498
2
    code = WSS_upgrade_header(header, conf, server->re);
1499


2
    cr_assert(code == HttpStatus_Forbidden);
1500
1501
2
    regfree(server->re);
1502
2
    WSS_free((void **)&server->re);
1503
2
    WSS_free((void **)&server);
1504
2
    WSS_config_free(conf);
1505
1506
2
    WSS_free((void**) &conf);
1507
2
    WSS_free((void**) &header->ws_connection);
1508
2
    WSS_free_header(header);
1509
2
}
1510
1511
4
Test(WSS_upgrade_header, supported_origin_missing_type) {
1512
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1513
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1514
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1515
2
    size_t l = strlen(HEADER_HTTP);
1516
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1517
2
    enum HttpStatus_Code code;
1518
2
    int fd = -1;
1519
1520
2
    sprintf(h, "%s", HEADER_HTTP);
1521
1522


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1523
1524
2
    header->content = h;
1525
2
    header->length = l;
1526
2
    header->host = "127.0.0.1";
1527
2
    header->ws_upgrade = "websocket";
1528
2
    header->ws_connection = (char *) WSS_malloc(8);
1529
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1530
2
    header->ws_origin = "127.0.0.1";
1531
1532
2
    code = WSS_parse_header(fd, header, conf);
1533
1534


2
    cr_assert(code == HttpStatus_OK);
1535
1536
2
    server->config = conf;
1537
2
    server->port = conf->port_http;
1538
2
    WSS_http_regex_init(server);
1539
2
    code = WSS_upgrade_header(header, conf, server->re);
1540


2
    cr_assert(code == HttpStatus_NotImplemented);
1541
1542
2
    code = WSS_upgrade_header(header, conf, server->re);
1543


2
    cr_assert(code == HttpStatus_NotImplemented);
1544
1545
2
    regfree(server->re);
1546
2
    WSS_free((void **)&server->re);
1547
2
    WSS_free((void **)&server);
1548
2
    WSS_config_free(conf);
1549
1550
2
    WSS_free((void**) &conf);
1551
2
    WSS_free((void**) &header->ws_connection);
1552
2
    WSS_free_header(header);
1553
2
}
1554
1555
4
Test(WSS_upgrade_header, wrong_type) {
1556
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1557
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1558
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1559
2
    size_t l = strlen(HEADER_HTTP);
1560
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1561
2
    enum HttpStatus_Code code;
1562
2
    int fd = -1;
1563
1564
2
    sprintf(h, "%s", HEADER_HTTP);
1565
1566


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1567
1568
2
    header->content = h;
1569
2
    header->length = l;
1570
2
    header->host = "127.0.0.1";
1571
2
    header->ws_upgrade = "websocket";
1572
2
    header->ws_connection = (char *) WSS_malloc(8);
1573
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1574
2
    header->ws_origin = "127.0.0.1";
1575
2
    header->ws_type = HIXIE76;
1576
1577
2
    code = WSS_parse_header(fd, header, conf);
1578
1579


2
    cr_assert(code == HttpStatus_OK);
1580
1581
2
    server->config = conf;
1582
2
    server->port = conf->port_http;
1583
2
    WSS_http_regex_init(server);
1584
2
    code = WSS_upgrade_header(header, conf, server->re);
1585


2
    cr_assert(code == HttpStatus_NotImplemented);
1586
1587
2
    code = WSS_upgrade_header(header, conf, server->re);
1588


2
    cr_assert(code == HttpStatus_NotImplemented);
1589
1590
2
    regfree(server->re);
1591
2
    WSS_free((void **)&server->re);
1592
2
    WSS_free((void **)&server);
1593
2
    WSS_config_free(conf);
1594
1595
2
    WSS_free((void**) &conf);
1596
2
    WSS_free((void**) &header->ws_connection);
1597
2
    WSS_free_header(header);
1598
2
}
1599
1600
4
Test(WSS_upgrade_header, supported_type_missing_key) {
1601
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1602
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1603
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1604
2
    size_t l = strlen(HEADER_HTTP);
1605
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1606
2
    enum HttpStatus_Code code;
1607
2
    int fd = -1;
1608
1609
2
    sprintf(h, "%s", HEADER_HTTP);
1610
1611


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1612
1613
2
    header->content = h;
1614
2
    header->length = l;
1615
2
    header->host = "127.0.0.1";
1616
2
    header->ws_upgrade = "websocket";
1617
2
    header->ws_connection = (char *) WSS_malloc(8);
1618
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1619
2
    header->ws_origin = "127.0.0.1";
1620
2
    header->ws_type = HYBI10;
1621
1622
2
    code = WSS_parse_header(fd, header, conf);
1623
1624


2
    cr_assert(code == HttpStatus_OK);
1625
1626
2
    server->config = conf;
1627
2
    server->port = conf->port_http;
1628
2
    WSS_http_regex_init(server);
1629
2
    code = WSS_upgrade_header(header, conf, server->re);
1630


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1631
1632
2
    code = WSS_upgrade_header(header, conf, server->re);
1633


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1634
1635
2
    regfree(server->re);
1636
2
    WSS_free((void **)&server->re);
1637
2
    WSS_free((void **)&server);
1638
2
    WSS_config_free(conf);
1639
1640
2
    WSS_free((void**) &conf);
1641
2
    WSS_free((void**) &header->ws_connection);
1642
2
    WSS_free_header(header);
1643
2
}
1644
1645
4
Test(WSS_upgrade_header, wrong_key_length) {
1646
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1647
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1648
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1649
2
    size_t l = strlen(HEADER_HTTP);
1650
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1651
2
    enum HttpStatus_Code code;
1652
2
    int fd = -1;
1653
1654
2
    sprintf(h, "%s", HEADER_HTTP);
1655
1656


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1657
1658
2
    header->content = h;
1659
2
    header->length = l;
1660
2
    header->host = "127.0.0.1";
1661
2
    header->ws_upgrade = "websocket";
1662
2
    header->ws_connection = (char *) WSS_malloc(8);
1663
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1664
2
    header->ws_origin = "127.0.0.1";
1665
2
    header->ws_key = "key";
1666
2
    header->ws_type = RFC6455;
1667
1668
2
    code = WSS_parse_header(fd, header, conf);
1669
1670


2
    cr_assert(code == HttpStatus_OK);
1671
1672
2
    server->config = conf;
1673
2
    server->port = conf->port_http;
1674
2
    WSS_http_regex_init(server);
1675
2
    code = WSS_upgrade_header(header, conf, server->re);
1676


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1677
1678
2
    code = WSS_upgrade_header(header, conf, server->re);
1679


2
    cr_assert(code == HttpStatus_UpgradeRequired);
1680
1681
2
    regfree(server->re);
1682
2
    WSS_free((void **)&server->re);
1683
2
    WSS_free((void **)&server);
1684
2
    WSS_config_free(conf);
1685
1686
2
    WSS_free((void**) &conf);
1687
2
    WSS_free((void**) &header->ws_connection);
1688
2
    WSS_free_header(header);
1689
2
}
1690
1691
4
Test(WSS_upgrade_header, supported_key_switching_protocols) {
1692
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1693
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1694
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1695
2
    size_t l = strlen(HEADER_HTTP);
1696
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1697
2
    enum HttpStatus_Code code;
1698
2
    int fd = -1;
1699
1700
2
    sprintf(h, "%s", HEADER_HTTP);
1701
1702


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1703
1704
2
    header->content = h;
1705
2
    header->length = l;
1706
2
    header->host = "127.0.0.1";
1707
2
    header->ws_upgrade = "websocket";
1708
2
    header->ws_connection = (char *) WSS_malloc(8);
1709
2
    sprintf(header->ws_connection, "%s", "Upgrade");
1710
2
    header->ws_origin = "127.0.0.1";
1711
2
    header->ws_type = RFC6455;
1712
2
    header->ws_key = "dGhlIHNhbXBsZSBub25jZQ==";
1713
1714
2
    code = WSS_parse_header(fd, header, conf);
1715
1716


2
    cr_assert(code == HttpStatus_OK);
1717
1718
2
    server->config = conf;
1719
2
    server->port = conf->port_http;
1720
2
    WSS_http_regex_init(server);
1721
2
    code = WSS_upgrade_header(header, conf, server->re);
1722


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1723
1724
2
    code = WSS_upgrade_header(header, conf, server->re);
1725


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1726
1727
2
    regfree(server->re);
1728
2
    WSS_free((void **)&server->re);
1729
2
    WSS_free((void **)&server);
1730
2
    WSS_config_free(conf);
1731
1732
2
    WSS_free((void**) &conf);
1733
2
    WSS_free((void**) &header->ws_connection);
1734
2
    WSS_free_header(header);
1735
2
}
1736
1737
4
Test(WSS_upgrade_header, valid_rfc6455) {
1738
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1739
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1740
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1741
2
    size_t l = strlen(HEADER_RFC6455);
1742
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1743
2
    enum HttpStatus_Code code;
1744
2
    int fd = -1;
1745
1746
2
    sprintf(h, "%s", HEADER_RFC6455);
1747
1748


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1749
1750
2
    WSS_load_subprotocols(conf);
1751
2
    WSS_load_extensions(conf);
1752
1753
2
    header->content = h;
1754
2
    header->length = l;
1755
1756
2
    code = WSS_parse_header(fd, header, conf);
1757
1758


2
    cr_assert(code == HttpStatus_OK);
1759


2
    cr_assert(1 == header->ws_extensions_count);
1760


2
    cr_assert(strncmp("permessage-deflate", header->ws_extensions[0]->name, 18) == 0);
1761


2
    cr_assert(NULL != header->ws_extensions);
1762


2
    cr_assert(NULL != header->ws_protocol);
1763
1764
2
    server->config = conf;
1765
2
    server->port = conf->port_http;
1766
2
    WSS_http_regex_init(server);
1767
2
    code = WSS_upgrade_header(header, conf, server->re);
1768


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1769
1770
2
    code = WSS_upgrade_header(header, conf, server->re);
1771


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1772
1773
2
    regfree(server->re);
1774
2
    WSS_free((void **)&server->re);
1775
2
    WSS_free((void **)&server);
1776
2
    WSS_destroy_subprotocols();
1777
2
    WSS_destroy_extensions();
1778
2
    WSS_config_free(conf);
1779
1780
2
    WSS_free((void**) &conf);
1781
2
    WSS_free_header(header);
1782
2
}
1783
1784
4
Test(WSS_upgrade_header, valid_hybi10) {
1785
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1786
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1787
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1788
2
    size_t l = strlen(HEADER_HYBI10);
1789
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1790
2
    enum HttpStatus_Code code;
1791
2
    int fd = -1;
1792
1793
2
    sprintf(h, "%s", HEADER_HYBI10);
1794
1795


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1796
1797
2
    WSS_load_subprotocols(conf);
1798
1799
2
    header->content = h;
1800
2
    header->length = l;
1801
1802
2
    code = WSS_parse_header(fd, header, conf);
1803
1804


2
    cr_assert(code == HttpStatus_OK);
1805


2
    cr_assert(NULL != header->ws_protocol);
1806
1807
2
    server->config = conf;
1808
2
    server->port = conf->port_http;
1809
2
    WSS_http_regex_init(server);
1810
2
    code = WSS_upgrade_header(header, conf, server->re);
1811


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1812
1813
2
    code = WSS_upgrade_header(header, conf, server->re);
1814


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1815
1816
2
    regfree(server->re);
1817
2
    WSS_free((void **)&server->re);
1818
2
    WSS_free((void **)&server);
1819
2
    WSS_destroy_subprotocols();
1820
2
    WSS_config_free(conf);
1821
1822
2
    WSS_free((void**) &conf);
1823
2
    WSS_free_header(header);
1824
2
}
1825
1826
4
Test(WSS_upgrade_header, valid_hybi07) {
1827
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1828
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1829
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1830
2
    size_t l = strlen(HEADER_HYBI07);
1831
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1832
2
    enum HttpStatus_Code code;
1833
2
    int fd = -1;
1834
1835
2
    sprintf(h, "%s", HEADER_HYBI07);
1836
1837


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1838
1839
2
    WSS_load_subprotocols(conf);
1840
1841
2
    header->content = h;
1842
2
    header->length = l;
1843
1844
2
    code = WSS_parse_header(fd, header, conf);
1845
1846


2
    cr_assert(code == HttpStatus_OK);
1847


2
    cr_assert(NULL != header->ws_protocol);
1848
1849
2
    server->config = conf;
1850
2
    server->port = conf->port_http;
1851
2
    WSS_http_regex_init(server);
1852
2
    code = WSS_upgrade_header(header, conf, server->re);
1853


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1854
1855
2
    code = WSS_upgrade_header(header, conf, server->re);
1856


2
    cr_assert(code == HttpStatus_SwitchingProtocols);
1857
1858
2
    regfree(server->re);
1859
2
    WSS_free((void **)&server->re);
1860
2
    WSS_free((void **)&server);
1861
2
    WSS_destroy_subprotocols();
1862
2
    WSS_config_free(conf);
1863
1864
2
    WSS_free((void**) &conf);
1865
2
    WSS_free_header(header);
1866
2
}
1867
1868
4
Test(WSS_upgrade_header, valid_hybi06) {
1869
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1870
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1871
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1872
2
    size_t l = strlen(HEADER_HYBI06);
1873
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1874
2
    enum HttpStatus_Code code;
1875
2
    int fd = -1;
1876
1877
2
    sprintf(h, "%s", HEADER_HYBI06);
1878
1879


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1880
1881
2
    WSS_load_subprotocols(conf);
1882
1883
2
    header->content = h;
1884
2
    header->length = l;
1885
1886
2
    code = WSS_parse_header(fd, header, conf);
1887
1888


2
    cr_assert(code == HttpStatus_OK);
1889


2
    cr_assert(NULL != header->ws_protocol);
1890
1891
2
    server->config = conf;
1892
2
    server->port = conf->port_http;
1893
2
    WSS_http_regex_init(server);
1894
2
    code = WSS_upgrade_header(header, conf, server->re);
1895


2
    cr_assert(code == HttpStatus_NotImplemented);
1896
1897
2
    code = WSS_upgrade_header(header, conf, server->re);
1898


2
    cr_assert(code == HttpStatus_NotImplemented);
1899
1900
2
    regfree(server->re);
1901
2
    WSS_free((void **)&server->re);
1902
2
    WSS_free((void **)&server);
1903
2
    WSS_destroy_subprotocols();
1904
2
    WSS_config_free(conf);
1905
1906
2
    WSS_free((void**) &conf);
1907
2
    WSS_free_header(header);
1908
2
}
1909
1910
4
Test(WSS_upgrade_header, valid_hybi05) {
1911
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1912
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1913
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1914
2
    size_t l = strlen(HEADER_HYBI05);
1915
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1916
2
    enum HttpStatus_Code code;
1917
2
    int fd = -1;
1918
1919
2
    sprintf(h, "%s", HEADER_HYBI05);
1920
1921


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1922
1923
2
    header->content = h;
1924
2
    header->length = l;
1925
1926
2
    code = WSS_parse_header(fd, header, conf);
1927
1928


2
    cr_assert(code == HttpStatus_OK);
1929
1930
2
    server->config = conf;
1931
2
    server->port = conf->port_http;
1932
2
    WSS_http_regex_init(server);
1933
2
    code = WSS_upgrade_header(header, conf, server->re);
1934


2
    cr_assert(code == HttpStatus_NotImplemented);
1935
1936
2
    code = WSS_upgrade_header(header, conf, server->re);
1937


2
    cr_assert(code == HttpStatus_NotImplemented);
1938
1939
2
    regfree(server->re);
1940
2
    WSS_free((void **)&server->re);
1941
2
    WSS_free((void **)&server);
1942
2
    WSS_config_free(conf);
1943
1944
2
    WSS_free((void**) &conf);
1945
2
    WSS_free_header(header);
1946
2
}
1947
1948
4
Test(WSS_upgrade_header, valid_hybi04) {
1949
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1950
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1951
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1952
2
    size_t l = strlen(HEADER_HYBI04);
1953
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1954
2
    enum HttpStatus_Code code;
1955
2
    int fd = -1;
1956
1957
2
    sprintf(h, "%s", HEADER_HYBI04);
1958
1959


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1960
1961
2
    header->content = h;
1962
2
    header->length = l;
1963
1964
2
    code = WSS_parse_header(fd, header, conf);
1965
1966


2
    cr_assert(code == HttpStatus_OK);
1967
1968
2
    server->config = conf;
1969
2
    server->port = conf->port_http;
1970
2
    WSS_http_regex_init(server);
1971
2
    code = WSS_upgrade_header(header, conf, server->re);
1972


2
    cr_assert(code == HttpStatus_NotImplemented);
1973
1974
2
    code = WSS_upgrade_header(header, conf, server->re);
1975


2
    cr_assert(code == HttpStatus_NotImplemented);
1976
1977
2
    regfree(server->re);
1978
2
    WSS_free((void **)&server->re);
1979
2
    WSS_free((void **)&server);
1980
2
    WSS_config_free(conf);
1981
1982
2
    WSS_free((void**) &conf);
1983
2
    WSS_free_header(header);
1984
2
}
1985
1986
4
Test(WSS_upgrade_header, valid_hixie76) {
1987
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
1988
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
1989
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
1990
2
    size_t l = strlen(HEADER_HIXIE76);
1991
2
    char *h = WSS_malloc(l*sizeof(char)+1);
1992
2
    enum HttpStatus_Code code;
1993
2
    int fd = -1;
1994
1995
2
    sprintf(h, "%s", HEADER_HIXIE76);
1996
1997


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
1998
1999
2
    header->content = h;
2000
2
    header->length = l;
2001
2002
2
    code = WSS_parse_header(fd, header, conf);
2003
2004


2
    cr_assert(code == HttpStatus_OK);
2005
2006
2
    server->config = conf;
2007
2
    server->port = conf->port_http;
2008
2
    WSS_http_regex_init(server);
2009
2
    code = WSS_upgrade_header(header, conf, server->re);
2010


2
    cr_assert(code == HttpStatus_NotImplemented);
2011
2012
2
    code = WSS_upgrade_header(header, conf, server->re);
2013


2
    cr_assert(code == HttpStatus_NotImplemented);
2014
2015
2
    regfree(server->re);
2016
2
    WSS_free((void **)&server->re);
2017
2
    WSS_free((void **)&server);
2018
2
    WSS_config_free(conf);
2019
2020
2
    WSS_free((void**) &conf);
2021
2
    WSS_free_header(header);
2022
2
}
2023
2024
4
Test(WSS_upgrade_header, valid_hixie75) {
2025
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
2026
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
2027
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
2028
2
    size_t l = strlen(HEADER_HIXIE75);
2029
2
    char *h = WSS_malloc(l*sizeof(char)+1);
2030
2
    enum HttpStatus_Code code;
2031
2
    int fd = -1;
2032
2033
2
    sprintf(h, "%s", HEADER_HIXIE75);
2034
2035


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
2036
2037
2
    WSS_load_subprotocols(conf);
2038
2039
2
    header->content = h;
2040
2
    header->length = l;
2041
2042
2
    code = WSS_parse_header(fd, header, conf);
2043
2044


2
    cr_assert(code == HttpStatus_OK);
2045


2
    cr_assert(NULL != header->ws_protocol);
2046
2047
2
    server->config = conf;
2048
2
    server->port = conf->port_http;
2049
2
    WSS_http_regex_init(server);
2050
2
    code = WSS_upgrade_header(header, conf, server->re);
2051


2
    cr_assert(code == HttpStatus_NotImplemented);
2052
2053
2
    code = WSS_upgrade_header(header, conf, server->re);
2054


2
    cr_assert(code == HttpStatus_NotImplemented);
2055
2056
2
    regfree(server->re);
2057
2
    WSS_free((void **)&server->re);
2058
2
    WSS_free((void **)&server);
2059
2
    WSS_config_free(conf);
2060
2061
2
    WSS_destroy_subprotocols();
2062
2
    WSS_free((void**) &conf);
2063
2
    WSS_free_header(header);
2064
2
}
2065
2066
4
Test(WSS_upgrade_header, valid_hixie75_reverse_protocol) {
2067
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
2068
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
2069
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
2070
2
    size_t l = strlen(HEADER_HIXIE75_REVERSE_PROTOCOL);
2071
2
    char *h = WSS_malloc(l*sizeof(char)+1);
2072
2
    enum HttpStatus_Code code;
2073
2
    int fd = -1;
2074
2075
2
    sprintf(h, "%s", HEADER_HIXIE75_REVERSE_PROTOCOL);
2076
2077


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
2078
2079
2
    WSS_load_subprotocols(conf);
2080
2081
2
    header->content = h;
2082
2
    header->length = l;
2083
2084
2
    code = WSS_parse_header(fd, header, conf);
2085
2086


2
    cr_assert(code == HttpStatus_OK);
2087
2088
2
    server->config = conf;
2089
2
    server->port = conf->port_http;
2090
2
    WSS_http_regex_init(server);
2091
2
    code = WSS_upgrade_header(header, conf, server->re);
2092


2
    cr_assert(code == HttpStatus_NotImplemented);
2093
2094
2
    code = WSS_upgrade_header(header, conf, server->re);
2095


2
    cr_assert(code == HttpStatus_NotImplemented);
2096
2097
2
    regfree(server->re);
2098
2
    WSS_free((void **)&server->re);
2099
2
    WSS_free((void **)&server);
2100
2
    WSS_destroy_subprotocols();
2101
2
    WSS_config_free(conf);
2102
2103
2
    WSS_free((void**) &conf);
2104
2
    WSS_free_header(header);
2105
2
}
2106
2107
4
Test(WSS_upgrade_header, valid_hixie75_with_no_protocol_not_impletented) {
2108
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
2109
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
2110
2
    wss_header_t *header = (wss_header_t *) WSS_malloc(sizeof(wss_header_t));
2111
2
    size_t l = strlen(HEADER_HIXIE75_NO_PROTOCOL);
2112
2
    char *h = WSS_malloc(l*sizeof(char)+1);
2113
2
    enum HttpStatus_Code code;
2114
2
    int fd = -1;
2115
2116
2
    sprintf(h, "%s", HEADER_HIXIE75_NO_PROTOCOL);
2117
2118


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
2119
2120
2
    header->content = h;
2121
2
    header->length = l;
2122
2123
2
    code = WSS_parse_header(fd, header, conf);
2124
2125


2
    cr_assert(code == HttpStatus_OK);
2126
2127
2
    server->config = conf;
2128
2
    server->port = conf->port_http;
2129
2
    WSS_http_regex_init(server);
2130
2
    code = WSS_upgrade_header(header, conf, server->re);
2131


2
    cr_assert(code == HttpStatus_NotImplemented);
2132
2133
2
    code = WSS_upgrade_header(header, conf, server->re);
2134


2
    cr_assert(code == HttpStatus_NotImplemented);
2135
2136
2
    regfree(server->re);
2137
2
    WSS_free((void **)&server->re);
2138
2
    WSS_free((void **)&server);
2139
2
    WSS_config_free(conf);
2140
2141
2
    WSS_free((void**) &conf);
2142
2
    WSS_free_header(header);
2143
2
}