Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 1 | /** |
| 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <setjmp.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 21 | #include <string.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 24 | |
| 25 | #include <cmocka.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 26 | #include <config.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 27 | #include <libyang/libyang.h> |
| 28 | #include <log.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 29 | #include <messages_p.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 30 | #include <session_client.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 31 | #include "tests/config.h" |
| 32 | |
| 33 | static int |
| 34 | setup_f(void **state) |
| 35 | { |
| 36 | (void)state; |
| 37 | |
| 38 | nc_verbosity(NC_VERB_VERBOSE); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | teardown_f(void **state) |
| 45 | { |
| 46 | (void)state; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 51 | static void |
| 52 | test_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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 69 | |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 70 | 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 | |
| 88 | static void |
| 89 | test_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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 95 | |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 96 | 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ák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 109 | /* function to check if values of getconfig rpc are set correctly */ |
| 110 | void |
| 111 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 122 | static void |
| 123 | test_nc_rpc_getconfig(void **state) |
| 124 | { |
| 125 | (void)state; |
| 126 | struct nc_rpc *rpc = NULL; |
| 127 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 128 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 130 | assert_non_null(rpc); |
Michal Vasko | 79142c2 | 2020-07-29 15:26:51 +0200 | [diff] [blame] | 131 | check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_WD_UNKNOWN); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 132 | nc_rpc_free(rpc); |
| 133 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 134 | /* create getconfig rpc with NC_PARAMTYPE_FREE */ |
| 135 | char *filter = strdup("string"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 136 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 137 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 147 | } |
| 148 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 149 | /* function to check if values of edit rpc are set correctly */ |
| 150 | void |
| 151 | check_edit(struct nc_rpc *rpc, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 152 | NC_RPC_EDIT_ERROPT error_opt, const char *edit_content) |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 153 | { |
| 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 165 | static void |
| 166 | test_nc_rpc_edit(void **state) |
| 167 | { |
| 168 | (void)state; |
| 169 | struct nc_rpc *rpc = NULL; |
| 170 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 171 | /* 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 173 | NC_RPC_EDIT_ERROPT_STOP, "url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 174 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 175 | check_edit(rpc, NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 176 | NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, "url"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 177 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 178 | |
| 179 | /* create edit rpc with NC_PARAMTYPE_FREE */ |
| 180 | char *str = strdup("string"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 181 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 182 | rpc = nc_rpc_edit(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_SET, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 183 | NC_RPC_EDIT_ERROPT_ROLLBACK, str, NC_PARAMTYPE_FREE); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 184 | assert_non_null(rpc); |
| 185 | check_edit(rpc, NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 186 | NC_RPC_EDIT_TESTOPT_SET, NC_RPC_EDIT_ERROPT_ROLLBACK, str); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 187 | 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 191 | NC_RPC_EDIT_ERROPT_CONTINUE, "url1", NC_PARAMTYPE_DUP_AND_FREE); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 192 | assert_non_null(rpc); |
| 193 | check_edit(rpc, NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 194 | NC_RPC_EDIT_TESTOPT_TEST, NC_RPC_EDIT_ERROPT_CONTINUE, "url1"); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 195 | nc_rpc_free(rpc); |
| 196 | } |
| 197 | |
| 198 | /* function to check if values of copy rpc are set correctly */ |
| 199 | void |
| 200 | check_copy(struct nc_rpc *rpc, NC_DATASTORE target, const char *url_trg, NC_DATASTORE source, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 201 | const char *url_or_config_src, NC_WD_MODE wd_mode) |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 202 | { |
| 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
| 215 | test_nc_rpc_copy(void **state) |
| 216 | { |
| 217 | (void)state; |
| 218 | struct nc_rpc *rpc = NULL; |
| 219 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 220 | /* create copy rpc with NC_PARAMTYPE_CONST */ |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 221 | rpc = nc_rpc_copy(NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 222 | NC_WD_ALL, NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 223 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 224 | check_copy(rpc, NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", NC_WD_ALL); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 225 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 226 | |
| 227 | /* create copy rpc with NC_PARAMTYPE_FREE */ |
| 228 | char *target = strdup("target"); |
| 229 | char *src = strdup("src"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 230 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 231 | rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 232 | NC_WD_ALL_TAG, NC_PARAMTYPE_FREE); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 233 | 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 239 | NC_WD_TRIM, NC_PARAMTYPE_DUP_AND_FREE); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 240 | 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 */ |
| 246 | void |
| 247 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
| 258 | test_nc_rpc_delete(void **state) |
| 259 | { |
| 260 | (void)state; |
| 261 | struct nc_rpc *rpc = NULL; |
| 262 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 263 | /* create delete rpc with NC_PARAMTYPE_CONST */ |
| 264 | rpc = nc_rpc_delete(NC_DATASTORE_RUNNING, "target-url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 265 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 266 | check_delete(rpc, NC_DATASTORE_RUNNING, "target-url"); |
| 267 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 268 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 269 | /* create delete rpc with NC_PARAMTYPE_FREE */ |
| 270 | char *url = strdup("url"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 271 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 272 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 281 | nc_rpc_free(rpc); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | test_nc_rpc_lock(void **state) |
| 286 | { |
| 287 | (void)state; |
| 288 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 289 | struct nc_rpc_lock *lock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 290 | |
| 291 | rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
| 292 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 293 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 298 | |
| 299 | nc_rpc_free(rpc); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | test_nc_rpc_unlock(void **state) |
| 304 | { |
| 305 | (void)state; |
| 306 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 307 | struct nc_rpc_lock *unlock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 308 | |
| 309 | rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING); |
| 310 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 311 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 312 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 313 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 316 | nc_rpc_free(rpc); |
| 317 | } |
| 318 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 319 | /* function to check if values of get rpc are set correctly */ |
| 320 | void |
| 321 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 331 | static void |
| 332 | test_nc_rpc_get(void **state) |
| 333 | { |
| 334 | (void)state; |
| 335 | struct nc_rpc *rpc = NULL; |
| 336 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 337 | /* create get rpc with NC_PARAMTYPE_CONST */ |
| 338 | rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 339 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 340 | check_get_rpc(rpc, "filter", NC_WD_ALL); |
| 341 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 342 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 343 | /* create get rpc with NC_PARAMTYPE_FREE */ |
| 344 | char *str = strdup("string"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 345 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 346 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 355 | nc_rpc_free(rpc); |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | test_nc_rpc_kill(void **state) |
| 360 | { |
| 361 | (void)state; |
| 362 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 363 | struct nc_rpc_kill *kill_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 364 | |
| 365 | rpc = nc_rpc_kill(10); |
| 366 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 367 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 372 | |
| 373 | nc_rpc_free(rpc); |
| 374 | } |
| 375 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 376 | /* function to check if values of commit rpc are set correctly */ |
| 377 | void |
| 378 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 390 | static void |
| 391 | test_nc_rpc_commit(void **state) |
| 392 | { |
| 393 | (void)state; |
| 394 | struct nc_rpc *rpc = NULL; |
| 395 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 396 | /* create commit rpc with NC_PARAMTYPE_CONST*/ |
| 397 | rpc = nc_rpc_commit(1, 100, "persist", "persist-id", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 398 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 399 | check_commit_rpc(rpc, 1, 100, "persist", "persist-id"); |
| 400 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 401 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 402 | /* create commit rpc with NC_PARAMTYPE_FREE*/ |
| 403 | char *str1 = strdup("str1"); |
| 404 | char *str2 = strdup("str2"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 405 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 406 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 415 | nc_rpc_free(rpc); |
| 416 | } |
| 417 | |
| 418 | static void |
| 419 | test_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ák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 426 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 427 | |
| 428 | nc_rpc_free(rpc); |
| 429 | } |
| 430 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 431 | /* function to check if values of cancel rpc are set correctly */ |
| 432 | void |
| 433 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 442 | static void |
| 443 | test_nc_rpc_cancel(void **state) |
| 444 | { |
| 445 | (void)state; |
| 446 | struct nc_rpc *rpc = NULL; |
| 447 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 448 | /* create cancel rpc with NC_PARAMTYPE_CONST*/ |
| 449 | rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 450 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 451 | check_cancel_rpc(rpc, "persist-id"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 452 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 453 | |
| 454 | /* create cancel rpc with NC_PARAMTYPE_FREE*/ |
| 455 | char *str = strdup("string"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 456 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 457 | 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 */ |
| 470 | void |
| 471 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void |
| 482 | test_nc_rpc_validate(void **state) |
| 483 | { |
| 484 | (void)state; |
| 485 | struct nc_rpc *rpc = NULL; |
| 486 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 487 | /* create validate rpc with NC_PARAMTYPE_CONST */ |
| 488 | rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 489 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 490 | check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 491 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 492 | |
| 493 | /* create validate rpc with NC_PARAMTYPE_FREE */ |
| 494 | char *str = strdup("string"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 495 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 496 | 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 */ |
| 509 | void |
| 510 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static void |
| 522 | test_nc_rpc_getschema(void **state) |
| 523 | { |
| 524 | (void)state; |
| 525 | struct nc_rpc *rpc = NULL; |
| 526 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 527 | /* create getchema with NC_PARAMTYPE_CONST*/ |
| 528 | rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 529 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 530 | check_getschema_rpc(rpc, "id", "version", "format"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 531 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 532 | |
| 533 | /* create getchema with NC_PARAMTYPE_FREE*/ |
| 534 | char *str1 = strdup("str1"); |
| 535 | char *str2 = strdup("str2"); |
| 536 | char *str3 = strdup("str3"); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 537 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 538 | 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 */ |
| 551 | void |
| 552 | check_subscribe_rpc(struct nc_rpc *rpc, const char *stream_name, const char *filter, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 553 | const char *start_time, const char *stop_time) |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 554 | { |
| 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static void |
| 566 | test_nc_rpc_subscribe(void **state) |
| 567 | { |
| 568 | (void)state; |
| 569 | struct nc_rpc *rpc = NULL; |
| 570 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 571 | /* create subscribe rpc with NC_PARAMTYPE_CONST*/ |
| 572 | rpc = nc_rpc_subscribe("stream-name", "filter", "start-time", "stop-time", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 573 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 574 | check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time"); |
| 575 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 576 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 577 | /* 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 582 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 583 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 592 | nc_rpc_free(rpc); |
| 593 | } |
| 594 | |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 595 | /* function to check if values of getdata rpc are set correctly */ |
| 596 | void |
| 597 | check_getdata(struct nc_rpc *rpc, char *datastore, const char *filter, const char *config_filter, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 598 | 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 Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 600 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 601 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETDATA); |
| 602 | struct nc_rpc_getdata *rpc_getdata = (struct nc_rpc_getdata *)rpc; |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 603 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 604 | 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 Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static void |
| 617 | test_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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 624 | |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 625 | 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 641 | snprintf(buf, sizeof(buf) - 1, "origin_filter%d", i + 1); |
| 642 | origin_filter[i] = strdup(buf); |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 643 | } |
| 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 652 | |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 653 | 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 */ |
| 660 | void |
| 661 | check_editdata(struct nc_rpc *rpc, char *datastore, NC_RPC_EDIT_DFLTOP default_op, const char *edit_content) |
| 662 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 663 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDITDATA); |
| 664 | struct nc_rpc_editdata *rpc_editdata = (struct nc_rpc_editdata *)rpc; |
Fred Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 665 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 666 | 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 Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static void |
| 673 | test_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 700 | int |
| 701 | main(void) |
| 702 | { |
| 703 | const struct CMUnitTest tests[] = { |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 704 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 706 | 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 Gan | ec58484 | 2021-01-13 15:16:22 +0800 | [diff] [blame] | 720 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 722 | }; |
| 723 | |
| 724 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 725 | } |