David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 1 | #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ák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 7 | #include <string.h> |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 8 | |
| 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 | |
| 17 | static int |
| 18 | setup_f(void **state) |
| 19 | { |
| 20 | (void)state; |
| 21 | |
| 22 | nc_verbosity(NC_VERB_VERBOSE); |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | static int |
| 28 | teardown_f(void **state) |
| 29 | { |
| 30 | (void)state; |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 35 | static void |
| 36 | test_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"); |
| 53 | rpc = nc_rpc_act_generic_xml(str, NC_PARAMTYPE_FREE); |
| 54 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC); |
| 55 | generic_rpc = (struct nc_rpc_act_generic *)rpc; |
| 56 | assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC); |
| 57 | assert_int_equal(generic_rpc->has_data, 0); |
| 58 | assert_string_equal(generic_rpc->content.xml_str, str); |
| 59 | nc_rpc_free(rpc); |
| 60 | |
| 61 | /* create generic rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 62 | rpc = nc_rpc_act_generic_xml("xml", NC_PARAMTYPE_DUP_AND_FREE); |
| 63 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC); |
| 64 | generic_rpc = (struct nc_rpc_act_generic *)rpc; |
| 65 | assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC); |
| 66 | assert_int_equal(generic_rpc->has_data, 0); |
| 67 | assert_string_equal(generic_rpc->content.xml_str, "xml"); |
| 68 | nc_rpc_free(rpc); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | test_nc_rpc_act_generic(void **state) |
| 73 | { |
| 74 | (void)state; |
| 75 | struct nc_rpc *rpc = NULL; |
| 76 | struct nc_rpc_act_generic *generic_rpc = NULL; |
| 77 | struct lyd_node node; |
| 78 | node.next = NULL; |
| 79 | node.prev = &node; |
| 80 | |
| 81 | rpc = nc_rpc_act_generic(&node, NC_PARAMTYPE_CONST); |
| 82 | assert_non_null(rpc); |
| 83 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_ACT_GENERIC); |
| 84 | generic_rpc = (struct nc_rpc_act_generic *)rpc; |
| 85 | assert_int_equal(generic_rpc->type, NC_RPC_ACT_GENERIC); |
| 86 | assert_int_equal(generic_rpc->has_data, 1); |
| 87 | assert_ptr_equal(generic_rpc->content.data, &node); |
| 88 | nc_rpc_free(rpc); |
| 89 | } |
| 90 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 91 | /* function to check if values of getconfig rpc are set correctly */ |
| 92 | void |
| 93 | check_getconfig(struct nc_rpc *rpc, enum NC_DATASTORE_TYPE source, char *filter, NC_WD_MODE wd_mode) |
| 94 | { |
| 95 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETCONFIG); |
| 96 | struct nc_rpc_getconfig *getconfig_rpc = (struct nc_rpc_getconfig *)rpc; |
| 97 | |
| 98 | assert_int_equal(getconfig_rpc->type, NC_RPC_GETCONFIG); |
| 99 | assert_int_equal(getconfig_rpc->source, source); |
| 100 | assert_string_equal(getconfig_rpc->filter, filter); |
| 101 | assert_int_equal(getconfig_rpc->wd_mode, wd_mode); |
| 102 | } |
| 103 | |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 104 | static void |
| 105 | test_nc_rpc_getconfig(void **state) |
| 106 | { |
| 107 | (void)state; |
| 108 | struct nc_rpc *rpc = NULL; |
| 109 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 110 | /* create getconfig rpc with NC_PARAMTYPE_CONST */ |
| 111 | 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] | 112 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 113 | check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 114 | nc_rpc_free(rpc); |
| 115 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 116 | /* create getconfig rpc with NC_PARAMTYPE_FREE */ |
| 117 | char *filter = strdup("string"); |
| 118 | rpc = nc_rpc_getconfig(NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
| 119 | assert_non_null(rpc); |
| 120 | check_getconfig(rpc, NC_DATASTORE_CONFIG, filter, NC_WD_EXPLICIT); |
| 121 | nc_rpc_free(rpc); |
| 122 | |
| 123 | /* create getconfig rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 124 | rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, "filter", NC_WD_ALL, NC_PARAMTYPE_DUP_AND_FREE); |
| 125 | assert_non_null(rpc); |
| 126 | check_getconfig(rpc, NC_DATASTORE_RUNNING, "filter", NC_WD_ALL); |
| 127 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 128 | } |
| 129 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 130 | /* function to check if values of edit rpc are set correctly */ |
| 131 | void |
| 132 | check_edit(struct nc_rpc *rpc, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt, |
| 133 | NC_RPC_EDIT_ERROPT error_opt, const char *edit_content) |
| 134 | { |
| 135 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_EDIT); |
| 136 | struct nc_rpc_edit *edit_rpc = (struct nc_rpc_edit *)rpc; |
| 137 | |
| 138 | assert_int_equal(edit_rpc->type, NC_RPC_EDIT); |
| 139 | assert_int_equal(edit_rpc->target, target); |
| 140 | assert_int_equal(edit_rpc->default_op, default_op); |
| 141 | assert_int_equal(edit_rpc->test_opt, test_opt); |
| 142 | assert_int_equal(edit_rpc->error_opt, error_opt); |
| 143 | assert_string_equal(edit_rpc->edit_cont, edit_content); |
| 144 | } |
| 145 | |
| 146 | |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 147 | static void |
| 148 | test_nc_rpc_edit(void **state) |
| 149 | { |
| 150 | (void)state; |
| 151 | struct nc_rpc *rpc = NULL; |
| 152 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 153 | /* create edit rpc with NC_PARAMTYPE_CONST */ |
| 154 | rpc = nc_rpc_edit(NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, NC_RPC_EDIT_TESTOPT_TESTSET, |
| 155 | NC_RPC_EDIT_ERROPT_STOP, "url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 156 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 157 | check_edit(rpc, NC_DATASTORE_RUNNING, NC_RPC_EDIT_DFLTOP_REPLACE, |
| 158 | NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, "url"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 159 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 160 | |
| 161 | /* create edit rpc with NC_PARAMTYPE_FREE */ |
| 162 | char *str = strdup("string"); |
| 163 | rpc = nc_rpc_edit(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_SET, |
| 164 | NC_RPC_EDIT_ERROPT_ROLLBACK, str, NC_PARAMTYPE_FREE); |
| 165 | assert_non_null(rpc); |
| 166 | check_edit(rpc, NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, |
| 167 | NC_RPC_EDIT_TESTOPT_SET, NC_RPC_EDIT_ERROPT_ROLLBACK, str); |
| 168 | nc_rpc_free(rpc); |
| 169 | |
| 170 | /* create edit rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 171 | rpc = nc_rpc_edit(NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, NC_RPC_EDIT_TESTOPT_TEST, |
| 172 | NC_RPC_EDIT_ERROPT_CONTINUE, "url1", NC_PARAMTYPE_DUP_AND_FREE); |
| 173 | assert_non_null(rpc); |
| 174 | check_edit(rpc, NC_DATASTORE_CONFIG, NC_RPC_EDIT_DFLTOP_NONE, |
| 175 | NC_RPC_EDIT_TESTOPT_TEST, NC_RPC_EDIT_ERROPT_CONTINUE, "url1"); |
| 176 | nc_rpc_free(rpc); |
| 177 | } |
| 178 | |
| 179 | /* function to check if values of copy rpc are set correctly */ |
| 180 | void |
| 181 | check_copy(struct nc_rpc *rpc, NC_DATASTORE target, const char *url_trg, NC_DATASTORE source, |
| 182 | const char *url_or_config_src, NC_WD_MODE wd_mode) |
| 183 | { |
| 184 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COPY); |
| 185 | struct nc_rpc_copy *copy_rpc = (struct nc_rpc_copy *)rpc; |
| 186 | |
| 187 | assert_int_equal(copy_rpc->type, NC_RPC_COPY); |
| 188 | assert_int_equal(copy_rpc->target, target); |
| 189 | assert_string_equal(copy_rpc->url_trg, url_trg); |
| 190 | assert_int_equal(copy_rpc->source, source); |
| 191 | assert_string_equal(copy_rpc->url_config_src, url_or_config_src); |
| 192 | assert_int_equal(copy_rpc->wd_mode, wd_mode); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void |
| 196 | test_nc_rpc_copy(void **state) |
| 197 | { |
| 198 | (void)state; |
| 199 | struct nc_rpc *rpc = NULL; |
| 200 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 201 | /* create copy rpc with NC_PARAMTYPE_CONST */ |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 202 | rpc = nc_rpc_copy(NC_DATASTORE_RUNNING, "target-url", NC_DATASTORE_RUNNING, "src-url", |
| 203 | NC_WD_ALL, NC_PARAMTYPE_CONST); |
| 204 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 205 | 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] | 206 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 207 | |
| 208 | /* create copy rpc with NC_PARAMTYPE_FREE */ |
| 209 | char *target = strdup("target"); |
| 210 | char *src = strdup("src"); |
| 211 | rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, |
| 212 | NC_WD_ALL_TAG, NC_PARAMTYPE_FREE); |
| 213 | assert_non_null(rpc); |
| 214 | check_copy(rpc, NC_DATASTORE_STARTUP, target, NC_DATASTORE_RUNNING, src, NC_WD_ALL_TAG); |
| 215 | nc_rpc_free(rpc); |
| 216 | |
| 217 | /* create copy rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 218 | rpc = nc_rpc_copy(NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url", |
| 219 | NC_WD_TRIM, NC_PARAMTYPE_DUP_AND_FREE); |
| 220 | assert_non_null(rpc); |
| 221 | check_copy(rpc, NC_DATASTORE_STARTUP, "url", NC_DATASTORE_CANDIDATE, "url", NC_WD_TRIM); |
| 222 | nc_rpc_free(rpc); |
| 223 | } |
| 224 | |
| 225 | /* function to check if values of delete rpc are set correctly */ |
| 226 | void |
| 227 | check_delete(struct nc_rpc *rpc, NC_DATASTORE target, const char *url) |
| 228 | { |
| 229 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DELETE); |
| 230 | struct nc_rpc_delete *delete_rpc = (struct nc_rpc_delete *)rpc; |
| 231 | |
| 232 | assert_int_equal(delete_rpc->type, NC_RPC_DELETE); |
| 233 | assert_int_equal(delete_rpc->target, target); |
| 234 | assert_string_equal(delete_rpc->url, url); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void |
| 238 | test_nc_rpc_delete(void **state) |
| 239 | { |
| 240 | (void)state; |
| 241 | struct nc_rpc *rpc = NULL; |
| 242 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 243 | /* create delete rpc with NC_PARAMTYPE_CONST */ |
| 244 | 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] | 245 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 246 | check_delete(rpc, NC_DATASTORE_RUNNING, "target-url"); |
| 247 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 248 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 249 | /* create delete rpc with NC_PARAMTYPE_FREE */ |
| 250 | char *url = strdup("url"); |
| 251 | rpc = nc_rpc_delete(NC_DATASTORE_CANDIDATE, url, NC_PARAMTYPE_FREE); |
| 252 | assert_non_null(rpc); |
| 253 | check_delete(rpc, NC_DATASTORE_CANDIDATE, url); |
| 254 | nc_rpc_free(rpc); |
| 255 | |
| 256 | /* create delete rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 257 | rpc = nc_rpc_delete(NC_DATASTORE_CONFIG, "target", NC_PARAMTYPE_DUP_AND_FREE); |
| 258 | assert_non_null(rpc); |
| 259 | check_delete(rpc, NC_DATASTORE_CONFIG, "target"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 260 | nc_rpc_free(rpc); |
| 261 | } |
| 262 | |
| 263 | static void |
| 264 | test_nc_rpc_lock(void **state) |
| 265 | { |
| 266 | (void)state; |
| 267 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 268 | struct nc_rpc_lock *lock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 269 | |
| 270 | rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
| 271 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 272 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_LOCK); |
| 273 | |
| 274 | lock_rpc = (struct nc_rpc_lock *)rpc; |
| 275 | assert_int_equal(lock_rpc->type, NC_RPC_LOCK); |
| 276 | assert_int_equal(lock_rpc->target, NC_DATASTORE_RUNNING); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 277 | |
| 278 | nc_rpc_free(rpc); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | test_nc_rpc_unlock(void **state) |
| 283 | { |
| 284 | (void)state; |
| 285 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 286 | struct nc_rpc_lock *unlock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 287 | |
| 288 | rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING); |
| 289 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 290 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 291 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 292 | unlock_rpc = (struct nc_rpc_lock *)rpc; |
| 293 | assert_int_equal(unlock_rpc->type, NC_RPC_UNLOCK); |
| 294 | assert_int_equal(unlock_rpc->target, NC_DATASTORE_RUNNING); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 295 | nc_rpc_free(rpc); |
| 296 | } |
| 297 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 298 | /* function to check if values of get rpc are set correctly */ |
| 299 | void |
| 300 | check_get_rpc(struct nc_rpc *rpc, const char *filter, NC_WD_MODE wd_mode) |
| 301 | { |
| 302 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GET); |
| 303 | struct nc_rpc_get *get_rpc = (struct nc_rpc_get *)rpc; |
| 304 | |
| 305 | assert_int_equal(get_rpc->type, NC_RPC_GET); |
| 306 | assert_string_equal(get_rpc->filter, filter); |
| 307 | assert_int_equal(get_rpc->wd_mode, wd_mode); |
| 308 | } |
| 309 | |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 310 | static void |
| 311 | test_nc_rpc_get(void **state) |
| 312 | { |
| 313 | (void)state; |
| 314 | struct nc_rpc *rpc = NULL; |
| 315 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 316 | /* create get rpc with NC_PARAMTYPE_CONST */ |
| 317 | rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 318 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 319 | check_get_rpc(rpc, "filter", NC_WD_ALL); |
| 320 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 321 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 322 | /* create get rpc with NC_PARAMTYPE_FREE */ |
| 323 | char *str = strdup("string"); |
| 324 | rpc = nc_rpc_get(str, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
| 325 | assert_non_null(rpc); |
| 326 | check_get_rpc(rpc, str, NC_WD_EXPLICIT); |
| 327 | nc_rpc_free(rpc); |
| 328 | |
| 329 | /* create get rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 330 | rpc = nc_rpc_get("filter-string", NC_WD_UNKNOWN, NC_PARAMTYPE_DUP_AND_FREE); |
| 331 | assert_non_null(rpc); |
| 332 | check_get_rpc(rpc, "filter-string", NC_WD_UNKNOWN); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 333 | nc_rpc_free(rpc); |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | test_nc_rpc_kill(void **state) |
| 338 | { |
| 339 | (void)state; |
| 340 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 341 | struct nc_rpc_kill *kill_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 342 | |
| 343 | rpc = nc_rpc_kill(10); |
| 344 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 345 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_KILL); |
| 346 | |
| 347 | kill_rpc = (struct nc_rpc_kill *)rpc; |
| 348 | assert_int_equal(kill_rpc->type, NC_RPC_KILL); |
| 349 | assert_int_equal(kill_rpc->sid, 10); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 350 | |
| 351 | nc_rpc_free(rpc); |
| 352 | } |
| 353 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 354 | /* function to check if values of commit rpc are set correctly */ |
| 355 | void |
| 356 | check_commit_rpc(struct nc_rpc *rpc, int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id) |
| 357 | { |
| 358 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_COMMIT); |
| 359 | struct nc_rpc_commit *commit_rpc = (struct nc_rpc_commit *)rpc; |
| 360 | |
| 361 | assert_int_equal(commit_rpc->type, NC_RPC_COMMIT); |
| 362 | assert_int_equal(commit_rpc->confirmed, confirmed); |
| 363 | assert_int_equal(commit_rpc->confirm_timeout, confirm_timeout); |
| 364 | assert_string_equal(commit_rpc->persist, persist); |
| 365 | assert_string_equal(commit_rpc->persist_id, persist_id); |
| 366 | } |
| 367 | |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 368 | static void |
| 369 | test_nc_rpc_commit(void **state) |
| 370 | { |
| 371 | (void)state; |
| 372 | struct nc_rpc *rpc = NULL; |
| 373 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 374 | /* create commit rpc with NC_PARAMTYPE_CONST*/ |
| 375 | 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] | 376 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 377 | check_commit_rpc(rpc, 1, 100, "persist", "persist-id"); |
| 378 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 379 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 380 | /* create commit rpc with NC_PARAMTYPE_FREE*/ |
| 381 | char *str1 = strdup("str1"); |
| 382 | char *str2 = strdup("str2"); |
| 383 | rpc = nc_rpc_commit(2, 5, str1, str2, NC_PARAMTYPE_FREE); |
| 384 | assert_non_null(rpc); |
| 385 | check_commit_rpc(rpc, 2, 5, str1, str2); |
| 386 | nc_rpc_free(rpc); |
| 387 | |
| 388 | /* create commit rpc with NC_PARAMTYPE_DUP_AND_FREE*/ |
| 389 | rpc = nc_rpc_commit(10, 200, "persistent", "persistent-id", NC_PARAMTYPE_DUP_AND_FREE); |
| 390 | assert_non_null(rpc); |
| 391 | check_commit_rpc(rpc, 10, 200, "persistent", "persistent-id"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 392 | nc_rpc_free(rpc); |
| 393 | } |
| 394 | |
| 395 | static void |
| 396 | test_nc_rpc_discard(void **state) |
| 397 | { |
| 398 | (void)state; |
| 399 | struct nc_rpc *rpc = NULL; |
| 400 | |
| 401 | rpc = nc_rpc_discard(); |
| 402 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 403 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 404 | |
| 405 | nc_rpc_free(rpc); |
| 406 | } |
| 407 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 408 | /* function to check if values of cancel rpc are set correctly */ |
| 409 | void |
| 410 | check_cancel_rpc(struct nc_rpc *rpc, const char *persist_id) |
| 411 | { |
| 412 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_CANCEL); |
| 413 | struct nc_rpc_cancel *cancel_rpc = (struct nc_rpc_cancel *)rpc; |
| 414 | |
| 415 | assert_int_equal(cancel_rpc->type, NC_RPC_CANCEL); |
| 416 | assert_string_equal(cancel_rpc->persist_id, persist_id); |
| 417 | } |
| 418 | |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 419 | static void |
| 420 | test_nc_rpc_cancel(void **state) |
| 421 | { |
| 422 | (void)state; |
| 423 | struct nc_rpc *rpc = NULL; |
| 424 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 425 | /* create cancel rpc with NC_PARAMTYPE_CONST*/ |
| 426 | rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 427 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 428 | check_cancel_rpc(rpc, "persist-id"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 429 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 430 | |
| 431 | /* create cancel rpc with NC_PARAMTYPE_FREE*/ |
| 432 | char *str = strdup("string"); |
| 433 | rpc = nc_rpc_cancel(str, NC_PARAMTYPE_FREE); |
| 434 | assert_non_null(rpc); |
| 435 | check_cancel_rpc(rpc, str); |
| 436 | nc_rpc_free(rpc); |
| 437 | |
| 438 | /* create cancel rpc with NC_PARAMTYPE_DUP_AND_FREE*/ |
| 439 | rpc = nc_rpc_cancel("id", NC_PARAMTYPE_DUP_AND_FREE); |
| 440 | assert_non_null(rpc); |
| 441 | check_cancel_rpc(rpc, "id"); |
| 442 | nc_rpc_free(rpc); |
| 443 | } |
| 444 | |
| 445 | /* function to check if values of validate rpc are set correctly */ |
| 446 | void |
| 447 | check_validate_rpc(struct nc_rpc *rpc, NC_DATASTORE source, const char *url_or_config) |
| 448 | { |
| 449 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_VALIDATE); |
| 450 | struct nc_rpc_validate *validate_rpc = (struct nc_rpc_validate *)rpc; |
| 451 | |
| 452 | assert_int_equal(validate_rpc->type, NC_RPC_VALIDATE); |
| 453 | assert_int_equal(validate_rpc->source, source); |
| 454 | assert_string_equal(validate_rpc->url_config_src, url_or_config); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static void |
| 458 | test_nc_rpc_validate(void **state) |
| 459 | { |
| 460 | (void)state; |
| 461 | struct nc_rpc *rpc = NULL; |
| 462 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 463 | /* create validate rpc with NC_PARAMTYPE_CONST */ |
| 464 | rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 465 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 466 | check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 467 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 468 | |
| 469 | /* create validate rpc with NC_PARAMTYPE_FREE */ |
| 470 | char *str = strdup("string"); |
| 471 | rpc = nc_rpc_validate(NC_DATASTORE_CANDIDATE, str, NC_PARAMTYPE_FREE); |
| 472 | assert_non_null(rpc); |
| 473 | check_validate_rpc(rpc, NC_DATASTORE_CANDIDATE, str); |
| 474 | nc_rpc_free(rpc); |
| 475 | |
| 476 | /* create validate rpc with NC_PARAMTYPE_DUP_AND_FREE */ |
| 477 | rpc = nc_rpc_validate(NC_DATASTORE_CONFIG, "url1", NC_PARAMTYPE_DUP_AND_FREE); |
| 478 | assert_non_null(rpc); |
| 479 | check_validate_rpc(rpc, NC_DATASTORE_CONFIG, "url1"); |
| 480 | nc_rpc_free(rpc); |
| 481 | } |
| 482 | |
| 483 | /* function to check if values of getschema rpc are set correctly */ |
| 484 | void |
| 485 | check_getschema_rpc(struct nc_rpc *rpc, const char *identifier, const char *version, const char *format) |
| 486 | { |
| 487 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_GETSCHEMA); |
| 488 | struct nc_rpc_getschema *getchema_rpc = (struct nc_rpc_getschema *)rpc; |
| 489 | |
| 490 | assert_int_equal(getchema_rpc->type, NC_RPC_GETSCHEMA); |
| 491 | assert_string_equal(getchema_rpc->identifier, identifier); |
| 492 | assert_string_equal(getchema_rpc->version, version); |
| 493 | assert_string_equal(getchema_rpc->format, format); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void |
| 497 | test_nc_rpc_getschema(void **state) |
| 498 | { |
| 499 | (void)state; |
| 500 | struct nc_rpc *rpc = NULL; |
| 501 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 502 | /* create getchema with NC_PARAMTYPE_CONST*/ |
| 503 | rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 504 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 505 | check_getschema_rpc(rpc, "id", "version", "format"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 506 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 507 | |
| 508 | /* create getchema with NC_PARAMTYPE_FREE*/ |
| 509 | char *str1 = strdup("str1"); |
| 510 | char *str2 = strdup("str2"); |
| 511 | char *str3 = strdup("str3"); |
| 512 | rpc = nc_rpc_getschema(str1, str2, str3, NC_PARAMTYPE_FREE); |
| 513 | assert_non_null(rpc); |
| 514 | check_getschema_rpc(rpc, str1, str2, str3); |
| 515 | nc_rpc_free(rpc); |
| 516 | |
| 517 | /* create getchema with NC_PARAMTYPE_DUP_AND_FREE*/ |
| 518 | rpc = nc_rpc_getschema("id1", "version1", "format1", NC_PARAMTYPE_DUP_AND_FREE); |
| 519 | assert_non_null(rpc); |
| 520 | check_getschema_rpc(rpc, "id1", "version1", "format1"); |
| 521 | nc_rpc_free(rpc); |
| 522 | } |
| 523 | |
| 524 | /* function to check if values of subscribe rpc are set correctly */ |
| 525 | void |
| 526 | check_subscribe_rpc(struct nc_rpc *rpc, const char *stream_name, const char *filter, |
| 527 | const char *start_time, const char *stop_time) |
| 528 | { |
| 529 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_SUBSCRIBE); |
| 530 | struct nc_rpc_subscribe *subscribe_rpc = (struct nc_rpc_subscribe *)rpc; |
| 531 | |
| 532 | assert_int_equal(subscribe_rpc->type, NC_RPC_SUBSCRIBE); |
| 533 | assert_string_equal(subscribe_rpc->stream, stream_name); |
| 534 | assert_string_equal(subscribe_rpc->filter, filter); |
| 535 | assert_string_equal(subscribe_rpc->start, start_time); |
| 536 | assert_string_equal(subscribe_rpc->stop, stop_time); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void |
| 540 | test_nc_rpc_subscribe(void **state) |
| 541 | { |
| 542 | (void)state; |
| 543 | struct nc_rpc *rpc = NULL; |
| 544 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 545 | /* create subscribe rpc with NC_PARAMTYPE_CONST*/ |
| 546 | 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] | 547 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 548 | check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time"); |
| 549 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 550 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 551 | /* create subscribe rpc with NC_PARAMTYPE_FREE*/ |
| 552 | char *str1 = strdup("str1"); |
| 553 | char *str2 = strdup("str2"); |
| 554 | char *str3 = strdup("str3"); |
| 555 | char *str4 = strdup("str4"); |
| 556 | rpc = nc_rpc_subscribe(str1, str2, str3, str4, NC_PARAMTYPE_FREE); |
| 557 | assert_non_null(rpc); |
| 558 | check_subscribe_rpc(rpc, str1, str2, str3, str4); |
| 559 | nc_rpc_free(rpc); |
| 560 | |
| 561 | /* create subscribe rpc with NC_PARAMTYPE_DUP_AND_FREE*/ |
| 562 | rpc = nc_rpc_subscribe("name", "filter-str", "start", "stop", NC_PARAMTYPE_DUP_AND_FREE); |
| 563 | assert_non_null(rpc); |
| 564 | check_subscribe_rpc(rpc, "name", "filter-str", "start", "stop"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 565 | nc_rpc_free(rpc); |
| 566 | } |
| 567 | |
| 568 | int |
| 569 | main(void) |
| 570 | { |
| 571 | const struct CMUnitTest tests[] = { |
David Sedlák | 2f2f2e3 | 2018-10-08 21:49:32 +0200 | [diff] [blame] | 572 | cmocka_unit_test_setup_teardown(test_nc_rpc_act_generic_xml, setup_f, teardown_f), |
| 573 | 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] | 574 | cmocka_unit_test_setup_teardown(test_nc_rpc_getconfig, setup_f, teardown_f), |
| 575 | cmocka_unit_test_setup_teardown(test_nc_rpc_edit, setup_f, teardown_f), |
| 576 | cmocka_unit_test_setup_teardown(test_nc_rpc_copy, setup_f, teardown_f), |
| 577 | cmocka_unit_test_setup_teardown(test_nc_rpc_delete, setup_f, teardown_f), |
| 578 | cmocka_unit_test_setup_teardown(test_nc_rpc_lock, setup_f, teardown_f), |
| 579 | cmocka_unit_test_setup_teardown(test_nc_rpc_unlock, setup_f, teardown_f), |
| 580 | cmocka_unit_test_setup_teardown(test_nc_rpc_get, setup_f, teardown_f), |
| 581 | cmocka_unit_test_setup_teardown(test_nc_rpc_kill, setup_f, teardown_f), |
| 582 | cmocka_unit_test_setup_teardown(test_nc_rpc_commit, setup_f, teardown_f), |
| 583 | cmocka_unit_test_setup_teardown(test_nc_rpc_discard, setup_f, teardown_f), |
| 584 | cmocka_unit_test_setup_teardown(test_nc_rpc_cancel, setup_f, teardown_f), |
| 585 | cmocka_unit_test_setup_teardown(test_nc_rpc_validate, setup_f, teardown_f), |
| 586 | cmocka_unit_test_setup_teardown(test_nc_rpc_getschema, setup_f, teardown_f), |
| 587 | cmocka_unit_test_setup_teardown(test_nc_rpc_subscribe, setup_f, teardown_f), |
| 588 | }; |
| 589 | |
| 590 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 591 | } |