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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 = NULL;
  22. json_bool have_expected = json_object_object_get_ex(jo, "expected", &expected);
  23. struct json_object *error = json_object_object_get(jo, "error");
  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 (!error && !have_expected) {
  32. printf("BAD TEST - no expected or error conditions in test: %s\n", json_object_to_json_string(jo));
  33. assert(0);
  34. }
  35. fflush(stdout);
  36. struct json_patch_error jperr;
  37. if (error) {
  38. assert(-1 == json_patch_apply(doc, patch, &res, &jperr));
  39. assert(jperr.errno_code != 0);
  40. printf("OK\n");
  41. printf(" => json_patch_apply failed as expected: %s at patch idx %zu: %s\n",
  42. strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
  43. json_object_put(res);
  44. } else {
  45. ret = json_patch_apply(doc, patch, &res, &jperr);
  46. if (ret) {
  47. fprintf(stderr, "json_patch_apply() returned '%d'\n", ret);
  48. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  49. fprintf(stderr, "Got: %s\n", res ? json_object_get_string(res) : "(null)");
  50. fprintf(stderr, "json_patch_apply failed: %s at patch idx %zu: %s\n",
  51. strerror(jperr.errno_code), jperr.patch_failure_idx, jperr.errmsg);
  52. fflush(stderr);
  53. assert(0);
  54. }
  55. // Note: res might be NULL if the whole document was removed
  56. assert(jperr.errno_code == 0);
  57. ret = json_object_equal(expected, res);
  58. if (ret == 0) {
  59. fprintf(stderr, "json_object_equal() returned '%d'\n", ret);
  60. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  61. fprintf(stderr, "Got: %s\n", json_object_get_string(res));
  62. fflush(stderr);
  63. assert(0);
  64. }
  65. json_object_put(res);
  66. res = NULL;
  67. printf("OK\n");
  68. }
  69. }
  70. void test_json_patch_using_file(const char *testdir, const char *filename)
  71. {
  72. char full_filename[PATH_MAX];
  73. (void)snprintf(full_filename, sizeof(full_filename), "%s/%s", testdir, filename);
  74. size_t ii;
  75. printf("Testing using file %s\n", filename);
  76. json_object *jo = json_object_from_file(full_filename);
  77. if (!jo) {
  78. fprintf(stderr, "FAIL: unable to open %s: %s\n", full_filename, strerror(errno));
  79. exit(EXIT_FAILURE);
  80. }
  81. for (ii = 0; ii < json_object_array_length(jo); ii++) {
  82. struct json_object *jo1 = json_object_array_get_idx(jo, ii);
  83. test_json_patch_op(jo1);
  84. }
  85. json_object_put(jo);
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. const char *testdir;
  90. if (argc < 2)
  91. {
  92. fprintf(stderr,
  93. "Usage: %s <testdir>\n"
  94. " <testdir> is the location of input files\n",
  95. argv[0]);
  96. return EXIT_FAILURE;
  97. }
  98. testdir = argv[1];
  99. // Test json_c_version.c
  100. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  101. {
  102. printf("FAIL: Output from json_c_version(): %s does not match %s",
  103. json_c_version(), JSON_C_VERSION);
  104. return EXIT_FAILURE;
  105. }
  106. if (json_c_version_num() != JSON_C_VERSION_NUM)
  107. {
  108. printf("FAIL: Output from json_c_version_num(): %d does not match %d",
  109. json_c_version_num(), JSON_C_VERSION_NUM);
  110. return EXIT_FAILURE;
  111. }
  112. test_json_patch_using_file(testdir, "json_patch_spec_tests.json");
  113. test_json_patch_using_file(testdir, "json_patch_tests.json");
  114. return 0;
  115. }