| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <math.h> |
| 28 |
#include "tests.h" |
| 29 |
|
| 30 |
static pool *p = NULL; |
| 31 |
|
| 32 |
static void set_up(void) { |
| 33 |
if (p == NULL) { |
| 34 |
p = make_sub_pool(NULL); |
| 35 |
} |
| 36 |
|
| 37 |
init_json(); |
| 38 |
|
| 39 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 40 |
pr_trace_set_levels("json", 1, 20); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
static void tear_down(void) { |
| 45 |
if (getenv("TEST_VERBOSE") != NULL) { |
| 46 |
pr_trace_set_levels("json", 0, 0); |
| 47 |
} |
| 48 |
|
| 49 |
finish_json(); |
| 50 |
|
| 51 |
if (p != NULL) { |
| 52 |
destroy_pool(p); |
| 53 |
p = NULL; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
START_TEST (json_object_free_test) { |
| 58 |
int res; |
| 59 |
|
| 60 |
mark_point(); |
| 61 |
res = pr_json_object_free(NULL); |
| 62 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 63 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 64 |
strerror(errno), errno); |
| 65 |
} |
| 66 |
END_TEST |
| 67 |
|
| 68 |
START_TEST (json_object_alloc_test) { |
| 69 |
int res; |
| 70 |
pr_json_object_t *json; |
| 71 |
|
| 72 |
mark_point(); |
| 73 |
json = pr_json_object_alloc(NULL); |
| 74 |
ck_assert_msg(json == NULL, "Failed to handle null pool"); |
| 75 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 76 |
strerror(errno), errno); |
| 77 |
|
| 78 |
mark_point(); |
| 79 |
json = pr_json_object_alloc(p); |
| 80 |
ck_assert_msg(json != NULL, "Failed to allocate object: %s", strerror(errno)); |
| 81 |
|
| 82 |
mark_point(); |
| 83 |
res = pr_json_object_free(json); |
| 84 |
ck_assert_msg(res == 0, "Failed to free object: %s", strerror(errno)); |
| 85 |
} |
| 86 |
END_TEST |
| 87 |
|
| 88 |
START_TEST (json_object_from_text_test) { |
| 89 |
pr_json_object_t *json; |
| 90 |
const char *text; |
| 91 |
|
| 92 |
mark_point(); |
| 93 |
json = pr_json_object_from_text(NULL, NULL); |
| 94 |
ck_assert_msg(json == NULL, "Failed to handle null pool"); |
| 95 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 96 |
strerror(errno), errno); |
| 97 |
|
| 98 |
mark_point(); |
| 99 |
json = pr_json_object_from_text(p, NULL); |
| 100 |
ck_assert_msg(json == NULL, "Failed to handle null text"); |
| 101 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 102 |
strerror(errno), errno); |
| 103 |
|
| 104 |
text = "foo bar"; |
| 105 |
|
| 106 |
mark_point(); |
| 107 |
json = pr_json_object_from_text(p, text); |
| 108 |
ck_assert_msg(json == NULL, "Failed to handle invalid text '%s'", text); |
| 109 |
ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM, |
| 110 |
strerror(errno), errno); |
| 111 |
|
| 112 |
text = "[\"foo\",\"bar\"]"; |
| 113 |
|
| 114 |
mark_point(); |
| 115 |
json = pr_json_object_from_text(p, text); |
| 116 |
ck_assert_msg(json == NULL, "Failed to handle non-object text '%s'", text); |
| 117 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 118 |
strerror(errno), errno); |
| 119 |
|
| 120 |
text = "{\"foo\":\"bar\"}"; |
| 121 |
|
| 122 |
mark_point(); |
| 123 |
json = pr_json_object_from_text(p, text); |
| 124 |
ck_assert_msg(json != NULL, "Failed to handle text '%s': %s", text, |
| 125 |
strerror(errno)); |
| 126 |
(void) pr_json_object_free(json); |
| 127 |
|
| 128 |
mark_point(); |
| 129 |
text = "{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":{\"key\":null}}}}}}}}}}}}}}}}}"; |
| 130 |
json = pr_json_object_from_text(p, text); |
| 131 |
ck_assert_msg(json == NULL, "Failed to handle depth-exceeding text '%s'", text); |
| 132 |
} |
| 133 |
END_TEST |
| 134 |
|
| 135 |
START_TEST (json_object_to_text_test) { |
| 136 |
const char *text, *expected; |
| 137 |
pr_json_object_t *json; |
| 138 |
|
| 139 |
mark_point(); |
| 140 |
text = pr_json_object_to_text(NULL, NULL, NULL); |
| 141 |
ck_assert_msg(text == NULL, "Failed to handle null pool"); |
| 142 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 143 |
strerror(errno), errno); |
| 144 |
|
| 145 |
mark_point(); |
| 146 |
text = pr_json_object_to_text(p, NULL, NULL); |
| 147 |
ck_assert_msg(text == NULL, "Failed to handle null json"); |
| 148 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 149 |
strerror(errno), errno); |
| 150 |
|
| 151 |
json = pr_json_object_alloc(p); |
| 152 |
|
| 153 |
mark_point(); |
| 154 |
text = pr_json_object_to_text(p, json, NULL); |
| 155 |
ck_assert_msg(text == NULL, "Failed to handle null indent"); |
| 156 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 157 |
strerror(errno), errno); |
| 158 |
|
| 159 |
expected = "{}"; |
| 160 |
|
| 161 |
mark_point(); |
| 162 |
text = pr_json_object_to_text(p, json, ""); |
| 163 |
ck_assert_msg(text != NULL, "Failed to get text for object: %s", |
| 164 |
strerror(errno)); |
| 165 |
ck_assert_msg(strcmp(text, expected) == 0, "Expected '%s', got '%s'", expected, |
| 166 |
text); |
| 167 |
|
| 168 |
(void) pr_json_object_set_string(p, json, "foo", "bar"); |
| 169 |
expected = "{\"foo\":\"bar\"}"; |
| 170 |
|
| 171 |
mark_point(); |
| 172 |
text = pr_json_object_to_text(p, json, ""); |
| 173 |
ck_assert_msg(text != NULL, "Failed to get text for object: %s", |
| 174 |
strerror(errno)); |
| 175 |
ck_assert_msg(strcmp(text, expected) == 0, "Expected '%s', got '%s'", expected, |
| 176 |
text); |
| 177 |
|
| 178 |
(void) pr_json_object_free(json); |
| 179 |
} |
| 180 |
END_TEST |
| 181 |
|
| 182 |
static int object_item_ok(const char *key, int val_type, const void *val, |
| 183 |
size_t valsz, void *user_data) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int object_item_fail(const char *key, int val_type, const void *val, |
| 188 |
size_t valsz, void *user_data) { |
| 189 |
errno = EPERM; |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
START_TEST(json_object_foreach_test) { |
| 194 |
int res; |
| 195 |
pr_json_object_t *json = NULL; |
| 196 |
const char *text; |
| 197 |
|
| 198 |
mark_point(); |
| 199 |
res = pr_json_object_foreach(NULL, NULL, NULL, NULL); |
| 200 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 201 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 202 |
strerror(errno), errno); |
| 203 |
|
| 204 |
mark_point(); |
| 205 |
res = pr_json_object_foreach(p, NULL, NULL, NULL); |
| 206 |
ck_assert_msg(res < 0, "Failed to handle null object"); |
| 207 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 208 |
strerror(errno), errno); |
| 209 |
|
| 210 |
json = pr_json_object_alloc(p); |
| 211 |
|
| 212 |
mark_point(); |
| 213 |
res = pr_json_object_foreach(p, json, NULL, NULL); |
| 214 |
ck_assert_msg(res < 0, "Failed to handle null callback"); |
| 215 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 216 |
strerror(errno), errno); |
| 217 |
|
| 218 |
(void) pr_json_object_free(json); |
| 219 |
|
| 220 |
text = "{\"foo\":true,\"bar\":false,\"baz\":{}}"; |
| 221 |
json = pr_json_object_from_text(p, text); |
| 222 |
|
| 223 |
mark_point(); |
| 224 |
res = pr_json_object_foreach(p, json, object_item_ok, NULL); |
| 225 |
ck_assert_msg(res == 0, "Failed to iterate over object: %s", strerror(errno)); |
| 226 |
|
| 227 |
mark_point(); |
| 228 |
res = pr_json_object_foreach(p, json, object_item_fail, NULL); |
| 229 |
ck_assert_msg(res < 0, "Failing callback succeeded unexpectedly"); |
| 230 |
ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM, |
| 231 |
strerror(errno), errno); |
| 232 |
|
| 233 |
(void) pr_json_object_free(json); |
| 234 |
} |
| 235 |
END_TEST |
| 236 |
|
| 237 |
START_TEST (json_object_count_test) { |
| 238 |
int res; |
| 239 |
pr_json_object_t *json; |
| 240 |
const char *text; |
| 241 |
|
| 242 |
mark_point(); |
| 243 |
res = pr_json_object_count(NULL); |
| 244 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 245 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 246 |
strerror(errno), errno); |
| 247 |
|
| 248 |
json = pr_json_object_alloc(p); |
| 249 |
|
| 250 |
mark_point(); |
| 251 |
res = pr_json_object_count(json); |
| 252 |
ck_assert_msg(res == 0, "Expected 0, got %d", res); |
| 253 |
|
| 254 |
(void) pr_json_object_free(json); |
| 255 |
|
| 256 |
text = "{\"foo\":true,\"bar\":false,\"baz\":1}"; |
| 257 |
json = pr_json_object_from_text(p, text); |
| 258 |
|
| 259 |
mark_point(); |
| 260 |
res = pr_json_object_count(json); |
| 261 |
ck_assert_msg(res == 3, "Expected 3, got %d", res); |
| 262 |
|
| 263 |
(void) pr_json_object_free(json); |
| 264 |
} |
| 265 |
END_TEST |
| 266 |
|
| 267 |
START_TEST (json_object_exists_test) { |
| 268 |
int res; |
| 269 |
pr_json_object_t *json; |
| 270 |
const char *key, *text; |
| 271 |
|
| 272 |
mark_point(); |
| 273 |
res = pr_json_object_exists(NULL, NULL); |
| 274 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 275 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 276 |
strerror(errno), errno); |
| 277 |
|
| 278 |
json = pr_json_object_alloc(p); |
| 279 |
|
| 280 |
mark_point(); |
| 281 |
res = pr_json_object_exists(json, NULL); |
| 282 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 283 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 284 |
strerror(errno), errno); |
| 285 |
|
| 286 |
key = "foo"; |
| 287 |
|
| 288 |
mark_point(); |
| 289 |
res = pr_json_object_exists(json, key); |
| 290 |
ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res); |
| 291 |
|
| 292 |
(void) pr_json_object_free(json); |
| 293 |
|
| 294 |
text = "{\"foo\":true,\"bar\":false,\"baz\":1}"; |
| 295 |
json = pr_json_object_from_text(p, text); |
| 296 |
|
| 297 |
mark_point(); |
| 298 |
res = pr_json_object_exists(json, key); |
| 299 |
ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res); |
| 300 |
|
| 301 |
(void) pr_json_object_free(json); |
| 302 |
} |
| 303 |
END_TEST |
| 304 |
|
| 305 |
START_TEST (json_object_remove_test) { |
| 306 |
int res; |
| 307 |
pr_json_object_t *json; |
| 308 |
const char *key, *text; |
| 309 |
|
| 310 |
mark_point(); |
| 311 |
res = pr_json_object_remove(NULL, NULL); |
| 312 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 313 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 314 |
strerror(errno), errno); |
| 315 |
|
| 316 |
json = pr_json_object_alloc(p); |
| 317 |
|
| 318 |
mark_point(); |
| 319 |
res = pr_json_object_remove(json, NULL); |
| 320 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 321 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 322 |
strerror(errno), errno); |
| 323 |
|
| 324 |
key = "foo"; |
| 325 |
|
| 326 |
mark_point(); |
| 327 |
res = pr_json_object_remove(json, key); |
| 328 |
ck_assert_msg(res == 0, "Failed to remove nonexistent key '%s': %s", key, |
| 329 |
strerror(errno)); |
| 330 |
|
| 331 |
res = pr_json_object_count(json); |
| 332 |
ck_assert_msg(res == 0, "Expected count 0, got %d", res); |
| 333 |
|
| 334 |
(void) pr_json_object_free(json); |
| 335 |
|
| 336 |
text = "{\"foo\":true,\"bar\":false,\"baz\":1}"; |
| 337 |
json = pr_json_object_from_text(p, text); |
| 338 |
|
| 339 |
mark_point(); |
| 340 |
res = pr_json_object_remove(json, key); |
| 341 |
ck_assert_msg(res == 0, "Failed to remove existing key '%s': %s", key, |
| 342 |
strerror(errno)); |
| 343 |
|
| 344 |
res = pr_json_object_count(json); |
| 345 |
ck_assert_msg(res == 2, "Expected count 2, got %d", res); |
| 346 |
|
| 347 |
mark_point(); |
| 348 |
res = pr_json_object_exists(json, key); |
| 349 |
ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res); |
| 350 |
|
| 351 |
(void) pr_json_object_free(json); |
| 352 |
} |
| 353 |
END_TEST |
| 354 |
|
| 355 |
START_TEST(json_object_get_bool_test) { |
| 356 |
int res, val; |
| 357 |
const char *key, *text; |
| 358 |
pr_json_object_t *json; |
| 359 |
|
| 360 |
mark_point(); |
| 361 |
res = pr_json_object_get_bool(NULL, NULL, NULL, NULL); |
| 362 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 363 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 364 |
strerror(errno), errno); |
| 365 |
|
| 366 |
mark_point(); |
| 367 |
res = pr_json_object_get_bool(p, NULL, NULL, NULL); |
| 368 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 369 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 370 |
strerror(errno), errno); |
| 371 |
|
| 372 |
json = pr_json_object_alloc(p); |
| 373 |
|
| 374 |
mark_point(); |
| 375 |
res = pr_json_object_get_bool(p, json, NULL, NULL); |
| 376 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 377 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 378 |
strerror(errno), errno); |
| 379 |
|
| 380 |
key = "foo"; |
| 381 |
|
| 382 |
mark_point(); |
| 383 |
res = pr_json_object_get_bool(p, json, key, NULL); |
| 384 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 385 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 386 |
strerror(errno), errno); |
| 387 |
|
| 388 |
mark_point(); |
| 389 |
res = pr_json_object_get_bool(p, json, key, &val); |
| 390 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 391 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 392 |
strerror(errno), errno); |
| 393 |
|
| 394 |
(void) pr_json_object_free(json); |
| 395 |
|
| 396 |
text = "{\"foo\":1,\"bar\":true}"; |
| 397 |
json = pr_json_object_from_text(p, text); |
| 398 |
|
| 399 |
mark_point(); |
| 400 |
res = pr_json_object_get_bool(p, json, key, &val); |
| 401 |
ck_assert_msg(res < 0, "Failed to handle non-boolean key '%s'", key); |
| 402 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 403 |
strerror(errno), errno); |
| 404 |
|
| 405 |
key = "bar"; |
| 406 |
|
| 407 |
mark_point(); |
| 408 |
res = pr_json_object_get_bool(p, json, key, &val); |
| 409 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 410 |
strerror(errno)); |
| 411 |
ck_assert_msg(val == TRUE, "Expected TRUE, got %d", val); |
| 412 |
|
| 413 |
(void) pr_json_object_free(json); |
| 414 |
} |
| 415 |
END_TEST |
| 416 |
|
| 417 |
START_TEST(json_object_set_bool_test) { |
| 418 |
int res, val = TRUE; |
| 419 |
const char *key; |
| 420 |
pr_json_object_t *json; |
| 421 |
|
| 422 |
mark_point(); |
| 423 |
res = pr_json_object_set_bool(NULL, NULL, NULL, 0); |
| 424 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 425 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 426 |
strerror(errno), errno); |
| 427 |
|
| 428 |
mark_point(); |
| 429 |
res = pr_json_object_set_bool(p, NULL, NULL, 0); |
| 430 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 431 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 432 |
strerror(errno), errno); |
| 433 |
|
| 434 |
json = pr_json_object_alloc(p); |
| 435 |
|
| 436 |
mark_point(); |
| 437 |
res = pr_json_object_set_bool(p, json, NULL, 0); |
| 438 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 439 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 440 |
strerror(errno), errno); |
| 441 |
|
| 442 |
key = "foo"; |
| 443 |
|
| 444 |
mark_point(); |
| 445 |
res = pr_json_object_set_bool(p, json, key, val); |
| 446 |
ck_assert_msg(res == 0, "Failed to set key '%s' to %d: %s", key, val, |
| 447 |
strerror(errno)); |
| 448 |
|
| 449 |
val = FALSE; |
| 450 |
|
| 451 |
mark_point(); |
| 452 |
res = pr_json_object_get_bool(p, json, key, &val); |
| 453 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 454 |
strerror(errno)); |
| 455 |
ck_assert_msg(val == TRUE, "Expected TRUE, got %d", val); |
| 456 |
|
| 457 |
(void) pr_json_object_free(json); |
| 458 |
} |
| 459 |
END_TEST |
| 460 |
|
| 461 |
START_TEST(json_object_get_null_test) { |
| 462 |
int res; |
| 463 |
const char *key, *text; |
| 464 |
pr_json_object_t *json; |
| 465 |
|
| 466 |
mark_point(); |
| 467 |
res = pr_json_object_get_null(NULL, NULL, NULL); |
| 468 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 469 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 470 |
strerror(errno), errno); |
| 471 |
|
| 472 |
mark_point(); |
| 473 |
res = pr_json_object_get_null(p, NULL, NULL); |
| 474 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 475 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 476 |
strerror(errno), errno); |
| 477 |
|
| 478 |
json = pr_json_object_alloc(p); |
| 479 |
|
| 480 |
mark_point(); |
| 481 |
res = pr_json_object_get_null(p, json, NULL); |
| 482 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 483 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 484 |
strerror(errno), errno); |
| 485 |
|
| 486 |
key = "foo"; |
| 487 |
|
| 488 |
mark_point(); |
| 489 |
res = pr_json_object_get_null(p, json, key); |
| 490 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 491 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 492 |
strerror(errno), errno); |
| 493 |
|
| 494 |
(void) pr_json_object_free(json); |
| 495 |
|
| 496 |
text = "{\"foo\":1,\"bar\":null}"; |
| 497 |
json = pr_json_object_from_text(p, text); |
| 498 |
|
| 499 |
mark_point(); |
| 500 |
res = pr_json_object_get_null(p, json, key); |
| 501 |
ck_assert_msg(res < 0, "Failed to handle non-null key '%s'", key); |
| 502 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 503 |
strerror(errno), errno); |
| 504 |
|
| 505 |
key = "bar"; |
| 506 |
|
| 507 |
mark_point(); |
| 508 |
res = pr_json_object_get_null(p, json, key); |
| 509 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 510 |
strerror(errno)); |
| 511 |
|
| 512 |
(void) pr_json_object_free(json); |
| 513 |
} |
| 514 |
END_TEST |
| 515 |
|
| 516 |
START_TEST(json_object_set_null_test) { |
| 517 |
int res; |
| 518 |
const char *key; |
| 519 |
pr_json_object_t *json; |
| 520 |
|
| 521 |
mark_point(); |
| 522 |
res = pr_json_object_set_null(NULL, NULL, NULL); |
| 523 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 524 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 525 |
strerror(errno), errno); |
| 526 |
|
| 527 |
mark_point(); |
| 528 |
res = pr_json_object_set_null(p, NULL, NULL); |
| 529 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 530 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 531 |
strerror(errno), errno); |
| 532 |
|
| 533 |
json = pr_json_object_alloc(p); |
| 534 |
|
| 535 |
mark_point(); |
| 536 |
res = pr_json_object_set_null(p, json, NULL); |
| 537 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 538 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 539 |
strerror(errno), errno); |
| 540 |
|
| 541 |
key = "foo"; |
| 542 |
|
| 543 |
mark_point(); |
| 544 |
res = pr_json_object_set_null(p, json, key); |
| 545 |
ck_assert_msg(res == 0, "Failed to set key '%s': %s", key, strerror(errno)); |
| 546 |
|
| 547 |
mark_point(); |
| 548 |
res = pr_json_object_get_null(p, json, key); |
| 549 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 550 |
strerror(errno)); |
| 551 |
|
| 552 |
(void) pr_json_object_free(json); |
| 553 |
} |
| 554 |
END_TEST |
| 555 |
|
| 556 |
START_TEST(json_object_get_number_test) { |
| 557 |
int res; |
| 558 |
double val; |
| 559 |
const char *key, *text; |
| 560 |
pr_json_object_t *json; |
| 561 |
|
| 562 |
mark_point(); |
| 563 |
res = pr_json_object_get_number(NULL, NULL, NULL, NULL); |
| 564 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 565 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 566 |
strerror(errno), errno); |
| 567 |
|
| 568 |
mark_point(); |
| 569 |
res = pr_json_object_get_number(p, NULL, NULL, NULL); |
| 570 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 571 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 572 |
strerror(errno), errno); |
| 573 |
|
| 574 |
json = pr_json_object_alloc(p); |
| 575 |
|
| 576 |
mark_point(); |
| 577 |
res = pr_json_object_get_number(p, json, NULL, NULL); |
| 578 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 579 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 580 |
strerror(errno), errno); |
| 581 |
|
| 582 |
key = "foo"; |
| 583 |
|
| 584 |
mark_point(); |
| 585 |
res = pr_json_object_get_number(p, json, key, NULL); |
| 586 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 587 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 588 |
strerror(errno), errno); |
| 589 |
|
| 590 |
mark_point(); |
| 591 |
res = pr_json_object_get_number(p, json, key, &val); |
| 592 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 593 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 594 |
strerror(errno), errno); |
| 595 |
|
| 596 |
(void) pr_json_object_free(json); |
| 597 |
|
| 598 |
text = "{\"foo\":false,\"bar\":7}"; |
| 599 |
json = pr_json_object_from_text(p, text); |
| 600 |
|
| 601 |
mark_point(); |
| 602 |
res = pr_json_object_get_number(p, json, key, &val); |
| 603 |
ck_assert_msg(res < 0, "Failed to handle non-number key '%s'", key); |
| 604 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 605 |
strerror(errno), errno); |
| 606 |
|
| 607 |
key = "bar"; |
| 608 |
|
| 609 |
mark_point(); |
| 610 |
res = pr_json_object_get_number(p, json, key, &val); |
| 611 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 612 |
strerror(errno)); |
| 613 |
ck_assert_msg(fabs(val) == fabs((double) 7.0), "Expected 7, got %e", val); |
| 614 |
|
| 615 |
(void) pr_json_object_free(json); |
| 616 |
} |
| 617 |
END_TEST |
| 618 |
|
| 619 |
START_TEST(json_object_set_number_test) { |
| 620 |
int res; |
| 621 |
double val = 7; |
| 622 |
const char *key; |
| 623 |
pr_json_object_t *json; |
| 624 |
|
| 625 |
mark_point(); |
| 626 |
res = pr_json_object_set_number(NULL, NULL, NULL, 0); |
| 627 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 628 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 629 |
strerror(errno), errno); |
| 630 |
|
| 631 |
mark_point(); |
| 632 |
res = pr_json_object_set_number(p, NULL, NULL, 0); |
| 633 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 634 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 635 |
strerror(errno), errno); |
| 636 |
|
| 637 |
json = pr_json_object_alloc(p); |
| 638 |
|
| 639 |
mark_point(); |
| 640 |
res = pr_json_object_set_number(p, json, NULL, 0); |
| 641 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 642 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 643 |
strerror(errno), errno); |
| 644 |
|
| 645 |
key = "foo"; |
| 646 |
|
| 647 |
mark_point(); |
| 648 |
res = pr_json_object_set_number(p, json, key, val); |
| 649 |
ck_assert_msg(res == 0, "Failed to set key '%s' to %f: %s", key, val, |
| 650 |
strerror(errno)); |
| 651 |
|
| 652 |
val = 3; |
| 653 |
|
| 654 |
mark_point(); |
| 655 |
res = pr_json_object_get_number(p, json, key, &val); |
| 656 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 657 |
strerror(errno)); |
| 658 |
ck_assert_msg(fabs(val) == fabs((double) 7.0), "Expected 7, got %e", val); |
| 659 |
|
| 660 |
(void) pr_json_object_free(json); |
| 661 |
} |
| 662 |
END_TEST |
| 663 |
|
| 664 |
START_TEST(json_object_get_string_test) { |
| 665 |
int res; |
| 666 |
const char *key, *val, *text; |
| 667 |
pr_json_object_t *json; |
| 668 |
|
| 669 |
mark_point(); |
| 670 |
res = pr_json_object_get_string(NULL, NULL, NULL, NULL); |
| 671 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 672 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 673 |
strerror(errno), errno); |
| 674 |
|
| 675 |
mark_point(); |
| 676 |
res = pr_json_object_get_string(p, NULL, NULL, NULL); |
| 677 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 678 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 679 |
strerror(errno), errno); |
| 680 |
|
| 681 |
json = pr_json_object_alloc(p); |
| 682 |
|
| 683 |
mark_point(); |
| 684 |
res = pr_json_object_get_string(p, json, NULL, NULL); |
| 685 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 686 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 687 |
strerror(errno), errno); |
| 688 |
|
| 689 |
key = "foo"; |
| 690 |
|
| 691 |
mark_point(); |
| 692 |
res = pr_json_object_get_string(p, json, key, NULL); |
| 693 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 694 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 695 |
strerror(errno), errno); |
| 696 |
|
| 697 |
mark_point(); |
| 698 |
res = pr_json_object_get_string(p, json, key, (char **) &val); |
| 699 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 700 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 701 |
strerror(errno), errno); |
| 702 |
|
| 703 |
(void) pr_json_object_free(json); |
| 704 |
|
| 705 |
text = "{\"foo\":false,\"bar\":\"baz\"}"; |
| 706 |
json = pr_json_object_from_text(p, text); |
| 707 |
|
| 708 |
mark_point(); |
| 709 |
res = pr_json_object_get_string(p, json, key, (char **) &val); |
| 710 |
ck_assert_msg(res < 0, "Failed to handle non-string key '%s'", key); |
| 711 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 712 |
strerror(errno), errno); |
| 713 |
|
| 714 |
key = "bar"; |
| 715 |
|
| 716 |
mark_point(); |
| 717 |
res = pr_json_object_get_string(p, json, key, (char **) &val); |
| 718 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 719 |
strerror(errno)); |
| 720 |
ck_assert_msg(strcmp(val, "baz") == 0, "Expected 'baz', got '%s'", val); |
| 721 |
|
| 722 |
(void) pr_json_object_free(json); |
| 723 |
|
| 724 |
text = "{\"foo\":\"\"}"; |
| 725 |
json = pr_json_object_from_text(p, text); |
| 726 |
|
| 727 |
key = "foo"; |
| 728 |
|
| 729 |
mark_point(); |
| 730 |
res = pr_json_object_get_string(p, json, key, (char **) &val); |
| 731 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 732 |
strerror(errno)); |
| 733 |
ck_assert_msg(strcmp(val, "") == 0, "Expected '', got '%s'", val); |
| 734 |
|
| 735 |
(void) pr_json_object_free(json); |
| 736 |
} |
| 737 |
END_TEST |
| 738 |
|
| 739 |
START_TEST(json_object_set_string_test) { |
| 740 |
int res; |
| 741 |
const char *key, *val; |
| 742 |
pr_json_object_t *json; |
| 743 |
|
| 744 |
mark_point(); |
| 745 |
res = pr_json_object_set_string(NULL, NULL, NULL, NULL); |
| 746 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 747 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 748 |
strerror(errno), errno); |
| 749 |
|
| 750 |
mark_point(); |
| 751 |
res = pr_json_object_set_string(p, NULL, NULL, NULL); |
| 752 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 753 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 754 |
strerror(errno), errno); |
| 755 |
|
| 756 |
json = pr_json_object_alloc(p); |
| 757 |
|
| 758 |
mark_point(); |
| 759 |
res = pr_json_object_set_string(p, json, NULL, NULL); |
| 760 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 761 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 762 |
strerror(errno), errno); |
| 763 |
|
| 764 |
key = "foo"; |
| 765 |
|
| 766 |
mark_point(); |
| 767 |
res = pr_json_object_set_string(p, json, key, NULL); |
| 768 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 769 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 770 |
strerror(errno), errno); |
| 771 |
|
| 772 |
val = "Hello, World!"; |
| 773 |
|
| 774 |
mark_point(); |
| 775 |
res = pr_json_object_set_string(p, json, key, val); |
| 776 |
ck_assert_msg(res == 0, "Failed to set key '%s' to '%s': %s", key, val, |
| 777 |
strerror(errno)); |
| 778 |
|
| 779 |
val = "glarg"; |
| 780 |
|
| 781 |
mark_point(); |
| 782 |
res = pr_json_object_get_string(p, json, key, (char **) &val); |
| 783 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 784 |
strerror(errno)); |
| 785 |
ck_assert_msg(strcmp(val, "Hello, World!") == 0, |
| 786 |
"Expected 'Hello, World!', got '%s'", val); |
| 787 |
|
| 788 |
(void) pr_json_object_free(json); |
| 789 |
} |
| 790 |
END_TEST |
| 791 |
|
| 792 |
START_TEST(json_object_get_array_test) { |
| 793 |
int res; |
| 794 |
const char *key, *text; |
| 795 |
char *expected = NULL, *str = NULL; |
| 796 |
pr_json_array_t *val = NULL; |
| 797 |
pr_json_object_t *json; |
| 798 |
|
| 799 |
mark_point(); |
| 800 |
res = pr_json_object_get_array(NULL, NULL, NULL, NULL); |
| 801 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 802 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 803 |
strerror(errno), errno); |
| 804 |
|
| 805 |
mark_point(); |
| 806 |
res = pr_json_object_get_array(p, NULL, NULL, NULL); |
| 807 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 808 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 809 |
strerror(errno), errno); |
| 810 |
|
| 811 |
json = pr_json_object_alloc(p); |
| 812 |
|
| 813 |
mark_point(); |
| 814 |
res = pr_json_object_get_array(p, json, NULL, NULL); |
| 815 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 816 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 817 |
strerror(errno), errno); |
| 818 |
|
| 819 |
key = "foo"; |
| 820 |
|
| 821 |
mark_point(); |
| 822 |
res = pr_json_object_get_array(p, json, key, NULL); |
| 823 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 824 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 825 |
strerror(errno), errno); |
| 826 |
|
| 827 |
mark_point(); |
| 828 |
res = pr_json_object_get_array(p, json, key, &val); |
| 829 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 830 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 831 |
strerror(errno), errno); |
| 832 |
|
| 833 |
(void) pr_json_object_free(json); |
| 834 |
|
| 835 |
text = "{\"foo\":false,\"bar\":[\"baz\"]}"; |
| 836 |
json = pr_json_object_from_text(p, text); |
| 837 |
|
| 838 |
mark_point(); |
| 839 |
res = pr_json_object_get_array(p, json, key, &val); |
| 840 |
ck_assert_msg(res < 0, "Failed to handle non-array key '%s'", key); |
| 841 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 842 |
strerror(errno), errno); |
| 843 |
|
| 844 |
key = "bar"; |
| 845 |
val = NULL; |
| 846 |
|
| 847 |
mark_point(); |
| 848 |
res = pr_json_object_get_array(p, json, key, &val); |
| 849 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 850 |
strerror(errno)); |
| 851 |
ck_assert_msg(val != NULL, "Expected array, got null"); |
| 852 |
|
| 853 |
expected = "[\"baz\"]"; |
| 854 |
str = pr_json_array_to_text(p, val, ""); |
| 855 |
ck_assert_msg(strcmp(str, expected) == 0, |
| 856 |
"Expected '%s', got '%s'", expected, str); |
| 857 |
|
| 858 |
mark_point(); |
| 859 |
(void) pr_json_array_free(val); |
| 860 |
|
| 861 |
mark_point(); |
| 862 |
(void) pr_json_object_free(json); |
| 863 |
} |
| 864 |
END_TEST |
| 865 |
|
| 866 |
START_TEST(json_object_set_array_test) { |
| 867 |
int res; |
| 868 |
const char *key, *text; |
| 869 |
pr_json_array_t *val = NULL; |
| 870 |
pr_json_object_t *json; |
| 871 |
|
| 872 |
mark_point(); |
| 873 |
res = pr_json_object_set_array(NULL, NULL, NULL, NULL); |
| 874 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 875 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 876 |
strerror(errno), errno); |
| 877 |
|
| 878 |
mark_point(); |
| 879 |
res = pr_json_object_set_array(p, NULL, NULL, NULL); |
| 880 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 881 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 882 |
strerror(errno), errno); |
| 883 |
|
| 884 |
json = pr_json_object_alloc(p); |
| 885 |
|
| 886 |
mark_point(); |
| 887 |
res = pr_json_object_set_array(p, json, NULL, NULL); |
| 888 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 889 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 890 |
strerror(errno), errno); |
| 891 |
|
| 892 |
key = "foo"; |
| 893 |
|
| 894 |
mark_point(); |
| 895 |
res = pr_json_object_set_array(p, json, key, val); |
| 896 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 897 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 898 |
strerror(errno), errno); |
| 899 |
|
| 900 |
text = "[1, 1, 2, 3, 5, 8]"; |
| 901 |
val = pr_json_array_from_text(p, text); |
| 902 |
|
| 903 |
mark_point(); |
| 904 |
res = pr_json_object_set_array(p, json, key, val); |
| 905 |
ck_assert_msg(res == 0, "Failed to set key '%s' to '%s': %s", key, text, |
| 906 |
strerror(errno)); |
| 907 |
|
| 908 |
val = NULL; |
| 909 |
|
| 910 |
mark_point(); |
| 911 |
res = pr_json_object_get_array(p, json, key, &val); |
| 912 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 913 |
strerror(errno)); |
| 914 |
ck_assert_msg(val != NULL, "Expected array, got null"); |
| 915 |
|
| 916 |
mark_point(); |
| 917 |
(void) pr_json_array_free(val); |
| 918 |
(void) pr_json_object_free(json); |
| 919 |
} |
| 920 |
END_TEST |
| 921 |
|
| 922 |
START_TEST(json_object_get_object_test) { |
| 923 |
int res; |
| 924 |
const char *key, *text; |
| 925 |
char *expected = NULL, *str = NULL; |
| 926 |
pr_json_object_t *json, *val = NULL; |
| 927 |
|
| 928 |
mark_point(); |
| 929 |
res = pr_json_object_get_object(NULL, NULL, NULL, NULL); |
| 930 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 931 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 932 |
strerror(errno), errno); |
| 933 |
|
| 934 |
mark_point(); |
| 935 |
res = pr_json_object_get_object(p, NULL, NULL, NULL); |
| 936 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 937 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 938 |
strerror(errno), errno); |
| 939 |
|
| 940 |
json = pr_json_object_alloc(p); |
| 941 |
|
| 942 |
mark_point(); |
| 943 |
res = pr_json_object_get_object(p, json, NULL, NULL); |
| 944 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 945 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 946 |
strerror(errno), errno); |
| 947 |
|
| 948 |
key = "foo"; |
| 949 |
|
| 950 |
mark_point(); |
| 951 |
res = pr_json_object_get_object(p, json, key, NULL); |
| 952 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 953 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 954 |
strerror(errno), errno); |
| 955 |
|
| 956 |
mark_point(); |
| 957 |
res = pr_json_object_get_object(p, json, key, &val); |
| 958 |
ck_assert_msg(res < 0, "Failed to handle nonexistent key '%s'", key); |
| 959 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 960 |
strerror(errno), errno); |
| 961 |
|
| 962 |
(void) pr_json_object_free(json); |
| 963 |
|
| 964 |
text = "{\"foo\":false,\"bar\":{\"baz\":null}}"; |
| 965 |
json = pr_json_object_from_text(p, text); |
| 966 |
|
| 967 |
mark_point(); |
| 968 |
res = pr_json_object_get_object(p, json, key, &val); |
| 969 |
ck_assert_msg(res < 0, "Failed to handle non-object key '%s'", key); |
| 970 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 971 |
strerror(errno), errno); |
| 972 |
|
| 973 |
key = "bar"; |
| 974 |
val = NULL; |
| 975 |
|
| 976 |
mark_point(); |
| 977 |
res = pr_json_object_get_object(p, json, key, &val); |
| 978 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 979 |
strerror(errno)); |
| 980 |
ck_assert_msg(val != NULL, "Expected object, got null"); |
| 981 |
|
| 982 |
expected = "{\"baz\":null}"; |
| 983 |
str = pr_json_object_to_text(p, val, ""); |
| 984 |
ck_assert_msg(strcmp(str, expected) == 0, |
| 985 |
"Expected '%s', got '%s'", expected, str); |
| 986 |
|
| 987 |
mark_point(); |
| 988 |
(void) pr_json_object_free(val); |
| 989 |
|
| 990 |
mark_point(); |
| 991 |
(void) pr_json_object_free(json); |
| 992 |
} |
| 993 |
END_TEST |
| 994 |
|
| 995 |
START_TEST(json_object_set_object_test) { |
| 996 |
int res; |
| 997 |
const char *key, *text; |
| 998 |
pr_json_object_t *json, *val = NULL; |
| 999 |
|
| 1000 |
mark_point(); |
| 1001 |
res = pr_json_object_set_object(NULL, NULL, NULL, NULL); |
| 1002 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1003 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1004 |
strerror(errno), errno); |
| 1005 |
|
| 1006 |
mark_point(); |
| 1007 |
res = pr_json_object_set_object(p, NULL, NULL, NULL); |
| 1008 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1009 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1010 |
strerror(errno), errno); |
| 1011 |
|
| 1012 |
json = pr_json_object_alloc(p); |
| 1013 |
|
| 1014 |
mark_point(); |
| 1015 |
res = pr_json_object_set_object(p, json, NULL, NULL); |
| 1016 |
ck_assert_msg(res < 0, "Failed to handle null key"); |
| 1017 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1018 |
strerror(errno), errno); |
| 1019 |
|
| 1020 |
key = "foo"; |
| 1021 |
|
| 1022 |
mark_point(); |
| 1023 |
res = pr_json_object_set_object(p, json, key, val); |
| 1024 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1025 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1026 |
strerror(errno), errno); |
| 1027 |
|
| 1028 |
text = "{\"eeny\":1,\"meeny\":2,\"miny\":3,\"moe\":false}"; |
| 1029 |
val = pr_json_object_from_text(p, text); |
| 1030 |
|
| 1031 |
mark_point(); |
| 1032 |
res = pr_json_object_set_object(p, json, key, val); |
| 1033 |
ck_assert_msg(res == 0, "Failed to set key '%s' to '%s': %s", key, text, |
| 1034 |
strerror(errno)); |
| 1035 |
|
| 1036 |
val = NULL; |
| 1037 |
|
| 1038 |
mark_point(); |
| 1039 |
res = pr_json_object_get_object(p, json, key, &val); |
| 1040 |
ck_assert_msg(res == 0, "Failed to handle existing key '%s': %s", key, |
| 1041 |
strerror(errno)); |
| 1042 |
ck_assert_msg(val != NULL, "Expected object, got null"); |
| 1043 |
|
| 1044 |
mark_point(); |
| 1045 |
(void) pr_json_object_free(val); |
| 1046 |
(void) pr_json_object_free(json); |
| 1047 |
} |
| 1048 |
END_TEST |
| 1049 |
|
| 1050 |
START_TEST(json_array_free_test) { |
| 1051 |
int res; |
| 1052 |
|
| 1053 |
mark_point(); |
| 1054 |
res = pr_json_array_free(NULL); |
| 1055 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1056 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1057 |
strerror(errno), errno); |
| 1058 |
} |
| 1059 |
END_TEST |
| 1060 |
|
| 1061 |
START_TEST(json_array_alloc_test) { |
| 1062 |
int res; |
| 1063 |
pr_json_array_t *json; |
| 1064 |
|
| 1065 |
mark_point(); |
| 1066 |
json = pr_json_array_alloc(NULL); |
| 1067 |
ck_assert_msg(json == NULL, "Failed to handle null pool"); |
| 1068 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1069 |
strerror(errno), errno); |
| 1070 |
|
| 1071 |
mark_point(); |
| 1072 |
json = pr_json_array_alloc(p); |
| 1073 |
ck_assert_msg(json != NULL, "Failed to allocate array: %s", strerror(errno)); |
| 1074 |
|
| 1075 |
mark_point(); |
| 1076 |
res = pr_json_array_free(json); |
| 1077 |
ck_assert_msg(res == 0, "Failed to free array: %s", strerror(errno)); |
| 1078 |
} |
| 1079 |
END_TEST |
| 1080 |
|
| 1081 |
START_TEST(json_array_from_text_test) { |
| 1082 |
pr_json_array_t *json; |
| 1083 |
const char *text; |
| 1084 |
|
| 1085 |
mark_point(); |
| 1086 |
json = pr_json_array_from_text(NULL, NULL); |
| 1087 |
ck_assert_msg(json == NULL, "Failed to handle null pool"); |
| 1088 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1089 |
strerror(errno), errno); |
| 1090 |
|
| 1091 |
mark_point(); |
| 1092 |
json = pr_json_array_from_text(p, NULL); |
| 1093 |
ck_assert_msg(json == NULL, "Failed to handle null text"); |
| 1094 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1095 |
strerror(errno), errno); |
| 1096 |
|
| 1097 |
text = "foo bar"; |
| 1098 |
|
| 1099 |
mark_point(); |
| 1100 |
json = pr_json_array_from_text(p, text); |
| 1101 |
ck_assert_msg(json == NULL, "Failed to handle invalid text '%s'", text); |
| 1102 |
ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM, |
| 1103 |
strerror(errno), errno); |
| 1104 |
|
| 1105 |
text = "{\"foo\":null,\"bar\":false}"; |
| 1106 |
|
| 1107 |
mark_point(); |
| 1108 |
json = pr_json_array_from_text(p, text); |
| 1109 |
ck_assert_msg(json == NULL, "Failed to handle non-array text '%s'", text); |
| 1110 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1111 |
strerror(errno), errno); |
| 1112 |
|
| 1113 |
text = "[\"foo\",\"bar\"]"; |
| 1114 |
|
| 1115 |
mark_point(); |
| 1116 |
json = pr_json_array_from_text(p, text); |
| 1117 |
ck_assert_msg(json != NULL, "Failed to handle text '%s': %s", text, |
| 1118 |
strerror(errno)); |
| 1119 |
(void) pr_json_array_free(json); |
| 1120 |
|
| 1121 |
mark_point(); |
| 1122 |
text = "[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]"; |
| 1123 |
json = pr_json_array_from_text(p, text); |
| 1124 |
ck_assert_msg(json == NULL, "Failed to handle depth-exceeding text '%s'", text); |
| 1125 |
} |
| 1126 |
END_TEST |
| 1127 |
|
| 1128 |
START_TEST(json_array_to_text_test) { |
| 1129 |
const char *text, *expected; |
| 1130 |
pr_json_array_t *json; |
| 1131 |
|
| 1132 |
mark_point(); |
| 1133 |
text = pr_json_array_to_text(NULL, NULL, NULL); |
| 1134 |
ck_assert_msg(text == NULL, "Failed to handle null pool"); |
| 1135 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1136 |
strerror(errno), errno); |
| 1137 |
|
| 1138 |
mark_point(); |
| 1139 |
text = pr_json_array_to_text(p, NULL, NULL); |
| 1140 |
ck_assert_msg(text == NULL, "Failed to handle null json"); |
| 1141 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1142 |
strerror(errno), errno); |
| 1143 |
|
| 1144 |
json = pr_json_array_alloc(p); |
| 1145 |
|
| 1146 |
mark_point(); |
| 1147 |
text = pr_json_array_to_text(p, json, NULL); |
| 1148 |
ck_assert_msg(text == NULL, "Failed to handle null indent"); |
| 1149 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1150 |
strerror(errno), errno); |
| 1151 |
|
| 1152 |
expected = "[]"; |
| 1153 |
|
| 1154 |
mark_point(); |
| 1155 |
text = pr_json_array_to_text(p, json, ""); |
| 1156 |
ck_assert_msg(text != NULL, "Failed to get text for array: %s", |
| 1157 |
strerror(errno)); |
| 1158 |
ck_assert_msg(strcmp(text, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1159 |
text); |
| 1160 |
|
| 1161 |
(void) pr_json_array_free(json); |
| 1162 |
} |
| 1163 |
END_TEST |
| 1164 |
|
| 1165 |
static int array_item_ok(int val_type, const void *val, size_t valsz, |
| 1166 |
void *user_data) { |
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static int array_item_fail(int val_type, const void *val, size_t valsz, |
| 1171 |
void *user_data) { |
| 1172 |
errno = EPERM; |
| 1173 |
return -1; |
| 1174 |
} |
| 1175 |
|
| 1176 |
START_TEST(json_array_foreach_test) { |
| 1177 |
int res; |
| 1178 |
pr_json_array_t *json = NULL; |
| 1179 |
const char *text; |
| 1180 |
|
| 1181 |
mark_point(); |
| 1182 |
res = pr_json_array_foreach(NULL, NULL, NULL, NULL); |
| 1183 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1184 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1185 |
strerror(errno), errno); |
| 1186 |
|
| 1187 |
mark_point(); |
| 1188 |
res = pr_json_array_foreach(p, NULL, NULL, NULL); |
| 1189 |
ck_assert_msg(res < 0, "Failed to handle null array"); |
| 1190 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1191 |
strerror(errno), errno); |
| 1192 |
|
| 1193 |
json = pr_json_array_alloc(p); |
| 1194 |
|
| 1195 |
mark_point(); |
| 1196 |
res = pr_json_array_foreach(p, json, NULL, NULL); |
| 1197 |
ck_assert_msg(res < 0, "Failed to handle null callback"); |
| 1198 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1199 |
strerror(errno), errno); |
| 1200 |
|
| 1201 |
(void) pr_json_array_free(json); |
| 1202 |
|
| 1203 |
text = "[\"foo\",true,\"bar\",false,\"baz\",1,{}]"; |
| 1204 |
json = pr_json_array_from_text(p, text); |
| 1205 |
|
| 1206 |
mark_point(); |
| 1207 |
res = pr_json_array_foreach(p, json, array_item_ok, NULL); |
| 1208 |
ck_assert_msg(res == 0, "Failed to iterate over array: %s", strerror(errno)); |
| 1209 |
|
| 1210 |
mark_point(); |
| 1211 |
res = pr_json_array_foreach(p, json, array_item_fail, NULL); |
| 1212 |
ck_assert_msg(res < 0, "Failing callback succeeded unexpectedly"); |
| 1213 |
ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM, |
| 1214 |
strerror(errno), errno); |
| 1215 |
|
| 1216 |
(void) pr_json_array_free(json); |
| 1217 |
} |
| 1218 |
END_TEST |
| 1219 |
|
| 1220 |
START_TEST(json_array_count_test) { |
| 1221 |
int res; |
| 1222 |
pr_json_array_t *json; |
| 1223 |
const char *text; |
| 1224 |
|
| 1225 |
mark_point(); |
| 1226 |
res = pr_json_array_count(NULL); |
| 1227 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1228 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1229 |
strerror(errno), errno); |
| 1230 |
|
| 1231 |
json = pr_json_array_alloc(p); |
| 1232 |
|
| 1233 |
mark_point(); |
| 1234 |
res = pr_json_array_count(json); |
| 1235 |
ck_assert_msg(res == 0, "Expected 0, got %d", res); |
| 1236 |
|
| 1237 |
(void) pr_json_array_free(json); |
| 1238 |
|
| 1239 |
text = "[\"foo\",true,\"bar\",false,\"baz\",1]"; |
| 1240 |
json = pr_json_array_from_text(p, text); |
| 1241 |
|
| 1242 |
mark_point(); |
| 1243 |
res = pr_json_array_count(json); |
| 1244 |
ck_assert_msg(res == 6, "Expected 6, got %d", res); |
| 1245 |
|
| 1246 |
(void) pr_json_array_free(json); |
| 1247 |
} |
| 1248 |
END_TEST |
| 1249 |
|
| 1250 |
START_TEST(json_array_exists_test) { |
| 1251 |
int res; |
| 1252 |
pr_json_array_t *json; |
| 1253 |
unsigned int idx; |
| 1254 |
const char *text; |
| 1255 |
|
| 1256 |
mark_point(); |
| 1257 |
res = pr_json_array_exists(NULL, 0); |
| 1258 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1259 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1260 |
strerror(errno), errno); |
| 1261 |
|
| 1262 |
json = pr_json_array_alloc(p); |
| 1263 |
|
| 1264 |
mark_point(); |
| 1265 |
res = pr_json_array_exists(json, 0); |
| 1266 |
ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res); |
| 1267 |
|
| 1268 |
(void) pr_json_array_free(json); |
| 1269 |
|
| 1270 |
text = "[\"foo\",true,\"bar\",false,\"baz\",1]"; |
| 1271 |
json = pr_json_array_from_text(p, text); |
| 1272 |
|
| 1273 |
idx = 3; |
| 1274 |
|
| 1275 |
mark_point(); |
| 1276 |
res = pr_json_array_exists(json, idx); |
| 1277 |
ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res); |
| 1278 |
|
| 1279 |
(void) pr_json_array_free(json); |
| 1280 |
} |
| 1281 |
END_TEST |
| 1282 |
|
| 1283 |
START_TEST(json_array_remove_test) { |
| 1284 |
int res; |
| 1285 |
pr_json_array_t *json; |
| 1286 |
unsigned int idx; |
| 1287 |
const char *text; |
| 1288 |
|
| 1289 |
mark_point(); |
| 1290 |
res = pr_json_array_remove(NULL, 0); |
| 1291 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1292 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1293 |
strerror(errno), errno); |
| 1294 |
|
| 1295 |
json = pr_json_array_alloc(p); |
| 1296 |
|
| 1297 |
idx = 2; |
| 1298 |
|
| 1299 |
mark_point(); |
| 1300 |
res = pr_json_array_remove(json, idx); |
| 1301 |
ck_assert_msg(res == 0, "Failed to remove nonexistent index %u: %s", idx, |
| 1302 |
strerror(errno)); |
| 1303 |
|
| 1304 |
res = pr_json_array_count(json); |
| 1305 |
ck_assert_msg(res == 0, "Expected count 0, got %d", res); |
| 1306 |
|
| 1307 |
(void) pr_json_array_free(json); |
| 1308 |
|
| 1309 |
text = "[\"foo\",true,\"bar\",false,\"baz\",1]"; |
| 1310 |
json = pr_json_array_from_text(p, text); |
| 1311 |
|
| 1312 |
mark_point(); |
| 1313 |
res = pr_json_array_remove(json, idx); |
| 1314 |
ck_assert_msg(res == 0, "Failed to remove existing index %u: %s", idx, |
| 1315 |
strerror(errno)); |
| 1316 |
|
| 1317 |
res = pr_json_array_count(json); |
| 1318 |
ck_assert_msg(res == 5, "Expected count 5, got %d", res); |
| 1319 |
|
| 1320 |
mark_point(); |
| 1321 |
res = pr_json_array_exists(json, idx); |
| 1322 |
ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res); |
| 1323 |
|
| 1324 |
(void) pr_json_array_free(json); |
| 1325 |
} |
| 1326 |
END_TEST |
| 1327 |
|
| 1328 |
START_TEST(json_array_get_bool_test) { |
| 1329 |
int res, val; |
| 1330 |
unsigned int idx; |
| 1331 |
const char *text; |
| 1332 |
pr_json_array_t *json; |
| 1333 |
|
| 1334 |
mark_point(); |
| 1335 |
res = pr_json_array_get_bool(NULL, NULL, 0, NULL); |
| 1336 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1337 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1338 |
strerror(errno), errno); |
| 1339 |
|
| 1340 |
mark_point(); |
| 1341 |
res = pr_json_array_get_bool(p, NULL, 0, NULL); |
| 1342 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1343 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1344 |
strerror(errno), errno); |
| 1345 |
|
| 1346 |
json = pr_json_array_alloc(p); |
| 1347 |
|
| 1348 |
mark_point(); |
| 1349 |
res = pr_json_array_get_bool(p, json, 0, NULL); |
| 1350 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1351 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1352 |
strerror(errno), errno); |
| 1353 |
|
| 1354 |
idx = 0; |
| 1355 |
|
| 1356 |
mark_point(); |
| 1357 |
res = pr_json_array_get_bool(p, json, idx, &val); |
| 1358 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1359 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1360 |
strerror(errno), errno); |
| 1361 |
|
| 1362 |
(void) pr_json_array_free(json); |
| 1363 |
|
| 1364 |
text = "[\"foo\",2,\"bar\",true]"; |
| 1365 |
json = pr_json_array_from_text(p, text); |
| 1366 |
|
| 1367 |
mark_point(); |
| 1368 |
res = pr_json_array_get_bool(p, json, idx, &val); |
| 1369 |
ck_assert_msg(res < 0, "Failed to handle non-boolean index %u", idx); |
| 1370 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1371 |
strerror(errno), errno); |
| 1372 |
|
| 1373 |
idx = 3; |
| 1374 |
|
| 1375 |
mark_point(); |
| 1376 |
res = pr_json_array_get_bool(p, json, idx, &val); |
| 1377 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1378 |
strerror(errno)); |
| 1379 |
ck_assert_msg(val == TRUE, "Expected TRUE, got %d", val); |
| 1380 |
|
| 1381 |
(void) pr_json_array_free(json); |
| 1382 |
} |
| 1383 |
END_TEST |
| 1384 |
|
| 1385 |
START_TEST(json_array_append_bool_test) { |
| 1386 |
int res, val = TRUE; |
| 1387 |
pr_json_array_t *json; |
| 1388 |
|
| 1389 |
mark_point(); |
| 1390 |
res = pr_json_array_append_bool(NULL, NULL, 0); |
| 1391 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1392 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1393 |
strerror(errno), errno); |
| 1394 |
|
| 1395 |
mark_point(); |
| 1396 |
res = pr_json_array_append_bool(p, NULL, 0); |
| 1397 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1398 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1399 |
strerror(errno), errno); |
| 1400 |
|
| 1401 |
json = pr_json_array_alloc(p); |
| 1402 |
|
| 1403 |
mark_point(); |
| 1404 |
res = pr_json_array_append_bool(p, json, val); |
| 1405 |
ck_assert_msg(res == 0, "Failed to append val %d: %s", val, strerror(errno)); |
| 1406 |
|
| 1407 |
val = FALSE; |
| 1408 |
|
| 1409 |
mark_point(); |
| 1410 |
res = pr_json_array_get_bool(p, json, 0, &val); |
| 1411 |
ck_assert_msg(res == 0, "Failed to handle existing index 0: %s", |
| 1412 |
strerror(errno)); |
| 1413 |
ck_assert_msg(val == TRUE, "Expected TRUE, got %d", val); |
| 1414 |
|
| 1415 |
(void) pr_json_array_free(json); |
| 1416 |
} |
| 1417 |
END_TEST |
| 1418 |
|
| 1419 |
START_TEST(json_array_get_null_test) { |
| 1420 |
int res; |
| 1421 |
unsigned int idx; |
| 1422 |
const char *text; |
| 1423 |
pr_json_array_t *json; |
| 1424 |
|
| 1425 |
mark_point(); |
| 1426 |
res = pr_json_array_get_null(NULL, NULL, 0); |
| 1427 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1428 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1429 |
strerror(errno), errno); |
| 1430 |
|
| 1431 |
mark_point(); |
| 1432 |
res = pr_json_array_get_null(p, NULL, 0); |
| 1433 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1434 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1435 |
strerror(errno), errno); |
| 1436 |
|
| 1437 |
json = pr_json_array_alloc(p); |
| 1438 |
|
| 1439 |
idx = 1; |
| 1440 |
|
| 1441 |
mark_point(); |
| 1442 |
res = pr_json_array_get_null(p, json, idx); |
| 1443 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1444 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1445 |
strerror(errno), errno); |
| 1446 |
|
| 1447 |
(void) pr_json_array_free(json); |
| 1448 |
|
| 1449 |
text = "[\"foo\",2,\"bar\",null]"; |
| 1450 |
json = pr_json_array_from_text(p, text); |
| 1451 |
|
| 1452 |
mark_point(); |
| 1453 |
res = pr_json_array_get_null(p, json, idx); |
| 1454 |
ck_assert_msg(res < 0, "Failed to handle non-null index %u", idx); |
| 1455 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1456 |
strerror(errno), errno); |
| 1457 |
|
| 1458 |
idx = 3; |
| 1459 |
|
| 1460 |
mark_point(); |
| 1461 |
res = pr_json_array_get_null(p, json, idx); |
| 1462 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1463 |
strerror(errno)); |
| 1464 |
|
| 1465 |
(void) pr_json_array_free(json); |
| 1466 |
} |
| 1467 |
END_TEST |
| 1468 |
|
| 1469 |
START_TEST(json_array_append_null_test) { |
| 1470 |
int res; |
| 1471 |
pr_json_array_t *json; |
| 1472 |
|
| 1473 |
mark_point(); |
| 1474 |
res = pr_json_array_append_null(NULL, NULL); |
| 1475 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1476 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1477 |
strerror(errno), errno); |
| 1478 |
|
| 1479 |
mark_point(); |
| 1480 |
res = pr_json_array_append_null(p, NULL); |
| 1481 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1482 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1483 |
strerror(errno), errno); |
| 1484 |
|
| 1485 |
json = pr_json_array_alloc(p); |
| 1486 |
|
| 1487 |
mark_point(); |
| 1488 |
res = pr_json_array_append_null(p, json); |
| 1489 |
ck_assert_msg(res == 0, "Failed to append null vall: %s", strerror(errno)); |
| 1490 |
|
| 1491 |
mark_point(); |
| 1492 |
res = pr_json_array_get_null(p, json, 0); |
| 1493 |
ck_assert_msg(res == 0, "Failed to handle existing index 0: %s", |
| 1494 |
strerror(errno)); |
| 1495 |
|
| 1496 |
(void) pr_json_array_free(json); |
| 1497 |
} |
| 1498 |
END_TEST |
| 1499 |
|
| 1500 |
START_TEST(json_array_get_number_test) { |
| 1501 |
int res; |
| 1502 |
double val; |
| 1503 |
unsigned int idx; |
| 1504 |
const char *text; |
| 1505 |
pr_json_array_t *json; |
| 1506 |
|
| 1507 |
mark_point(); |
| 1508 |
res = pr_json_array_get_number(NULL, NULL, 0, NULL); |
| 1509 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1510 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1511 |
strerror(errno), errno); |
| 1512 |
|
| 1513 |
mark_point(); |
| 1514 |
res = pr_json_array_get_number(p, NULL, 0, NULL); |
| 1515 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1516 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1517 |
strerror(errno), errno); |
| 1518 |
|
| 1519 |
json = pr_json_array_alloc(p); |
| 1520 |
|
| 1521 |
mark_point(); |
| 1522 |
res = pr_json_array_get_number(p, json, 0, NULL); |
| 1523 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1524 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1525 |
strerror(errno), errno); |
| 1526 |
|
| 1527 |
idx = 3; |
| 1528 |
|
| 1529 |
mark_point(); |
| 1530 |
res = pr_json_array_get_number(p, json, idx, &val); |
| 1531 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1532 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1533 |
strerror(errno), errno); |
| 1534 |
|
| 1535 |
(void) pr_json_array_free(json); |
| 1536 |
|
| 1537 |
text = "[\"foo\",2,\"bar\",true]"; |
| 1538 |
json = pr_json_array_from_text(p, text); |
| 1539 |
|
| 1540 |
mark_point(); |
| 1541 |
res = pr_json_array_get_number(p, json, idx, &val); |
| 1542 |
ck_assert_msg(res < 0, "Failed to handle non-number index %u", idx); |
| 1543 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1544 |
strerror(errno), errno); |
| 1545 |
|
| 1546 |
idx = 1; |
| 1547 |
|
| 1548 |
mark_point(); |
| 1549 |
res = pr_json_array_get_number(p, json, idx, &val); |
| 1550 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1551 |
strerror(errno)); |
| 1552 |
ck_assert_msg(fabs(val) == fabs((double) 2.0), "Expected 2, got '%e'", val); |
| 1553 |
|
| 1554 |
(void) pr_json_array_free(json); |
| 1555 |
} |
| 1556 |
END_TEST |
| 1557 |
|
| 1558 |
START_TEST(json_array_append_number_test) { |
| 1559 |
int res; |
| 1560 |
double val = 7; |
| 1561 |
pr_json_array_t *json; |
| 1562 |
|
| 1563 |
mark_point(); |
| 1564 |
res = pr_json_array_append_number(NULL, NULL, 0); |
| 1565 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1566 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1567 |
strerror(errno), errno); |
| 1568 |
|
| 1569 |
mark_point(); |
| 1570 |
res = pr_json_array_append_number(p, NULL, 0); |
| 1571 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1572 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1573 |
strerror(errno), errno); |
| 1574 |
|
| 1575 |
json = pr_json_array_alloc(p); |
| 1576 |
|
| 1577 |
mark_point(); |
| 1578 |
res = pr_json_array_append_number(p, json, val); |
| 1579 |
ck_assert_msg(res == 0, "Failed to append val %e: %s", val, strerror(errno)); |
| 1580 |
|
| 1581 |
val = 2; |
| 1582 |
|
| 1583 |
mark_point(); |
| 1584 |
res = pr_json_array_get_number(p, json, 0, &val); |
| 1585 |
ck_assert_msg(res == 0, "Failed to handle existing index 0: %s", |
| 1586 |
strerror(errno)); |
| 1587 |
ck_assert_msg(fabs(val) == fabs((double) 7.0), "Expected 7, got %e", val); |
| 1588 |
|
| 1589 |
(void) pr_json_array_free(json); |
| 1590 |
} |
| 1591 |
END_TEST |
| 1592 |
|
| 1593 |
START_TEST(json_array_get_string_test) { |
| 1594 |
int res; |
| 1595 |
unsigned int idx; |
| 1596 |
const char *text, *val; |
| 1597 |
pr_json_array_t *json; |
| 1598 |
|
| 1599 |
mark_point(); |
| 1600 |
res = pr_json_array_get_string(NULL, NULL, 0, NULL); |
| 1601 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1602 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1603 |
strerror(errno), errno); |
| 1604 |
|
| 1605 |
mark_point(); |
| 1606 |
res = pr_json_array_get_string(p, NULL, 0, NULL); |
| 1607 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1608 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1609 |
strerror(errno), errno); |
| 1610 |
|
| 1611 |
json = pr_json_array_alloc(p); |
| 1612 |
|
| 1613 |
mark_point(); |
| 1614 |
res = pr_json_array_get_string(p, json, 0, NULL); |
| 1615 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1616 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1617 |
strerror(errno), errno); |
| 1618 |
|
| 1619 |
idx = 3; |
| 1620 |
|
| 1621 |
mark_point(); |
| 1622 |
res = pr_json_array_get_string(p, json, idx, (char **) &val); |
| 1623 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1624 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1625 |
strerror(errno), errno); |
| 1626 |
|
| 1627 |
(void) pr_json_array_free(json); |
| 1628 |
|
| 1629 |
text = "[\"foo\",2,\"bar\",true]"; |
| 1630 |
json = pr_json_array_from_text(p, text); |
| 1631 |
|
| 1632 |
mark_point(); |
| 1633 |
res = pr_json_array_get_string(p, json, idx, (char **) &val); |
| 1634 |
ck_assert_msg(res < 0, "Failed to handle non-string index %u", idx); |
| 1635 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1636 |
strerror(errno), errno); |
| 1637 |
|
| 1638 |
idx = 0; |
| 1639 |
|
| 1640 |
mark_point(); |
| 1641 |
res = pr_json_array_get_string(p, json, idx, (char **) &val); |
| 1642 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1643 |
strerror(errno)); |
| 1644 |
ck_assert_msg(strcmp(val, "foo") == 0, "Expected 'foo', got '%s'", val); |
| 1645 |
|
| 1646 |
(void) pr_json_array_free(json); |
| 1647 |
} |
| 1648 |
END_TEST |
| 1649 |
|
| 1650 |
START_TEST(json_array_append_string_test) { |
| 1651 |
int res; |
| 1652 |
const char *val; |
| 1653 |
pr_json_array_t *json; |
| 1654 |
|
| 1655 |
mark_point(); |
| 1656 |
res = pr_json_array_append_string(NULL, NULL, NULL); |
| 1657 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1658 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1659 |
strerror(errno), errno); |
| 1660 |
|
| 1661 |
mark_point(); |
| 1662 |
res = pr_json_array_append_string(p, NULL, NULL); |
| 1663 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1664 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1665 |
strerror(errno), errno); |
| 1666 |
|
| 1667 |
json = pr_json_array_alloc(p); |
| 1668 |
|
| 1669 |
mark_point(); |
| 1670 |
res = pr_json_array_append_string(p, json, NULL); |
| 1671 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1672 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1673 |
strerror(errno), errno); |
| 1674 |
|
| 1675 |
val = "foo!"; |
| 1676 |
|
| 1677 |
mark_point(); |
| 1678 |
res = pr_json_array_append_string(p, json, val); |
| 1679 |
ck_assert_msg(res == 0, "Failed to append val '%s': %s", val, |
| 1680 |
strerror(errno)); |
| 1681 |
|
| 1682 |
val = NULL; |
| 1683 |
|
| 1684 |
mark_point(); |
| 1685 |
res = pr_json_array_get_string(p, json, 0, (char **) &val); |
| 1686 |
ck_assert_msg(res == 0, "Failed to handle existing index 0: %s", |
| 1687 |
strerror(errno)); |
| 1688 |
ck_assert_msg(strcmp(val, "foo!") == 0, "Expected 'foo!', got '%s'", val); |
| 1689 |
|
| 1690 |
(void) pr_json_array_free(json); |
| 1691 |
} |
| 1692 |
END_TEST |
| 1693 |
|
| 1694 |
START_TEST(json_array_get_array_test) { |
| 1695 |
int res; |
| 1696 |
unsigned int idx; |
| 1697 |
const char *text; |
| 1698 |
char *expected = NULL, *str = NULL; |
| 1699 |
pr_json_array_t *json, *val = NULL; |
| 1700 |
|
| 1701 |
mark_point(); |
| 1702 |
res = pr_json_array_get_array(NULL, NULL, 0, NULL); |
| 1703 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1704 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1705 |
strerror(errno), errno); |
| 1706 |
|
| 1707 |
mark_point(); |
| 1708 |
res = pr_json_array_get_array(p, NULL, 0, NULL); |
| 1709 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1710 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1711 |
strerror(errno), errno); |
| 1712 |
|
| 1713 |
json = pr_json_array_alloc(p); |
| 1714 |
|
| 1715 |
mark_point(); |
| 1716 |
res = pr_json_array_get_array(p, json, 0, NULL); |
| 1717 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1718 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1719 |
strerror(errno), errno); |
| 1720 |
|
| 1721 |
idx = 0; |
| 1722 |
|
| 1723 |
mark_point(); |
| 1724 |
res = pr_json_array_get_array(p, json, idx, &val); |
| 1725 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1726 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1727 |
strerror(errno), errno); |
| 1728 |
|
| 1729 |
(void) pr_json_array_free(json); |
| 1730 |
|
| 1731 |
text = "[\"foo\",false,\"bar\",[\"baz\"]]"; |
| 1732 |
json = pr_json_array_from_text(p, text); |
| 1733 |
|
| 1734 |
mark_point(); |
| 1735 |
res = pr_json_array_get_array(p, json, idx, &val); |
| 1736 |
ck_assert_msg(res < 0, "Failed to handle non-array index %u", idx); |
| 1737 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1738 |
strerror(errno), errno); |
| 1739 |
|
| 1740 |
idx = 3; |
| 1741 |
val = NULL; |
| 1742 |
|
| 1743 |
mark_point(); |
| 1744 |
res = pr_json_array_get_array(p, json, idx, &val); |
| 1745 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1746 |
strerror(errno)); |
| 1747 |
ck_assert_msg(val != NULL, "Expected array, got null"); |
| 1748 |
|
| 1749 |
expected = "[\"baz\"]"; |
| 1750 |
str = pr_json_array_to_text(p, val, ""); |
| 1751 |
ck_assert_msg(strcmp(str, expected) == 0, |
| 1752 |
"Expected '%s', got '%s'", expected, str); |
| 1753 |
|
| 1754 |
mark_point(); |
| 1755 |
(void) pr_json_array_free(val); |
| 1756 |
|
| 1757 |
mark_point(); |
| 1758 |
(void) pr_json_array_free(json); |
| 1759 |
} |
| 1760 |
END_TEST |
| 1761 |
|
| 1762 |
START_TEST(json_array_append_array_test) { |
| 1763 |
int res; |
| 1764 |
unsigned int idx; |
| 1765 |
const char *text; |
| 1766 |
pr_json_array_t *json, *val = NULL; |
| 1767 |
|
| 1768 |
mark_point(); |
| 1769 |
res = pr_json_array_append_array(NULL, NULL, NULL); |
| 1770 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1771 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1772 |
strerror(errno), errno); |
| 1773 |
|
| 1774 |
mark_point(); |
| 1775 |
res = pr_json_array_append_array(p, NULL, NULL); |
| 1776 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1777 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1778 |
strerror(errno), errno); |
| 1779 |
|
| 1780 |
json = pr_json_array_alloc(p); |
| 1781 |
|
| 1782 |
mark_point(); |
| 1783 |
res = pr_json_array_append_array(p, json, NULL); |
| 1784 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1785 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1786 |
strerror(errno), errno); |
| 1787 |
|
| 1788 |
text = "[1, 1, 2, 3, 5, 8]"; |
| 1789 |
val = pr_json_array_from_text(p, text); |
| 1790 |
|
| 1791 |
mark_point(); |
| 1792 |
res = pr_json_array_append_array(p, json, val); |
| 1793 |
ck_assert_msg(res == 0, "Failed to append array: %s", strerror(errno)); |
| 1794 |
|
| 1795 |
val = NULL; |
| 1796 |
idx = 0; |
| 1797 |
|
| 1798 |
mark_point(); |
| 1799 |
res = pr_json_array_get_array(p, json, idx, &val); |
| 1800 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1801 |
strerror(errno)); |
| 1802 |
ck_assert_msg(val != NULL, "Expected array, got null"); |
| 1803 |
|
| 1804 |
mark_point(); |
| 1805 |
(void) pr_json_array_free(val); |
| 1806 |
(void) pr_json_array_free(json); |
| 1807 |
} |
| 1808 |
END_TEST |
| 1809 |
|
| 1810 |
START_TEST(json_array_get_object_test) { |
| 1811 |
int res; |
| 1812 |
unsigned int idx; |
| 1813 |
const char *text; |
| 1814 |
char *expected = NULL, *str = NULL; |
| 1815 |
pr_json_object_t *val = NULL; |
| 1816 |
pr_json_array_t *json; |
| 1817 |
|
| 1818 |
mark_point(); |
| 1819 |
res = pr_json_array_get_object(NULL, NULL, 0, NULL); |
| 1820 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1821 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1822 |
strerror(errno), errno); |
| 1823 |
|
| 1824 |
mark_point(); |
| 1825 |
res = pr_json_array_get_object(p, NULL, 0, NULL); |
| 1826 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1827 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1828 |
strerror(errno), errno); |
| 1829 |
|
| 1830 |
json = pr_json_array_alloc(p); |
| 1831 |
|
| 1832 |
mark_point(); |
| 1833 |
res = pr_json_array_get_object(p, json, 0, NULL); |
| 1834 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1835 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1836 |
strerror(errno), errno); |
| 1837 |
|
| 1838 |
idx = 0; |
| 1839 |
|
| 1840 |
mark_point(); |
| 1841 |
res = pr_json_array_get_object(p, json, idx, &val); |
| 1842 |
ck_assert_msg(res < 0, "Failed to handle nonexistent index %u", idx); |
| 1843 |
ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT, |
| 1844 |
strerror(errno), errno); |
| 1845 |
|
| 1846 |
(void) pr_json_array_free(json); |
| 1847 |
|
| 1848 |
text = "[\"foo\",false,\"bar\",{}]"; |
| 1849 |
json = pr_json_array_from_text(p, text); |
| 1850 |
|
| 1851 |
mark_point(); |
| 1852 |
res = pr_json_array_get_object(p, json, idx, &val); |
| 1853 |
ck_assert_msg(res < 0, "Failed to handle non-object index %u", idx); |
| 1854 |
ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST, |
| 1855 |
strerror(errno), errno); |
| 1856 |
|
| 1857 |
idx = 3; |
| 1858 |
val = NULL; |
| 1859 |
|
| 1860 |
mark_point(); |
| 1861 |
res = pr_json_array_get_object(p, json, idx, &val); |
| 1862 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1863 |
strerror(errno)); |
| 1864 |
ck_assert_msg(val != NULL, "Expected object, got null"); |
| 1865 |
|
| 1866 |
expected = "{}"; |
| 1867 |
str = pr_json_object_to_text(p, val, ""); |
| 1868 |
ck_assert_msg(strcmp(str, expected) == 0, |
| 1869 |
"Expected '%s', got '%s'", expected, str); |
| 1870 |
|
| 1871 |
mark_point(); |
| 1872 |
(void) pr_json_object_free(val); |
| 1873 |
|
| 1874 |
mark_point(); |
| 1875 |
(void) pr_json_array_free(json); |
| 1876 |
} |
| 1877 |
END_TEST |
| 1878 |
|
| 1879 |
START_TEST(json_array_append_object_test) { |
| 1880 |
int res; |
| 1881 |
unsigned int idx; |
| 1882 |
const char *text; |
| 1883 |
pr_json_object_t *val = NULL; |
| 1884 |
pr_json_array_t *json; |
| 1885 |
|
| 1886 |
mark_point(); |
| 1887 |
res = pr_json_array_append_object(NULL, NULL, NULL); |
| 1888 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1889 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1890 |
strerror(errno), errno); |
| 1891 |
|
| 1892 |
mark_point(); |
| 1893 |
res = pr_json_array_append_object(p, NULL, NULL); |
| 1894 |
ck_assert_msg(res < 0, "Failed to handle null json"); |
| 1895 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1896 |
strerror(errno), errno); |
| 1897 |
|
| 1898 |
json = pr_json_array_alloc(p); |
| 1899 |
|
| 1900 |
mark_point(); |
| 1901 |
res = pr_json_array_append_object(p, json, NULL); |
| 1902 |
ck_assert_msg(res < 0, "Failed to handle null val"); |
| 1903 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1904 |
strerror(errno), errno); |
| 1905 |
|
| 1906 |
text = "{\"foo\":1,\"bar\":2}"; |
| 1907 |
val = pr_json_object_from_text(p, text); |
| 1908 |
|
| 1909 |
mark_point(); |
| 1910 |
res = pr_json_array_append_object(p, json, val); |
| 1911 |
ck_assert_msg(res == 0, "Failed to append object: %s", strerror(errno)); |
| 1912 |
|
| 1913 |
val = NULL; |
| 1914 |
idx = 0; |
| 1915 |
|
| 1916 |
mark_point(); |
| 1917 |
res = pr_json_array_get_object(p, json, idx, &val); |
| 1918 |
ck_assert_msg(res == 0, "Failed to handle existing index %u: %s", idx, |
| 1919 |
strerror(errno)); |
| 1920 |
ck_assert_msg(val != NULL, "Expected object, got null"); |
| 1921 |
|
| 1922 |
mark_point(); |
| 1923 |
(void) pr_json_object_free(val); |
| 1924 |
(void) pr_json_array_free(json); |
| 1925 |
} |
| 1926 |
END_TEST |
| 1927 |
|
| 1928 |
START_TEST(json_text_validate_test) { |
| 1929 |
int res; |
| 1930 |
const char *text; |
| 1931 |
|
| 1932 |
mark_point(); |
| 1933 |
res = pr_json_text_validate(NULL, NULL); |
| 1934 |
ck_assert_msg(res < 0, "Failed to handle null pool"); |
| 1935 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1936 |
strerror(errno), errno); |
| 1937 |
|
| 1938 |
mark_point(); |
| 1939 |
res = pr_json_text_validate(p, NULL); |
| 1940 |
ck_assert_msg(res < 0, "Failed to handle null text"); |
| 1941 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1942 |
strerror(errno), errno); |
| 1943 |
|
| 1944 |
text = "foo bar"; |
| 1945 |
|
| 1946 |
mark_point(); |
| 1947 |
res = pr_json_text_validate(p, text); |
| 1948 |
ck_assert_msg(res == FALSE, "Failed to handle invalid text '%s'", text); |
| 1949 |
|
| 1950 |
text = "[{}]"; |
| 1951 |
|
| 1952 |
mark_point(); |
| 1953 |
res = pr_json_text_validate(p, text); |
| 1954 |
ck_assert_msg(res == TRUE, "Failed to handle valid text '%s'", text); |
| 1955 |
} |
| 1956 |
END_TEST |
| 1957 |
|
| 1958 |
START_TEST(json_type_name_test) { |
| 1959 |
const char *res, *expected; |
| 1960 |
|
| 1961 |
res = pr_json_type_name(0); |
| 1962 |
ck_assert_msg(res == NULL, "Failed to handle invalid JSON type ID 0"); |
| 1963 |
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, |
| 1964 |
strerror(errno), errno); |
| 1965 |
|
| 1966 |
expected = "boolean"; |
| 1967 |
res = pr_json_type_name(PR_JSON_TYPE_BOOL); |
| 1968 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_BOOL: %s", |
| 1969 |
strerror(errno)); |
| 1970 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1971 |
res); |
| 1972 |
|
| 1973 |
expected = "number"; |
| 1974 |
res = pr_json_type_name(PR_JSON_TYPE_NUMBER); |
| 1975 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_NUMBER: %s", |
| 1976 |
strerror(errno)); |
| 1977 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1978 |
res); |
| 1979 |
|
| 1980 |
expected = "null"; |
| 1981 |
res = pr_json_type_name(PR_JSON_TYPE_NULL); |
| 1982 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_NULL: %s", |
| 1983 |
strerror(errno)); |
| 1984 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1985 |
res); |
| 1986 |
|
| 1987 |
expected = "string"; |
| 1988 |
res = pr_json_type_name(PR_JSON_TYPE_STRING); |
| 1989 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_STRING: %s", |
| 1990 |
strerror(errno)); |
| 1991 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1992 |
res); |
| 1993 |
|
| 1994 |
expected = "array"; |
| 1995 |
res = pr_json_type_name(PR_JSON_TYPE_ARRAY); |
| 1996 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_ARRAY: %s", |
| 1997 |
strerror(errno)); |
| 1998 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 1999 |
res); |
| 2000 |
|
| 2001 |
expected = "object"; |
| 2002 |
res = pr_json_type_name(PR_JSON_TYPE_OBJECT); |
| 2003 |
ck_assert_msg(res != NULL, "Failed to handle JSON_TYPE_OBJECT: %s", |
| 2004 |
strerror(errno)); |
| 2005 |
ck_assert_msg(strcmp(res, expected) == 0, "Expected '%s', got '%s'", expected, |
| 2006 |
res); |
| 2007 |
} |
| 2008 |
END_TEST |
| 2009 |
|
| 2010 |
Suite *tests_get_json_suite(void) { |
| 2011 |
Suite *suite; |
| 2012 |
TCase *testcase; |
| 2013 |
|
| 2014 |
suite = suite_create("json"); |
| 2015 |
testcase = tcase_create("base"); |
| 2016 |
|
| 2017 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 2018 |
|
| 2019 |
tcase_add_test(testcase, json_object_free_test); |
| 2020 |
tcase_add_test(testcase, json_object_alloc_test); |
| 2021 |
tcase_add_test(testcase, json_object_from_text_test); |
| 2022 |
tcase_add_test(testcase, json_object_to_text_test); |
| 2023 |
tcase_add_test(testcase, json_object_foreach_test); |
| 2024 |
tcase_add_test(testcase, json_object_count_test); |
| 2025 |
tcase_add_test(testcase, json_object_exists_test); |
| 2026 |
tcase_add_test(testcase, json_object_remove_test); |
| 2027 |
tcase_add_test(testcase, json_object_get_bool_test); |
| 2028 |
tcase_add_test(testcase, json_object_set_bool_test); |
| 2029 |
tcase_add_test(testcase, json_object_get_null_test); |
| 2030 |
tcase_add_test(testcase, json_object_set_null_test); |
| 2031 |
tcase_add_test(testcase, json_object_get_number_test); |
| 2032 |
tcase_add_test(testcase, json_object_set_number_test); |
| 2033 |
tcase_add_test(testcase, json_object_get_string_test); |
| 2034 |
tcase_add_test(testcase, json_object_set_string_test); |
| 2035 |
tcase_add_test(testcase, json_object_get_array_test); |
| 2036 |
tcase_add_test(testcase, json_object_set_array_test); |
| 2037 |
tcase_add_test(testcase, json_object_get_object_test); |
| 2038 |
tcase_add_test(testcase, json_object_set_object_test); |
| 2039 |
|
| 2040 |
tcase_add_test(testcase, json_array_free_test); |
| 2041 |
tcase_add_test(testcase, json_array_alloc_test); |
| 2042 |
tcase_add_test(testcase, json_array_from_text_test); |
| 2043 |
tcase_add_test(testcase, json_array_to_text_test); |
| 2044 |
tcase_add_test(testcase, json_array_foreach_test); |
| 2045 |
tcase_add_test(testcase, json_array_count_test); |
| 2046 |
tcase_add_test(testcase, json_array_exists_test); |
| 2047 |
tcase_add_test(testcase, json_array_remove_test); |
| 2048 |
tcase_add_test(testcase, json_array_get_bool_test); |
| 2049 |
tcase_add_test(testcase, json_array_append_bool_test); |
| 2050 |
tcase_add_test(testcase, json_array_get_null_test); |
| 2051 |
tcase_add_test(testcase, json_array_append_null_test); |
| 2052 |
tcase_add_test(testcase, json_array_get_number_test); |
| 2053 |
tcase_add_test(testcase, json_array_append_number_test); |
| 2054 |
tcase_add_test(testcase, json_array_get_string_test); |
| 2055 |
tcase_add_test(testcase, json_array_append_string_test); |
| 2056 |
tcase_add_test(testcase, json_array_get_array_test); |
| 2057 |
tcase_add_test(testcase, json_array_append_array_test); |
| 2058 |
tcase_add_test(testcase, json_array_get_object_test); |
| 2059 |
tcase_add_test(testcase, json_array_append_object_test); |
| 2060 |
|
| 2061 |
tcase_add_test(testcase, json_text_validate_test); |
| 2062 |
tcase_add_test(testcase, json_type_name_test); |
| 2063 |
|
| 2064 |
suite_add_tcase(suite, testcase); |
| 2065 |
return suite; |
| 2066 |
} |