blob: 51a244fc470582d0d1696c58deeef7178cb5bb4e [file] [log] [blame]
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001#include <errno.h>
2#include <setjmp.h>
David Sedlák19af2662018-10-01 13:42:08 +02003#include <stdio.h>
4#include <stdlib.h>
David Sedlák3983ed02018-10-07 23:17:19 +02005#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +02006#include <sys/socket.h>
7#include <sys/types.h>
David Sedlák19af2662018-10-01 13:42:08 +02008
9#include <cmocka.h>
David Sedlák19af2662018-10-01 13:42:08 +020010#include <config.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020011#include <libyang/libyang.h>
12#include <log.h>
David Sedlák19af2662018-10-01 13:42:08 +020013#include <messages_p.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020014#include <session_client.h>
David Sedlák19af2662018-10-01 13:42:08 +020015#include "tests/config.h"
16
17static int
18setup_f(void **state)
19{
20 (void)state;
21
22 nc_verbosity(NC_VERB_VERBOSE);
23
24 return 0;
25}
26
27static int
28teardown_f(void **state)
29{
30 (void)state;
31
32 return 0;
33}
34
David Sedlák2f2f2e32018-10-08 21:49:32 +020035static void
36test_nc_rpc_act_generic_xml(void **state)
37{
38 (void)state;
39 struct nc_rpc *rpc = NULL;
40 struct nc_rpc_act_generic *generic_rpc = NULL;
41
42 /* create generic rpc with NC_PARAMTYPE_CONST */
43 rpc = nc_rpc_act_generic_xml("xml", NC_PARAMTYPE_CONST);
44 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
45 generic_rpc = (struct nc_rpc_act_generic *)rpc;
46 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
47 assert_int_equal(generic_rpc->has_data, 0);
48 assert_string_equal(generic_rpc->content.xml_str, "xml");
49 nc_rpc_free(rpc);
50
51 /* create generic rpc with NC_PARAMTYPE_FREE */
52 char *str = strdup("str");
Michal Vaskob83a3fa2021-05-26 09:53:42 +020053
David Sedlák2f2f2e32018-10-08 21:49:32 +020054 rpc = nc_rpc_act_generic_xml(str, NC_PARAMTYPE_FREE);
55 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
56 generic_rpc = (struct nc_rpc_act_generic *)rpc;
57 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
58 assert_int_equal(generic_rpc->has_data, 0);
59 assert_string_equal(generic_rpc->content.xml_str, str);
60 nc_rpc_free(rpc);
61
62 /* create generic rpc with NC_PARAMTYPE_DUP_AND_FREE */
63 rpc = nc_rpc_act_generic_xml("xml", NC_PARAMTYPE_DUP_AND_FREE);
64 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
65 generic_rpc = (struct nc_rpc_act_generic *)rpc;
66 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
67 assert_int_equal(generic_rpc->has_data, 0);
68 assert_string_equal(generic_rpc->content.xml_str, "xml");
69 nc_rpc_free(rpc);
70}
71
72static void
73test_nc_rpc_act_generic(void **state)
74{
75 (void)state;
76 struct nc_rpc *rpc = NULL;
77 struct nc_rpc_act_generic *generic_rpc = NULL;
78 struct lyd_node node;
Michal Vaskob83a3fa2021-05-26 09:53:42 +020079
David Sedlák2f2f2e32018-10-08 21:49:32 +020080 node.next = NULL;
81 node.prev = &node;
82
83 rpc = nc_rpc_act_generic(&node, NC_PARAMTYPE_CONST);
84 assert_non_null(rpc);
85 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC);
86 generic_rpc = (struct nc_rpc_act_generic *)rpc;
87 assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC);
88 assert_int_equal(generic_rpc->has_data, 1);
89 assert_ptr_equal(generic_rpc->content.data, &node);
90 nc_rpc_free(rpc);
91}
92
David Sedlák3983ed02018-10-07 23:17:19 +020093/* function to check if values of getconfig rpc are set correctly */
94void
95check_getconfig(struct nc_rpc *rpc, enum NC_DATASTORE_TYPE source, char *filter, NC_WD_MODE wd_mode)
96{
97 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETCONFIG);
98 struct nc_rpc_getconfig *getconfig_rpc = (struct nc_rpc_getconfig *)rpc;
99
100 assert_int_equal(getconfig_rpc->type, NC_RPC_GETCONFIG);
101 assert_int_equal(getconfig_rpc->source, source);
102 assert_string_equal(getconfig_rpc->filter, filter);
103 assert_int_equal(getconfig_rpc->wd_mode, wd_mode);
104}
105
David Sedlák19af2662018-10-01 13:42:08 +0200106static void
107test_nc_rpc_getconfig(void **state)
108{
109 (void)state;
110 struct nc_rpc *rpc = NULL;
111
David Sedlák3983ed02018-10-07 23:17:19 +0200112 /* create getconfig rpc with NC_PARAMTYPE_CONST */
113 rpc = nc_rpc_getconfig(NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200114 assert_non_null(rpc);
Michal Vasko79142c22020-07-29 15:26:51 +0200115 check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN);
David Sedlák19af2662018-10-01 13:42:08 +0200116 nc_rpc_free(rpc);
117
David Sedlák3983ed02018-10-07 23:17:19 +0200118 /* create getconfig rpc with NC_PARAMTYPE_FREE */
119 char *filter = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200120
David Sedlák3983ed02018-10-07 23:17:19 +0200121 rpc = nc_rpc_getconfig(NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
122 assert_non_null(rpc);
123 check_getconfig(rpc, NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT);
124 nc_rpc_free(rpc);
125
126 /* create getconfig rpc with NC_PARAMTYPE_DUP_AND_FREE */
127 rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, "filter", NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE);
128 assert_non_null(rpc);
129 check_getconfig(rpc, NC_DATASTORE_RUNNING, "filter", NC_WD_ALL);
130 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200131}
132
David Sedlák3983ed02018-10-07 23:17:19 +0200133/* function to check if values of edit rpc are set correctly */
134void
135check_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 +0200136 NC_RPC_EDIT_ERROPT error_opt, const char *edit_content)
David Sedlák3983ed02018-10-07 23:17:19 +0200137{
138 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDIT);
139 struct nc_rpc_edit *edit_rpc = (struct nc_rpc_edit *)rpc;
140
141 assert_int_equal(edit_rpc->type, NC_RPC_EDIT);
142 assert_int_equal(edit_rpc->target, target);
143 assert_int_equal(edit_rpc->default_op, default_op);
144 assert_int_equal(edit_rpc->test_opt, test_opt);
145 assert_int_equal(edit_rpc->error_opt, error_opt);
146 assert_string_equal(edit_rpc->edit_cont, edit_content);
147}
148
David Sedlák19af2662018-10-01 13:42:08 +0200149static void
150test_nc_rpc_edit(void **state)
151{
152 (void)state;
153 struct nc_rpc *rpc = NULL;
154
David Sedlák3983ed02018-10-07 23:17:19 +0200155 /* create edit rpc with NC_PARAMTYPE_CONST */
156 rpc = nc_rpc_edit(NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, NC_RPC_EDIT_TESTOPT_TESTSET,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200157 NC_RPC_EDIT_ERROPT_STOP, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200158 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200159 check_edit(rpc, NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200160 NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200161 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200162
163 /* create edit rpc with NC_PARAMTYPE_FREE */
164 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200165
David Sedlák3983ed02018-10-07 23:17:19 +0200166 rpc = nc_rpc_edit(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_SET,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200167 NC_RPC_EDIT_ERROPT_ROLLBACK, str, NC_PARAMTYPE_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200168 assert_non_null(rpc);
169 check_edit(rpc, NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200170 NC_RPC_EDIT_TESTOPT_SET, NC_RPC_EDIT_ERROPT_ROLLBACK, str);
David Sedlák3983ed02018-10-07 23:17:19 +0200171 nc_rpc_free(rpc);
172
173 /* create edit rpc with NC_PARAMTYPE_DUP_AND_FREE */
174 rpc = nc_rpc_edit(NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, NC_RPC_EDIT_TESTOPT_TEST,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200175 NC_RPC_EDIT_ERROPT_CONTINUE, "url1", NC_PARAMTYPE_DUP_AND_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200176 assert_non_null(rpc);
177 check_edit(rpc, NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200178 NC_RPC_EDIT_TESTOPT_TEST, NC_RPC_EDIT_ERROPT_CONTINUE, "url1");
David Sedlák3983ed02018-10-07 23:17:19 +0200179 nc_rpc_free(rpc);
180}
181
182/* function to check if values of copy rpc are set correctly */
183void
184check_copy(struct nc_rpc *rpc, NC_DATASTORE target, const char *url_trg, NC_DATASTORE source,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200185 const char *url_or_config_src, NC_WD_MODE wd_mode)
David Sedlák3983ed02018-10-07 23:17:19 +0200186{
187 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COPY);
188 struct nc_rpc_copy *copy_rpc = (struct nc_rpc_copy *)rpc;
189
190 assert_int_equal(copy_rpc->type, NC_RPC_COPY);
191 assert_int_equal(copy_rpc->target, target);
192 assert_string_equal(copy_rpc->url_trg, url_trg);
193 assert_int_equal(copy_rpc->source, source);
194 assert_string_equal(copy_rpc->url_config_src, url_or_config_src);
195 assert_int_equal(copy_rpc->wd_mode, wd_mode);
David Sedlák19af2662018-10-01 13:42:08 +0200196}
197
198static void
199test_nc_rpc_copy(void **state)
200{
201 (void)state;
202 struct nc_rpc *rpc = NULL;
203
David Sedlák3983ed02018-10-07 23:17:19 +0200204 /* create copy rpc with NC_PARAMTYPE_CONST */
David Sedlák19af2662018-10-01 13:42:08 +0200205 rpc = nc_rpc_copy(NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200206 NC_WD_ALL, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200207 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200208 check_copy(rpc, NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", NC_WD_ALL);
David Sedlák19af2662018-10-01 13:42:08 +0200209 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200210
211 /* create copy rpc with NC_PARAMTYPE_FREE */
212 char *target = strdup("target");
213 char *src = strdup("src");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200214
David Sedlák3983ed02018-10-07 23:17:19 +0200215 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200216 NC_WD_ALL_TAG, NC_PARAMTYPE_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200217 assert_non_null(rpc);
218 check_copy(rpc, NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, NC_WD_ALL_TAG);
219 nc_rpc_free(rpc);
220
221 /* create copy rpc with NC_PARAMTYPE_DUP_AND_FREE */
222 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200223 NC_WD_TRIM, NC_PARAMTYPE_DUP_AND_FREE);
David Sedlák3983ed02018-10-07 23:17:19 +0200224 assert_non_null(rpc);
225 check_copy(rpc, NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url", NC_WD_TRIM);
226 nc_rpc_free(rpc);
227}
228
229/* function to check if values of delete rpc are set correctly */
230void
231check_delete(struct nc_rpc *rpc, NC_DATASTORE target, const char *url)
232{
233 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DELETE);
234 struct nc_rpc_delete *delete_rpc = (struct nc_rpc_delete *)rpc;
235
236 assert_int_equal(delete_rpc->type, NC_RPC_DELETE);
237 assert_int_equal(delete_rpc->target, target);
238 assert_string_equal(delete_rpc->url, url);
David Sedlák19af2662018-10-01 13:42:08 +0200239}
240
241static void
242test_nc_rpc_delete(void **state)
243{
244 (void)state;
245 struct nc_rpc *rpc = NULL;
246
David Sedlák3983ed02018-10-07 23:17:19 +0200247 /* create delete rpc with NC_PARAMTYPE_CONST */
248 rpc = nc_rpc_delete(NC_DATASTORE_RUNNING, "target-url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200249 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200250 check_delete(rpc, NC_DATASTORE_RUNNING, "target-url");
251 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200252
David Sedlák3983ed02018-10-07 23:17:19 +0200253 /* create delete rpc with NC_PARAMTYPE_FREE */
254 char *url = strdup("url");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200255
David Sedlák3983ed02018-10-07 23:17:19 +0200256 rpc = nc_rpc_delete(NC_DATASTORE_CANDIDATE, url, NC_PARAMTYPE_FREE);
257 assert_non_null(rpc);
258 check_delete(rpc, NC_DATASTORE_CANDIDATE, url);
259 nc_rpc_free(rpc);
260
261 /* create delete rpc with NC_PARAMTYPE_DUP_AND_FREE */
262 rpc = nc_rpc_delete(NC_DATASTORE_CONFIG, "target", NC_PARAMTYPE_DUP_AND_FREE);
263 assert_non_null(rpc);
264 check_delete(rpc, NC_DATASTORE_CONFIG, "target");
David Sedlák19af2662018-10-01 13:42:08 +0200265 nc_rpc_free(rpc);
266}
267
268static void
269test_nc_rpc_lock(void **state)
270{
271 (void)state;
272 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200273 struct nc_rpc_lock *lock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200274
275 rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
276 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200277 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_LOCK);
278
279 lock_rpc = (struct nc_rpc_lock *)rpc;
280 assert_int_equal(lock_rpc->type, NC_RPC_LOCK);
281 assert_int_equal(lock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200282
283 nc_rpc_free(rpc);
284}
285
286static void
287test_nc_rpc_unlock(void **state)
288{
289 (void)state;
290 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200291 struct nc_rpc_lock *unlock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200292
293 rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING);
294 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200295 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK);
David Sedlák19af2662018-10-01 13:42:08 +0200296
David Sedlák3983ed02018-10-07 23:17:19 +0200297 unlock_rpc = (struct nc_rpc_lock *)rpc;
298 assert_int_equal(unlock_rpc->type, NC_RPC_UNLOCK);
299 assert_int_equal(unlock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200300 nc_rpc_free(rpc);
301}
302
David Sedlák3983ed02018-10-07 23:17:19 +0200303/* function to check if values of get rpc are set correctly */
304void
305check_get_rpc(struct nc_rpc *rpc, const char *filter, NC_WD_MODE wd_mode)
306{
307 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GET);
308 struct nc_rpc_get *get_rpc = (struct nc_rpc_get *)rpc;
309
310 assert_int_equal(get_rpc->type, NC_RPC_GET);
311 assert_string_equal(get_rpc->filter, filter);
312 assert_int_equal(get_rpc->wd_mode, wd_mode);
313}
314
David Sedlák19af2662018-10-01 13:42:08 +0200315static void
316test_nc_rpc_get(void **state)
317{
318 (void)state;
319 struct nc_rpc *rpc = NULL;
320
David Sedlák3983ed02018-10-07 23:17:19 +0200321 /* create get rpc with NC_PARAMTYPE_CONST */
322 rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200323 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200324 check_get_rpc(rpc, "filter", NC_WD_ALL);
325 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200326
David Sedlák3983ed02018-10-07 23:17:19 +0200327 /* create get rpc with NC_PARAMTYPE_FREE */
328 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200329
David Sedlák3983ed02018-10-07 23:17:19 +0200330 rpc = nc_rpc_get(str, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
331 assert_non_null(rpc);
332 check_get_rpc(rpc, str, NC_WD_EXPLICIT);
333 nc_rpc_free(rpc);
334
335 /* create get rpc with NC_PARAMTYPE_DUP_AND_FREE */
336 rpc = nc_rpc_get("filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_DUP_AND_FREE);
337 assert_non_null(rpc);
338 check_get_rpc(rpc, "filter-string", NC_WD_UNKNOWN);
David Sedlák19af2662018-10-01 13:42:08 +0200339 nc_rpc_free(rpc);
340}
341
342static void
343test_nc_rpc_kill(void **state)
344{
345 (void)state;
346 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200347 struct nc_rpc_kill *kill_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200348
349 rpc = nc_rpc_kill(10);
350 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200351 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_KILL);
352
353 kill_rpc = (struct nc_rpc_kill *)rpc;
354 assert_int_equal(kill_rpc->type, NC_RPC_KILL);
355 assert_int_equal(kill_rpc->sid, 10);
David Sedlák19af2662018-10-01 13:42:08 +0200356
357 nc_rpc_free(rpc);
358}
359
David Sedlák3983ed02018-10-07 23:17:19 +0200360/* function to check if values of commit rpc are set correctly */
361void
362check_commit_rpc(struct nc_rpc *rpc, int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id)
363{
364 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COMMIT);
365 struct nc_rpc_commit *commit_rpc = (struct nc_rpc_commit *)rpc;
366
367 assert_int_equal(commit_rpc->type, NC_RPC_COMMIT);
368 assert_int_equal(commit_rpc->confirmed, confirmed);
369 assert_int_equal(commit_rpc->confirm_timeout, confirm_timeout);
370 assert_string_equal(commit_rpc->persist, persist);
371 assert_string_equal(commit_rpc->persist_id, persist_id);
372}
373
David Sedlák19af2662018-10-01 13:42:08 +0200374static void
375test_nc_rpc_commit(void **state)
376{
377 (void)state;
378 struct nc_rpc *rpc = NULL;
379
David Sedlák3983ed02018-10-07 23:17:19 +0200380 /* create commit rpc with NC_PARAMTYPE_CONST*/
381 rpc = nc_rpc_commit(1, 100, "persist", "persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200382 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200383 check_commit_rpc(rpc, 1, 100, "persist", "persist-id");
384 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200385
David Sedlák3983ed02018-10-07 23:17:19 +0200386 /* create commit rpc with NC_PARAMTYPE_FREE*/
387 char *str1 = strdup("str1");
388 char *str2 = strdup("str2");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200389
David Sedlák3983ed02018-10-07 23:17:19 +0200390 rpc = nc_rpc_commit(2, 5, str1, str2, NC_PARAMTYPE_FREE);
391 assert_non_null(rpc);
392 check_commit_rpc(rpc, 2, 5, str1, str2);
393 nc_rpc_free(rpc);
394
395 /* create commit rpc with NC_PARAMTYPE_DUP_AND_FREE*/
396 rpc = nc_rpc_commit(10, 200, "persistent", "persistent-id", NC_PARAMTYPE_DUP_AND_FREE);
397 assert_non_null(rpc);
398 check_commit_rpc(rpc, 10, 200, "persistent", "persistent-id");
David Sedlák19af2662018-10-01 13:42:08 +0200399 nc_rpc_free(rpc);
400}
401
402static void
403test_nc_rpc_discard(void **state)
404{
405 (void)state;
406 struct nc_rpc *rpc = NULL;
407
408 rpc = nc_rpc_discard();
409 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200410 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD);
David Sedlák19af2662018-10-01 13:42:08 +0200411
412 nc_rpc_free(rpc);
413}
414
David Sedlák3983ed02018-10-07 23:17:19 +0200415/* function to check if values of cancel rpc are set correctly */
416void
417check_cancel_rpc(struct nc_rpc *rpc, const char *persist_id)
418{
419 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_CANCEL);
420 struct nc_rpc_cancel *cancel_rpc = (struct nc_rpc_cancel *)rpc;
421
422 assert_int_equal(cancel_rpc->type, NC_RPC_CANCEL);
423 assert_string_equal(cancel_rpc->persist_id, persist_id);
424}
425
David Sedlák19af2662018-10-01 13:42:08 +0200426static void
427test_nc_rpc_cancel(void **state)
428{
429 (void)state;
430 struct nc_rpc *rpc = NULL;
431
David Sedlák3983ed02018-10-07 23:17:19 +0200432 /* create cancel rpc with NC_PARAMTYPE_CONST*/
433 rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200434 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200435 check_cancel_rpc(rpc, "persist-id");
David Sedlák19af2662018-10-01 13:42:08 +0200436 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200437
438 /* create cancel rpc with NC_PARAMTYPE_FREE*/
439 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200440
David Sedlák3983ed02018-10-07 23:17:19 +0200441 rpc = nc_rpc_cancel(str, NC_PARAMTYPE_FREE);
442 assert_non_null(rpc);
443 check_cancel_rpc(rpc, str);
444 nc_rpc_free(rpc);
445
446 /* create cancel rpc with NC_PARAMTYPE_DUP_AND_FREE*/
447 rpc = nc_rpc_cancel("id", NC_PARAMTYPE_DUP_AND_FREE);
448 assert_non_null(rpc);
449 check_cancel_rpc(rpc, "id");
450 nc_rpc_free(rpc);
451}
452
453/* function to check if values of validate rpc are set correctly */
454void
455check_validate_rpc(struct nc_rpc *rpc, NC_DATASTORE source, const char *url_or_config)
456{
457 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_VALIDATE);
458 struct nc_rpc_validate *validate_rpc = (struct nc_rpc_validate *)rpc;
459
460 assert_int_equal(validate_rpc->type, NC_RPC_VALIDATE);
461 assert_int_equal(validate_rpc->source, source);
462 assert_string_equal(validate_rpc->url_config_src, url_or_config);
David Sedlák19af2662018-10-01 13:42:08 +0200463}
464
465static void
466test_nc_rpc_validate(void **state)
467{
468 (void)state;
469 struct nc_rpc *rpc = NULL;
470
David Sedlák3983ed02018-10-07 23:17:19 +0200471 /* create validate rpc with NC_PARAMTYPE_CONST */
472 rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200473 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200474 check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200475 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200476
477 /* create validate rpc with NC_PARAMTYPE_FREE */
478 char *str = strdup("string");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200479
David Sedlák3983ed02018-10-07 23:17:19 +0200480 rpc = nc_rpc_validate(NC_DATASTORE_CANDIDATE, str, NC_PARAMTYPE_FREE);
481 assert_non_null(rpc);
482 check_validate_rpc(rpc, NC_DATASTORE_CANDIDATE, str);
483 nc_rpc_free(rpc);
484
485 /* create validate rpc with NC_PARAMTYPE_DUP_AND_FREE */
486 rpc = nc_rpc_validate(NC_DATASTORE_CONFIG, "url1", NC_PARAMTYPE_DUP_AND_FREE);
487 assert_non_null(rpc);
488 check_validate_rpc(rpc, NC_DATASTORE_CONFIG, "url1");
489 nc_rpc_free(rpc);
490}
491
492/* function to check if values of getschema rpc are set correctly */
493void
494check_getschema_rpc(struct nc_rpc *rpc, const char *identifier, const char *version, const char *format)
495{
496 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETSCHEMA);
497 struct nc_rpc_getschema *getchema_rpc = (struct nc_rpc_getschema *)rpc;
498
499 assert_int_equal(getchema_rpc->type, NC_RPC_GETSCHEMA);
500 assert_string_equal(getchema_rpc->identifier, identifier);
501 assert_string_equal(getchema_rpc->version, version);
502 assert_string_equal(getchema_rpc->format, format);
David Sedlák19af2662018-10-01 13:42:08 +0200503}
504
505static void
506test_nc_rpc_getschema(void **state)
507{
508 (void)state;
509 struct nc_rpc *rpc = NULL;
510
David Sedlák3983ed02018-10-07 23:17:19 +0200511 /* create getchema with NC_PARAMTYPE_CONST*/
512 rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200513 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200514 check_getschema_rpc(rpc, "id", "version", "format");
David Sedlák19af2662018-10-01 13:42:08 +0200515 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200516
517 /* create getchema with NC_PARAMTYPE_FREE*/
518 char *str1 = strdup("str1");
519 char *str2 = strdup("str2");
520 char *str3 = strdup("str3");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200521
David Sedlák3983ed02018-10-07 23:17:19 +0200522 rpc = nc_rpc_getschema(str1, str2, str3, NC_PARAMTYPE_FREE);
523 assert_non_null(rpc);
524 check_getschema_rpc(rpc, str1, str2, str3);
525 nc_rpc_free(rpc);
526
527 /* create getchema with NC_PARAMTYPE_DUP_AND_FREE*/
528 rpc = nc_rpc_getschema("id1", "version1", "format1", NC_PARAMTYPE_DUP_AND_FREE);
529 assert_non_null(rpc);
530 check_getschema_rpc(rpc, "id1", "version1", "format1");
531 nc_rpc_free(rpc);
532}
533
534/* function to check if values of subscribe rpc are set correctly */
535void
536check_subscribe_rpc(struct nc_rpc *rpc, const char *stream_name, const char *filter,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200537 const char *start_time, const char *stop_time)
David Sedlák3983ed02018-10-07 23:17:19 +0200538{
539 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_SUBSCRIBE);
540 struct nc_rpc_subscribe *subscribe_rpc = (struct nc_rpc_subscribe *)rpc;
541
542 assert_int_equal(subscribe_rpc->type, NC_RPC_SUBSCRIBE);
543 assert_string_equal(subscribe_rpc->stream, stream_name);
544 assert_string_equal(subscribe_rpc->filter, filter);
545 assert_string_equal(subscribe_rpc->start, start_time);
546 assert_string_equal(subscribe_rpc->stop, stop_time);
David Sedlák19af2662018-10-01 13:42:08 +0200547}
548
549static void
550test_nc_rpc_subscribe(void **state)
551{
552 (void)state;
553 struct nc_rpc *rpc = NULL;
554
David Sedlák3983ed02018-10-07 23:17:19 +0200555 /* create subscribe rpc with NC_PARAMTYPE_CONST*/
556 rpc = nc_rpc_subscribe("stream-name", "filter", "start-time", "stop-time", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200557 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200558 check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time");
559 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200560
David Sedlák3983ed02018-10-07 23:17:19 +0200561 /* create subscribe rpc with NC_PARAMTYPE_FREE*/
562 char *str1 = strdup("str1");
563 char *str2 = strdup("str2");
564 char *str3 = strdup("str3");
565 char *str4 = strdup("str4");
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200566
David Sedlák3983ed02018-10-07 23:17:19 +0200567 rpc = nc_rpc_subscribe(str1, str2, str3, str4, NC_PARAMTYPE_FREE);
568 assert_non_null(rpc);
569 check_subscribe_rpc(rpc, str1, str2, str3, str4);
570 nc_rpc_free(rpc);
571
572 /* create subscribe rpc with NC_PARAMTYPE_DUP_AND_FREE*/
573 rpc = nc_rpc_subscribe("name", "filter-str", "start", "stop", NC_PARAMTYPE_DUP_AND_FREE);
574 assert_non_null(rpc);
575 check_subscribe_rpc(rpc, "name", "filter-str", "start", "stop");
David Sedlák19af2662018-10-01 13:42:08 +0200576 nc_rpc_free(rpc);
577}
578
Fred Ganec584842021-01-13 15:16:22 +0800579/* function to check if values of getdata rpc are set correctly */
580void
581check_getdata(struct nc_rpc *rpc, char *datastore, const char *filter, const char *config_filter,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200582 char **origin_filter, int origin_filter_count, int negated_origin_filter, uint16_t max_depth,
583 int with_origin, NC_WD_MODE wd_mode)
Fred Ganec584842021-01-13 15:16:22 +0800584{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200585 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETDATA);
586 struct nc_rpc_getdata *rpc_getdata = (struct nc_rpc_getdata *)rpc;
Fred Ganec584842021-01-13 15:16:22 +0800587
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200588 assert_int_equal(rpc_getdata->type, NC_RPC_GETDATA);
589 assert_string_equal(rpc_getdata->datastore, datastore);
590 assert_string_equal(rpc_getdata->filter, filter);
591 assert_string_equal(rpc_getdata->config_filter, config_filter);
592 assert_string_equal(*rpc_getdata->origin_filter, *origin_filter);
593 assert_int_equal(rpc_getdata->origin_filter_count, origin_filter_count);
594 assert_int_equal(rpc_getdata->negated_origin_filter, negated_origin_filter);
595 assert_int_equal(rpc_getdata->max_depth, max_depth);
596 assert_int_equal(rpc_getdata->with_origin, with_origin);
597 assert_int_equal(rpc_getdata->wd_mode, wd_mode);
Fred Ganec584842021-01-13 15:16:22 +0800598}
599
600static void
601test_nc_rpc_getdata(void **state)
602{
603 (void)state;
604 struct nc_rpc *rpc = NULL;
605
606 /* create getdata rpc with NC_PARAMTYPE_CONST */
607 char *origin_filters = "origin_filter";
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200608
Fred Ganec584842021-01-13 15:16:22 +0800609 rpc = nc_rpc_getdata("candidate", "filter", "true", &origin_filters, 1, 1, 3, 1, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);
610 assert_non_null(rpc);
611 check_getdata(rpc, "candidate", "filter", "true", &origin_filters, 1, 1, 3, 1, NC_WD_UNKNOWN);
612 nc_rpc_free(rpc);
613
614 /* create getdata rpc with NC_PARAMTYPE_FREE */
615 char *datastore = strdup("running");
616 char *filter = strdup("filter");
617 char *config_filter = strdup("true");
618 char buf[20] = {0};
619 char **origin_filter;
620 int origin_filter_count = 2;
621
622 origin_filter = calloc(origin_filter_count, sizeof *origin_filter);
623 assert_non_null(origin_filter);
624 for (int i = 0; i < origin_filter_count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200625 snprintf(buf, sizeof(buf) - 1, "origin_filter%d", i + 1);
626 origin_filter[i] = strdup(buf);
Fred Ganec584842021-01-13 15:16:22 +0800627 }
628
629 rpc = nc_rpc_getdata(datastore, filter, config_filter, origin_filter, origin_filter_count, 2, 3, 1, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
630 assert_non_null(rpc);
631 check_getdata(rpc, datastore, filter, config_filter, origin_filter, origin_filter_count, 2, 3, 1, NC_WD_EXPLICIT);
632 nc_rpc_free(rpc);
633
634 /* create getdata rpc with NC_PARAMTYPE_DUP_AND_FREE */
635 char *origin_filter1 = "origin_filter1";
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200636
Fred Ganec584842021-01-13 15:16:22 +0800637 rpc = nc_rpc_getdata("startup", "filter1", "false", &origin_filter1, 1, 0, 3, 1, NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE);
638 assert_non_null(rpc);
639 check_getdata(rpc, "startup", "filter1", "false", &origin_filter1, 1, 0, 3, 1, NC_WD_ALL);
640 nc_rpc_free(rpc);
641}
642
643/* function to check if values of editdata rpc are set correctly */
644void
645check_editdata(struct nc_rpc *rpc, char *datastore, NC_RPC_EDIT_DFLTOP default_op, const char *edit_content)
646{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200647 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDITDATA);
648 struct nc_rpc_editdata *rpc_editdata = (struct nc_rpc_editdata *)rpc;
Fred Ganec584842021-01-13 15:16:22 +0800649
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200650 assert_int_equal(rpc_editdata->type, NC_RPC_EDITDATA);
651 assert_string_equal(rpc_editdata->datastore, datastore);
652 assert_int_equal(rpc_editdata->default_op, default_op);
653 assert_string_equal(rpc_editdata->edit_cont, edit_content);
Fred Ganec584842021-01-13 15:16:22 +0800654}
655
656static void
657test_nc_rpc_editdata(void **state)
658{
659 (void)state;
660 struct nc_rpc *rpc = NULL;
661
662 /* create editdata rpc with NC_PARAMTYPE_CONST */
663 rpc = nc_rpc_editdata("candidate", NC_RPC_EDIT_DFLTOP_UNKNOWN, "edit", NC_PARAMTYPE_CONST);
664 assert_non_null(rpc);
665 check_editdata(rpc, "candidate", NC_RPC_EDIT_DFLTOP_UNKNOWN, "edit");
666 nc_rpc_free(rpc);
667
668 /* create editdata rpc with NC_PARAMTYPE_FREE */
669 char *datastore = strdup("running");
670 char *edit_cont = strdup("edit_data");
671
672 rpc = nc_rpc_editdata(datastore, NC_RPC_EDIT_DFLTOP_MERGE, edit_cont, NC_PARAMTYPE_FREE);
673 assert_non_null(rpc);
674 check_editdata(rpc, datastore, NC_RPC_EDIT_DFLTOP_MERGE, edit_cont);
675 nc_rpc_free(rpc);
676
677 /* create editdata rpc with NC_PARAMTYPE_DUP_AND_FREE */
678 rpc = nc_rpc_editdata("startup", NC_RPC_EDIT_DFLTOP_REPLACE, "edit_cont", NC_PARAMTYPE_DUP_AND_FREE);
679 assert_non_null(rpc);
680 check_editdata(rpc, "startup", NC_RPC_EDIT_DFLTOP_REPLACE, "edit_cont");
681 nc_rpc_free(rpc);
682}
683
David Sedlák19af2662018-10-01 13:42:08 +0200684int
685main(void)
686{
687 const struct CMUnitTest tests[] = {
David Sedlák2f2f2e32018-10-08 21:49:32 +0200688 cmocka_unit_test_setup_teardown(test_nc_rpc_act_generic_xml, setup_f, teardown_f),
689 cmocka_unit_test_setup_teardown(test_nc_rpc_act_generic, setup_f, teardown_f),
David Sedlák19af2662018-10-01 13:42:08 +0200690 cmocka_unit_test_setup_teardown(test_nc_rpc_getconfig, setup_f, teardown_f),
691 cmocka_unit_test_setup_teardown(test_nc_rpc_edit, setup_f, teardown_f),
692 cmocka_unit_test_setup_teardown(test_nc_rpc_copy, setup_f, teardown_f),
693 cmocka_unit_test_setup_teardown(test_nc_rpc_delete, setup_f, teardown_f),
694 cmocka_unit_test_setup_teardown(test_nc_rpc_lock, setup_f, teardown_f),
695 cmocka_unit_test_setup_teardown(test_nc_rpc_unlock, setup_f, teardown_f),
696 cmocka_unit_test_setup_teardown(test_nc_rpc_get, setup_f, teardown_f),
697 cmocka_unit_test_setup_teardown(test_nc_rpc_kill, setup_f, teardown_f),
698 cmocka_unit_test_setup_teardown(test_nc_rpc_commit, setup_f, teardown_f),
699 cmocka_unit_test_setup_teardown(test_nc_rpc_discard, setup_f, teardown_f),
700 cmocka_unit_test_setup_teardown(test_nc_rpc_cancel, setup_f, teardown_f),
701 cmocka_unit_test_setup_teardown(test_nc_rpc_validate, setup_f, teardown_f),
702 cmocka_unit_test_setup_teardown(test_nc_rpc_getschema, setup_f, teardown_f),
703 cmocka_unit_test_setup_teardown(test_nc_rpc_subscribe, setup_f, teardown_f),
Fred Ganec584842021-01-13 15:16:22 +0800704 cmocka_unit_test_setup_teardown(test_nc_rpc_getdata, setup_f, teardown_f),
705 cmocka_unit_test_setup_teardown(test_nc_rpc_editdata, setup_f, teardown_f),
David Sedlák19af2662018-10-01 13:42:08 +0200706 };
707
708 return cmocka_run_group_tests(tests, NULL, NULL);
709}