Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <setjmp.h> |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 5 | #include <sys/socket.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 6 | #include <sys/types.h> |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 7 | |
| 8 | #include <cmocka.h> |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 9 | #include <config.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 10 | #include <libyang/libyang.h> |
| 11 | #include <log.h> |
| 12 | #include <session_client.h> |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 13 | #include "tests/config.h" |
| 14 | |
| 15 | static int |
| 16 | setup_f(void **state) |
| 17 | { |
| 18 | (void)state; |
| 19 | |
| 20 | nc_verbosity(NC_VERB_VERBOSE); |
| 21 | |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int |
| 26 | teardown_f(void **state) |
| 27 | { |
| 28 | (void)state; |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | MOCK 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 | |
| 43 | MOCK int |
| 44 | __wrap_SSL_connect(SSL *ssl) |
| 45 | { |
| 46 | (void)ssl; |
| 47 | |
| 48 | return (int)mock(); |
| 49 | } |
| 50 | |
| 51 | MOCK int |
| 52 | __wrap_nc_handshake_io(struct nc_session *session) |
| 53 | { |
| 54 | (void)session; |
| 55 | |
| 56 | return (int)mock(); |
| 57 | } |
| 58 | |
| 59 | MOCK int |
| 60 | __wrap_nc_ctx_check_and_fill(struct nc_session *session) |
| 61 | { |
| 62 | (void)session; |
| 63 | |
| 64 | return (int)mock(); |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | test_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 | |
| 96 | static void |
| 97 | test_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 | |
| 116 | static void |
| 117 | test_nc_connect_tls_succesfull(void **state) |
| 118 | { |
| 119 | (void)state; |
| 120 | int ret; |
| 121 | struct nc_session *session; |
| 122 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 123 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 124 | assert_int_equal(ret, 0); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 125 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 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 | |
| 140 | static void |
| 141 | test_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 | |
| 167 | static void |
| 168 | test_nc_connect_tls_handshake_failed(void **state) |
| 169 | { |
| 170 | (void)state; |
| 171 | int ret; |
| 172 | struct nc_session *session; |
| 173 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 174 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 175 | assert_int_equal(ret, 0); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 176 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 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 | |
| 188 | int |
| 189 | main(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); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 200 | } |