GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_subprotocols.c Lines: 149 149 100.0 %
Date: 2020-12-10 21:44:00 Branches: 36 120 30.0 %

Line Branch Exec Source
1
#include <string.h>
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <criterion/criterion.h>
5
6
#include "alloc.h"
7
#include "config.h"
8
#include "subprotocols.h"
9
10
#define WSS_SUBPROTOCOL_NO_FILE "resources/test-sub-no-file.so"
11
#define WSS_SUBPROTOCOL_NO_SET_ALLOCATORS "resources/test-sub-no-set-allocators.so"
12
#define WSS_SUBPROTOCOL_NO_INIT "resources/test-sub-no-init.so"
13
#define WSS_SUBPROTOCOL_NO_CONNECT "resources/test-sub-no-connect.so"
14
#define WSS_SUBPROTOCOL_NO_MESSAGE "resources/test-sub-no-message.so"
15
#define WSS_SUBPROTOCOL_NO_WRITE "resources/test-sub-no-write.so"
16
#define WSS_SUBPROTOCOL_NO_CLOSE "resources/test-sub-no-close.so"
17
#define WSS_SUBPROTOCOL_NO_DESTROY "resources/test-sub-no-destroy.so"
18
19
10
static void setup(void) {
20
#ifdef USE_RPMALLOC
21
10
    rpmalloc_initialize();
22
#endif
23
10
}
24
25
10
static void teardown(void) {
26
#ifdef USE_RPMALLOC
27
10
    rpmalloc_finalize();
28
#endif
29
10
}
30
31
TestSuite(WSS_load_subprotocols, .init = setup, .fini = teardown);
32
33
4
Test(WSS_load_subprotocols, no_config) {
34
2
    WSS_load_subprotocols(NULL);
35
36


2
    cr_assert(NULL == WSS_find_subprotocol(NULL));
37
38
2
    WSS_destroy_subprotocols();
39
2
}
40
41
4
Test(WSS_load_subprotocols, no_file) {
42
2
    size_t length = 1;
43
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
44
45
2
    conf->subprotocols_length = 1;
46
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
47
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_FILE)+1));
48
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_FILE);
49
50
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
51
2
    conf->subprotocols_config[0] = NULL;
52
53
2
    WSS_load_subprotocols(conf);
54
55


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-file"));
56
57
2
    WSS_destroy_subprotocols();
58
2
    WSS_free((void**) &conf->subprotocols[0]);
59
2
    WSS_config_free(conf);
60
2
    WSS_free((void**) &conf);
61
2
}
62
63
4
Test(WSS_load_subprotocols, no_set_allocators) {
64
2
    size_t length = 1;
65
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
66
67
2
    conf->subprotocols_length = 1;
68
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
69
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_SET_ALLOCATORS)+1));
70
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_SET_ALLOCATORS);
71
72
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
73
2
    conf->subprotocols_config[0] = NULL;
74
75
2
    WSS_load_subprotocols(conf);
76
77


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-set-allocators"));
78
79
2
    WSS_destroy_subprotocols();
80
2
    WSS_free((void**) &conf->subprotocols[0]);
81
2
    WSS_config_free(conf);
82
2
    WSS_free((void**) &conf);
83
2
}
84
85
4
Test(WSS_load_subprotocols, no_init) {
86
2
    size_t length = 1;
87
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
88
89
2
    conf->subprotocols_length = 1;
90
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
91
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_INIT)+1));
92
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_INIT);
93
94
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
95
2
    conf->subprotocols_config[0] = NULL;
96
97
2
    WSS_load_subprotocols(conf);
98
99


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-init"));
100
101
2
    WSS_destroy_subprotocols();
102
2
    WSS_free((void**) &conf->subprotocols[0]);
103
2
    WSS_config_free(conf);
104
2
    WSS_free((void**) &conf);
