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 | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 35 | /* function to check if values of getconfig rpc are set correctly */ |
| 36 | void |
| 37 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 48 | static void |
| 49 | test_nc_rpc_getconfig(void **state) |
| 50 | { |
| 51 | (void)state; |
| 52 | struct nc_rpc *rpc = NULL; |
| 53 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 54 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 56 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 57 | check_getconfig(rpc, NC_DATASTORE_CANDIDATE, "filter-string", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 58 | nc_rpc_free(rpc); |
| 59 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 60 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 72 | } |
| 73 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 74 | /* function to check if values of edit rpc are set correctly */ |
| 75 | void |
| 76 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 91 | static void |
| 92 | test_nc_rpc_edit(void **state) |
| 93 | { |
| 94 | (void)state; |
| 95 | struct nc_rpc *rpc = NULL; |
| 96 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 97 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 100 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 101 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 103 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 104 | |
| 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 */ |
| 124 | void |
| 125 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void |
| 140 | test_nc_rpc_copy(void **state) |
| 141 | { |
| 142 | (void)state; |
| 143 | struct nc_rpc *rpc = NULL; |
| 144 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 145 | /* create copy rpc with NC_PARAMTYPE_CONST */ |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 146 | 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ák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 149 | 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] | 150 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 151 | |
| 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 */ |
| 170 | void |
| 171 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void |
| 182 | test_nc_rpc_delete(void **state) |
| 183 | { |
| 184 | (void)state; |
| 185 | struct nc_rpc *rpc = NULL; |
| 186 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 187 | /* create delete rpc with NC_PARAMTYPE_CONST */ |
| 188 | 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] | 189 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 190 | check_delete(rpc, NC_DATASTORE_RUNNING, "target-url"); |
| 191 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 192 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 193 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 204 | nc_rpc_free(rpc); |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | test_nc_rpc_lock(void **state) |
| 209 | { |
| 210 | (void)state; |
| 211 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 212 | struct nc_rpc_lock *lock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 213 | |
| 214 | rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
| 215 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 216 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 221 | |
| 222 | nc_rpc_free(rpc); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | test_nc_rpc_unlock(void **state) |
| 227 | { |
| 228 | (void)state; |
| 229 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 230 | struct nc_rpc_lock *unlock_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 231 | |
| 232 | rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING); |
| 233 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 234 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_UNLOCK); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 235 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 236 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 239 | nc_rpc_free(rpc); |
| 240 | } |
| 241 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 242 | /* function to check if values of get rpc are set correctly */ |
| 243 | void |
| 244 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 254 | static void |
| 255 | test_nc_rpc_get(void **state) |
| 256 | { |
| 257 | (void)state; |
| 258 | struct nc_rpc *rpc = NULL; |
| 259 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 260 | /* create get rpc with NC_PARAMTYPE_CONST */ |
| 261 | rpc = nc_rpc_get("filter", NC_WD_ALL, NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 262 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 263 | check_get_rpc(rpc, "filter", NC_WD_ALL); |
| 264 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 265 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 266 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 277 | nc_rpc_free(rpc); |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | test_nc_rpc_kill(void **state) |
| 282 | { |
| 283 | (void)state; |
| 284 | struct nc_rpc *rpc = NULL; |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 285 | struct nc_rpc_kill *kill_rpc = NULL; |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 286 | |
| 287 | rpc = nc_rpc_kill(10); |
| 288 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 289 | 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 294 | |
| 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 commit rpc are set correctly */ |
| 299 | void |
| 300 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 312 | static void |
| 313 | test_nc_rpc_commit(void **state) |
| 314 | { |
| 315 | (void)state; |
| 316 | struct nc_rpc *rpc = NULL; |
| 317 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 318 | /* create commit rpc with NC_PARAMTYPE_CONST*/ |
| 319 | 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] | 320 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 321 | check_commit_rpc(rpc, 1, 100, "persist", "persist-id"); |
| 322 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 323 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 324 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 336 | nc_rpc_free(rpc); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | test_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ák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 347 | assert_int_equal(nc_rpc_get_type(rpc), NC_RPC_DISCARD); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 348 | |
| 349 | nc_rpc_free(rpc); |
| 350 | } |
| 351 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 352 | /* function to check if values of cancel rpc are set correctly */ |
| 353 | void |
| 354 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 363 | static void |
| 364 | test_nc_rpc_cancel(void **state) |
| 365 | { |
| 366 | (void)state; |
| 367 | struct nc_rpc *rpc = NULL; |
| 368 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 369 | /* create cancel rpc with NC_PARAMTYPE_CONST*/ |
| 370 | rpc = nc_rpc_cancel("persist-id", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 371 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 372 | check_cancel_rpc(rpc, "persist-id"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 373 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 374 | |
| 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 */ |
| 390 | void |
| 391 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void |
| 402 | test_nc_rpc_validate(void **state) |
| 403 | { |
| 404 | (void)state; |
| 405 | struct nc_rpc *rpc = NULL; |
| 406 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 407 | /* create validate rpc with NC_PARAMTYPE_CONST */ |
| 408 | rpc = nc_rpc_validate(NC_DATASTORE_RUNNING, "url", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 409 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 410 | check_validate_rpc(rpc, NC_DATASTORE_RUNNING, "url"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 411 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 412 | |
| 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 */ |
| 428 | void |
| 429 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void |
| 441 | test_nc_rpc_getschema(void **state) |
| 442 | { |
| 443 | (void)state; |
| 444 | struct nc_rpc *rpc = NULL; |
| 445 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 446 | /* create getchema with NC_PARAMTYPE_CONST*/ |
| 447 | rpc = nc_rpc_getschema("id", "version", "format", NC_PARAMTYPE_CONST); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 448 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 449 | check_getschema_rpc(rpc, "id", "version", "format"); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 450 | nc_rpc_free(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 451 | |
| 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 */ |
| 469 | void |
| 470 | check_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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | test_nc_rpc_subscribe(void **state) |
| 485 | { |
| 486 | (void)state; |
| 487 | struct nc_rpc *rpc = NULL; |
| 488 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 489 | /* create subscribe rpc with NC_PARAMTYPE_CONST*/ |
| 490 | 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] | 491 | assert_non_null(rpc); |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 492 | check_subscribe_rpc(rpc, "stream-name", "filter", "start-time", "stop-time"); |
| 493 | nc_rpc_free(rpc); |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 494 | |
David Sedlák | 3983ed0 | 2018-10-07 23:17:19 +0200 | [diff] [blame] | 495 | /* 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ák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 509 | nc_rpc_free(rpc); |
| 510 | } |
| 511 | |
| 512 | int |
| 513 | main(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 | } |