blob: dfcdd5a816d09cd3e4dac8e0cd3f5ed230ae36be [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#include <pthread.h>
24#include <assert.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29
30#include <libyang/libyang.h>
31
32#include <session_client.h>
33#include <session_server.h>
34#include <log.h>
35#include "config.h"
36
37/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010038#define NC_ACCEPT_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010039/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010040#define NC_PS_POLL_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010041/* sec */
Michal Vasko15592cc2016-02-09 09:52:43 +010042#define CLIENT_SSH_AUTH_TIMEOUT 10
Michal Vasko734360b2016-02-08 15:38:25 +010043
44pthread_barrier_t barrier;
45
46static int
47setup_lib(void)
48{
49 nc_verbosity(NC_VERB_VERBOSE);
50
51#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
52 nc_ssh_tls_init();
53#elif defined(ENABLE_SSH)
54 nc_ssh_init();
55#elif defined(ENABLE_TLS)
56 nc_tls_init();
57#endif
58
59 return 0;
60}
61
62static int
63teardown_lib(void)
64{
65#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
66 nc_ssh_tls_destroy();
67#elif defined(ENABLE_SSH)
68 nc_ssh_destroy();
69#elif defined(ENABLE_TLS)
70 nc_tls_destroy();
71#endif
72
73 return 0;
74}
75
76#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
77
78static void *
79server_thread(void *arg)
80{
81 (void)arg;
82 int ret;
83 struct nc_pollsession *ps;
84 struct nc_session *session;
85
86 ps = nc_ps_new();
87 assert(ps);
88
89 pthread_barrier_wait(&barrier);
90
91#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
92 ret = nc_accept(NC_ACCEPT_TIMEOUT, &session);
93 assert(ret == 1);
94
95 nc_ps_add_session(ps, session);
96 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT);
97 assert(ret == 3);
98 nc_ps_clear(ps);
99#endif
100
101 ret = nc_accept(NC_ACCEPT_TIMEOUT, &session);
102 assert(ret == 1);
103
104 nc_ps_add_session(ps, session);
105 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT);
106 assert(ret == 3);
107 nc_ps_clear(ps);
108
109 nc_ps_free(ps);
110
111 nc_thread_destroy();
112 return NULL;
113}
114
115#endif /* ENABLE_SSH || ENABLE_TLS */
116
117#ifdef ENABLE_SSH
118
119static void *
120ssh_add_endpt_thread(void *arg)
121{
122 (void)arg;
123 int ret;
124
125 pthread_barrier_wait(&barrier);
126
127 ret = nc_server_ssh_add_endpt_listen("tertiary", "0.0.0.0", 6003);
128 assert(!ret);
129
130 return NULL;
131}
132
133static void *
134ssh_endpt_set_port_thread(void *arg)
135{
136 (void)arg;
137 int ret;
138
139 pthread_barrier_wait(&barrier);
140
141 ret = nc_server_ssh_endpt_set_port("quaternary", 6005);
142 assert(!ret);
143
144 return NULL;
145}
146
147static void *
148ssh_del_endpt_thread(void *arg)
149{
150 (void)arg;
151 int ret;
152
153 pthread_barrier_wait(&barrier);
154
155 ret = nc_server_ssh_del_endpt("secondary");
156 assert(!ret);
157
158 return NULL;
159}
160
161static void *
162ssh_endpt_set_hostkey_thread(void *arg)
163{
164 (void)arg;
165 int ret;
166
167 pthread_barrier_wait(&barrier);
168
Michal Vaskob477b222016-02-09 14:30:15 +0100169 ret = nc_server_ssh_endpt_set_hostkey("main", TESTS_DIR"/data/key_dsa");
Michal Vasko734360b2016-02-08 15:38:25 +0100170 assert(!ret);
171
172 return NULL;
173}
174
175static void *
176ssh_endpt_set_banner_thread(void *arg)
177{
178 (void)arg;
179 int ret;
180
181 pthread_barrier_wait(&barrier);
182
183 ret = nc_server_ssh_endpt_set_banner("main", "Howdy, partner!");
184 assert(!ret);
185
186 return NULL;
187}
188
189static void *
190ssh_endpt_set_auth_methods_thread(void *arg)
191{
192 (void)arg;
193 int ret;
194
195 pthread_barrier_wait(&barrier);
196
197 ret = nc_server_ssh_endpt_set_auth_methods("main", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE);
198 assert(!ret);
199
200 return NULL;
201}
202
203static void *
204ssh_endpt_set_auth_attempts_thread(void *arg)
205{
206 (void)arg;
207 int ret;
208
209 pthread_barrier_wait(&barrier);
210
211 ret = nc_server_ssh_endpt_set_auth_attempts("main", 2);
212 assert(!ret);
213
214 return NULL;
215}
216
217static void *
218ssh_endpt_set_auth_timeout_thread(void *arg)
219{
220 (void)arg;
221 int ret;
222
223 pthread_barrier_wait(&barrier);
224
225 ret = nc_server_ssh_endpt_set_auth_timeout("main", 5);
226 assert(!ret);
227
228 return NULL;
229}
230
231static void *
232ssh_endpt_add_authkey_thread(void *arg)
233{
234 (void)arg;
235 int ret;
236
237 pthread_barrier_wait(&barrier);
238
Michal Vaskob477b222016-02-09 14:30:15 +0100239 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_rsa.pub", "test3");
Michal Vasko734360b2016-02-08 15:38:25 +0100240 assert(!ret);
241
242 return NULL;
243}
244
245static void *
246ssh_endpt_del_authkey_thread(void *arg)
247{
248 (void)arg;
249 int ret;
250
251 pthread_barrier_wait(&barrier);
252
Michal Vaskob477b222016-02-09 14:30:15 +0100253 ret = nc_server_ssh_endpt_del_authkey("main", TESTS_DIR"/data/key_ecdsa.pub", "test2");
Michal Vasko734360b2016-02-08 15:38:25 +0100254 assert(!ret);
255
256 return NULL;
257}
258
259static void *
260ssh_client_thread(void *arg)
261{
262 (void)arg;
263 int ret;
264 long timeout = CLIENT_SSH_AUTH_TIMEOUT;
265 uint32_t port;
266 ssh_session sshsession;
267 ssh_key pubkey, privkey;
268 struct nc_session *session;
269
270 /* We cannot use nc_connect_ssh(), because we want to skip the knownhost check.
271 ret = nc_client_ssh_set_username("test");
272 assert_int_equal(ret, 0);
273
274 ret = nc_client_ssh_add_keypair("data/key_dsa.pub", "data/key_dsa");
275 assert_int_equal(ret, 0);
276
277 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
278 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
279 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
280
281 session = nc_session_connect("127.0.0.1", NC_PORT_SSH, NULL);*/
282
283 port = 6001;
284
285 sshsession = ssh_new();
286 ssh_options_set(sshsession, SSH_OPTIONS_HOST, "127.0.0.1");
287 ssh_options_set(sshsession, SSH_OPTIONS_PORT, &port);
288 ssh_options_set(sshsession, SSH_OPTIONS_USER, "test");
289 ssh_options_set(sshsession, SSH_OPTIONS_TIMEOUT, &timeout);
290 ssh_options_set(sshsession, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
291
292 ret = ssh_connect(sshsession);
293 assert(ret == SSH_OK);
294
295 /* authentication */
296 ret = ssh_userauth_none(sshsession, NULL);
297 assert(ret == SSH_AUTH_DENIED);
298 assert(ssh_userauth_list(sshsession, NULL) & SSH_AUTH_METHOD_PUBLICKEY);
Michal Vaskob477b222016-02-09 14:30:15 +0100299 ret = ssh_pki_import_pubkey_file(TESTS_DIR"/data/key_dsa.pub", &pubkey);
Michal Vasko734360b2016-02-08 15:38:25 +0100300 assert(ret == SSH_OK);
301 ret = ssh_userauth_try_publickey(sshsession, NULL, pubkey);
302 assert(ret == SSH_AUTH_SUCCESS);
Michal Vaskob477b222016-02-09 14:30:15 +0100303 ret = ssh_pki_import_privkey_file(TESTS_DIR"/data/key_dsa", NULL, NULL, NULL, &privkey);
Michal Vasko734360b2016-02-08 15:38:25 +0100304 assert(ret == SSH_OK);
305 ret = ssh_userauth_publickey(sshsession, NULL, privkey);
306 assert(ret == SSH_AUTH_SUCCESS);
307 ssh_key_free(pubkey);
308 ssh_key_free(privkey);
309
310 session = nc_connect_libssh(sshsession, NULL);
311 assert(session);
312
313 nc_session_free(session);
314
315 nc_client_ssh_destroy_opts();
316
317 nc_thread_destroy();
318 return NULL;
319}
320
321pid_t
322fork_ssh_client(void)
323{
324 pid_t client_pid;
325
326 if (!(client_pid = fork())) {
327 /* cleanup */
328 //nc_server_destroy();
329 //ly_ctx_destroy(ctx, NULL);
330 pthread_barrier_destroy(&barrier);
331
332 ssh_client_thread(NULL);
333
334 teardown_lib();
335 exit(0);
336 }
337
338 return client_pid;
339}
340
341pthread_t
342thread_ssh_client(void)
343{
344 pthread_t client_tid;
345
346 pthread_create(&client_tid, NULL, ssh_client_thread, NULL);
347
348 return client_tid;
349}
350
351#endif /* ENABLE_SSH */
352
353#ifdef ENABLE_TLS
354
355static void *
356tls_add_endpt_thread(void *arg)
357{
358 (void)arg;
359 int ret;
360
361 pthread_barrier_wait(&barrier);
362
363 ret = nc_server_tls_add_endpt_listen("tertiary", "0.0.0.0", 6503);
364 assert(!ret);
365
366 return NULL;
367}
368
369static void *
370tls_endpt_set_port_thread(void *arg)
371{
372 (void)arg;
373 int ret;
374
375 pthread_barrier_wait(&barrier);
376
377 ret = nc_server_tls_endpt_set_port("quaternary", 6505);
378 assert(!ret);
379
380 return NULL;
381}
382
383static void *
384tls_del_endpt_thread(void *arg)
385{
386 (void)arg;
387 int ret;
388
389 pthread_barrier_wait(&barrier);
390
391 ret = nc_server_tls_del_endpt("secondary");
392 assert(!ret);
393
394 return NULL;
395}
396
397static void *
398tls_endpt_set_cert_thread(void *arg)
399{
400 (void)arg;
401 int ret;
402
403 pthread_barrier_wait(&barrier);
404
405 ret = nc_server_tls_endpt_set_cert("quaternary", "MIIEKjCCAxICCQDqSTPpuoUZkzANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJB\n"
406 "VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\n"
407 "cyBQdHkgTHRkMREwDwYDVQQDDAhzZXJ2ZXJjYTAeFw0xNjAyMDgxMTE0MzdaFw0y\n"
408 "NjAyMDUxMTE0MzdaMFYxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRl\n"
409 "MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDzANBgNVBAMMBnNl\n"
410 "cnZlcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOqI7Y3w5r8kD9WZ\n"
411 "CMAaa/e3ig7nm76aIJUR0Xb1bk6X/4FNVQKwEJsBodOYupZvE5FZdZ6DJSMSyQ3F\n"
412 "rJWnlZ+isr7F9B4bELV8Kj6sJGuVAr+mpcH/4rwL3DaXF9Y9Lf7iBgiOHUoip80A\n"
413 "sn9BU4q80JI6w2VHd5ng4TUE67gmpRleIHzViKt3taBrsAJ9bS5bvaE6xOB8zKYG\n"
414 "zRFOsDZrEqqcBsVIWC6EmjO29HS5qj/mXM0ktFGnNDxTZHoRkNgmCE/NH+fNKOFx\n"
415 "raCwlFBpKemAky+GdgngRGiQAVowyAx/nSmCFAalKc+E4ddoFwD/oft6iOvvXqaX\n"
416 "h6368wEQ7Hy48FDcUCbHtUEgK4wMrX9BSrRh6zkXO1tE4ghb0dM2qFDS0ypO3p04\n"
417 "kUPa31mTgLuOH1LzwmlwxOs113mlYKCgqOFR5YaN+nq1HI5RATPo5NvCMpG2RrQW\n"
418 "+ooCr2GtbT0oHmJv8yaBVY0HJ69eLnIv37dfjWvoTiBKBBIisXAD5Nm9rwSjZUSF\n"
419 "u1iyd7u2YrkBCUzZuvt3BOPpX8GgQgagU6BPnac76FF6DMhRUXlBXdTuWsbuH14L\n"
420 "dNIzGjkMZhNL/Tpkf6S/z1iH5VReGc+clTjWGg1XO5fr3mNKBGa7hDydIZRIMbgs\n"
421 "y63DIY7n5dqhNkO30CGmr/9TagVZAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAEVr\n"
422 "4skCpwuMuR+3WCmH6S17sYzWMYogJCGQdbZtFqmf4W3EDlNClk4HszAeUdmROMj6\n"
423 "MdqNDUnDM/GPxHB4Aje1DZOH1h68CCAl9W32LFRDC0KaUOquuYIG4rnZADJl6P4T\n"
424 "WVlaXfuE2bQjE7iYPhWGNWJtkb7JNIHmB8EAIa4tt3+XJs+vZiSpVDpiP2ucgrCn\n"
425 "BltsK0iOMPDLVlXdk1hpU5HvlMXdBHQebfTiCFDQSX7ViKc4wSJUHDt4CyoCzchY\n"
426 "mbQIcTc7uNDE5chQWV8Z3Vxkp4yuqZM3HdLskoo4IgFDOoj8eCAi+58+YRuKpaEQ\n"
427 "fWt+A9rvlaOApWryMW4=");
428 assert(!ret);
429
430 nc_thread_destroy();
431 return NULL;
432}
433
434static void *
435tls_endpt_set_key_thread(void *arg)
436{
437 (void)arg;
438 int ret;
439
440 pthread_barrier_wait(&barrier);
441
442 ret = nc_server_tls_endpt_set_key("quaternary", "MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n"
443 "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n"
444 "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n"
445 "fNWIq3e1oGuwAn1tL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DjXmnpR2vvhMU/FWb//21p/88o=", 1);
491 assert(!ret);
492
493 nc_thread_destroy();
494 return NULL;
495}
496
497static void *
498tls_endpt_add_trusted_cert_thread(void *arg)
499{
500 (void)arg;
501 int ret;
502
503 pthread_barrier_wait(&barrier);
504
505 ret = nc_server_tls_endpt_add_trusted_cert("quaternary", "MIIDgzCCAmugAwIBAgIJAL+y0WMRGax0MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV\n"
506 "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n"
507 "aWRnaXRzIFB0eSBMdGQxETAPBgNVBAMMCGNsaWVudGNhMB4XDTE2MDExMTEyMTAx\n"
508 "OVoXDTE4MTAzMTEyMTAxOVowWDELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUt\n"
509 "U3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDERMA8GA1UE\n"
510 "AwwIY2xpZW50Y2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCw7Eyq\n"
511 "5T5tX6tAv5DHHfWNuaD/a3gVIBlGRWMAXkFWWJEa3o6leIjKxoDnL6tcBWNVJ+Gw\n"
512 "32MHerpHY6o5czsEHQ2XsOgodyFqe5cvx0kjQbjYQqnIMrslcdvSYuNe/ItqFP/w\n"
513 "uxb6kQbCYnCQKd/qhdhfoXjIHcnXpZzMCPKQ/uqls7LANJymtQkAuzydlf3+UqoG\n"
514 "4oo04GXK1Dc0A12cgCXxf+kWx7x34ctx2VEvDsJzw6LiZm8czOWjMFcuqqm/+kla\n"
515 "N3+6O7Z1kZlft/KNSrOYtc45xKNoSVrdVwFLkxipVDfOql6/DmWfE8iVmlX3QflO\n"
516 "u3+fzZZQpR5jYzUNAgMBAAGjUDBOMB0GA1UdDgQWBBTjBbQJ6p/mjnjBWXLgXXXW\n"
517 "a3ieoTAfBgNVHSMEGDAWgBTjBbQJ6p/mjnjBWXLgXXXWa3ieoTAMBgNVHRMEBTAD\n"
518 "AQH/MA0GCSqGSIb3DQEBBQUAA4IBAQAZr9b0YTaDV5XZr/QQPP1pvHkN3Ezbm9F4\n"
519 "MiYe4e0QnM9JtjNLDKq1dDnqVDQ/BYdupWWh0398tObFACssWkm4aubPG7LVh5Ck\n"
520 "O8I8i/GHiXYLmYT22hslWe5dFvidUICkTXoj1h5X2vwfBrNTI1+gnVXXw842xCvU\n"
521 "sgq28vGMSXLSYKBNaP/llXNmqW35oLs6CwVuiCL7Go0IDIOmiXN2bssb87hZSw3B\n"
522 "6iwU78wYshJUGZjLaK9PuMvFYJLFWSAePA2Yb+aEv80wMbX1oANSryU7Uf5BJk8V\n"
523 "kO3mlRDh2b1/5Gb5xA2vU2z3ReHdPNy6qSx0Mk4XJvQw9FsVHZ13");
524 assert(!ret);
525
526 nc_thread_destroy();
527 return NULL;
528}
529
530static void *
531tls_endpt_set_trusted_ca_paths_thread(void *arg)
532{
533 (void)arg;
534 int ret;
535
536 pthread_barrier_wait(&barrier);
537
Michal Vaskob477b222016-02-09 14:30:15 +0100538 ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR"/data/serverca.pem", "data");
Michal Vasko734360b2016-02-08 15:38:25 +0100539 assert(!ret);
540
541 nc_thread_destroy();
542 return NULL;
543}
544
545static void *
546tls_endpt_clear_certs_thread(void *arg)
547{
548 (void)arg;
549
550 pthread_barrier_wait(&barrier);
551
552 nc_server_tls_endpt_clear_certs("quaternary");
553
554 return NULL;
555}
556
557static void *
558tls_endpt_set_crl_paths_thread(void *arg)
559{
560 (void)arg;
561 int ret;
562
563 pthread_barrier_wait(&barrier);
564
565 ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data");
566 assert(!ret);
567
568 nc_thread_destroy();
569 return NULL;
570}
571
572static void *
573tls_endpt_clear_crls_thread(void *arg)
574{
575 (void)arg;
576
577 pthread_barrier_wait(&barrier);
578
579 nc_server_tls_endpt_clear_crls("quaternary");
580
581 return NULL;
582}
583
584static void *
585tls_endpt_add_ctn_thread(void *arg)
586{
587 (void)arg;
588 int ret;
589
590 pthread_barrier_wait(&barrier);
591
592 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",
593 NC_TLS_CTN_SAN_IP_ADDRESS, NULL);
594 assert(!ret);
595
596 return NULL;
597}
598
599static void *
600tls_endpt_del_ctn_thread(void *arg)
601{
602 (void)arg;
603 int ret;
604
605 pthread_barrier_wait(&barrier);
606
607 ret = nc_server_tls_endpt_del_ctn("main", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL);
608 assert(!ret);
609
610 return NULL;
611}
612
613static void *
614tls_client_thread(void *arg)
615{
616 (void)arg;
617 int ret;
618 struct nc_session *session;
619
Michal Vaskob477b222016-02-09 14:30:15 +0100620 ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key");
Michal Vasko734360b2016-02-08 15:38:25 +0100621 assert(!ret);
Michal Vaskobc2ae552016-02-09 14:38:47 +0100622 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data");
Michal Vasko734360b2016-02-08 15:38:25 +0100623 assert(!ret);
624
625 session = nc_connect_tls("127.0.0.1", 6501, NULL);
626 assert(session);
627
628 nc_session_free(session);
629
630 nc_client_tls_destroy_opts();
631
632 nc_thread_destroy();
633 return NULL;
634}
635
636pid_t
637fork_tls_client(void)
638{
639 pid_t client_pid;
640
641 if (!(client_pid = fork())) {
642 /* cleanup */
643 //nc_server_destroy();
644 //ly_ctx_destroy(ctx, NULL);
645 pthread_barrier_destroy(&barrier);
646
647 tls_client_thread(NULL);
648
649 teardown_lib();
650 exit(0);
651 }
652
653 return client_pid;
654}
655
656pthread_t
657thread_tls_client(void)
658{
659 pthread_t client_tid;
660
661 pthread_create(&client_tid, NULL, tls_client_thread, NULL);
662
663 return client_tid;
664}
665
666#endif /* ENABLE_TLS */
667
668static void *(*thread_funcs[])(void *) = {
669#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
670 server_thread,
671#endif
672#ifdef ENABLE_SSH
673 ssh_add_endpt_thread,
674 ssh_endpt_set_port_thread,
675 ssh_del_endpt_thread,
676 ssh_endpt_set_hostkey_thread,
677 ssh_endpt_set_banner_thread,
678 ssh_endpt_set_auth_methods_thread,
679 ssh_endpt_set_auth_attempts_thread,
680 ssh_endpt_set_auth_timeout_thread,
681 ssh_endpt_add_authkey_thread,
682 ssh_endpt_del_authkey_thread,
683#endif
684#ifdef ENABLE_TLS
685 tls_add_endpt_thread,
686 tls_endpt_set_port_thread,
687 tls_del_endpt_thread,
688 tls_endpt_set_cert_thread,
689 tls_endpt_set_key_thread,
690 tls_endpt_add_trusted_cert_thread,
691 tls_endpt_set_trusted_ca_paths_thread,
692 tls_endpt_clear_certs_thread,
693 tls_endpt_set_crl_paths_thread,
694 tls_endpt_clear_crls_thread,
695 tls_endpt_add_ctn_thread,
696 tls_endpt_del_ctn_thread,
697#endif
698};
699
700const int thread_count = sizeof thread_funcs / sizeof *thread_funcs;
701
702static void
703clients_start_cleanup(void)
704{
705 //static pid_t pids[2] = {0, 0};
706 static pthread_t tids[2] = {0, 0};
707
708#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
709 /*if (pids[0] && pids[1]) {
710 waitpid(pids[0], NULL, 0);
711 waitpid(pids[1], NULL, 0);
712 return;
713 }
714 pids[0] = fork_ssh_client();
715 pids[1] = fork_tls_client();*/
716
717 if (tids[0] && tids[1]) {
718 pthread_join(tids[0], NULL);
719 pthread_join(tids[1], NULL);
720 return;
721 }
722 tids[0] = thread_ssh_client();
723 tids[1] = thread_tls_client();
724#elif defined(ENABLE_SSH)
725 /*if (pids[0]) {
726 waitpid(pids[0], NULL, 0);
727 return;
728 }
729 pids[0] = fork_ssh_client();*/
730
731 if (tids[0]) {
732 pthread_join(tids[0], NULL);
733 return;
734 }
735 tids[0] = thread_ssh_client();
736#elif defined(ENABLE_TLS)
737 /*if (pids[1]) {
738 waitpid(pids[1], NULL, 0);
739 return;
740 }
741 pids[1] = fork_tls_client();*/
742
743 if (tids[1]) {
744 pthread_join(tids[1], NULL);
745 return;
746 }
747 tids[1] = thread_tls_client();
748#else
749 if (!tids[0] && !tids[1]) {
750 return;
751 }
752#endif
753}
754
755int
756main(void)
757{
758 struct ly_ctx *ctx;
759 int ret, i;
760 pthread_t tids[thread_count];
761
762 setup_lib();
763
764 ctx = ly_ctx_new(TESTS_DIR"/../schemas");
765 assert(ctx);
766 ly_ctx_load_module(ctx, "ietf-netconf", NULL);
767 nc_server_init(ctx);
768
769 pthread_barrier_init(&barrier, NULL, thread_count);
770
771#ifdef ENABLE_SSH
772 /* do first, so that client can connect on SSH */
773 ret = nc_server_ssh_add_endpt_listen("main", "0.0.0.0", 6001);
774 assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100775 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_dsa.pub", "test");
Michal Vasko734360b2016-02-08 15:38:25 +0100776 assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100777 ret = nc_server_ssh_endpt_set_hostkey("main", TESTS_DIR"/data/key_rsa");
Michal Vasko734360b2016-02-08 15:38:25 +0100778 assert(!ret);
779
780 /* for ssh_endpt_del_authkey */
Michal Vaskob477b222016-02-09 14:30:15 +0100781 ret = nc_server_ssh_endpt_add_authkey("main", TESTS_DIR"/data/key_ecdsa.pub", "test2");
Michal Vasko734360b2016-02-08 15:38:25 +0100782 assert(!ret);
783
784 /* for ssh_del_endpt */
785 ret = nc_server_ssh_add_endpt_listen("secondary", "0.0.0.0", 6002);
786 assert(!ret);
787
788 /* for ssh_endpt_set_port */
789 ret = nc_server_ssh_add_endpt_listen("quaternary", "0.0.0.0", 6004);
790 assert(!ret);
791#endif
792
793#ifdef ENABLE_TLS
794 /* do first, so that client can connect on TLS */
795 ret = nc_server_tls_add_endpt_listen("main", "0.0.0.0", 6501);
796 assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100797 ret = nc_server_tls_endpt_set_cert_path("main", TESTS_DIR"/data/server.crt");
Michal Vasko734360b2016-02-08 15:38:25 +0100798 assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100799 ret = nc_server_tls_endpt_set_key_path("main", TESTS_DIR"/data/server.key");
Michal Vasko734360b2016-02-08 15:38:25 +0100800 assert(!ret);
Michal Vaskob477b222016-02-09 14:30:15 +0100801 ret = nc_server_tls_endpt_add_trusted_cert_path("main", TESTS_DIR"/data/client.crt");
Michal Vasko734360b2016-02-08 15:38:25 +0100802 assert(!ret);
803 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");
804 assert(!ret);
805
806 /* for tls_del_endpt */
807 ret = nc_server_tls_add_endpt_listen("secondary", "0.0.0.0", 6502);
808 assert(!ret);
809
810 /* for tls_endpt_set_port */
811 ret = nc_server_tls_add_endpt_listen("quaternary", "0.0.0.0", 6504);
812 assert(!ret);
813
814 /* for tls_endpt_del_ctn */
815 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);
816 assert(!ret);
817#endif
818
Michal Vaskoe241c9f2016-02-09 11:04:58 +0100819 ret = nc_client_schema_searchpath(TESTS_DIR"/../schemas");
820 assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100821 clients_start_cleanup();
822
823 /* threads'n'stuff */
824 ret = 0;
825 for (i = 0; i < thread_count; ++i) {
826 ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL);
827 }
828 assert(!ret);
829
830 /* cleanup */
831 for (i = 0; i < thread_count; ++i) {
832 pthread_join(tids[i], NULL);
833 }
834
835 clients_start_cleanup();
836
837 pthread_barrier_destroy(&barrier);
838
Michal Vaskoe241c9f2016-02-09 11:04:58 +0100839 nc_client_schema_searchpath(NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100840 nc_server_destroy();
841 ly_ctx_destroy(ctx, NULL);
842
843 teardown_lib();
844
845 return 0;
846}