roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_auth.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
| 4 | * @brief libnetconf2 Linux PAM keyboard-interactive authentication test |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2022 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <pthread.h> |
| 20 | #include <setjmp.h> |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 28 | #include "tests/config.h" |
| 29 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 30 | #define NC_ACCEPT_TIMEOUT 2000 |
| 31 | #define NC_PS_POLL_TIMEOUT 2000 |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 32 | |
| 33 | struct ly_ctx *ctx; |
| 34 | |
| 35 | struct test_state { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 36 | pthread_barrier_t barrier; |
| 37 | }; |
| 38 | |
| 39 | const char *data = |
| 40 | "<netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">\n" |
| 41 | " <listen yang:operation=\"create\">\n" |
| 42 | " <idle-timeout>10</idle-timeout>\n" |
| 43 | " <endpoint>\n" |
| 44 | " <name>default-ssh</name>\n" |
| 45 | " <ssh>\n" |
| 46 | " <tcp-server-parameters>\n" |
| 47 | " <local-address>127.0.0.1</local-address>\n" |
| 48 | " <local-port>10005</local-port>\n" |
| 49 | " </tcp-server-parameters>\n" |
| 50 | " <ssh-server-parameters>\n" |
| 51 | " <server-identity>\n" |
| 52 | " <host-key>\n" |
| 53 | " <name>key</name>\n" |
| 54 | " <public-key>\n" |
| 55 | " <local-definition>\n" |
| 56 | " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n" |
| 57 | " <public-key>MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1VArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6alwf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4gfNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+ZczSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+dKYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FKtGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/ft1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBToE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yVONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEAAQ==</public-key>\n" |
| 58 | " <private-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:rsa-private-key-format</private-key-format>\n" |
| 59 | " <cleartext-private-key>MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1VArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6alwf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4gfNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+ZczSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+dKYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FKtGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/ft1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBToE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yVONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEAAQKCAgAeRZw75Oszoqj0jfMmMILdD3Cfad+dY3FvLESYESeyt0XAX8XoOed6ymQj1qPGxQGGkkBvPEgv1b3jrC8Rhfb3Ct39Z7mRpTar5iHhwwBUboBTUmQ0vR173iAHX8sw2Oa17mCO/CDlr8Fu4Xc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R3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8TbstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=</cleartext-private-key>\n" |
| 60 | " </local-definition>\n" |
| 61 | " </public-key>\n" |
| 62 | " </host-key>\n" |
| 63 | " </server-identity>\n" |
| 64 | " <client-authentication>\n" |
| 65 | " <users>\n" |
| 66 | " <user>\n" |
| 67 | " <name>test_pk</name>\n" |
| 68 | " <public-keys>\n" |
| 69 | " <local-definition>\n" |
| 70 | " <public-key>\n" |
| 71 | " <name>test</name>\n" |
| 72 | " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n" |
| 73 | " <public-key>AAAAB3NzaC1yc2EAAAADAQABAAABAQDPavVALiM7QwTIUAndO8E9GOkSDQWjuEwkzbJ3kOBPa7kkq71UOZFeecDjFb9eipkljfFys/JYHGQaYVF8/svT0KV5h7HlutRdF6yvqSEbjpbTORb27pdHX3iFEyDCwCIoq9vMeX+wyXnteyn01GpIL0ig0WAnvkqX/SPjuplX5ZItUSr0MhXM7fNSX50BD6G8IO0/djUcdMUcjTjGv73SxB9ZzLvxnhXuUJbzEJJJLj6qajyEIVaJSa73vA33JCD8qzarrsuITojVLPDFmeHwSAoB5dP86yop6e6ypuXzKxxef6yNXcE8oTj8UFYBIXsgIP2nBvWk41EaK0Vk3YFl</public-key>\n" |
| 74 | " </public-key>\n" |
| 75 | " </local-definition>\n" |
| 76 | " </public-keys>\n" |
| 77 | " </user>\n" |
| 78 | " <user>\n" |
| 79 | " <name>test_int</name>\n" |
| 80 | " <keyboard-interactive xmlns=\"urn:cesnet:libnetconf2-netconf-server\">\n" |
| 81 | " <pam-config-file-name>netconf.conf</pam-config-file-name>\n" |
| 82 | " <pam-config-file-dir>" BUILD_DIR "/tests</pam-config-file-dir>\n" |
| 83 | " </keyboard-interactive>\n" |
| 84 | " </user>\n" |
| 85 | " <user>\n" |
| 86 | " <name>test_pw</name>\n" |
| 87 | " <password>$6$xyz$lomVe5tZ2Gz9uSKKywzXuPcHhqjIByhBbqdUTx/jAwUnw7JRp7QHd4ORiEVqxeZg1NEJkHux.mETo9BFPSh1x.</password>\n" |
| 88 | " </user>\n" |
| 89 | " <user>\n" |
| 90 | " <name>test_none</name>\n" |
| 91 | " <none/>\n" |
| 92 | " </user>\n" |
| 93 | " </users>\n" |
| 94 | " </client-authentication>\n" |
| 95 | " <transport-params>\n" |
| 96 | " <host-key>\n" |
| 97 | " <host-key-alg xmlns:sshpka=\"urn:ietf:params:xml:ns:yang:iana-ssh-public-key-algs\">sshpka:rsa-sha2-512</host-key-alg>\n" |
| 98 | " </host-key>\n" |
| 99 | " <key-exchange>\n" |
| 100 | " <key-exchange-alg xmlns:sshkea=\"urn:ietf:params:xml:ns:yang:iana-ssh-key-exchange-algs\">sshkea:curve25519-sha256</key-exchange-alg>\n" |
| 101 | " </key-exchange>\n" |
| 102 | " <encryption>\n" |
| 103 | " <encryption-alg xmlns:sshea=\"urn:ietf:params:xml:ns:yang:iana-ssh-encryption-algs\">sshea:aes256-ctr</encryption-alg>\n" |
| 104 | " </encryption>\n" |
| 105 | " <mac>\n" |
| 106 | " <mac-alg xmlns:sshma=\"urn:ietf:params:xml:ns:yang:iana-ssh-mac-algs\">sshma:hmac-sha2-512</mac-alg>\n" |
| 107 | " </mac>\n" |
| 108 | " </transport-params>\n" |
| 109 | " </ssh-server-parameters>\n" |
| 110 | " </ssh>\n" |
| 111 | " </endpoint>\n" |
| 112 | " </listen>\n" |
| 113 | "</netconf-server>\n"; |
| 114 | |
| 115 | static void * |
| 116 | server_thread(void *arg) |
| 117 | { |
| 118 | int ret; |
| 119 | NC_MSG_TYPE msgtype; |
| 120 | struct nc_session *session; |
| 121 | struct nc_pollsession *ps; |
| 122 | struct test_state *state = arg; |
| 123 | |
| 124 | (void) arg; |
| 125 | |
| 126 | ps = nc_ps_new(); |
| 127 | assert_non_null(ps); |
| 128 | |
| 129 | /* accept a session and add it to the poll session structure */ |
| 130 | pthread_barrier_wait(&state->barrier); |
| 131 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 132 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 133 | |
| 134 | ret = nc_ps_add_session(ps, session); |
| 135 | assert_int_equal(ret, 0); |
| 136 | |
| 137 | do { |
| 138 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 139 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 140 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 141 | |
| 142 | nc_ps_clear(ps, 1, NULL); |
| 143 | nc_ps_free(ps); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 147 | static char * |
| 148 | auth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv) |
| 149 | { |
| 150 | (void) instruction; |
| 151 | (void) echo; |
| 152 | (void) auth_name; |
| 153 | (void) priv; |
| 154 | |
| 155 | /* send the replies to keyboard-interactive authentication */ |
| 156 | if (strstr(prompt, "backwards")) { |
| 157 | return strdup("tni_tset"); |
| 158 | } else if (strstr(prompt, "1+1")) { |
| 159 | return strdup("2"); |
| 160 | } else { |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void * |
| 166 | client_thread_interactive(void *arg) |
| 167 | { |
| 168 | int ret; |
| 169 | struct nc_session *session = NULL; |
| 170 | struct test_state *state = arg; |
| 171 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 172 | /* skip all hostkey and known_hosts checks */ |
| 173 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 174 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 175 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 176 | assert_int_equal(ret, 0); |
| 177 | |
| 178 | ret = nc_client_ssh_set_username("test_int"); |
| 179 | assert_int_equal(ret, 0); |
| 180 | |
| 181 | /* set keyboard-interactive authentication callback */ |
| 182 | nc_client_ssh_set_auth_interactive_clb(auth_interactive, NULL); |
| 183 | |
| 184 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
| 185 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 186 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 1); |
| 187 | |
| 188 | pthread_barrier_wait(&state->barrier); |
| 189 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 190 | assert_non_null(session); |
| 191 | |
| 192 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | test_nc_auth_interactive(void **state) |
| 198 | { |
| 199 | int ret, i; |
| 200 | pthread_t tids[2]; |
| 201 | |
| 202 | assert_non_null(state); |
| 203 | |
| 204 | ret = pthread_create(&tids[0], NULL, client_thread_interactive, *state); |
| 205 | assert_int_equal(ret, 0); |
| 206 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 207 | assert_int_equal(ret, 0); |
| 208 | |
| 209 | for (i = 0; i < 2; i++) { |
| 210 | pthread_join(tids[i], NULL); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static char * |
| 215 | auth_password(const char *username, const char *hostname, void *priv) |
| 216 | { |
| 217 | (void) hostname; |
| 218 | (void) priv; |
| 219 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 220 | /* set the reply to password authentication */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 221 | if (!strcmp(username, "test_pw")) { |
| 222 | return strdup("testpw"); |
| 223 | } else { |
| 224 | return NULL; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void * |
| 229 | client_thread_password(void *arg) |
| 230 | { |
| 231 | int ret; |
| 232 | struct nc_session *session = NULL; |
| 233 | struct test_state *state = arg; |
| 234 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 235 | /* skip all hostkey and known_hosts checks */ |
| 236 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 237 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 238 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 239 | assert_int_equal(ret, 0); |
| 240 | |
| 241 | ret = nc_client_ssh_set_username("test_pw"); |
| 242 | assert_int_equal(ret, 0); |
| 243 | |
| 244 | nc_client_ssh_set_auth_password_clb(auth_password, NULL); |
| 245 | |
| 246 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
| 247 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, 1); |
| 248 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 249 | |
| 250 | pthread_barrier_wait(&state->barrier); |
| 251 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 252 | assert_non_null(session); |
| 253 | |
| 254 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | static void |
| 259 | test_nc_auth_password(void **state) |
| 260 | { |
| 261 | int ret, i; |
| 262 | pthread_t tids[2]; |
| 263 | |
| 264 | assert_non_null(state); |
| 265 | |
| 266 | ret = pthread_create(&tids[0], NULL, client_thread_password, *state); |
| 267 | assert_int_equal(ret, 0); |
| 268 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 269 | assert_int_equal(ret, 0); |
| 270 | |
| 271 | for (i = 0; i < 2; i++) { |
| 272 | pthread_join(tids[i], NULL); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void * |
| 277 | client_thread_pubkey(void *arg) |
| 278 | { |
| 279 | int ret; |
| 280 | struct nc_session *session = NULL; |
| 281 | struct test_state *state = arg; |
| 282 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 283 | /* skip all hostkey and known_hosts checks */ |
| 284 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 285 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 286 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 287 | assert_int_equal(ret, 0); |
| 288 | |
| 289 | ret = nc_client_ssh_set_username("test_pk"); |
| 290 | assert_int_equal(ret, 0); |
| 291 | |
| 292 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| 293 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 294 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 295 | |
| 296 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa"); |
| 297 | assert_int_equal(ret, 0); |
| 298 | |
| 299 | pthread_barrier_wait(&state->barrier); |
| 300 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 301 | assert_non_null(session); |
| 302 | |
| 303 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | test_nc_auth_pubkey(void **state) |
| 309 | { |
| 310 | int ret, i; |
| 311 | pthread_t tids[2]; |
| 312 | |
| 313 | assert_non_null(state); |
| 314 | |
| 315 | ret = pthread_create(&tids[0], NULL, client_thread_pubkey, *state); |
| 316 | assert_int_equal(ret, 0); |
| 317 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 318 | assert_int_equal(ret, 0); |
| 319 | |
| 320 | for (i = 0; i < 2; i++) { |
| 321 | pthread_join(tids[i], NULL); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static void * |
| 326 | client_thread_none(void *arg) |
| 327 | { |
| 328 | int ret; |
| 329 | struct nc_session *session = NULL; |
| 330 | struct test_state *state = arg; |
| 331 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 332 | /* skip all hostkey and known_hosts checks */ |
| 333 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 334 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 335 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 336 | assert_int_equal(ret, 0); |
| 337 | |
| 338 | ret = nc_client_ssh_set_username("test_none"); |
| 339 | assert_int_equal(ret, 0); |
| 340 | |
| 341 | pthread_barrier_wait(&state->barrier); |
| 342 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 343 | assert_non_null(session); |
| 344 | |
| 345 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | test_nc_auth_none(void **state) |
| 351 | { |
| 352 | int ret, i; |
| 353 | pthread_t tids[2]; |
| 354 | |
| 355 | assert_non_null(state); |
| 356 | |
| 357 | ret = pthread_create(&tids[0], NULL, client_thread_none, *state); |
| 358 | assert_int_equal(ret, 0); |
| 359 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 360 | assert_int_equal(ret, 0); |
| 361 | |
| 362 | for (i = 0; i < 2; i++) { |
| 363 | pthread_join(tids[i], NULL); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | setup_f(void **state) |
| 369 | { |
| 370 | int ret; |
| 371 | struct lyd_node *tree; |
| 372 | struct test_state *test_state; |
| 373 | |
| 374 | nc_verbosity(NC_VERB_VERBOSE); |
| 375 | |
| 376 | /* init barrier */ |
| 377 | test_state = malloc(sizeof *test_state); |
| 378 | assert_non_null(test_state); |
| 379 | |
| 380 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 381 | assert_int_equal(ret, 0); |
| 382 | |
| 383 | *state = test_state; |
| 384 | |
| 385 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 386 | assert_int_equal(ret, 0); |
| 387 | |
| 388 | ret = nc_server_init_ctx(&ctx); |
| 389 | assert_int_equal(ret, 0); |
| 390 | |
| 391 | ret = nc_server_config_load_modules(&ctx); |
| 392 | assert_int_equal(ret, 0); |
| 393 | |
| 394 | /* parse yang data */ |
| 395 | ret = lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_NO_STATE | LYD_PARSE_STRICT, LYD_VALIDATE_NO_STATE, &tree); |
| 396 | assert_int_equal(ret, 0); |
| 397 | |
| 398 | /* configure the server based on the data */ |
roman | f6f37a5 | 2023-05-25 14:27:51 +0200 | [diff] [blame] | 399 | ret = nc_server_config_setup_diff(tree); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 400 | assert_int_equal(ret, 0); |
| 401 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 402 | ret = nc_server_init(); |
| 403 | assert_int_equal(ret, 0); |
| 404 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 405 | lyd_free_all(tree); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int |
| 411 | teardown_f(void **state) |
| 412 | { |
| 413 | int ret = 0; |
| 414 | struct test_state *test_state; |
| 415 | |
| 416 | assert_non_null(state); |
| 417 | test_state = *state; |
| 418 | |
| 419 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 420 | assert_int_equal(ret, 0); |
| 421 | |
| 422 | free(*state); |
| 423 | nc_client_destroy(); |
| 424 | nc_server_destroy(); |
| 425 | ly_ctx_destroy(ctx); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int |
| 431 | main(void) |
| 432 | { |
| 433 | const struct CMUnitTest tests[] = { |
| 434 | cmocka_unit_test_setup_teardown(test_nc_auth_interactive, setup_f, teardown_f), |
| 435 | cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, teardown_f), |
| 436 | cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, teardown_f), |
| 437 | cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, teardown_f) |
| 438 | }; |
| 439 | |
| 440 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 441 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 442 | } |