GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_config.c Lines: 175 175 100.0 %
Date: 2020-12-10 21:44:00 Branches: 312 1040 30.0 %

Line Branch Exec Source
1
#include <signal.h>
2
#include <stddef.h>
3
#include <criterion/criterion.h>
4
5
#include "alloc.h"
6
#include "config.h"
7
#include "error.h"
8
#include "str.h"
9
10
16
static void setup(void) {
11
#ifdef USE_RPMALLOC
12
16
    rpmalloc_initialize();
13
#endif
14
16
}
15
16
16
static void teardown(void) {
17
#ifdef USE_RPMALLOC
18
16
    rpmalloc_finalize();
19
#endif
20
16
}
21
22
TestSuite(WSS_config_load, .init = setup, .fini = teardown);
23
24
4
Test(WSS_config_load, none_existing_file) {
25
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
26
27


2
    cr_assert(WSS_CONFIG_LOAD_ERROR == WSS_config_load(conf, "resources/none_existing.json"));
28
29
2
    WSS_free((void**) &conf);
30
2
}
31
32
4
Test(WSS_config_load, no_starting_object) {
33
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
34
35


2
    cr_assert(WSS_CONFIG_JSON_ROOT_ERROR == WSS_config_load(conf, "resources/test_no_starting_object_wss.json"));
36
37
2
    WSS_free((void**) &conf);
38
2
}
39
40
4
Test(WSS_config_load, invalid_config) {
41
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
42
43


2
    cr_assert(WSS_CONFIG_JSON_PARSE_ERROR == WSS_config_load(conf, "resources/test_invalid_wss.json"));
44
45
2
    WSS_free((void**) &conf);
46
2
}
47
48
4
Test(WSS_config_load, invalid_host_config) {
49
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
50
51


2
    cr_assert(WSS_CONFIG_INVALID_HOST == WSS_config_load(conf, "resources/test_invalid_host_wss.json"));
52
53
2
    WSS_free((void**) &conf);
54
2
}
55
56
4
Test(WSS_config_load, invalid_origin_config) {
57
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
58
59


2
    cr_assert(WSS_CONFIG_INVALID_ORIGIN == WSS_config_load(conf, "resources/test_invalid_origin_wss.json"));
60
61
2
    WSS_free((void**) &conf);
62
2
}
63
64
4
Test(WSS_config_load, invalid_path_config) {
65
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
66
67


2
    cr_assert(WSS_CONFIG_INVALID_PATH == WSS_config_load(conf, "resources/test_invalid_path_wss.json"));
68
69
2
    WSS_free((void**) &conf);
70
2
}
71
72
4
Test(WSS_config_load, invalid_query_config) {
73
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
74
75


2
    cr_assert(WSS_CONFIG_INVALID_QUERY == WSS_config_load(conf, "resources/test_invalid_query_wss.json"));
76
77
2
    WSS_free((void**) &conf);
78
2
}
79
80
4
Test(WSS_config_load, invalid_subprotocol_config) {
81
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
82
83


2
    cr_assert(WSS_CONFIG_INVALID_SUBPROTOCOL == WSS_config_load(conf, "resources/test_invalid_subprotocol_wss.json"));
84
85
2
    WSS_free((void**) &conf);
86
2
}
87
88
4
Test(WSS_config_load, invalid_subprotocol_config2) {
89
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
90
91


2
    cr_assert(WSS_CONFIG_INVALID_SUBPROTOCOL == WSS_config_load(conf, "resources/test_invalid_subprotocol2_wss.json"));
92
93
2
    WSS_free((void**) &conf);
94
2
}
95
96
4
Test(WSS_config_load, invalid_extension_config) {
97
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
98
99


2
    cr_assert(WSS_CONFIG_INVALID_EXTENSION == WSS_config_load(conf, "resources/test_invalid_extension_wss.json"));
100
101
2
    WSS_free((void**) &conf);
102
2
}
103
104
4
Test(WSS_config_load, invalid_extension_config2) {
105
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
106
107


2
    cr_assert(WSS_CONFIG_INVALID_EXTENSION == WSS_config_load(conf, "resources/test_invalid_extension2_wss.json"));
108
109
2
    WSS_free((void**) &conf);
110
2
}
111
112
4
Test(WSS_config_load, subprotocol_and_extension_with_invalid_config) {
113
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
114
115


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_subprotocol_and_extension_with_invalid_config_wss.json"));
116
117
2
    WSS_free((void**) &conf);
118
2
}
119
120
4
Test(WSS_config_load, no_port_config) {
121
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
122
123


2
    cr_assert(WSS_CONFIG_NO_PORT_ERROR == WSS_config_load(conf, "resources/test_no_port_wss.json"));
124
125
2
    WSS_free((void**) &conf);
126
2
}
127
128
4
Test(WSS_config_load, valid_config_empty_arrays) {
129
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
130
131


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss_empty_arrays.json"));
132
133
    // Favicon
134


2
    cr_expect(strncmp(conf->favicon, "favicon.ico", 11) == 0);
135
136
    // Check SSL
137


2
    cr_expect(strncmp(conf->ssl_ca_file, "root.pem", 8) == 0);
138


2
    cr_expect(strncmp(conf->ssl_key, "key.pem", 7) == 0);
139


2
    cr_expect(strncmp(conf->ssl_cert, "cert.pem", 8) == 0);
140


2
    cr_expect(strncmp(conf->ssl_ca_path, "/usr/lib/ssl/certs/", 19) == 0);
141


2
    cr_expect(strncmp(conf->ssl_dhparam, "dhparam.pem", 11) == 0);
142


2
    cr_expect(strncmp(conf->ssl_cipher_list, "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256", 437) == 0);
143


2
    cr_expect(strncmp(conf->ssl_cipher_suites, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_8_SHA256:TLS_AES_128_CCM_SHA256", 122) == 0);
144


2
    cr_expect(conf->ssl_compression == false);
145


2
    cr_expect(conf->ssl_peer_cert == false);
146
147
    // Log
148


2
    cr_expect(conf->log == 7);
149
150
    // Hosts
151


2
    cr_expect(conf->hosts == NULL);
152


2
    cr_expect(conf->hosts_length == 0);
153
154
    // Origins
155


2
    cr_expect(conf->origins == NULL);
156


2
    cr_expect(conf->origins_length == 0);
157
158
    // Paths
159


2
    cr_expect(conf->paths == NULL);
160


2
    cr_expect(conf->paths_length == 0);
161
162
    // Query parameters
163


2
    cr_expect(conf->queries == NULL);
164


2
    cr_expect(conf->queries_length == 0);
165
166
    // Timeouts
167


2
    cr_expect(conf->timeout_poll == -1);
168


2
    cr_expect(conf->timeout_read == -1);
169


2
    cr_expect(conf->timeout_write == -1);
170


2
    cr_expect(conf->timeout_client == -1);
171


2
    cr_expect(conf->timeout_pings == 0);
172
173
    // Ports
174


2
    cr_expect(conf->port_http == 9010);
175


2
    cr_expect(conf->port_https == 9011);
176
177
    // Sizes
178


2
    cr_expect(conf->size_uri == 128);
179


2
    cr_expect(conf->size_ringbuffer == 128);
180


2
    cr_expect(conf->size_buffer == 25600);
181


2
    cr_expect(conf->size_header == 1024);
182


2
    cr_expect(conf->size_thread == 524288);
183


2
    cr_expect(conf->size_frame == 16777215);
184


2
    cr_expect(conf->size_payload == 1024);
185


2
    cr_expect(conf->max_frames == 1048576);
186
187
    // Pool
188


2
    cr_expect(conf->pool_workers == 4);
189


2
    cr_expect(conf->pool_retries == 5);
190
191
    // Subprotocols
192


2
    cr_expect(conf->subprotocols_length == 0);
193


2
    cr_expect(conf->subprotocols == NULL);
194
195
    // Extensions
196


2
    cr_expect(conf->extensions_length == 0);
197


2
    cr_expect(conf->extensions_config == NULL);
198


2
    cr_expect(conf->extensions == NULL);
199
200


2
    cr_assert(WSS_config_free(conf) == WSS_SUCCESS);
201
2
    WSS_free((void**) &conf);
202
2
}
203
204
4
Test(WSS_config_load, valid_config) {
205
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
206
207


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
208
209
    // Favicon
210


2
    cr_expect(strncmp(conf->favicon, "favicon.ico", 11) == 0);
211
212
    // Check SSL
213


2
    cr_expect(strncmp(conf->ssl_ca_file, "root.pem", 8) == 0);
214


2
    cr_expect(strncmp(conf->ssl_key, "key.pem", 7) == 0);
215


2
    cr_expect(strncmp(conf->ssl_cert, "cert.pem", 8) == 0);
216


2
    cr_expect(strncmp(conf->ssl_ca_path, "/usr/lib/ssl/certs/", 19) == 0);
217


2
    cr_expect(strncmp(conf->ssl_dhparam, "dhparam.pem", 11) == 0);
218


2
    cr_expect(strncmp(conf->ssl_cipher_list, "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256", 437) == 0);
219


2
    cr_expect(strncmp(conf->ssl_cipher_suites, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_8_SHA256:TLS_AES_128_CCM_SHA256", 122) == 0);
220


2
    cr_expect(conf->ssl_compression == false);
221


2
    cr_expect(conf->ssl_peer_cert == false);
222
223
    // Log
224


2
    cr_expect(conf->log == 7);
225
226
    // Hosts
227


2
    cr_expect(strinarray("localhost", (const char **)conf->hosts, conf->hosts_length) == 0);
228


2
    cr_expect(strinarray("127.0.0.1", (const char **)conf->hosts, conf->hosts_length) == 0);
229
230
    // Origins
231


2
    cr_expect(strinarray("localhost", (const char **)conf->origins, conf->origins_length) == 0);
232


2
    cr_expect(strinarray("127.0.0.1", (const char **)conf->origins, conf->origins_length) == 0);
233
234
    // Paths
235


2
    cr_expect(strinarray("test/path", (const char **)conf->paths, conf->paths_length) == 0);
236


2
    cr_expect(strinarray("another/test/path", (const char **)conf->paths, conf->paths_length) == 0);
237
238
    // Query parameters
239


2
    cr_expect(strinarray("csrf_token=[^&]*", (const char **)conf->queries, conf->queries_length) == 0);
240


2
    cr_expect(strinarray("access_token=[^&]*", (const char **)conf->queries, conf->queries_length) == 0);
241
242
    // Timeouts
243


2
    cr_expect(conf->timeout_poll == 10);
244


2
    cr_expect(conf->timeout_read == 10);
245


2
    cr_expect(conf->timeout_write == 10);
246


2
    cr_expect(conf->timeout_client == 600);
247


2
    cr_expect(conf->timeout_pings == 1);
248
249
    // Ports
250


2
    cr_expect(conf->port_http == 9010);
251


2
    cr_expect(conf->port_https == 9011);
252
253
    // Sizes
254


2
    cr_expect(conf->size_uri == 128);
255


2
    cr_expect(conf->size_ringbuffer == 128);
256


2
    cr_expect(conf->size_buffer == 25600);
257


2
    cr_expect(conf->size_header == 1024);
258


2
    cr_expect(conf->size_thread == 524288);
259


2
    cr_expect(conf->size_frame == 128);
260


2
    cr_expect(conf->size_payload == 1024);
261


2
    cr_expect(conf->max_frames == 1048576);
262
263
    // Pool
264


2
    cr_expect(conf->pool_workers == 4);
265


2
    cr_expect(conf->pool_retries == 5);
266
267
    // Subprotocols
268


2
    cr_expect(conf->subprotocols_length == 2);
269


2
    cr_expect(strinarray("subprotocols/echo/echo.so", (const char **)conf->subprotocols, conf->subprotocols_length) == 0);
270


2
    cr_expect(strinarray("subprotocols/broadcast/broadcast.so", (const char **)conf->subprotocols, conf->subprotocols_length) == 0);
271
272
    // Extensions
273


2
    cr_expect(conf->extensions_length == 1);
274


2
    cr_expect(strinarray("extensions/permessage-deflate/permessage-deflate.so", (const char **)conf->extensions, conf->extensions_length) == 0);
275


2
    cr_expect(strinarray("server_max_window_bits=10;client_max_window_bits=10;memory_level=8", (const char **)conf->extensions_config, conf->extensions_length) == 0);
276
277


2
    cr_assert(WSS_config_free(conf) == WSS_SUCCESS);
278
2
    WSS_free((void**) &conf);
279
2
}
280
281
TestSuite(WSS_config_free, .init = setup, .fini = teardown);
282
283
4
Test(WSS_config_free, size_zero) {
284
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
285
286
2
    WSS_config_load(conf, "resources/test_wss.json");
287
288


2
    cr_assert(WSS_config_free(conf) == WSS_SUCCESS);
289
290


2
    cr_expect(conf->data == NULL);
291


2
    cr_expect(conf->hosts == NULL);
292


2
    cr_expect(conf->origins == NULL);
293
294
2
    WSS_free((void**) &conf);
295
2
}