blob: 989c325c95f6fdf1328760564eb4e841136941d0 [file] [log] [blame]
Michal Vaskoba9f3582023-02-22 10:26:32 +01001/**
2 * @file test_client_messages.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4 * @brief client messages test
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
16
Michal Vaskob83a3fa2021-05-26 09:53:42 +020017#include <errno.h>
18#include <setjmp.h>
David Sedlák19af2662018-10-01 13:42:08 +020019#include <stdio.h>
20#include <stdlib.h>
David Sedlák3983ed02018-10-07 23:17:19 +020021#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <sys/socket.h>
23#include <sys/types.h>
David Sedlák19af2662018-10-01 13:42:08 +020024
25#include <cmocka.h>
David Sedlák19af2662018-10-01 13:42:08 +020026#include <config.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020027#include <libyang/libyang.h>
28#include <log.h>
David Sedlák19af2662018-10-01 13:42:08 +020029#include <messages_p.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <session_client.h>
David Sedlák19af2662018-10-01 13:42:08 +020031#include "tests/config.h"
32
33static int
34setup_f(void **state)
35{
36 (void)state;
37
38 nc_verbosity(NC_VERB_VERBOSE);
39
40 return 0;
41}
42
43static int
44teardown_f(void **state)
45{
46 (void)state;
47
48 return 0;
49}
50
David Sedlák2f2f2e32018-10-08 21:49:32 +020051static void
52test_nc_rpc_act_generic_xml(void **state)
53{
54 (void)state;
55 struct nc_rpc *rpc = NULL;
56 struct nc_rpc_act_generic *generic_rpc = NULL;
57
58 /* create generic rpc with NC_PARAMTYPE_CONST */
59 rpc = nc_rpc_act_generic_xml("xml", NC_PARAMTYPE_CONST);
60 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
61 generic_rpc = (struct nc_rpc_act_generic *)rpc;
62 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
63 assert_int_equal(generic_rpc->has_data, 0);
64 assert_string_equal(generic_rpc->content.xml_str, "xml");
65 nc_rpc_free(rpc);
66
67 /* create generic rpc with NC_PARAMTYPE_FREE */
68 char *str = strdup("str");
Michal Vaskob83a3fa2021-05-26 09:53:42 +020069
David Sedlák2f2f2e32018-10-08 21:49:32 +020070 rpc = nc_rpc_act_generic_xml(str, NC_PARAMTYPE_FREE);
71 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
72 generic_rpc = (struct nc_rpc_act_generic *)rpc;
73 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
74 assert_int_equal(generic_rpc->has_data, 0);
75 assert_string_equal(generic_rpc->content.xml_str, str);
76 nc_rpc_free(rpc);
77
78 /* create generic rpc with NC_PARAMTYPE_DUP_AND_FREE */
79 rpc = nc_rpc_act_generic_xml("xml", NC_PARAMTYPE_DUP_AND_FREE);
80 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
81 generic_rpc = (struct nc_rpc_act_generic *)rpc;
82 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
83 assert_int_equal(generic_rpc->has_data, 0);
84 assert_string_equal(generic_rpc->content.xml_str, "xml");
85 nc_rpc_free(rpc);
86}
87
88static void
89test_nc_rpc_act_generic(void **state)
90{
91 (void)state;
92 struct nc_rpc *rpc = NULL;
93 struct nc_rpc_act_generic *generic_rpc = NULL;
94 struct lyd_node node;
Michal Vaskob83a3fa2021-05-26 09:53:42 +020095
David Sedlák2f2f2e32018-10-08 21:49:32 +020096 node.next = NULL;
97 node.prev = &node;
98
99 rpc = nc_rpc_act_generic(&node, NC_PARAMTYPE_CONST);
100 assert_non_null(rpc);
101 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
102 generic_rpc = (struct nc_rpc_act_generic *)rpc;
103 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
104 assert_int_equal(generic_rpc->has_data, 1);
105 assert_ptr_equal(generic_rpc->content.data, &node);
106 nc_rpc_free(rpc);
107}
108
David Sedlák3983ed02018-10-07 23:17:19 +0200109/* function to check if values of getconfig rpc are set correctly */
110void
111check_getconfig(struct nc_rpc *rpc, enum NC_DATASTORE_TYPE source, char *filter, NC_WD_MODE wd_mode)
112{
113 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETCONFIG);
114 struct nc_rpc_getconfig *getconfig_rpc = (struct nc_rpc_getconfig *)rpc;
115
116 assert_int_equal(getconfig_rpc->type, NC_RPC_GETCONFIG);
117 assert_int_equal(getconfig_rpc->source, source);
118 assert_string_equal(getconfig_rpc->filter, filter);
119 assert_int_equal(getconfig_rpc->wd_mode, wd_mode);
120}
121
David Sedlák19af2662018-10-01 13:42:08 +0200122static void
123test_nc_rpc_getconfig(void **state)
124{
125 (void)state;
126 struct nc_rpc *rpc = NULL;
127
David Sedlák3983ed02018-10-07 23:17:19 +0200128 /* create getconfig rpc with NC_PARAMTYPE_CONST */
129 rpc = nc_rpc_getconfig(NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200130 assert_non_null(rpc);
Michal Vasko79142c22020-07-29 15:26:51 +0200131 check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN);
David Sedlák19af2662018-10-01 13:42:08 +0200132 nc_rpc_free(rpc);
133
David Sedlák3983ed02018-10-07 23:17:19 +0200134 /* create getconfig rpc with NC_PARAMTYPE_FREE */
135 char *filter = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200136
David Sedlák3983ed02018-10-07 23:17:19 +0200137 rpc = nc_rpc_getconfig(NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
138 assert_non_null(rpc);
139 check_getconfig(rpc, NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT);
140 nc_rpc_free(rpc);
141
142 /* create getconfig rpc with NC_PARAMTYPE_DUP_AND_FREE */
143 rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, "filter", NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE);
144 assert_non_null(rpc);
145 check_getconfig(rpc, NC_DATASTORE_RUNNING, "filter", NC_WD_ALL);
146 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200147}
148
David Sedlák3983ed02018-10-07 23:17:19 +0200149/* function to check if values of edit rpc are set correctly */
150void
151check_edit(struct nc_rpc *rpc, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200152 NC_RPC_EDIT_ERROPT error_opt, const char *edit_content)
David Sedlák3983ed02018-10-07 23:17:19 +0200153{
154 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDIT);
155 struct nc_rpc_edit *edit_rpc = (struct nc_rpc_edit *)rpc;
156
157 assert_int_equal(edit_rpc->type, NC_RPC_EDIT);
158 assert_int_equal(edit_rpc->target, target);
159 assert_int_equal(edit_rpc->default_op, default_op);
160 assert_int_equal(edit_rpc->test_opt, test_opt);
161 assert_int_equal(edit_rpc->error_opt, error_opt);
162 assert_string_equal(edit_rpc->edit_cont, edit_content);
163}
164
David Sedlák19af2662018-10-01 13:42:08 +0200165static void
166test_nc_rpc_edit(void **state)
167{
168 (void)state;
169 struct nc_rpc *rpc = NULL;
170
David Sedlák3983ed02018-10-07 23:17:19 +0200171 /* create edit rpc with NC_PARAMTYPE_CONST */
172 rpc = nc_rpc_edit(NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, NC_RPC_EDIT_TESTOPT_TESTSET,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200173 NC_RPC_EDIT_ERROPT_STOP, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200174 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200175 check_edit(rpc, NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200176 NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200177 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200178
179 /* create edit rpc with NC_PARAMTYPE_FREE */
180 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200181
David Sedlák3983ed02018-10-07 23:17:19 +0200182 rpc = nc_rpc_edit(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_SET,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200183 NC_RPC_EDIT_ERROPT_ROLLBACK, str, NC_PARAMTYPE_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200184 assert_non_null(rpc);
185 check_edit(rpc, NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200186 NC_RPC_EDIT_TESTOPT_SET, NC_RPC_EDIT_ERROPT_ROLLBACK, str);
David Sedlák3983ed02018-10-07 23:17:19 +0200187 nc_rpc_free(rpc);
188
189 /* create edit rpc with NC_PARAMTYPE_DUP_AND_FREE */
190 rpc = nc_rpc_edit(NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, NC_RPC_EDIT_TESTOPT_TEST,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200191 NC_RPC_EDIT_ERROPT_CONTINUE, "url1", NC_PARAMTYPE_DUP_AND_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200192 assert_non_null(rpc);
193 check_edit(rpc, NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200194 NC_RPC_EDIT_TESTOPT_TEST, NC_RPC_EDIT_ERROPT_CONTINUE, "url1");
David Sedlák3983ed02018-10-07 23:17:19 +0200195 nc_rpc_free(rpc);
196}
197
198/* function to check if values of copy rpc are set correctly */
199void
200check_copy(struct nc_rpc *rpc, NC_DATASTORE target, const char *url_trg, NC_DATASTORE source,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200201 const char *url_or_config_src, NC_WD_MODE wd_mode)
David Sedlák3983ed02018-10-07 23:17:19 +0200202{
203 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COPY);
204 struct nc_rpc_copy *copy_rpc = (struct nc_rpc_copy *)rpc;
205
206 assert_int_equal(copy_rpc->type, NC_RPC_COPY);
207 assert_int_equal(copy_rpc->target, target);
208 assert_string_equal(copy_rpc->url_trg, url_trg);
209 assert_int_equal(copy_rpc->source, source);
210 assert_string_equal(copy_rpc->url_config_src, url_or_config_src);
211 assert_int_equal(copy_rpc->wd_mode, wd_mode);
David Sedlák19af2662018-10-01 13:42:08 +0200212}
213
214static void
215test_nc_rpc_copy(void **state)
216{
217 (void)state;
218 struct nc_rpc *rpc = NULL;
219
David Sedlák3983ed02018-10-07 23:17:19 +0200220 /* create copy rpc with NC_PARAMTYPE_CONST */
David Sedlák19af2662018-10-01 13:42:08 +0200221 rpc = nc_rpc_copy(NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200222 NC_WD_ALL, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200223 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200224 check_copy(rpc, NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", NC_WD_ALL);
David Sedlák19af2662018-10-01 13:42:08 +0200225 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200226
227 /* create copy rpc with NC_PARAMTYPE_FREE */
228 char *target = strdup("target");
229 char *src = strdup("src");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200230
David Sedlák3983ed02018-10-07 23:17:19 +0200231 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200232 NC_WD_ALL_TAG, NC_PARAMTYPE_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200233 assert_non_null(rpc);
234 check_copy(rpc, NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, NC_WD_ALL_TAG);
235 nc_rpc_free(rpc);
236
237 /* create copy rpc with NC_PARAMTYPE_DUP_AND_FREE */
238 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200239 NC_WD_TRIM, NC_PARAMTYPE_DUP_AND_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200240 assert_non_null(rpc);
241 check_copy(rpc, NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url", NC_WD_TRIM);
242 nc_rpc_free(rpc);
243}
244
245/* function to check if values of delete rpc are set correctly */
246void
247check_delete(struct nc_rpc *rpc, NC_DATASTORE target, const char *url)
248{
249 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DELETE);
250 struct nc_rpc_delete *delete_rpc = (struct nc_rpc_delete *)rpc;
251
252 assert_int_equal(delete_rpc->type, NC_RPC_DELETE);
253 assert_int_equal(delete_rpc->target, target);
254 assert_string_equal(delete_rpc->url, url);
David Sedlák19af2662018-10-01 13:42:08 +0200255}
256
257static void
258test_nc_rpc_delete(void **state)
259{
260 (void)state;
261 struct nc_rpc *rpc = NULL;
262
David Sedlák3983ed02018-10-07 23:17:19 +0200263 /* create delete rpc with NC_PARAMTYPE_CONST */
264 rpc = nc_rpc_delete(NC_DATASTORE_RUNNING, "target-url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200265 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200266 check_delete(rpc, NC_DATASTORE_RUNNING, "target-url");
267 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200268
David Sedlák3983ed02018-10-07 23:17:19 +0200269 /* create delete rpc with NC_PARAMTYPE_FREE */
270 char *url = strdup("url");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200271
David Sedlák3983ed02018-10-07 23:17:19 +0200272 rpc = nc_rpc_delete(NC_DATASTORE_CANDIDATE, url, NC_PARAMTYPE_FREE);
273 assert_non_null(rpc);
274 check_delete(rpc, NC_DATASTORE_CANDIDATE, url);
275 nc_rpc_free(rpc);
276
277 /* create delete rpc with NC_PARAMTYPE_DUP_AND_FREE */
278 rpc = nc_rpc_delete(NC_DATASTORE_CONFIG, "target", NC_PARAMTYPE_DUP_AND_FREE);
279 assert_non_null(rpc);
280 check_delete(rpc, NC_DATASTORE_CONFIG, "target");
David Sedlák19af2662018-10-01 13:42:08 +0200281 nc_rpc_free(rpc);
282}
283
284static void
285test_nc_rpc_lock(void **state)
286{
287 (void)state;
288 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200289 struct nc_rpc_lock *lock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200290
291 rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
292 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200293 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_LOCK);
294
295 lock_rpc = (struct nc_rpc_lock *)rpc;
296 assert_int_equal(lock_rpc->type, NC_RPC_LOCK);
297 assert_int_equal(lock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200298
299 nc_rpc_free(rpc);
300}
301
302static void
303test_nc_rpc_unlock(void **state)
304{
305 (void)state;
306 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200307 struct nc_rpc_lock *unlock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200308
309 rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING);
310 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200311 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK);
David Sedlák19af2662018-10-01 13:42:08 +0200312
David Sedlák3983ed02018-10-07 23:17:19 +0200313 unlock_rpc = (struct nc_rpc_lock *)rpc;
314 assert_int_equal(unlock_rpc->type, NC_RPC_UNLOCK);
315 assert_int_equal(unlock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200316 nc_rpc_free(rpc);
317}
318
David Sedlák3983ed02018-10-07 23:17:19 +0200319/* function to check if values of get rpc are set correctly */
320void
321check_get_rpc(struct nc_rpc *rpc, const char *filter, NC_WD_MODE wd_mode)
322{
323 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GET);
324 struct nc_rpc_get *get_rpc = (struct nc_rpc_get *)rpc;
325
326 assert_int_equal(get_rpc->type, NC_RPC_GET);
327 assert_string_equal(get_rpc->filter, filter);
328 assert_int_equal(get_rpc->wd_mode, wd_mode);
329}
330
David Sedlák19af2662018-10-01 13:42:08 +0200331static void
332test_nc_rpc_get(void **state)
333{
334 (void)state;
335 struct nc_rpc *rpc = NULL;
336
David Sedlák3983ed02018-10-07 23:17:19 +0200337 /* create get rpc with NC_PARAMTYPE_CONST */
338 rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200339 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200340 check_get_rpc(rpc, "filter", NC_WD_ALL);
341 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200342
David Sedlák3983ed02018-10-07 23:17:19 +0200343 /* create get rpc with NC_PARAMTYPE_FREE */
344 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200345
David Sedlák3983ed02018-10-07 23:17:19 +0200346 rpc = nc_rpc_get(str, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
347 assert_non_null(rpc);
348 check_get_rpc(rpc, str, NC_WD_EXPLICIT);
349 nc_rpc_free(rpc);
350
351 /* create get rpc with NC_PARAMTYPE_DUP_AND_FREE */
352 rpc = nc_rpc_get("filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_DUP_AND_FREE);
353 assert_non_null(rpc);
354 check_get_rpc(rpc, "filter-string", NC_WD_UNKNOWN);
David Sedlák19af2662018-10-01 13:42:08 +0200355 nc_rpc_free(rpc);
356}
357
358static void
359test_nc_rpc_kill(void **state)
360{
361 (void)state;
362 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200363 struct nc_rpc_kill *kill_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200364
365 rpc = nc_rpc_kill(10);
366 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200367 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_KILL);
368
369 kill_rpc = (struct nc_rpc_kill *)rpc;
370 assert_int_equal(kill_rpc->type, NC_RPC_KILL);
371 assert_int_equal(kill_rpc->sid, 10);
David Sedlák19af2662018-10-01 13:42:08 +0200372
373 nc_rpc_free(rpc);
374}
375
David Sedlák3983ed02018-10-07 23:17:19 +0200376/* function to check if values of commit rpc are set correctly */
377void
378check_commit_rpc(struct nc_rpc *rpc, int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id)
379{
380 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COMMIT);
381 struct nc_rpc_commit *commit_rpc = (struct nc_rpc_commit *)rpc;
382
383 assert_int_equal(commit_rpc->type, NC_RPC_COMMIT);
384 assert_int_equal(commit_rpc->confirmed, confirmed);
385 assert_int_equal(commit_rpc->confirm_timeout, confirm_timeout);
386 assert_string_equal(commit_rpc->persist, persist);
387 assert_string_equal(commit_rpc->persist_id, persist_id);
388}
389
David Sedlák19af2662018-10-01 13:42:08 +0200390static void
391test_nc_rpc_commit(void **state)
392{
393 (void)state;
394 struct nc_rpc *rpc = NULL;
395
David Sedlák3983ed02018-10-07 23:17:19 +0200396 /* create commit rpc with NC_PARAMTYPE_CONST*/
397 rpc = nc_rpc_commit(1, 100, "persist", "persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200398 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200399 check_commit_rpc(rpc, 1, 100, "persist", "persist-id");
400 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200401
David Sedlák3983ed02018-10-07 23:17:19 +0200402 /* create commit rpc with NC_PARAMTYPE_FREE*/
403 char *str1 = strdup("str1");
404 char *str2 = strdup("str2");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200405
David Sedlák3983ed02018-10-07 23:17:19 +0200406 rpc = nc_rpc_commit(2, 5, str1, str2, NC_PARAMTYPE_FREE);
407 assert_non_null(rpc);
408 check_commit_rpc(rpc, 2, 5, str1, str2);
409 nc_rpc_free(rpc);
410
411 /* create commit rpc with NC_PARAMTYPE_DUP_AND_FREE*/
412 rpc = nc_rpc_commit(10, 200, "persistent", "persistent-id", NC_PARAMTYPE_DUP_AND_FREE);
413 assert_non_null(rpc);
414 check_commit_rpc(rpc, 10, 200, "persistent", "persistent-id");
David Sedlák19af2662018-10-01 13:42:08 +0200415 nc_rpc_free(rpc);
416}
417
418static void
419test_nc_rpc_discard(void **state)
420{
421 (void)state;
422 struct nc_rpc *rpc = NULL;
423
424 rpc = nc_rpc_discard();
425 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200426 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD);
David Sedlák19af2662018-10-01 13:42:08 +0200427
428 nc_rpc_free(rpc);
429}
430
David Sedlák3983ed02018-10-07 23:17:19 +0200431/* function to check if values of cancel rpc are set correctly */
432void
433check_cancel_rpc(struct nc_rpc *rpc, const char *persist_id)
434{
435 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_CANCEL);
436 struct nc_rpc_cancel *cancel_rpc = (struct nc_rpc_cancel *)rpc;
437
438 assert_int_equal(cancel_rpc->type, NC_RPC_CANCEL);
439 assert_string_equal(cancel_rpc->persist_id, persist_id);
440}
441
David Sedlák19af2662018-10-01 13:42:08 +0200442static void
443test_nc_rpc_cancel(void **state)
444{
445 (void)state;
446 struct nc_rpc *rpc = NULL;
447
David Sedlák3983ed02018-10-07 23:17:19 +0200448 /* create cancel rpc with NC_PARAMTYPE_CONST*/
449 rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200450 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200451 check_cancel_rpc(rpc, "persist-id");
David Sedlák19af2662018-10-01 13:42:08 +0200452 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200453
454 /* create cancel rpc with NC_PARAMTYPE_FREE*/
455 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200456
David Sedlák3983ed02018-10-07 23:17:19 +0200457 rpc = nc_rpc_cancel(str, NC_PARAMTYPE_FREE);
458 assert_non_null(rpc);
459 check_cancel_rpc(rpc, str);
460 nc_rpc_free(rpc);
461
462 /* create cancel rpc with NC_PARAMTYPE_DUP_AND_FREE*/
463 rpc = nc_rpc_cancel("id", NC_PARAMTYPE_DUP_AND_FREE);
464 assert_non_null(rpc);
465 check_cancel_rpc(rpc, "id");
466 nc_rpc_free(rpc);
467}
468
469/* function to check if values of validate rpc are set correctly */
470void
471check_validate_rpc(struct nc_rpc *rpc, NC_DATASTORE source, const char *url_or_config)
472{
473 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_VALIDATE);
474 struct nc_rpc_validate *validate_rpc = (struct nc_rpc_validate *)rpc;
475
476 assert_int_equal(validate_rpc->type, NC_RPC_VALIDATE);
477 assert_int_equal(validate_rpc->source, source);
478 assert_string_equal(validate_rpc->url_config_src, url_or_config);
David Sedlák19af2662018-10-01 13:42:08 +0200479}
480
481static void
482test_nc_rpc_validate(void **state)
483{
484 (void)state;
485 struct nc_rpc *rpc = NULL;
486
David Sedlák3983ed02018-10-07 23:17:19 +0200487 /* create validate rpc with NC_PARAMTYPE_CONST */
488 rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200489 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200490 check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200491 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200492
493 /* create validate rpc with NC_PARAMTYPE_FREE */
494 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200495
David Sedlák3983ed02018-10-07 23:17:19 +0200496 rpc = nc_rpc_validate(NC_DATASTORE_CANDIDATE, str, NC_PARAMTYPE_FREE);
497 assert_non_null(rpc);
498 check_validate_rpc(rpc, NC_DATASTORE_CANDIDATE, str);
499 nc_rpc_free(rpc);
500
501 /* create validate rpc with NC_PARAMTYPE_DUP_AND_FREE */
502 rpc = nc_rpc_validate(NC_DATASTORE_CONFIG, "url1", NC_PARAMTYPE_DUP_AND_FREE);
503 assert_non_null(rpc);
504 check_validate_rpc(rpc, NC_DATASTORE_CONFIG, "url1");
505 nc_rpc_free(rpc);
506}
507
508/* function to check if values of getschema rpc are set correctly */
509void
510check_getschema_rpc(struct nc_rpc *rpc, const char *identifier, const char *version, const char *format)
511{
512 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETSCHEMA);
513 struct nc_rpc_getschema *getchema_rpc = (struct nc_rpc_getschema *)rpc;
514
515 assert_int_equal(getchema_rpc->type, NC_RPC_GETSCHEMA);
516 assert_string_equal(getchema_rpc->identifier, identifier);
517 assert_string_equal(getchema_rpc->version, version);
518 assert_string_equal(getchema_rpc->format, format);
David Sedlák19af2662018-10-01 13:42:08 +0200519}
520
521static void
522test_nc_rpc_getschema(void **state)
523{
524 (void)state;
525 struct nc_rpc *rpc = NULL;
526
David Sedlák3983ed02018-10-07 23:17:19 +0200527 /* create getchema with NC_PARAMTYPE_CONST*/
528 rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200529 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200530 check_getschema_rpc(rpc, "id", "version", "format");
David Sedlák19af2662018-10-01 13:42:08 +0200531 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200532
533 /* create getchema with NC_PARAMTYPE_FREE*/
534 char *str1 = strdup("str1");
535 char *str2 = strdup("str2");
536 char *str3 = strdup("str3");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200537
David Sedlák3983ed02018-10-07 23:17:19 +0200538 rpc = nc_rpc_getschema(str1, str2, str3, NC_PARAMTYPE_FREE);
539 assert_non_null(rpc);
540 check_getschema_rpc(rpc, str1, str2, str3);
541 nc_rpc_free(rpc);
542
543 /* create getchema with NC_PARAMTYPE_DUP_AND_FREE*/
544 rpc = nc_rpc_getschema("id1", "version1", "format1", NC_PARAMTYPE_DUP_AND_FREE);
545 assert_non_null(rpc);
546 check_getschema_rpc(rpc, "id1", "version1", "format1");
547 nc_rpc_free(rpc);
548}
549
550/* function to check if values of subscribe rpc are set correctly */
551void
552check_subscribe_rpc(struct nc_rpc *rpc, const char *stream_name, const char *filter,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200553 const char *start_time, const char *stop_time)
David Sedlák3983ed02018-10-07 23:17:19 +0200554{
555 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_SUBSCRIBE);
556 struct nc_rpc_subscribe *subscribe_rpc = (struct nc_rpc_subscribe *)rpc;
557
558 assert_int_equal(subscribe_rpc->type, NC_RPC_SUBSCRIBE);
559 assert_string_equal(subscribe_rpc->stream, stream_name);
560 assert_string_equal(subscribe_rpc->filter, filter);
561 assert_string_equal(subscribe_rpc->start, start_time);
562 assert_string_equal(subscribe_rpc->stop, stop_time);
David Sedlák19af2662018-10-01 13:42:08 +0200563}
564
565static void
566test_nc_rpc_subscribe(void **state)
567{
568 (void)state;
569 struct nc_rpc *rpc = NULL;
570
David Sedlák3983ed02018-10-07 23:17:19 +0200571 /* create subscribe rpc with NC_PARAMTYPE_CONST*/
572 rpc = nc_rpc_subscribe("stream-name", "filter", "start-time", "stop-time", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200573 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200574 check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time");
575 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200576
David Sedlák3983ed02018-10-07 23:17:19 +0200577 /* create subscribe rpc with NC_PARAMTYPE_FREE*/
578 char *str1 = strdup("str1");
579 char *str2 = strdup("str2");
580 char *str3 = strdup("str3");
581 char *str4 = strdup("str4");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200582
David Sedlák3983ed02018-10-07 23:17:19 +0200583 rpc = nc_rpc_subscribe(str1, str2, str3, str4, NC_PARAMTYPE_FREE);
584 assert_non_null(rpc);
585 check_subscribe_rpc(rpc, str1, str2, str3, str4);
586 nc_rpc_free(rpc);
587
588 /* create subscribe rpc with NC_PARAMTYPE_DUP_AND_FREE*/
589 rpc = nc_rpc_subscribe("name", "filter-str", "start", "stop", NC_PARAMTYPE_DUP_AND_FREE);
590 assert_non_null(rpc);
591 check_subscribe_rpc(rpc, "name", "filter-str", "start", "stop");
David Sedlák19af2662018-10-01 13:42:08 +0200592 nc_rpc_free(rpc);
593}
594
Fred Ganec584842021-01-13 15:16:22 +0800595/* function to check if values of getdata rpc are set correctly */
596void
597check_getdata(struct nc_rpc *rpc, char *datastore, const char *filter, const char *config_filter,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200598 char **origin_filter, int origin_filter_count, int negated_origin_filter, uint16_t max_depth,
599 int with_origin, NC_WD_MODE wd_mode)
Fred Ganec584842021-01-13 15:16:22 +0800600{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200601 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETDATA);
602 struct nc_rpc_getdata *rpc_getdata = (struct nc_rpc_getdata *)rpc;
Fred Ganec584842021-01-13 15:16:22 +0800603
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200604 assert_int_equal(rpc_getdata->type, NC_RPC_GETDATA);
605 assert_string_equal(rpc_getdata->datastore, datastore);
606 assert_string_equal(rpc_getdata->filter, filter);
607 assert_string_equal(rpc_getdata->config_filter, config_filter);
608 assert_string_equal(*rpc_getdata->origin_filter, *origin_filter);
609 assert_int_equal(rpc_getdata->origin_filter_count, origin_filter_count);
610 assert_int_equal(rpc_getdata->negated_origin_filter, negated_origin_filter);
611 assert_int_equal(rpc_getdata->max_depth, max_depth);
612 assert_int_equal(rpc_getdata->with_origin, with_origin);
613 assert_int_equal(rpc_getdata->wd_mode, wd_mode);
Fred Ganec584842021-01-13 15:16:22 +0800614}
615
616static void
617test_nc_rpc_getdata(void **state)
618{
619 (void)state;
620 struct nc_rpc *rpc = NULL;
621
622 /* create getdata rpc with NC_PARAMTYPE_CONST */
623 char *origin_filters = "origin_filter";
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200624
Fred Ganec584842021-01-13 15:16:22 +0800625 rpc = nc_rpc_getdata("candidate", "filter", "true", &origin_filters, 1, 1, 3, 1, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);
626 assert_non_null(rpc);
627 check_getdata(rpc, "candidate", "filter", "true", &origin_filters, 1, 1, 3, 1, NC_WD_UNKNOWN);
628 nc_rpc_free(rpc);
629
630 /* create getdata rpc with NC_PARAMTYPE_FREE */
631 char *datastore = strdup("running");
632 char *filter = strdup("filter");
633 char *config_filter = strdup("true");
634 char buf[20] = {0};
635 char **origin_filter;
636 int origin_filter_count = 2;
637
638 origin_filter = calloc(origin_filter_count, sizeof *origin_filter);
639 assert_non_null(origin_filter);
640 for (int i = 0; i < origin_filter_count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200641 snprintf(buf, sizeof(buf) - 1, "origin_filter%d", i + 1);
642 origin_filter[i] = strdup(buf);
Fred Ganec584842021-01-13 15:16:22 +0800643 }
644
645 rpc = nc_rpc_getdata(datastore, filter, config_filter, origin_filter, origin_filter_count, 2, 3, 1, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
646 assert_non_null(rpc);
647 check_getdata(rpc, datastore, filter, config_filter, origin_filter, origin_filter_count, 2, 3, 1, NC_WD_EXPLICIT);
648 nc_rpc_free(rpc);
649
650 /* create getdata rpc with NC_PARAMTYPE_DUP_AND_FREE */
651 char *origin_filter1 = "origin_filter1";
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200652
Fred Ganec584842021-01-13 15:16:22 +0800653 rpc = nc_rpc_getdata("startup", "filter1", "false", &origin_filter1, 1, 0, 3, 1, NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE);
654 assert_non_null(rpc);
655 check_getdata(rpc, "startup", "filter1", "false", &origin_filter1, 1, 0, 3, 1, NC_WD_ALL);
656 nc_rpc_free(rpc);
657}
658
659/* function to check if values of editdata rpc are set correctly */
660void
661check_editdata(struct nc_rpc *rpc, char *datastore, NC_RPC_EDIT_DFLTOP default_op, const char *edit_content)
662{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200663 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDITDATA);
664 struct nc_rpc_editdata *rpc_editdata = (struct nc_rpc_editdata *)rpc;
Fred Ganec584842021-01-13 15:16:22 +0800665
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200666 assert_int_equal(rpc_editdata->type, NC_RPC_EDITDATA);
667 assert_string_equal(rpc_editdata->datastore, datastore);
668 assert_int_equal(rpc_editdata->default_op, default_op);
669 assert_string_equal(rpc_editdata->edit_cont, edit_content);
Fred Ganec584842021-01-13 15:16:22 +0800670}
671
672static void
673test_nc_rpc_editdata(void **state)
674{
675 (void)state;
676 struct nc_rpc *rpc = NULL;
677
678 /* create editdata rpc with NC_PARAMTYPE_CONST */
679 rpc = nc_rpc_editdata("candidate", NC_RPC_EDIT_DFLTOP_UNKNOWN, "edit", NC_PARAMTYPE_CONST);
680 assert_non_null(rpc);
681 check_editdata(rpc, "candidate", NC_RPC_EDIT_DFLTOP_UNKNOWN, "edit");
682 nc_rpc_free(rpc);
683
684 /* create editdata rpc with NC_PARAMTYPE_FREE */
685 char *datastore = strdup("running");
686 char *edit_cont = strdup("edit_data");
687
688 rpc = nc_rpc_editdata(datastore, NC_RPC_EDIT_DFLTOP_MERGE, edit_cont, NC_PARAMTYPE_FREE);
689 assert_non_null(rpc);
690 check_editdata(rpc, datastore, NC_RPC_EDIT_DFLTOP_MERGE, edit_cont);
691 nc_rpc_free(rpc);
692
693 /* create editdata rpc with NC_PARAMTYPE_DUP_AND_FREE */
694 rpc = nc_rpc_editdata("startup", NC_RPC_EDIT_DFLTOP_REPLACE, "edit_cont", NC_PARAMTYPE_DUP_AND_FREE);
695 assert_non_null(rpc);
696 check_editdata(rpc, "startup", NC_RPC_EDIT_DFLTOP_REPLACE, "edit_cont");
697 nc_rpc_free(rpc);
698}
699
David Sedlák19af2662018-10-01 13:42:08 +0200700int
701main(void)
702{
703 const struct CMUnitTest tests[] = {
David Sedlák2f2f2e32018-10-08 21:49:32 +0200704 cmocka_unit_test_setup_teardown(test_nc_rpc_act_generic_xml, setup_f, teardown_f),
705 cmocka_unit_test_setup_teardown(test_nc_rpc_act_generic, setup_f, teardown_f),
David Sedlák19af2662018-10-01 13:42:08 +0200706 cmocka_unit_test_setup_teardown(test_nc_rpc_getconfig, setup_f, teardown_f),
707 cmocka_unit_test_setup_teardown(test_nc_rpc_edit, setup_f, teardown_f),
708 cmocka_unit_test_setup_teardown(test_nc_rpc_copy, setup_f, teardown_f),
709 cmocka_unit_test_setup_teardown(test_nc_rpc_delete, setup_f, teardown_f),
710 cmocka_unit_test_setup_teardown(test_nc_rpc_lock, setup_f, teardown_f),
711 cmocka_unit_test_setup_teardown(test_nc_rpc_unlock, setup_f, teardown_f),
712 cmocka_unit_test_setup_teardown(test_nc_rpc_get, setup_f, teardown_f),
713 cmocka_unit_test_setup_teardown(test_nc_rpc_kill, setup_f, teardown_f),
714 cmocka_unit_test_setup_teardown(test_nc_rpc_commit, setup_f, teardown_f),
715 cmocka_unit_test_setup_teardown(test_nc_rpc_discard, setup_f, teardown_f),
716 cmocka_unit_test_setup_teardown(test_nc_rpc_cancel, setup_f, teardown_f),
717 cmocka_unit_test_setup_teardown(test_nc_rpc_validate, setup_f, teardown_f),
718 cmocka_unit_test_setup_teardown(test_nc_rpc_getschema, setup_f, teardown_f),
719 cmocka_unit_test_setup_teardown(test_nc_rpc_subscribe, setup_f, teardown_f),
Fred Ganec584842021-01-13 15:16:22 +0800720 cmocka_unit_test_setup_teardown(test_nc_rpc_getdata, setup_f, teardown_f),
721 cmocka_unit_test_setup_teardown(test_nc_rpc_editdata, setup_f, teardown_f),
David Sedlák19af2662018-10-01 13:42:08 +0200722 };
723
724 return cmocka_run_group_tests(tests, NULL, NULL);
725}