GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_socket.c Lines: 127 127 100.0 %
Date: 2020-12-10 21:44:00 Branches: 111 370 30.0 %

Line Branch Exec Source
1
#include <criterion/criterion.h>
2
3
#include "alloc.h"
4
#include "server.h"
5
#include "http.h"
6
#include "socket.h"
7
#include "rpmalloc.h"
8
9
16
static void setup(void) {
10
#ifdef USE_RPMALLOC
11
16
    rpmalloc_initialize();
12
#endif
13
16
}
14
15
16
static void teardown(void) {
16
#ifdef USE_RPMALLOC
17
16
    rpmalloc_finalize();
18
#endif
19
16
}
20
21
TestSuite(WSS_socket_create, .init = setup, .fini = teardown);
22
23
4
Test(WSS_socket_create, null_server) {
24


2
    cr_assert(WSS_SOCKET_CREATE_ERROR == WSS_socket_create(NULL));
25
2
}
26
27
4
Test(WSS_socket_create, creating_socket) {
28
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
29
30


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
31
32
    // Cleanup
33
2
    WSS_http_server_free(server);
34
2
    pthread_mutex_destroy(&server->lock);
35
2
    WSS_free((void **) &server);
36
2
}
37
38
TestSuite(WSS_socket_reuse, .init = setup, .fini = teardown);
39
40
4
Test(WSS_socket_reuse, invalid_fd) {
41


2
    cr_assert(WSS_SOCKET_REUSE_ERROR == WSS_socket_reuse(-1));
42
2
}
43
44
4
Test(WSS_socket_reuse, reuse_socket) {
45
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
46
47


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
48


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
49
50
    // Cleanup
51
2
    WSS_http_server_free(server);
52
2
    pthread_mutex_destroy(&server->lock);
53
2
    WSS_free((void **) &server);
54
2
}
55
56
TestSuite(WSS_socket_bind, .init = setup, .fini = teardown);
57
58
4
Test(WSS_socket_bind, null_server) {
59


2
    cr_assert(WSS_SOCKET_BIND_ERROR == WSS_socket_bind(NULL));
60
2
}
61
62
4
Test(WSS_socket_bind, invalid_server_port) {
63
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
64
2
    server->port = -1;
65
66


2
    cr_assert(WSS_SOCKET_BIND_ERROR == WSS_socket_bind(server));
67
68
    // Cleanup
69
2
    WSS_http_server_free(server);
70
2
    pthread_mutex_destroy(&server->lock);
71
2
    WSS_free((void **) &server);
72
2
}
73
74
4
Test(WSS_socket_bind, invalid_server_fd) {
75
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
76
2
    server->port = 999;
77
2
    server->fd = -1;
78
79


2
    cr_assert(WSS_SOCKET_BIND_ERROR == WSS_socket_bind(server));
80
81
    // Cleanup
82
2
    WSS_http_server_free(server);
83
2
    pthread_mutex_destroy(&server->lock);
84
2
    WSS_free((void **) &server);
85
2
}
86
87
4
Test(WSS_socket_bind, bind_socket) {
88
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
89
2
    server->port = 4567;
90
91


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
92


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
93


2
    cr_assert(WSS_SUCCESS == WSS_socket_bind(server));
94
95
    // Cleanup
96
2
    WSS_http_server_free(server);
97
2
    pthread_mutex_destroy(&server->lock);
98
2
    WSS_free((void **) &server);
99
2
}
100
101
TestSuite(WSS_socket_non_blocking, .init = setup, .fini = teardown);
102
103
4
Test(WSS_socket_non_blocking, invalid_fd) {
104


2
    cr_assert(WSS_SOCKET_NONBLOCKED_ERROR == WSS_socket_non_blocking(-1));
105
2
}
106
107
4
Test(WSS_socket_non_blocking, non_blocking_socket) {
108
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
109
2
    server->port = 4567;
110
111


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
112


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
113


2
    cr_assert(WSS_SUCCESS == WSS_socket_bind(server));
114


2
    cr_assert(WSS_SUCCESS == WSS_socket_non_blocking(server->fd));
115
116
    // Cleanup
117
2
    WSS_http_server_free(server);
118
2
    pthread_mutex_destroy(&server->lock);
119
2
    WSS_free((void **) &server);
120
2
}
121
122
TestSuite(WSS_socket_listen, .init = setup, .fini = teardown);
123
124
4
Test(WSS_socket_listen, invalid_fd) {
125


2
    cr_assert(WSS_SOCKET_LISTEN_ERROR == WSS_socket_listen(-1));
126
2
}
127
128
4
Test(WSS_socket_listen, listen_socket) {
129
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
130
2
    server->port = 4567;
131
132


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
133


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
134


2
    cr_assert(WSS_SUCCESS == WSS_socket_bind(server));
135


2
    cr_assert(WSS_SUCCESS == WSS_socket_non_blocking(server->fd));
136


2
    cr_assert(WSS_SUCCESS == WSS_socket_listen(server->fd));
137
138
    // Cleanup
139
2
    WSS_http_server_free(server);
140
2
    pthread_mutex_destroy(&server->lock);
141
2
    WSS_free((void **) &server);
142
2
}
143
144
TestSuite(WSS_socket_threadpool, .init = setup, .fini = teardown);
145
146
4
Test(WSS_socket_threadpool, null_server) {
147


2
    cr_assert(WSS_THREADPOOL_CREATE_ERROR == WSS_socket_threadpool(NULL));
148
2
}
149
150
4
Test(WSS_socket_threadpool, null_config) {
151
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
152


2
    cr_assert(WSS_THREADPOOL_CREATE_ERROR == WSS_socket_threadpool(server));
153
2
}
154
155
4
Test(WSS_socket_threadpool, invalid_create_threadpool_params) {
156
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
157
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
158
159
2
    server->port = conf->port_http;
160
2
    server->config = conf;
161
162


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
163


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
164


2
    cr_assert(WSS_SUCCESS == WSS_socket_bind(server));
165


2
    cr_assert(WSS_SUCCESS == WSS_socket_non_blocking(server->fd));
166


2
    cr_assert(WSS_SUCCESS == WSS_socket_listen(server->fd));
167


2
    cr_assert(WSS_THREADPOOL_CREATE_ERROR == WSS_socket_threadpool(server));
168
169
    // Cleanup
170
2
    WSS_http_server_free(server);
171
2
    pthread_mutex_destroy(&server->lock);
172
2
    WSS_free((void **) &server);
173
2
    WSS_free((void**) &conf);
174
2
}
175
176
4
Test(WSS_socket_threadpool, threadpool_socket) {
177
2
    wss_server_t *server = (wss_server_t *) WSS_malloc(sizeof(wss_server_t));
178
2
    wss_config_t *conf = (wss_config_t *) WSS_malloc(sizeof(wss_config_t));
179
180


2
    cr_assert(WSS_SUCCESS == WSS_config_load(conf, "resources/test_wss.json"));
181
182
2
    server->port = conf->port_http;
183
2
    server->config = conf;
184
185


2
    cr_assert(WSS_SUCCESS == WSS_socket_create(server));
186


2
    cr_assert(WSS_SUCCESS == WSS_socket_reuse(server->fd));
187


2
    cr_assert(WSS_SUCCESS == WSS_socket_bind(server));
188


2
    cr_assert(WSS_SUCCESS == WSS_socket_non_blocking(server->fd));
189


2
    cr_assert(WSS_SUCCESS == WSS_socket_listen(server->fd));
190


2
    cr_assert(WSS_SUCCESS == WSS_socket_threadpool(server));
191
192
    // Cleanup
193
2
    WSS_http_server_free(server);
194
2
    pthread_mutex_destroy(&server->lock);
195
2
    WSS_free((void **) &server);
196
2
    WSS_config_free(conf);
197
2
    WSS_free((void**) &conf);
198
2
}