blob: 2e5908bf7a4d5ba1bc8a2260549b64f58b106a18 [file] [log] [blame]
David Sedlák19af2662018-10-01 13:42:08 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <setjmp.h>
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <errno.h>
David Sedlák3983ed02018-10-07 23:17:19 +02007#include <string.h>
David Sedlák19af2662018-10-01 13:42:08 +02008
9#include <cmocka.h>
10#include <libyang/libyang.h>
11#include <session_client.h>
12#include <log.h>
13#include <config.h>
14#include <messages_p.h>
15#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ák3983ed02018-10-07 23:17:19 +020035/* function to check if values of getconfig rpc are set correctly */
36void
37check_getconfig(struct nc_rpc *rpc, enum NC_DATASTORE_TYPE source, char *filter, NC_WD_MODE wd_mode)
38{
39 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETCONFIG);
40 struct nc_rpc_getconfig *getconfig_rpc = (struct nc_rpc_getconfig *)rpc;
41
42 assert_int_equal(getconfig_rpc->type, NC_RPC_GETCONFIG);
43 assert_int_equal(getconfig_rpc->source, source);
44 assert_string_equal(getconfig_rpc->filter, filter);
45 assert_int_equal(getconfig_rpc->wd_mode, wd_mode);
46}
47
David Sedlák19af2662018-10-01 13:42:08 +020048static void
49test_nc_rpc_getconfig(void **state)
50{
51 (void)state;
52 struct nc_rpc *rpc = NULL;
53
David Sedlák3983ed02018-10-07 23:17:19 +020054 /* create getconfig rpc with NC_PARAMTYPE_CONST */
55 rpc = nc_rpc_getconfig(NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +020056 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +020057 check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +020058 nc_rpc_free(rpc);
59
David Sedlák3983ed02018-10-07 23:17:19 +020060 /* create getconfig rpc with NC_PARAMTYPE_FREE */
61 char *filter = strdup("string");
62 rpc = nc_rpc_getconfig(NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
63 assert_non_null(rpc);
64 check_getconfig(rpc, NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT);
65 nc_rpc_free(rpc);
66
67 /* create getconfig rpc with NC_PARAMTYPE_DUP_AND_FREE */
68 rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, "filter", NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE);
69 assert_non_null(rpc);
70 check_getconfig(rpc, NC_DATASTORE_RUNNING, "filter", NC_WD_ALL);
71 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +020072}
73
David Sedlák3983ed02018-10-07 23:17:19 +020074/* function to check if values of edit rpc are set correctly */
75void
76check_edit(struct nc_rpc *rpc, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt,
77 NC_RPC_EDIT_ERROPT error_opt, const char *edit_content)
78{
79 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDIT);
80 struct nc_rpc_edit *edit_rpc = (struct nc_rpc_edit *)rpc;
81
82 assert_int_equal(edit_rpc->type, NC_RPC_EDIT);
83 assert_int_equal(edit_rpc->target, target);
84 assert_int_equal(edit_rpc->default_op, default_op);
85 assert_int_equal(edit_rpc->test_opt, test_opt);
86 assert_int_equal(edit_rpc->error_opt, error_opt);
87 assert_string_equal(edit_rpc->edit_cont, edit_content);
88}
89
90
David Sedlák19af2662018-10-01 13:42:08 +020091static void
92test_nc_rpc_edit(void **state)
93{
94 (void)state;
95 struct nc_rpc *rpc = NULL;
96
David Sedlák3983ed02018-10-07 23:17:19 +020097 /* create edit rpc with NC_PARAMTYPE_CONST */
98 rpc = nc_rpc_edit(NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, NC_RPC_EDIT_TESTOPT_TESTSET,
99 NC_RPC_EDIT_ERROPT_STOP, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200100 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200101 check_edit(rpc, NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE,
102 NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200103 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200104
105 /* create edit rpc with NC_PARAMTYPE_FREE */
106 char *str = strdup("string");
107 rpc = nc_rpc_edit(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_SET,
108 NC_RPC_EDIT_ERROPT_ROLLBACK, str, NC_PARAMTYPE_FREE);
109 assert_non_null(rpc);
110 check_edit(rpc, NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE,
111 NC_RPC_EDIT_TESTOPT_SET, NC_RPC_EDIT_ERROPT_ROLLBACK, str);
112 nc_rpc_free(rpc);
113
114 /* create edit rpc with NC_PARAMTYPE_DUP_AND_FREE */
115 rpc = nc_rpc_edit(NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, NC_RPC_EDIT_TESTOPT_TEST,
116 NC_RPC_EDIT_ERROPT_CONTINUE, "url1", NC_PARAMTYPE_DUP_AND_FREE);
117 assert_non_null(rpc);
118 check_edit(rpc, NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE,
119 NC_RPC_EDIT_TESTOPT_TEST, NC_RPC_EDIT_ERROPT_CONTINUE, "url1");
120 nc_rpc_free(rpc);
121}
122
123/* function to check if values of copy rpc are set correctly */
124void
125check_copy(struct nc_rpc *rpc, NC_DATASTORE target, const char *url_trg, NC_DATASTORE source,
126 const char *url_or_config_src, NC_WD_MODE wd_mode)
127{
128 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COPY);
129 struct nc_rpc_copy *copy_rpc = (struct nc_rpc_copy *)rpc;
130
131 assert_int_equal(copy_rpc->type, NC_RPC_COPY);
132 assert_int_equal(copy_rpc->target, target);
133 assert_string_equal(copy_rpc->url_trg, url_trg);
134 assert_int_equal(copy_rpc->source, source);
135 assert_string_equal(copy_rpc->url_config_src, url_or_config_src);
136 assert_int_equal(copy_rpc->wd_mode, wd_mode);
David Sedlák19af2662018-10-01 13:42:08 +0200137}
138
139static void
140test_nc_rpc_copy(void **state)
141{
142 (void)state;
143 struct nc_rpc *rpc = NULL;
144
David Sedlák3983ed02018-10-07 23:17:19 +0200145 /* create copy rpc with NC_PARAMTYPE_CONST */
David Sedlák19af2662018-10-01 13:42:08 +0200146 rpc = nc_rpc_copy(NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url",
147 NC_WD_ALL, NC_PARAMTYPE_CONST);
148 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200149 check_copy(rpc, NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", NC_WD_ALL);
David Sedlák19af2662018-10-01 13:42:08 +0200150 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200151
152 /* create copy rpc with NC_PARAMTYPE_FREE */
153 char *target = strdup("target");
154 char *src = strdup("src");
155 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src,
156 NC_WD_ALL_TAG, NC_PARAMTYPE_FREE);
157 assert_non_null(rpc);
158 check_copy(rpc, NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, NC_WD_ALL_TAG);
159 nc_rpc_free(rpc);
160
161 /* create copy rpc with NC_PARAMTYPE_DUP_AND_FREE */
162 rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url",
163 NC_WD_TRIM, NC_PARAMTYPE_DUP_AND_FREE);
164 assert_non_null(rpc);
165 check_copy(rpc, NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url", NC_WD_TRIM);
166 nc_rpc_free(rpc);
167}
168
169/* function to check if values of delete rpc are set correctly */
170void
171check_delete(struct nc_rpc *rpc, NC_DATASTORE target, const char *url)
172{
173 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DELETE);
174 struct nc_rpc_delete *delete_rpc = (struct nc_rpc_delete *)rpc;
175
176 assert_int_equal(delete_rpc->type, NC_RPC_DELETE);
177 assert_int_equal(delete_rpc->target, target);
178 assert_string_equal(delete_rpc->url, url);
David Sedlák19af2662018-10-01 13:42:08 +0200179}
180
181static void
182test_nc_rpc_delete(void **state)
183{
184 (void)state;
185 struct nc_rpc *rpc = NULL;
186
David Sedlák3983ed02018-10-07 23:17:19 +0200187 /* create delete rpc with NC_PARAMTYPE_CONST */
188 rpc = nc_rpc_delete(NC_DATASTORE_RUNNING, "target-url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200189 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200190 check_delete(rpc, NC_DATASTORE_RUNNING, "target-url");
191 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200192
David Sedlák3983ed02018-10-07 23:17:19 +0200193 /* create delete rpc with NC_PARAMTYPE_FREE */
194 char *url = strdup("url");
195 rpc = nc_rpc_delete(NC_DATASTORE_CANDIDATE, url, NC_PARAMTYPE_FREE);
196 assert_non_null(rpc);
197 check_delete(rpc, NC_DATASTORE_CANDIDATE, url);
198 nc_rpc_free(rpc);
199
200 /* create delete rpc with NC_PARAMTYPE_DUP_AND_FREE */
201 rpc = nc_rpc_delete(NC_DATASTORE_CONFIG, "target", NC_PARAMTYPE_DUP_AND_FREE);
202 assert_non_null(rpc);
203 check_delete(rpc, NC_DATASTORE_CONFIG, "target");
David Sedlák19af2662018-10-01 13:42:08 +0200204 nc_rpc_free(rpc);
205}
206
207static void
208test_nc_rpc_lock(void **state)
209{
210 (void)state;
211 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200212 struct nc_rpc_lock *lock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200213
214 rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
215 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200216 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_LOCK);
217
218 lock_rpc = (struct nc_rpc_lock *)rpc;
219 assert_int_equal(lock_rpc->type, NC_RPC_LOCK);
220 assert_int_equal(lock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200221
222 nc_rpc_free(rpc);
223}
224
225static void
226test_nc_rpc_unlock(void **state)
227{
228 (void)state;
229 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200230 struct nc_rpc_lock *unlock_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200231
232 rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING);
233 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200234 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK);
David Sedlák19af2662018-10-01 13:42:08 +0200235
David Sedlák3983ed02018-10-07 23:17:19 +0200236 unlock_rpc = (struct nc_rpc_lock *)rpc;
237 assert_int_equal(unlock_rpc->type, NC_RPC_UNLOCK);
238 assert_int_equal(unlock_rpc->target, NC_DATASTORE_RUNNING);
David Sedlák19af2662018-10-01 13:42:08 +0200239 nc_rpc_free(rpc);
240}
241
David Sedlák3983ed02018-10-07 23:17:19 +0200242/* function to check if values of get rpc are set correctly */
243void
244check_get_rpc(struct nc_rpc *rpc, const char *filter, NC_WD_MODE wd_mode)
245{
246 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GET);
247 struct nc_rpc_get *get_rpc = (struct nc_rpc_get *)rpc;
248
249 assert_int_equal(get_rpc->type, NC_RPC_GET);
250 assert_string_equal(get_rpc->filter, filter);
251 assert_int_equal(get_rpc->wd_mode, wd_mode);
252}
253
David Sedlák19af2662018-10-01 13:42:08 +0200254static void
255test_nc_rpc_get(void **state)
256{
257 (void)state;
258 struct nc_rpc *rpc = NULL;
259
David Sedlák3983ed02018-10-07 23:17:19 +0200260 /* create get rpc with NC_PARAMTYPE_CONST */
261 rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200262 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200263 check_get_rpc(rpc, "filter", NC_WD_ALL);
264 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200265
David Sedlák3983ed02018-10-07 23:17:19 +0200266 /* create get rpc with NC_PARAMTYPE_FREE */
267 char *str = strdup("string");
268 rpc = nc_rpc_get(str, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
269 assert_non_null(rpc);
270 check_get_rpc(rpc, str, NC_WD_EXPLICIT);
271 nc_rpc_free(rpc);
272
273 /* create get rpc with NC_PARAMTYPE_DUP_AND_FREE */
274 rpc = nc_rpc_get("filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_DUP_AND_FREE);
275 assert_non_null(rpc);
276 check_get_rpc(rpc, "filter-string", NC_WD_UNKNOWN);
David Sedlák19af2662018-10-01 13:42:08 +0200277 nc_rpc_free(rpc);
278}
279
280static void
281test_nc_rpc_kill(void **state)
282{
283 (void)state;
284 struct nc_rpc *rpc = NULL;
David Sedlák3983ed02018-10-07 23:17:19 +0200285 struct nc_rpc_kill *kill_rpc = NULL;
David Sedlák19af2662018-10-01 13:42:08 +0200286
287 rpc = nc_rpc_kill(10);
288 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200289 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_KILL);
290
291 kill_rpc = (struct nc_rpc_kill *)rpc;
292 assert_int_equal(kill_rpc->type, NC_RPC_KILL);
293 assert_int_equal(kill_rpc->sid, 10);
David Sedlák19af2662018-10-01 13:42:08 +0200294
295 nc_rpc_free(rpc);
296}
297
David Sedlák3983ed02018-10-07 23:17:19 +0200298/* function to check if values of commit rpc are set correctly */
299void
300check_commit_rpc(struct nc_rpc *rpc, int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id)
301{
302 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COMMIT);
303 struct nc_rpc_commit *commit_rpc = (struct nc_rpc_commit *)rpc;
304
305 assert_int_equal(commit_rpc->type, NC_RPC_COMMIT);
306 assert_int_equal(commit_rpc->confirmed, confirmed);
307 assert_int_equal(commit_rpc->confirm_timeout, confirm_timeout);
308 assert_string_equal(commit_rpc->persist, persist);
309 assert_string_equal(commit_rpc->persist_id, persist_id);
310}
311
David Sedlák19af2662018-10-01 13:42:08 +0200312static void
313test_nc_rpc_commit(void **state)
314{
315 (void)state;
316 struct nc_rpc *rpc = NULL;
317
David Sedlák3983ed02018-10-07 23:17:19 +0200318 /* create commit rpc with NC_PARAMTYPE_CONST*/
319 rpc = nc_rpc_commit(1, 100, "persist", "persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200320 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200321 check_commit_rpc(rpc, 1, 100, "persist", "persist-id");
322 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200323
David Sedlák3983ed02018-10-07 23:17:19 +0200324 /* create commit rpc with NC_PARAMTYPE_FREE*/
325 char *str1 = strdup("str1");
326 char *str2 = strdup("str2");
327 rpc = nc_rpc_commit(2, 5, str1, str2, NC_PARAMTYPE_FREE);
328 assert_non_null(rpc);
329 check_commit_rpc(rpc, 2, 5, str1, str2);
330 nc_rpc_free(rpc);
331
332 /* create commit rpc with NC_PARAMTYPE_DUP_AND_FREE*/
333 rpc = nc_rpc_commit(10, 200, "persistent", "persistent-id", NC_PARAMTYPE_DUP_AND_FREE);
334 assert_non_null(rpc);
335 check_commit_rpc(rpc, 10, 200, "persistent", "persistent-id");
David Sedlák19af2662018-10-01 13:42:08 +0200336 nc_rpc_free(rpc);
337}
338
339static void
340test_nc_rpc_discard(void **state)
341{
342 (void)state;
343 struct nc_rpc *rpc = NULL;
344
345 rpc = nc_rpc_discard();
346 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200347 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD);
David Sedlák19af2662018-10-01 13:42:08 +0200348
349 nc_rpc_free(rpc);
350}
351
David Sedlák3983ed02018-10-07 23:17:19 +0200352/* function to check if values of cancel rpc are set correctly */
353void
354check_cancel_rpc(struct nc_rpc *rpc, const char *persist_id)
355{
356 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_CANCEL);
357 struct nc_rpc_cancel *cancel_rpc = (struct nc_rpc_cancel *)rpc;
358
359 assert_int_equal(cancel_rpc->type, NC_RPC_CANCEL);
360 assert_string_equal(cancel_rpc->persist_id, persist_id);
361}
362
David Sedlák19af2662018-10-01 13:42:08 +0200363static void
364test_nc_rpc_cancel(void **state)
365{
366 (void)state;
367 struct nc_rpc *rpc = NULL;
368
David Sedlák3983ed02018-10-07 23:17:19 +0200369 /* create cancel rpc with NC_PARAMTYPE_CONST*/
370 rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200371 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200372 check_cancel_rpc(rpc, "persist-id");
David Sedlák19af2662018-10-01 13:42:08 +0200373 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200374
375 /* create cancel rpc with NC_PARAMTYPE_FREE*/
376 char *str = strdup("string");
377 rpc = nc_rpc_cancel(str, NC_PARAMTYPE_FREE);
378 assert_non_null(rpc);
379 check_cancel_rpc(rpc, str);
380 nc_rpc_free(rpc);
381
382 /* create cancel rpc with NC_PARAMTYPE_DUP_AND_FREE*/
383 rpc = nc_rpc_cancel("id", NC_PARAMTYPE_DUP_AND_FREE);
384 assert_non_null(rpc);
385 check_cancel_rpc(rpc, "id");
386 nc_rpc_free(rpc);
387}
388
389/* function to check if values of validate rpc are set correctly */
390void
391check_validate_rpc(struct nc_rpc *rpc, NC_DATASTORE source, const char *url_or_config)
392{
393 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_VALIDATE);
394 struct nc_rpc_validate *validate_rpc = (struct nc_rpc_validate *)rpc;
395
396 assert_int_equal(validate_rpc->type, NC_RPC_VALIDATE);
397 assert_int_equal(validate_rpc->source, source);
398 assert_string_equal(validate_rpc->url_config_src, url_or_config);
David Sedlák19af2662018-10-01 13:42:08 +0200399}
400
401static void
402test_nc_rpc_validate(void **state)
403{
404 (void)state;
405 struct nc_rpc *rpc = NULL;
406
David Sedlák3983ed02018-10-07 23:17:19 +0200407 /* create validate rpc with NC_PARAMTYPE_CONST */
408 rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200409 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200410 check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url");
David Sedlák19af2662018-10-01 13:42:08 +0200411 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200412
413 /* create validate rpc with NC_PARAMTYPE_FREE */
414 char *str = strdup("string");
415 rpc = nc_rpc_validate(NC_DATASTORE_CANDIDATE, str, NC_PARAMTYPE_FREE);
416 assert_non_null(rpc);
417 check_validate_rpc(rpc, NC_DATASTORE_CANDIDATE, str);
418 nc_rpc_free(rpc);
419
420 /* create validate rpc with NC_PARAMTYPE_DUP_AND_FREE */
421 rpc = nc_rpc_validate(NC_DATASTORE_CONFIG, "url1", NC_PARAMTYPE_DUP_AND_FREE);
422 assert_non_null(rpc);
423 check_validate_rpc(rpc, NC_DATASTORE_CONFIG, "url1");
424 nc_rpc_free(rpc);
425}
426
427/* function to check if values of getschema rpc are set correctly */
428void
429check_getschema_rpc(struct nc_rpc *rpc, const char *identifier, const char *version, const char *format)
430{
431 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETSCHEMA);
432 struct nc_rpc_getschema *getchema_rpc = (struct nc_rpc_getschema *)rpc;
433
434 assert_int_equal(getchema_rpc->type, NC_RPC_GETSCHEMA);
435 assert_string_equal(getchema_rpc->identifier, identifier);
436 assert_string_equal(getchema_rpc->version, version);
437 assert_string_equal(getchema_rpc->format, format);
David Sedlák19af2662018-10-01 13:42:08 +0200438}
439
440static void
441test_nc_rpc_getschema(void **state)
442{
443 (void)state;
444 struct nc_rpc *rpc = NULL;
445
David Sedlák3983ed02018-10-07 23:17:19 +0200446 /* create getchema with NC_PARAMTYPE_CONST*/
447 rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200448 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200449 check_getschema_rpc(rpc, "id", "version", "format");
David Sedlák19af2662018-10-01 13:42:08 +0200450 nc_rpc_free(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200451
452 /* create getchema with NC_PARAMTYPE_FREE*/
453 char *str1 = strdup("str1");
454 char *str2 = strdup("str2");
455 char *str3 = strdup("str3");
456 rpc = nc_rpc_getschema(str1, str2, str3, NC_PARAMTYPE_FREE);
457 assert_non_null(rpc);
458 check_getschema_rpc(rpc, str1, str2, str3);
459 nc_rpc_free(rpc);
460
461 /* create getchema with NC_PARAMTYPE_DUP_AND_FREE*/
462 rpc = nc_rpc_getschema("id1", "version1", "format1", NC_PARAMTYPE_DUP_AND_FREE);
463 assert_non_null(rpc);
464 check_getschema_rpc(rpc, "id1", "version1", "format1");
465 nc_rpc_free(rpc);
466}
467
468/* function to check if values of subscribe rpc are set correctly */
469void
470check_subscribe_rpc(struct nc_rpc *rpc, const char *stream_name, const char *filter,
471 const char *start_time, const char *stop_time)
472{
473 assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_SUBSCRIBE);
474 struct nc_rpc_subscribe *subscribe_rpc = (struct nc_rpc_subscribe *)rpc;
475
476 assert_int_equal(subscribe_rpc->type, NC_RPC_SUBSCRIBE);
477 assert_string_equal(subscribe_rpc->stream, stream_name);
478 assert_string_equal(subscribe_rpc->filter, filter);
479 assert_string_equal(subscribe_rpc->start, start_time);
480 assert_string_equal(subscribe_rpc->stop, stop_time);
David Sedlák19af2662018-10-01 13:42:08 +0200481}
482
483static void
484test_nc_rpc_subscribe(void **state)
485{
486 (void)state;
487 struct nc_rpc *rpc = NULL;
488
David Sedlák3983ed02018-10-07 23:17:19 +0200489 /* create subscribe rpc with NC_PARAMTYPE_CONST*/
490 rpc = nc_rpc_subscribe("stream-name", "filter", "start-time", "stop-time", NC_PARAMTYPE_CONST);
David Sedlák19af2662018-10-01 13:42:08 +0200491 assert_non_null(rpc);
David Sedlák3983ed02018-10-07 23:17:19 +0200492 check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time");
493 nc_rpc_free(rpc);
David Sedlák19af2662018-10-01 13:42:08 +0200494
David Sedlák3983ed02018-10-07 23:17:19 +0200495 /* create subscribe rpc with NC_PARAMTYPE_FREE*/
496 char *str1 = strdup("str1");
497 char *str2 = strdup("str2");
498 char *str3 = strdup("str3");
499 char *str4 = strdup("str4");
500 rpc = nc_rpc_subscribe(str1, str2, str3, str4, NC_PARAMTYPE_FREE);
501 assert_non_null(rpc);
502 check_subscribe_rpc(rpc, str1, str2, str3, str4);
503 nc_rpc_free(rpc);
504
505 /* create subscribe rpc with NC_PARAMTYPE_DUP_AND_FREE*/
506 rpc = nc_rpc_subscribe("name", "filter-str", "start", "stop", NC_PARAMTYPE_DUP_AND_FREE);
507 assert_non_null(rpc);
508 check_subscribe_rpc(rpc, "name", "filter-str", "start", "stop");
David Sedlák19af2662018-10-01 13:42:08 +0200509 nc_rpc_free(rpc);
510}
511
512int
513main(void)
514{
515 const struct CMUnitTest tests[] = {
516 cmocka_unit_test_setup_teardown(test_nc_rpc_getconfig, setup_f, teardown_f),
517 cmocka_unit_test_setup_teardown(test_nc_rpc_edit, setup_f, teardown_f),
518 cmocka_unit_test_setup_teardown(test_nc_rpc_copy, setup_f, teardown_f),
519 cmocka_unit_test_setup_teardown(test_nc_rpc_delete, setup_f, teardown_f),
520 cmocka_unit_test_setup_teardown(test_nc_rpc_lock, setup_f, teardown_f),
521 cmocka_unit_test_setup_teardown(test_nc_rpc_unlock, setup_f, teardown_f),
522 cmocka_unit_test_setup_teardown(test_nc_rpc_get, setup_f, teardown_f),
523 cmocka_unit_test_setup_teardown(test_nc_rpc_kill, setup_f, teardown_f),
524 cmocka_unit_test_setup_teardown(test_nc_rpc_commit, setup_f, teardown_f),
525 cmocka_unit_test_setup_teardown(test_nc_rpc_discard, setup_f, teardown_f),
526 cmocka_unit_test_setup_teardown(test_nc_rpc_cancel, setup_f, teardown_f),
527 cmocka_unit_test_setup_teardown(test_nc_rpc_validate, setup_f, teardown_f),
528 cmocka_unit_test_setup_teardown(test_nc_rpc_getschema, setup_f, teardown_f),
529 cmocka_unit_test_setup_teardown(test_nc_rpc_subscribe, setup_f, teardown_f),
530 };
531
532 return cmocka_run_group_tests(tests, NULL, NULL);
533}