You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_json_patch.c 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include "strerror_override.h"
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "config.h"
  11. #include "json.h"
  12. #include "snprintf_compat.h"
  13. #ifndef PATH_MAX
  14. #define PATH_MAX 256
  15. #endif
  16. void test_json_patch_op(struct json_object *jo)
  17. {
  18. const char *comment = json_object_get_string(json_object_object_get(jo, "comment"));
  19. struct json_object *doc = json_object_object_get(jo, "doc");
  20. struct json_object *patch = json_object_object_get(jo, "patch");
  21. struct json_object *expected = json_object_object_get(jo, "expected");
  22. struct json_object *error = json_object_object_get(jo, "error");
  23. int disabled_test = json_object_get_boolean(json_object_object_get(jo, "disabled_in_json_c"));
  24. const char *error_s = json_object_get_string(error);
  25. struct json_object *res = NULL;
  26. int ret;
  27. printf("Testing '%s', doc '%s' patch '%s' : ",
  28. comment ? comment : error_s,
  29. json_object_get_string(doc),
  30. json_object_get_string(patch));
  31. if (disabled_test) {
  32. printf("SKIPPING - disabled in the test spec\n");
  33. return;
  34. }
  35. if (!error && !expected) {
  36. printf("SKIPPING - no expected or error conditions in test\n");
  37. return;
  38. }
  39. fflush(stdout);
  40. struct json_patch_error jperr;
  41. if (error) {
  42. assert(-1 == json_patch_apply(doc, patch, &res, &jperr));
  43. assert(jperr.errno_code != 0);
  44. printf("OK\n");
  45. printf(" => json_patch_apply failed as expected: %s at patch idx %zu: %s\n",
  46. strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
  47. json_object_put(res);
  48. } else {
  49. ret = json_patch_apply(doc, patch, &res, &jperr);
  50. if (ret) {
  51. fprintf(stderr, "json_patch_apply() returned '%d'\n", ret);
  52. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  53. fprintf(stderr, "Got: %s\n", json_object_get_string(res));
  54. fprintf(stderr, "json_patch_apply failed: %s at patch idx %zu: %s\n",
  55. strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
  56. fflush(stderr);
  57. assert(0);
  58. }
  59. assert(res != NULL);
  60. assert(jperr.errno_code == 0);
  61. ret = json_object_equal(expected, res);
  62. if (ret == 0) {
  63. fprintf(stderr, "json_object_equal() returned '%d'\n", ret);
  64. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  65. fprintf(stderr, "Got: %s\n", json_object_get_string(res));
  66. fflush(stderr);
  67. assert(0);
  68. }
  69. json_object_put(res);
  70. res = NULL;
  71. printf("OK\n");
  72. }
  73. }
  74. void test_json_patch_using_file(const char *testdir, const char *filename)
  75. {
  76. char full_filename[PATH_MAX];
  77. (void)snprintf(full_filename, sizeof(full_filename), "%s/%s", testdir, filename);
  78. size_t ii;
  79. json_object *jo = json_object_from_file(full_filename);
  80. if (!jo) {
  81. fprintf(stderr, "FAIL: unable to open %s: %s\n", full_filename, strerror(errno));
  82. exit(EXIT_FAILURE);
  83. }
  84. for (ii = 0; ii < json_object_array_length(jo); ii++) {
  85. struct json_object *jo1 = json_object_array_get_idx(jo, ii);
  86. test_json_patch_op(jo1);
  87. }
  88. json_object_put(jo);
  89. }
  90. int main(int argc, char **argv)
  91. {
  92. const char *testdir;
  93. if (argc < 2)
  94. {
  95. fprintf(stderr,
  96. "Usage: %s <testdir>\n"
  97. " <testdir> is the location of input files\n",
  98. argv[0]);
  99. return EXIT_FAILURE;
  100. }
  101. testdir = argv[1];
  102. // Test json_c_version.c
  103. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  104. {
  105. printf("FAIL: Output from json_c_version(): %s does not match %s",
  106. json_c_version(), JSON_C_VERSION);
  107. return EXIT_FAILURE;
  108. }
  109. if (json_c_version_num() != JSON_C_VERSION_NUM)
  110. {
  111. printf("FAIL: Output from json_c_version_num(): %d does not match %d",
  112. json_c_version_num(), JSON_C_VERSION_NUM);
  113. return EXIT_FAILURE;
  114. }
  115. test_json_patch_using_file(testdir, "json_patch_spec_tests.json");
  116. test_json_patch_using_file(testdir, "json_patch_tests.json");
  117. return 0;
  118. }