105
2
}
106
107
4
Test(WSS_load_subprotocols, no_connect) {
108
2
    size_t length = 1;
109
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
110
111
2
    conf->subprotocols_length = 1;
112
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
113
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_CONNECT)+1));
114
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_CONNECT);
115
116
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
117
2
    conf->subprotocols_config[0] = NULL;
118
119
2
    WSS_load_subprotocols(conf);
120
121


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-connect"));
122
123
2
    WSS_destroy_subprotocols();
124
2
    WSS_free((void**) &conf->subprotocols[0]);
125
2
    WSS_config_free(conf);
126
2
    WSS_free((void**) &conf);
127
2
}
128
129
4
Test(WSS_load_subprotocols, no_message) {
130
2
    size_t length = 1;
131
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
132
133
2
    conf->subprotocols_length = 1;
134
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
135
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_MESSAGE)+1));
136
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_MESSAGE);
137
138
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
139
2
    conf->subprotocols_config[0] = NULL;
140
141
2
    WSS_load_subprotocols(conf);
142
143


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-message"));
144
145
2
    WSS_destroy_subprotocols();
146
2
    WSS_free((void**) &conf->subprotocols[0]);
147
2
    WSS_config_free(conf);
148
2
    WSS_free((void**) &conf);
149
2
}
150
151
4
Test(WSS_load_subprotocols, no_write) {
152
2
    size_t length = 1;
153
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
154
155
2
    conf->subprotocols_length = 1;
156
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
157
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_WRITE)+1));
158
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_WRITE);
159
160
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
161
2
    conf->subprotocols_config[0] = NULL;
162
163
2
    WSS_load_subprotocols(conf);
164
165


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-write"));
166
167
2
    WSS_destroy_subprotocols();
168
2
    WSS_free((void**) &conf->subprotocols[0]);
169
2
    WSS_config_free(conf);
170
2
    WSS_free((void**) &conf);
171
2
}
172
173
4
Test(WSS_load_subprotocols, no_close) {
174
2
    size_t length = 1;
175
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
176
177
2
    conf->subprotocols_length = 1;
178
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
179
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_CLOSE)+1));
180
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_CLOSE);
181
182
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
183
2
    conf->subprotocols_config[0] = NULL;
184
185
2
    WSS_load_subprotocols(conf);
186
187


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-close"));
188
189
2
    WSS_destroy_subprotocols();
190
2
    WSS_free((void**) &conf->subprotocols[0]);
191
2
    WSS_config_free(conf);
192
2
    WSS_free((void**) &conf);
193
2
}
194
195
4
Test(WSS_load_subprotocols, no_destroy) {
196
2
    size_t length = 1;
197
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
198
199
2
    conf->subprotocols_length = 1;
200
2
    conf->subprotocols = WSS_calloc(length, sizeof(char *));
201
2
    conf->subprotocols[0] = WSS_malloc(sizeof(char)*(strlen(WSS_SUBPROTOCOL_NO_DESTROY)+1));
202
2
    sprintf(conf->subprotocols[0], "%s", WSS_SUBPROTOCOL_NO_DESTROY);
203
204
2
    conf->subprotocols_config = WSS_calloc(length, sizeof(char *));
205
2
    conf->subprotocols_config[0] = NULL;
206
207
2
    WSS_load_subprotocols(conf);
208
209


2
    cr_assert(NULL == WSS_find_subprotocol("test-sub-no-destroy"));
210
211
2
    WSS_destroy_subprotocols();
212
2
    WSS_free((void**) &conf->subprotocols[0]);
213
2
    WSS_config_free(conf);
214
2
    WSS_free((void**) &conf);
215
2
}
216
217
4
Test(WSS_load_subprotocols, successful) {
218
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
219


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
220
221
2
    WSS_load_subprotocols(conf);
222
223


2
    cr_assert(NULL != WSS_find_subprotocol("echo"));
224


2
    cr_assert(NULL != WSS_find_subprotocol("broadcast"));
225
226
2
    WSS_destroy_subprotocols();
227
2
    WSS_config_free(conf);
228
2
    WSS_free((void**) &conf);
229
2
}
230