blob: 4a92281be0c9944e414026b0ba3cc695b3839e2b [file] [log] [blame]
David Sedlák6fdf1ec2018-09-30 21:42:31 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <setjmp.h>
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <errno.h>
7
8#include <cmocka.h>
9#include <libyang/libyang.h>
10#include <session_client.h>
11#include <log.h>
12#include <config.h>
13#include "tests/config.h"
14
15static int
16setup_f(void **state)
17{
18 (void)state;
19
20 nc_verbosity(NC_VERB_VERBOSE);
21
22 return 0;
23}
24
25static int
26teardown_f(void **state)
27{
28 (void)state;
29
30 return 0;
31}
32
33MOCK int
34__wrap_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
35{
36 (void)sockfd;
37 (void)addr;
38 (void)addrlen;
39
40 return (int)mock();
41}
42
43MOCK int
44__wrap_SSL_connect(SSL *ssl)
45{
46 (void)ssl;
47
48 return (int)mock();
49}
50
51MOCK int
52__wrap_nc_handshake_io(struct nc_session *session)
53{
54 (void)session;
55
56 return (int)mock();
57}
58
59MOCK int
60__wrap_nc_ctx_check_and_fill(struct nc_session *session)
61{
62 (void)session;
63
64 return (int)mock();
65}
66
67static void
68test_nc_client_tls_setting_cert_key_paths(void **state)
69{
70 (void)state;
71 const char *cert, *key;
72 int ret;
73
74 nc_client_init();
75
76 /* no certificats are set, nc_client_tls_get_cert_key_paths should output NULL */
77 nc_client_tls_get_cert_key_paths(&cert, &key);
78 assert_null(cert);
79 assert_null(key);
80
81 /* set certificate path */
82 ret = nc_client_tls_set_cert_key_paths("cert_path", "key_path");
83 assert_int_equal(ret, 0);
84 nc_client_tls_get_cert_key_paths(&cert, &key);
85 assert_string_equal(cert, "cert_path");
86 assert_string_equal(key, "key_path");
87
88 /* override certificate path */
89 ret = nc_client_tls_set_cert_key_paths("cert_path1", "key_path1");
90 assert_int_equal(ret, 0);
91 nc_client_tls_get_cert_key_paths(&cert, &key);
92 assert_string_equal(cert, "cert_path1");
93 assert_string_equal(key, "key_path1");
94}
95
96static void
97test_nc_client_tls_setting_trusted_ca_paths(void **state)
98{
99 (void)state;
100 const char *file, *dir;
101 int ret;
102
103 ret = nc_client_tls_set_trusted_ca_paths("ca_file", "ca_dir");
104 assert_int_equal(ret, 0);
105 nc_client_tls_get_trusted_ca_paths(&file, &dir);
106 assert_string_equal("ca_file", file);
107 assert_string_equal("ca_dir", dir);
108
109 ret = nc_client_tls_set_trusted_ca_paths("ca_file1", "ca_dir1");
110 assert_int_equal(ret, 0);
111 nc_client_tls_get_trusted_ca_paths(&file, &dir);
112 assert_string_equal("ca_file1", file);
113 assert_string_equal("ca_dir1", dir);
114}
115
116static void
117test_nc_connect_tls_succesfull(void **state)
118{
119 (void)state;
120 int ret;
121 struct nc_session *session;
122
123 ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key");
124 assert_int_equal(ret, 0);
125 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data");
126 assert_int_equal(ret, 0);
127
128 will_return(__wrap_connect, 0);
129 will_return(__wrap_SSL_connect, 1);
130
131 /* fake succesfull handshake */
132 will_return(__wrap_nc_handshake_io, 3);
133 will_return(__wrap_nc_ctx_check_and_fill, 0);
134 session = nc_connect_tls("0.0.0.0", 6001, NULL);
135 assert_non_null(session);
136
137 nc_session_free(session, NULL);
138}
139
140static void
141test_nc_client_tls_setting_crl_paths(void **state)
142{
143 (void)state;
144 const char *file, *dir;
145 int ret;
146
147 nc_client_tls_get_crl_paths(&file, &dir);
148 assert_null(file);
149 assert_null(dir);
150
151 ret = nc_client_tls_set_crl_paths("file", "dir");
152 assert_int_equal(ret, 0);
153 nc_client_tls_get_crl_paths(&file, &dir);
154 assert_string_equal(file, "file");
155 assert_string_equal(dir, "dir");
156
157 ret = nc_client_tls_set_crl_paths("file1", "dir1");
158 assert_int_equal(ret, 0);
159 nc_client_tls_get_crl_paths(&file, &dir);
160 assert_string_equal(file, "file1");
161 assert_string_equal(dir, "dir1");
162
163 /* destroy client */
164 nc_client_destroy();
165}
166
167static void
168test_nc_connect_tls_handshake_failed(void **state)
169{
170 (void)state;
171 int ret;
172 struct nc_session *session;
173
174 ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key");
175 assert_int_equal(ret, 0);
176 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data");
177 assert_int_equal(ret, 0);
178
179 will_return(__wrap_connect, 0);
180 will_return(__wrap_SSL_connect, 1);
181
182 /* fake failed handshake */
183 will_return(__wrap_nc_handshake_io, 0);
184 session = nc_connect_tls("0.0.0.0", 6001, NULL);
185 assert_null(session);
186}
187
188int
189main(void)
190{
191 const struct CMUnitTest tests[] = {
192 cmocka_unit_test_setup_teardown(test_nc_client_tls_setting_cert_key_paths, setup_f, teardown_f),
193 cmocka_unit_test_setup_teardown(test_nc_connect_tls_handshake_failed, setup_f, teardown_f),
194 cmocka_unit_test_setup_teardown(test_nc_connect_tls_succesfull, setup_f, teardown_f),
195 cmocka_unit_test_setup_teardown(test_nc_client_tls_setting_trusted_ca_paths, setup_f, teardown_f),
196 cmocka_unit_test_setup_teardown(test_nc_client_tls_setting_crl_paths, setup_f, teardown_f),
197 };
198
199 return cmocka_run_group_tests(tests, NULL, NULL);
200}