blob: 5ba9881b0611a84a2719f249b97fe1bf983c400d [file] [log] [blame]
Michal Vasko734360b2016-02-08 15:38:25 +01001/**
2 * \file test_server_thread.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 tests - thread-safety of all server functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoad1cc6a2016-02-26 15:06:06 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko734360b2016-02-08 15:38:25 +010013 */
14
15#include <pthread.h>
Michal Vasko734360b2016-02-08 15:38:25 +010016#include <unistd.h>
17#include <stdlib.h>
Michal Vasko64b31a22016-03-03 11:00:00 +010018#include <stdio.h>
Michal Vaskoa28be9a2016-03-01 15:20:47 +010019#include <string.h>
Michal Vasko734360b2016-02-08 15:38:25 +010020#include <sys/types.h>
21#include <sys/wait.h>
22
23#include <libyang/libyang.h>
24
25#include <session_client.h>
26#include <session_server.h>
27#include <log.h>
28#include "config.h"
29
30/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010031#define NC_ACCEPT_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010032/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010033#define NC_PS_POLL_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010034/* sec */
Michal Vasko15592cc2016-02-09 09:52:43 +010035#define CLIENT_SSH_AUTH_TIMEOUT 10
Michal Vasko734360b2016-02-08 15:38:25 +010036
Michal Vasko64b31a22016-03-03 11:00:00 +010037#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
38
Michal Vasko734360b2016-02-08 15:38:25 +010039pthread_barrier_t barrier;
40
Radek Krejci53691be2016-02-22 13:58:37 +010041#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +010042
43static void *
44server_thread(void *arg)
45{
46 (void)arg;
Michal Vasko71090fc2016-05-24 16:37:28 +020047 NC_MSG_TYPE msgtype;
Michal Vasko734360b2016-02-08 15:38:25 +010048 int ret;
49 struct nc_pollsession *ps;
50 struct nc_session *session;
51
52 ps = nc_ps_new();
Michal Vasko64b31a22016-03-03 11:00:00 +010053 nc_assert(ps);
Michal Vasko734360b2016-02-08 15:38:25 +010054
55 pthread_barrier_wait(&barrier);
56
Radek Krejci53691be2016-02-22 13:58:37 +010057#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko71090fc2016-05-24 16:37:28 +020058 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session);
59 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010060
61 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020062 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
63 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010064 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010065#endif
66
Michal Vasko71090fc2016-05-24 16:37:28 +020067 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session);
68 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010069
70 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020071 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
72 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010073 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010074
75 nc_ps_free(ps);
76
77 nc_thread_destroy();
78 return NULL;
79}
80
Radek Krejci53691be2016-02-22 13:58:37 +010081#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko734360b2016-02-08 15:38:25 +010082
Radek Krejci53691be2016-02-22 13:58:37 +010083#ifdef NC_ENABLED_SSH
Michal Vasko734360b2016-02-08 15:38:25 +010084
85static void *
86ssh_add_endpt_thread(void *arg)
87{
88 (void)arg;
89 int ret;
90
91 pthread_barrier_wait(&barrier);
92
93 ret = nc_server_ssh_add_endpt_listen("tertiary", "0.0.0.0", 6003);
Michal Vasko64b31a22016-03-03 11:00:00 +010094 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +010095
96 return NULL;
97}
98
99static void *
100ssh_endpt_set_port_thread(void *arg)
101{
102 (void)arg;
103 int ret;
104
105 pthread_barrier_wait(&barrier);
106
107 ret = nc_server_ssh_endpt_set_port("quaternary", 6005);
Michal Vasko64b31a22016-03-03 11:00:00 +0100108 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100109
110 return NULL;
111}
112
113static void *
114ssh_del_endpt_thread(void *arg)
115{
116 (void)arg;
117 int ret;
118
119 pthread_barrier_wait(&barrier);
120
121 ret = nc_server_ssh_del_endpt("secondary");
Michal Vasko64b31a22016-03-03 11:00:00 +0100122 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100123
124 return NULL;
125}
126
127static void *
128ssh_endpt_set_hostkey_thread(void *arg)
129{
130 (void)arg;
131 int ret;
132
133 pthread_barrier_wait(&barrier);
134
Michal Vaskob477b222016-02-09 14:30:15 +0100135 ret = nc_server_ssh_endpt_set_hostkey("main", TESTS_DIR"/data/key_dsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100136 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100137
138 return NULL;
139}
140
141static void *
142ssh_endpt_set_banner_thread(void *arg)
143{
144 (void)arg;
145 int ret;
146
147 pthread_barrier_wait(&barrier);
148
149 ret = nc_server_ssh_endpt_set_banner("main", "Howdy, partner!");
Michal Vasko64b31a22016-03-03 11:00:00 +0100150 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100151
152 return NULL;
153}
154
155static void *
156ssh_endpt_set_auth_methods_thread(void *arg)
157{
158 (void)arg;
159 int ret;
160
161 pthread_barrier_wait(&barrier);
162
163 ret = nc_server_ssh_endpt_set_auth_methods("main", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE);
Michal Vasko64b31a22016-03-03 11:00:00 +0100164 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100165
166 return NULL;
167}
168
169static void *
170ssh_endpt_set_auth_attempts_thread(void *arg)
171{
172 (void)arg;
173 int ret;
174
175 pthread_barrier_wait(&barrier);
176
177 ret = nc_server_ssh_endpt_set_auth_attempts("main", 2);
Michal Vasko64b31a22016-03-03 11:00:00 +0100178 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100179
180 return NULL;
181}
182
183static void *
184ssh_endpt_set_auth_timeout_thread(void *arg)
185{
186 (void)arg;
187 int ret;
188
189 pthread_barrier_wait(&barrier);
190
191 ret = nc_server_ssh_endpt_set_auth_timeout("main", 5);
Michal Vasko64b31a22016-03-03 11:00:00 +0100192 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100193
194 return NULL;
195}
196
197static void *
198ssh_endpt_add_authkey_thread(void *arg)
199{
200 (void)arg;
201 int ret;
202
203 pthread_barrier_wait(&barrier);
204
Michal Vaskob477b222016-02-09 14:30:15 +0100205 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_rsa.pub", "test3");
Michal Vasko64b31a22016-03-03 11:00:00 +0100206 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100207
208 return NULL;
209}
210
211static void *
212ssh_endpt_del_authkey_thread(void *arg)
213{
214 (void)arg;
215 int ret;
216
217 pthread_barrier_wait(&barrier);
218
Michal Vaskob477b222016-02-09 14:30:15 +0100219 ret = nc_server_ssh_endpt_del_authkey("main", TESTS_DIR"/data/key_ecdsa.pub", "test2");
Michal Vasko64b31a22016-03-03 11:00:00 +0100220 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100221
222 return NULL;
223}
224
Michal Vasko693eb112016-02-18 13:28:49 +0100225static int
226ssh_hostkey_check_clb(const char *hostname, ssh_session session)
227{
228 (void)hostname;
229 (void)session;
230
231 return 0;
232}
233
Michal Vasko734360b2016-02-08 15:38:25 +0100234static void *
235ssh_client_thread(void *arg)
236{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100237 int ret, read_pipe = *(int *)arg;
238 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100239 struct nc_session *session;
240
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100241 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100242 nc_assert(ret == 9);
243 nc_assert(!strncmp(buf, "ssh_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100244
Michal Vasko693eb112016-02-18 13:28:49 +0100245 /* skip the knownhost check */
246 nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb);
Michal Vasko734360b2016-02-08 15:38:25 +0100247
Michal Vasko693eb112016-02-18 13:28:49 +0100248 ret = nc_client_ssh_set_username("test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100249 nc_assert(!ret);
Michal Vasko693eb112016-02-18 13:28:49 +0100250
251 ret = nc_client_ssh_add_keypair(TESTS_DIR"/data/key_dsa.pub", TESTS_DIR"/data/key_dsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100252 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100253
254 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
255 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
256 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
257
Michal Vasko693eb112016-02-18 13:28:49 +0100258 session = nc_connect_ssh("127.0.0.1", 6001, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100259 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100260
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100261 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100262
Michal Vasko734360b2016-02-08 15:38:25 +0100263 nc_thread_destroy();
264 return NULL;
265}
266
Radek Krejci53691be2016-02-22 13:58:37 +0100267#endif /* NC_ENABLED_SSH */
Michal Vasko734360b2016-02-08 15:38:25 +0100268
Radek Krejci53691be2016-02-22 13:58:37 +0100269#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100270
271static void *
272tls_add_endpt_thread(void *arg)
273{
274 (void)arg;
275 int ret;
276
277 pthread_barrier_wait(&barrier);
278
279 ret = nc_server_tls_add_endpt_listen("tertiary", "0.0.0.0", 6503);
Michal Vasko64b31a22016-03-03 11:00:00 +0100280 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100281
282 return NULL;
283}
284
285static void *
286tls_endpt_set_port_thread(void *arg)
287{
288 (void)arg;
289 int ret;
290
291 pthread_barrier_wait(&barrier);
292
293 ret = nc_server_tls_endpt_set_port("quaternary", 6505);
Michal Vasko64b31a22016-03-03 11:00:00 +0100294 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100295
296 return NULL;
297}
298
299static void *
300tls_del_endpt_thread(void *arg)
301{
302 (void)arg;
303 int ret;
304
305 pthread_barrier_wait(&barrier);
306
307 ret = nc_server_tls_del_endpt("secondary");
Michal Vasko64b31a22016-03-03 11:00:00 +0100308 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100309
310 return NULL;
311}
312
313static void *
314tls_endpt_set_cert_thread(void *arg)
315{
316 (void)arg;
317 int ret;
318
319 pthread_barrier_wait(&barrier);
320
321 ret = nc_server_tls_end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UXlBXdTuWsbuH14L\n"
336 "dNIzGjkMZhNL/Tpkf6S/z1iH5VReGc+clTjWGg1XO5fr3mNKBGa7hDydIZRIMbgs\n"
337 "y63DIY7n5dqhNkO30CGmr/9TagVZAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAEVr\n"
338 "4skCpwuMuR+3WCmH6S17sYzWMYogJCGQdbZtFqmf4W3EDlNClk4HszAeUdmROMj6\n"
339 "MdqNDUnDM/GPxHB4Aje1DZOH1h68CCAl9W32LFRDC0KaUOquuYIG4rnZADJl6P4T\n"
340 "WVlaXfuE2bQjE7iYPhWGNWJtkb7JNIHmB8EAIa4tt3+XJs+vZiSpVDpiP2ucgrCn\n"
341 "BltsK0iOMPDLVlXdk1hpU5HvlMXdBHQebfTiCFDQSX7ViKc4wSJUHDt4CyoCzchY\n"
342 "mbQIcTc7uNDE5chQWV8Z3Vxkp4yuqZM3HdLskoo4IgFDOoj8eCAi+58+YRuKpaEQ\n"
343 "fWt+A9rvlaOApWryMW4=");
Michal Vasko64b31a22016-03-03 11:00:00 +0100344 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100345
346 nc_thread_destroy();
347 return NULL;
348}
349
350static void *
351tls_endpt_set_key_thread(void *arg)
352{
353 (void)arg;
354 int ret;
355
356 pthread_barrier_wait(&barrier);
357
358 ret = nc_server_tls_endpt_set_key("quaternary", "MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n"
359 "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n"
360 "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n"
361 "fNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+Zc\n"
362 "zSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL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JU9NLUH9/b\n"
393 "BY/JxGpy+NzcsHHOOQTwTdRIjviIOAo7fgQn2RyX0k+zXE8/7zqjqvji9zyemdNu\n"
394 "8we4uJICSntyvJwkbj/hrufTKEnBrwXpzfVn1EsH+6w32ZPBGLUhT75txJ8r56SR\n"
395 "q7l1XPU9vxovmT+lSMFF/Y0j1MbHWnds5H1shoFPNtYTvWBL/gfPHjIc+H23zsiu\n"
396 "3XlZAoIBAC2xB/Pnpoi9vOUMiqFH36AXtYa1DURy+AqCFlYlClMvb7YgvQ1w1eJv\n"
397 "nwrHSLk7HdKhnwGsLPduuRRH8q0n/osnoOutSQroE0n41UyIv2ZNccRwNmSzQcai\n"
398 "rBu2dSz02hlsh2otNl5IuGpOqXyPjXBpW4qGD6n2tH7THALnLC0BHtTSQVQsJsRM\n"
399 "3gX39LoiWvLDp2qJvplm6rTpi8Rgap6rZSqHe1yNKIxxD2vlr/WY9SMgLXYASO4S\n"
400 "SBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinb\n"
401 "Tsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW\n"
402 "8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPo\n"
403 "Swr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JB\n"
404 "dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n"
405 "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n"
406 "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=", 1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100407 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100408
409 nc_thread_destroy();
410 return NULL;
411}
412
413static void *
414tls_endpt_add_trusted_cert_thread(void *arg)
415{
416 (void)arg;
417 int ret;
418
419 pthread_barrier_wait(&barrier);
420
421 ret = nc_server_tls_endpt_add_trusted_cert("quaternary", "MIIDgzCCAmugAwIBAgIJAL+y0WMRGax0MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV\n"
422 "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n"
423 "aWRnaXRzIFB0eSBMdGQxETAPBgNVBAMMCGNsaWVudGNhMB4XDTE2MDExMTEyMTAx\n"
424 "OVoXDTE4MTAzMTEyMTAxOVowWDELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUt\n"
425 "U3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDERMA8GA1UE\n"
426 "AwwIY2xpZW50Y2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCw7Eyq\n"
427 "5T5tX6tAv5DHHfWNuaD/a3gVIBlGRWMAXkFWWJEa3o6leIjKxoDnL6tcBWNVJ+Gw\n"
428 "32MHerpHY6o5czsEHQ2XsOgodyFqe5cvx0kjQbjYQqnIMrslcdvSYuNe/ItqFP/w\n"
429 "uxb6kQbCYnCQKd/qhdhfoXjIHcnXpZzMCPKQ/uqls7LANJymtQkAuzydlf3+UqoG\n"
430 "4oo04GXK1Dc0A12cgCXxf+kWx7x34ctx2VEvDsJzw6LiZm8czOWjMFcuqqm/+kla\n"
431 "N3+6O7Z1kZlft/KNSrOYtc45xKNoSVrdVwFLkxipVDfOql6/DmWfE8iVmlX3QflO\n"
432 "u3+fzZZQpR5jYzUNAgMBAAGjUDBOMB0GA1UdDgQWBBTjBbQJ6p/mjnjBWXLgXXXW\n"
433 "a3ieoTAfBgNVHSMEGDAWgBTjBbQJ6p/mjnjBWXLgXXXWa3ieoTAMBgNVHRMEBTAD\n"
434 "AQH/MA0GCSqGSIb3DQEBBQUAA4IBAQAZr9b0YTaDV5XZr/QQPP1pvHkN3Ezbm9F4\n"
435 "MiYe4e0QnM9JtjNLDKq1dDnqVDQ/BYdupWWh0398tObFACssWkm4aubPG7LVh5Ck\n"
436 "O8I8i/GHiXYLmYT22hslWe5dFvidUICkTXoj1h5X2vwfBrNTI1+gnVXXw842xCvU\n"
437 "sgq28vGMSXLSYKBNaP/llXNmqW35oLs6CwVuiCL7Go0IDIOmiXN2bssb87hZSw3B\n"
438 "6iwU78wYshJUGZjLaK9PuMvFYJLFWSAePA2Yb+aEv80wMbX1oANSryU7Uf5BJk8V\n"
439 "kO3mlRDh2b1/5Gb5xA2vU2z3ReHdPNy6qSx0Mk4XJvQw9FsVHZ13");
Michal Vasko64b31a22016-03-03 11:00:00 +0100440 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100441
442 nc_thread_destroy();
443 return NULL;
444}
445
446static void *
447tls_endpt_set_trusted_ca_paths_thread(void *arg)
448{
449 (void)arg;
450 int ret;
451
452 pthread_barrier_wait(&barrier);
453
Michal Vaskob477b222016-02-09 14:30:15 +0100454 ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR"/data/serverca.pem", "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100455 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100456
457 nc_thread_destroy();
458 return NULL;
459}
460
461static void *
462tls_endpt_clear_certs_thread(void *arg)
463{
464 (void)arg;
465
466 pthread_barrier_wait(&barrier);
467
468 nc_server_tls_endpt_clear_certs("quaternary");
469
470 return NULL;
471}
472
473static void *
474tls_endpt_set_crl_paths_thread(void *arg)
475{
476 (void)arg;
477 int ret;
478
479 pthread_barrier_wait(&barrier);
480
481 ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100482 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100483
484 nc_thread_destroy();
485 return NULL;
486}
487
488static void *
489tls_endpt_clear_crls_thread(void *arg)
490{
491 (void)arg;
492
493 pthread_barrier_wait(&barrier);
494
495 nc_server_tls_endpt_clear_crls("quaternary");
496
497 return NULL;
498}
499
500static void *
501tls_endpt_add_ctn_thread(void *arg)
502{
503 (void)arg;
504 int ret;
505
506 pthread_barrier_wait(&barrier);
507
508 ret = nc_server_tls_endpt_add_ctn("main", 0, "02:F0:F1:F2:F3:F4:F5:F6:F7:F8:F9:10:11:12:EE:FF:A0:A1:A2:A3",
509 NC_TLS_CTN_SAN_IP_ADDRESS, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100510 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100511
512 return NULL;
513}
514
515static void *
516tls_endpt_del_ctn_thread(void *arg)
517{
518 (void)arg;
519 int ret;
520
521 pthread_barrier_wait(&barrier);
522
523 ret = nc_server_tls_endpt_del_ctn("main", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100524 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100525
526 return NULL;
527}
528
529static void *
530tls_client_thread(void *arg)
531{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100532 int ret, read_pipe = *(int *)arg;
533 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100534 struct nc_session *session;
535
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100536 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100537 nc_assert(ret == 9);
538 nc_assert(!strncmp(buf, "tls_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100539
Michal Vaskob477b222016-02-09 14:30:15 +0100540 ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key");
Michal Vasko64b31a22016-03-03 11:00:00 +0100541 nc_assert(!ret);
Michal Vaskobc2ae552016-02-09 14:38:47 +0100542 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100543 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100544
545 session = nc_connect_tls("127.0.0.1", 6501, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100546 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100547
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100548 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100549
Michal Vasko734360b2016-02-08 15:38:25 +0100550 nc_thread_destroy();
551 return NULL;
552}
553
Radek Krejci53691be2016-02-22 13:58:37 +0100554#endif /* NC_ENABLED_TLS */
Michal Vasko734360b2016-02-08 15:38:25 +0100555
556static void *(*thread_funcs[])(void *) = {
Radek Krejci53691be2016-02-22 13:58:37 +0100557#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +0100558 server_thread,
559#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100560#ifdef NC_ENABLED_SSH
Michal Vasko734360b2016-02-08 15:38:25 +0100561 ssh_add_endpt_thread,
562 ssh_endpt_set_port_thread,
563 ssh_del_endpt_thread,
564 ssh_endpt_set_hostkey_thread,
565 ssh_endpt_set_banner_thread,
566 ssh_endpt_set_auth_methods_thread,
567 ssh_endpt_set_auth_attempts_thread,
568 ssh_endpt_set_auth_timeout_thread,
569 ssh_endpt_add_authkey_thread,
570 ssh_endpt_del_authkey_thread,
571#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100572#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100573 tls_add_endpt_thread,
574 tls_endpt_set_port_thread,
575 tls_del_endpt_thread,
576 tls_endpt_set_cert_thread,
577 tls_endpt_set_key_thread,
578 tls_endpt_add_trusted_cert_thread,
579 tls_endpt_set_trusted_ca_paths_thread,
580 tls_endpt_clear_certs_thread,
581 tls_endpt_set_crl_paths_thread,
582 tls_endpt_clear_crls_thread,
583 tls_endpt_add_ctn_thread,
584 tls_endpt_del_ctn_thread,
585#endif
586};
587
588const int thread_count = sizeof thread_funcs / sizeof *thread_funcs;
589
Radek Krejci53691be2016-02-22 13:58:37 +0100590#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100591const int client_count = 2;
592pid_t pids[2];
593int pipes[4];
Michal Vasko734360b2016-02-08 15:38:25 +0100594#else
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100595const int client_count = 1;
596pid_t pids[1];
597int pipes[2];
598#endif
599
600static void
601client_fork(void)
602{
603 int ret, clients = 0;
604
605#ifdef NC_ENABLED_SSH
606 pipe(pipes + clients * 2);
607
608 if (!(pids[clients] = fork())) {
609 nc_client_init();
610
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100611 ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas");
Michal Vasko64b31a22016-03-03 11:00:00 +0100612 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100613
614 /* close write */
615 close(pipes[clients * 2 + 1]);
616 ssh_client_thread(&pipes[clients * 2]);
617 close(pipes[clients * 2]);
618 nc_client_destroy();
619 exit(0);
Michal Vasko734360b2016-02-08 15:38:25 +0100620 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100621 /* close read */
622 close(pipes[clients * 2]);
623
624 ++clients;
625#endif
626
627#ifdef NC_ENABLED_TLS
628 pipe(pipes + clients * 2);
629
630 if (!(pids[clients] = fork())) {
631 nc_client_init();
632
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100633 ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas");
Michal Vasko64b31a22016-03-03 11:00:00 +0100634 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100635
636 /* close write */
637 close(pipes[clients * 2 + 1]);
638 tls_client_thread(&pipes[clients * 2]);
639 close(pipes[clients * 2]);
640 nc_client_destroy();
641 exit(0);
642 }
643 /* close read */
644 close(pipes[clients * 2]);
645
646 ++clients;
Michal Vasko734360b2016-02-08 15:38:25 +0100647#endif
648}
649
650int
651main(void)
652{
653 struct ly_ctx *ctx;
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100654 int ret, i, clients = 0;
Michal Vasko734360b2016-02-08 15:38:25 +0100655 pthread_t tids[thread_count];
656
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100657 nc_verbosity(NC_VERB_VERBOSE);
658
659 client_fork();
Michal Vasko734360b2016-02-08 15:38:25 +0100660
661 ctx = ly_ctx_new(TESTS_DIR"/../schemas");
Michal Vasko64b31a22016-03-03 11:00:00 +0100662 nc_assert(ctx);
Michal Vasko734360b2016-02-08 15:38:25 +0100663 ly_ctx_load_module(ctx, "ietf-netconf", NULL);
664 nc_server_init(ctx);
665
666 pthread_barrier_init(&barrier, NULL, thread_count);
667
Radek Krejci53691be2016-02-22 13:58:37 +0100668#ifdef NC_ENABLED_SSH
Michal Vasko734360b2016-02-08 15:38:25 +0100669 /* do first, so that client can connect on SSH */
670 ret = nc_server_ssh_add_endpt_listen("main", "0.0.0.0", 6001);
Michal Vasko64b31a22016-03-03 11:00:00 +0100671 nc_assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100672 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_dsa.pub", "test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100673 nc_assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100674 ret = nc_server_ssh_endpt_set_hostkey("main", TESTS_DIR"/data/key_rsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100675 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100676
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100677 /* client ready */
678 ret = write(pipes[clients * 2 + 1], "ssh_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100679 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100680 ++clients;
681
Michal Vasko734360b2016-02-08 15:38:25 +0100682 /* for ssh_endpt_del_authkey */
Michal Vaskob477b222016-02-09 14:30:15 +0100683 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_ecdsa.pub", "test2");
Michal Vasko64b31a22016-03-03 11:00:00 +0100684 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100685
686 /* for ssh_del_endpt */
687 ret = nc_server_ssh_add_endpt_listen("secondary", "0.0.0.0", 6002);
Michal Vasko64b31a22016-03-03 11:00:00 +0100688 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100689
690 /* for ssh_endpt_set_port */
691 ret = nc_server_ssh_add_endpt_listen("quaternary", "0.0.0.0", 6004);
Michal Vasko64b31a22016-03-03 11:00:00 +0100692 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100693#endif
694
Radek Krejci53691be2016-02-22 13:58:37 +0100695#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100696 /* do first, so that client can connect on TLS */
697 ret = nc_server_tls_add_endpt_listen("main", "0.0.0.0", 6501);
Michal Vasko64b31a22016-03-03 11:00:00 +0100698 nc_assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100699 ret = nc_server_tls_endpt_set_cert_path("main", TESTS_DIR"/data/server.crt");
Michal Vasko64b31a22016-03-03 11:00:00 +0100700 nc_assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100701 ret = nc_server_tls_endpt_set_key_path("main", TESTS_DIR"/data/server.key");
Michal Vasko64b31a22016-03-03 11:00:00 +0100702 nc_assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100703 ret = nc_server_tls_endpt_add_trusted_cert_path("main", TESTS_DIR"/data/client.crt");
Michal Vasko64b31a22016-03-03 11:00:00 +0100704 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100705 ret = nc_server_tls_endpt_add_ctn("main", 0, "02:D3:03:0E:77:21:E2:14:1F:E5:75:48:98:6B:FD:8A:63:BB:DE:40:34", NC_TLS_CTN_SPECIFIED, "test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100706 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100707
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100708 /* client ready */
709 ret = write(pipes[clients * 2 + 1], "tls_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100710 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100711 ++clients;
712
Michal Vasko734360b2016-02-08 15:38:25 +0100713 /* for tls_del_endpt */
714 ret = nc_server_tls_add_endpt_listen("secondary", "0.0.0.0", 6502);
Michal Vasko64b31a22016-03-03 11:00:00 +0100715 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100716
717 /* for tls_endpt_set_port */
718 ret = nc_server_tls_add_endpt_listen("quaternary", "0.0.0.0", 6504);
Michal Vasko64b31a22016-03-03 11:00:00 +0100719 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100720
721 /* for tls_endpt_del_ctn */
722 ret = nc_server_tls_endpt_add_ctn("main", 0, "02:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:A0:A1:A2:A3", NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100723 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100724#endif
725
Michal Vasko734360b2016-02-08 15:38:25 +0100726 /* threads'n'stuff */
727 ret = 0;
728 for (i = 0; i < thread_count; ++i) {
729 ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL);
730 }
Michal Vasko64b31a22016-03-03 11:00:00 +0100731 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100732
733 /* cleanup */
734 for (i = 0; i < thread_count; ++i) {
735 pthread_join(tids[i], NULL);
736 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100737 for (i = 0; i < client_count; ++i) {
738 waitpid(pids[i], NULL, 0);
739 close(pipes[i * 2 + 1]);
740 }
Michal Vasko734360b2016-02-08 15:38:25 +0100741
742 pthread_barrier_destroy(&barrier);
743
Michal Vasko734360b2016-02-08 15:38:25 +0100744 nc_server_destroy();
745 ly_ctx_destroy(ctx, NULL);
746
Michal Vasko734360b2016-02-08 15:38:25 +0100747 return 0;
748}