-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.c
259 lines (213 loc) · 8.41 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
static int test_init(struct harness_t *harness)
{
/* This function may be called multiple times. */
BTASSERT(ssl_module_init() == 0);
BTASSERT(ssl_module_init() == 0);
return (0);
}
static int test_client(struct harness_t *harness_p)
{
struct ssl_context_t context;
struct ssl_socket_t ssl_socket;
struct socket_t socket;
struct inet_addr_t addr;
char buf[8];
const char *server_hostname_p;
const char *cipher_p;
const char *protocol_p;
int number_of_secret_bits;
/* Create a context with default settings. */
BTASSERT(ssl_context_init(&context, ssl_protocol_tls_v1_0) == 0);
BTASSERT(ssl_context_set_verify_mode(&context,
ssl_verify_mode_cert_none_t) == 0);
BTASSERT(ssl_context_set_verify_mode(&context,
ssl_verify_mode_cert_required_t) == 0);
BTASSERT(ssl_context_load_verify_location(&context, "foo") == 0);
/* Create a socket and connect to the server. */
BTASSERT(socket_open_tcp(&socket) == 0);
inet_aton("1.2.3.4", &addr.ip);
addr.port = 1234;
BTASSERT(socket_connect(&socket, &addr) == 0);
/* Wrap the socket in SSL. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
0,
"client_foo") == 0);
/* Test a few functions. */
BTASSERT(ssl_socket_size(&ssl_socket) == 0);
server_hostname_p = ssl_socket_get_server_hostname(&ssl_socket);
BTASSERT(strcmp(server_hostname_p, "client_foo") == 0);
BTASSERT(ssl_socket_get_cipher(&ssl_socket,
&cipher_p,
&protocol_p,
&number_of_secret_bits) == 0);
BTASSERT(strcmp(cipher_p, "TLS-RSA-WITH-AES-256-GCM-SHA384") == 0);
BTASSERT(strcmp(protocol_p, "TLSv1.1") == 0);
BTASSERT(number_of_secret_bits == -1);
/* Transfer data to and from the server. */
BTASSERT(ssl_socket_write(&ssl_socket, "hello", 6) == 6);
BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 8) == 8);
BTASSERT(strcmp("goodbye", buf) == 0);
/* Close the SSL connection. */
BTASSERT(ssl_socket_close(&ssl_socket) == 0);
/* Close the connection. */
BTASSERT(socket_close(&socket) == 0);
BTASSERT(ssl_context_destroy(&context) == 0);
return (0);
}
static int test_server(struct harness_t *harness_p)
{
struct ssl_context_t context;
struct ssl_socket_t ssl_socket;
struct socket_t listener, socket;
struct inet_addr_t addr;
char buf[8];
static char certificate[] = "";
static char key[] = "";
const char *server_hostname_p;
const char *cipher_p;
const char *protocol_p;
int number_of_secret_bits;
/* Create a context with default settings. */
BTASSERT(ssl_context_init(&context, ssl_protocol_tls_v1_0) == 0);
BTASSERT(ssl_context_load_cert_chain(&context,
&certificate[0],
&key[0]) == 0);
/* Create a socket and connect to the server. */
BTASSERT(socket_open_tcp(&listener) == 0);
BTASSERT(socket_listen(&listener, 5) == 0);
BTASSERT(socket_accept(&listener, &socket, &addr) == 0);
/* Wrap the socket in SSL. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
SSL_SOCKET_SERVER_SIDE,
NULL) == 0);
/* Test a few functions. */
BTASSERT(chan_size(&ssl_socket) == 0);
server_hostname_p = ssl_socket_get_server_hostname(&ssl_socket);
BTASSERT(server_hostname_p == NULL);
BTASSERT(ssl_socket_get_cipher(&ssl_socket,
&cipher_p,
&protocol_p,
&number_of_secret_bits) == 0);
BTASSERT(strcmp(cipher_p, "TLS-RSA-WITH-AES-256-GCM-SHA384") == 0);
BTASSERT(strcmp(protocol_p, "TLSv1.1") == 0);
BTASSERT(number_of_secret_bits == -1);
/* Transfer data to and from the server. */
BTASSERT(chan_read(&ssl_socket, &buf[0], 6) == 6);
BTASSERT(strcmp("hello", buf) == 0);
BTASSERT(chan_write(&ssl_socket, "goodbye", 8) == 8);
/* Close the SSL connection. */
BTASSERT(ssl_socket_close(&ssl_socket) == 0);
/* Close the connection. */
BTASSERT(socket_close(&socket) == 0);
BTASSERT(ssl_context_destroy(&context) == 0);
return (0);
}
static int test_client_server_context(struct harness_t *harness_p)
{
struct ssl_context_t context;
struct ssl_socket_t ssl_socket;
struct socket_t socket;
/* Create a context with default settings. */
BTASSERT(ssl_context_init(&context, ssl_protocol_tls_v1_0) == 0);
/* Create a socket and connect to the server. */
BTASSERT(socket_open_tcp(&socket) == 0);
/* Wrap the socket in SSL, as a client. */
BTASSERT(ssl_socket_open(&ssl_socket, &context, &socket, 0, NULL) == 0);
/* Wrap the socket in SSL, as a server. Should fail. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
SSL_SOCKET_SERVER_SIDE,
NULL) == -1);
/* Close the SSL connection. */
BTASSERT(ssl_socket_close(&ssl_socket) == 0);
BTASSERT(ssl_context_destroy(&context) == 0);
return (0);
}
static int test_errors(struct harness_t *harness_p)
{
struct ssl_context_t context;
struct ssl_context_t context2;
struct ssl_socket_t ssl_socket;
struct ssl_socket_t ssl_socket2;
struct socket_t socket;
/* Out of resources. */
BTASSERT(ssl_context_init(&context, ssl_protocol_tls_v1_0) == 0);
BTASSERT(ssl_context_init(&context2, ssl_protocol_tls_v1_0) == -1);
/* Out of resources. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
0,
NULL) == 0);
BTASSERT(ssl_socket_open(&ssl_socket2,
&context,
&socket,
0,
NULL) == -1);
BTASSERT(ssl_socket_close(&ssl_socket) == 0);
/* Setup failure in setup. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
0,
NULL) == -1);
/* Setup failure in handover. */
BTASSERT(ssl_socket_open(&ssl_socket,
&context,
&socket,
0,
NULL) == -1);
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_init, "test_init" },
{ test_client, "test_client" },
{ test_server, "test_server" },
{ test_client_server_context, "test_client_server_context" },
{ test_errors, "test_errors" },
{ NULL, NULL }
};
sys_start();
ssl_module_init();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}