blob: ba09984df8fae498fa6dbfd341674f78bb494b10 [file] [log] [blame]
romanf6f37a52023-05-25 14:27:51 +02001/**
2 * @file test_replace.c
3 * @author Roman Janota <xjanot04@fit.vutbr.cz>
4 * @brief libnetconf2 Non-diff YANG data configuration test
5 *
6 * @copyright
7 * Copyright (c) 2023 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
16#define _GNU_SOURCE
17
18#include <errno.h>
19#include <pthread.h>
20#include <setjmp.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cmocka.h>
27
28#include "tests/config.h"
29
30#define NC_ACCEPT_TIMEOUT 2000
31#define NC_PS_POLL_TIMEOUT 2000
32
33struct ly_ctx *ctx;
34
35struct test_state {
36 pthread_barrier_t barrier;
37};
38
39const char *old_data =
40 "<netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\">\n"
41 " <listen>\n"
42 " <idle-timeout>10</idle-timeout>\n"
43 " <endpoint>\n"
44 " <name>old-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>old_key</name>\n"
54 " <public-key>\n"
roman7fdc84d2023-06-06 13:14:53 +020055 " <inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +020056 " <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+9eppeHrfrz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wGsLPduuRRH8q0n/osnoOutSQroE0n41UyIv2ZNccRwNmSzQcairBu2dSz02hlsh2otNl5IuGpOqXyPjXBpW4qGD6n2tH7THALnLC0BHtTSQVQsJsRM3gX39LoiWvLDp2qJvplm6rTpi8Rgap6rZSqHe1yNKIxxD2vlr/WY9SMgLXYASO4SSBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinbTsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPoSwr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JBdOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/KqDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8TbstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=</cleartext-private-key>\n"
roman7fdc84d2023-06-06 13:14:53 +020060 " </inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +020061 " </public-key>\n"
62 " </host-key>\n"
63 " </server-identity>\n"
64 " <client-authentication>\n"
65 " <users>\n"
66 " <user>\n"
67 " <name>old_user</name>\n"
68 " <password>$6$xyz$lomVe5tZ2Gz9uSKKywzXuPcHhqjIByhBbqdUTx/jAwUnw7JRp7QHd4ORiEVqxeZg1NEJkHux.mETo9BFPSh1x.</password>\n"
69 " </user>\n"
70 " </users>\n"
71 " </client-authentication>\n"
72 " </ssh-server-parameters>\n"
73 " </ssh>\n"
74 " </endpoint>\n"
75 " </listen>\n"
76 "</netconf-server>\n";
77
78const char *new_data =
79 "<netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\">\n"
80 " <listen>\n"
81 " <idle-timeout>10</idle-timeout>\n"
82 " <endpoint>\n"
83 " <name>new-ssh</name>\n"
84 " <ssh>\n"
85 " <tcp-server-parameters>\n"
86 " <local-address>127.0.0.1</local-address>\n"
87 " <local-port>10005</local-port>\n"
88 " </tcp-server-parameters>\n"
89 " <ssh-server-parameters>\n"
90 " <server-identity>\n"
91 " <host-key>\n"
92 " <name>new_key</name>\n"
93 " <public-key>\n"
roman7fdc84d2023-06-06 13:14:53 +020094 " <inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +020095 " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n"
96 " <public-key>AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=</public-key>\n"
97 " <private-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ec-private-key-format</private-key-format>\n"
98 " <cleartext-private-key>MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw==</cleartext-private-key>\n"
roman7fdc84d2023-06-06 13:14:53 +020099 " </inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +0200100 " </public-key>\n"
101 " </host-key>\n"
102 " </server-identity>\n"
103 " <client-authentication>\n"
104 " <users>\n"
105 " <user>\n"
106 " <name>new_user</name>\n"
107 " <public-keys>\n"
roman7fdc84d2023-06-06 13:14:53 +0200108 " <inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +0200109 " <public-key>\n"
110 " <name>test</name>\n"
111 " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n"
112 " <public-key>AAAAB3NzaC1yc2EAAAADAQABAAABAQDPavVALiM7QwTIUAndO8E9GOkSDQWjuEwkzbJ3kOBPa7kkq71UOZFeecDjFb9eipkljfFys/JYHGQaYVF8/svT0KV5h7HlutRdF6yvqSEbjpbTORb27pdHX3iFEyDCwCIoq9vMeX+wyXnteyn01GpIL0ig0WAnvkqX/SPjuplX5ZItUSr0MhXM7fNSX50BD6G8IO0/djUcdMUcjTjGv73SxB9ZzLvxnhXuUJbzEJJJLj6qajyEIVaJSa73vA33JCD8qzarrsuITojVLPDFmeHwSAoB5dP86yop6e6ypuXzKxxef6yNXcE8oTj8UFYBIXsgIP2nBvWk41EaK0Vk3YFl</public-key>\n"
113 " </public-key>\n"
roman7fdc84d2023-06-06 13:14:53 +0200114 " </inline-definition>\n"
romanf6f37a52023-05-25 14:27:51 +0200115 " </public-keys>\n"
116 " </user>\n"
117 " </users>\n"
118 " </client-authentication>\n"
119 " </ssh-server-parameters>\n"
120 " </ssh>\n"
121 " </endpoint>\n"
122 " </listen>\n"
123 "</netconf-server>\n";
124
125static void *
126server_thread(void *arg)
127{
128 int ret;
129 NC_MSG_TYPE msgtype;
130 struct nc_session *session;
131 struct nc_pollsession *ps;
132 struct test_state *state = arg;
133
134 ps = nc_ps_new();
135 assert_non_null(ps);
136
137 /* accept a session and add it to the poll session structure */
138 pthread_barrier_wait(&state->barrier);
139 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
140 assert_int_equal(msgtype, NC_MSG_HELLO);
141
142 ret = nc_ps_add_session(ps, session);
143 assert_int_equal(ret, 0);
144
145 do {
146 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
147 assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
148 } while (!(ret & NC_PSPOLL_SESSION_TERM));
149
150 nc_ps_clear(ps, 1, NULL);
151 nc_ps_free(ps);
romanf6f37a52023-05-25 14:27:51 +0200152 return NULL;
153}
154
155static void *
156client_thread(void *arg)
157{
158 int ret;
159 struct nc_session *session = NULL;
160 struct test_state *state = arg;
161
162 /* skip all hostkey and known_hosts checks */
163 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
164
165 /* set directory where to search for modules */
166 ret = nc_client_set_schema_searchpath(MODULES_DIR);
167 assert_int_equal(ret, 0);
168
169 /* set ssh username */
roman142718b2023-06-29 09:15:29 +0200170 ret = nc_client_ssh_set_username("new_client");
romanf6f37a52023-05-25 14:27:51 +0200171 assert_int_equal(ret, 0);
172
173 /* add client's key pair */
174 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
175 assert_int_equal(ret, 0);
176
177 /* wait for the server to reach polling */
178 pthread_barrier_wait(&state->barrier);
179
180 /* connect */
181 session = nc_connect_ssh("127.0.0.1", 10005, NULL);
182 assert_non_null(session);
183
184 nc_session_free(session, NULL);
romanf6f37a52023-05-25 14:27:51 +0200185 return NULL;
186}
187
188static void
189nc_test_replace(void **state)
190{
191 int ret, i;
192 pthread_t tids[2];
193
194 assert_non_null(state);
195
196 ret = pthread_create(&tids[0], NULL, client_thread, *state);
197 assert_int_equal(ret, 0);
198 ret = pthread_create(&tids[1], NULL, server_thread, *state);
199 assert_int_equal(ret, 0);
200
201 for (i = 0; i < 2; i++) {
202 pthread_join(tids[i], NULL);
203 }
204}
205
206static int
207setup_f(void **state)
208{
209 int ret;
210 struct lyd_node *old_tree = NULL, *new_tree = NULL;
211 struct test_state *test_state;
212
213 nc_verbosity(NC_VERB_VERBOSE);
214
215 /* init barrier */
216 test_state = malloc(sizeof *test_state);
217 assert_non_null(test_state);
218
219 ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
220 assert_int_equal(ret, 0);
221
222 *state = test_state;
223
224 /* create new context */
225 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
226 assert_int_equal(ret, 0);
227
228 /* load default modules into context */
229 ret = nc_server_init_ctx(&ctx);
230 assert_int_equal(ret, 0);
231
232 /* load ietf-netconf-server module and it's imports into context */
233 ret = nc_server_config_load_modules(&ctx);
234 assert_int_equal(ret, 0);
235
roman142718b2023-06-29 09:15:29 +0200236 ret = nc_server_config_new_address_port(ctx, "old", NC_TI_LIBSSH, "127.0.0.1", 10005, &old_tree);
237 assert_int_equal(ret, 0);
238
239 ret = nc_server_config_new_ssh_hostkey(ctx, "old", "old_key", TESTS_DIR "/data/key_rsa", NULL, &old_tree);
240 assert_int_equal(ret, 0);
241
roman8ba6efa2023-07-12 15:27:52 +0200242 ret = nc_server_config_new_ssh_user_password(ctx, "old", "old_client", "passwd", &old_tree);
romanf6f37a52023-05-25 14:27:51 +0200243 assert_int_equal(ret, 0);
244
245 /* configure the server based on the yang data, treat them as if every node had replace operation */
246 ret = nc_server_config_setup_data(old_tree);
247 assert_int_equal(ret, 0);
248
roman142718b2023-06-29 09:15:29 +0200249 ret = nc_server_config_new_address_port(ctx, "new", NC_TI_LIBSSH, "127.0.0.1", 10005, &new_tree);
250 assert_int_equal(ret, 0);
251
252 ret = nc_server_config_new_ssh_hostkey(ctx, "new", "new_key", TESTS_DIR "/data/key_rsa", NULL, &new_tree);
253 assert_int_equal(ret, 0);
254
roman8ba6efa2023-07-12 15:27:52 +0200255 ret = nc_server_config_new_ssh_user_pubkey(ctx, "new", "new_client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &new_tree);
romanf6f37a52023-05-25 14:27:51 +0200256 assert_int_equal(ret, 0);
257
258 /* configure the server based on the yang data, meaning
259 * everything configured will be deleted and only the new data applied
260 */
261 ret = nc_server_config_setup_data(new_tree);
262 assert_int_equal(ret, 0);
263
romanf6f37a52023-05-25 14:27:51 +0200264 /* initialize the server */
265 ret = nc_server_init();
266 assert_int_equal(ret, 0);
267
268 lyd_free_all(old_tree);
269 lyd_free_all(new_tree);
270
271 return 0;
272}
273
274static int
275teardown_f(void **state)
276{
277 int ret = 0;
278 struct test_state *test_state;
279
280 assert_non_null(state);
281 test_state = *state;
282
283 ret = pthread_barrier_destroy(&test_state->barrier);
284 assert_int_equal(ret, 0);
285
286 free(*state);
287 nc_client_destroy();
288 nc_server_destroy();
289 ly_ctx_destroy(ctx);
290
291 return 0;
292}
293
294int
295main(void)
296{
297 const struct CMUnitTest tests[] = {
298 cmocka_unit_test_setup_teardown(nc_test_replace, setup_f, teardown_f),
299 };
300
301 setenv("CMOCKA_TEST_ABORT", "1", 1);
302 return cmocka_run_group_tests(tests, NULL, NULL);
303}