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.4 kB

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