jsoncheckertest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/document.h"
  16. using namespace rapidjson;
  17. static char* ReadFile(const char* filename, size_t& length) {
  18. const char *paths[] = {
  19. "jsonchecker",
  20. "bin/jsonchecker",
  21. "../bin/jsonchecker",
  22. "../../bin/jsonchecker",
  23. "../../../bin/jsonchecker"
  24. };
  25. char buffer[1024];
  26. FILE *fp = 0;
  27. for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  28. sprintf(buffer, "%s/%s", paths[i], filename);
  29. fp = fopen(buffer, "rb");
  30. if (fp)
  31. break;
  32. }
  33. if (!fp)
  34. return 0;
  35. fseek(fp, 0, SEEK_END);
  36. length = static_cast<size_t>(ftell(fp));
  37. fseek(fp, 0, SEEK_SET);
  38. char* json = static_cast<char*>(malloc(length + 1));
  39. size_t readLength = fread(json, 1, length, fp);
  40. json[readLength] = '\0';
  41. fclose(fp);
  42. return json;
  43. }
  44. struct NoOpHandler {
  45. bool Null() { return true; }
  46. bool Bool(bool) { return true; }
  47. bool Int(int) { return true; }
  48. bool Uint(unsigned) { return true; }
  49. bool Int64(int64_t) { return true; }
  50. bool Uint64(uint64_t) { return true; }
  51. bool Double(double) { return true; }
  52. bool RawNumber(const char*, SizeType, bool) { return true; }
  53. bool String(const char*, SizeType, bool) { return true; }
  54. bool StartObject() { return true; }
  55. bool Key(const char*, SizeType, bool) { return true; }
  56. bool EndObject(SizeType) { return true; }
  57. bool StartArray() { return true; }
  58. bool EndArray(SizeType) { return true; }
  59. };
  60. TEST(JsonChecker, Reader) {
  61. char filename[256];
  62. // jsonchecker/failXX.json
  63. for (int i = 1; i <= 33; i++) {
  64. if (i == 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
  65. continue;
  66. if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
  67. continue;
  68. sprintf(filename, "fail%d.json", i);
  69. size_t length;
  70. char* json = ReadFile(filename, length);
  71. if (!json) {
  72. printf("jsonchecker file %s not found", filename);
  73. ADD_FAILURE();
  74. continue;
  75. }
  76. // Test stack-based parsing.
  77. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  78. document.Parse(json);
  79. EXPECT_TRUE(document.HasParseError()) << filename;
  80. // Test iterative parsing.
  81. document.Parse<kParseIterativeFlag>(json);
  82. EXPECT_TRUE(document.HasParseError()) << filename;
  83. // Test iterative pull-parsing.
  84. Reader reader;
  85. StringStream ss(json);
  86. NoOpHandler h;
  87. reader.IterativeParseInit();
  88. while (!reader.IterativeParseComplete()) {
  89. if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
  90. break;
  91. }
  92. EXPECT_TRUE(reader.HasParseError()) << filename;
  93. free(json);
  94. }
  95. // passX.json
  96. for (int i = 1; i <= 3; i++) {
  97. sprintf(filename, "pass%d.json", i);
  98. size_t length;
  99. char* json = ReadFile(filename, length);
  100. if (!json) {
  101. printf("jsonchecker file %s not found", filename);
  102. continue;
  103. }
  104. // Test stack-based parsing.
  105. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  106. document.Parse(json);
  107. EXPECT_FALSE(document.HasParseError()) << filename;
  108. // Test iterative parsing.
  109. document.Parse<kParseIterativeFlag>(json);
  110. EXPECT_FALSE(document.HasParseError()) << filename;
  111. // Test iterative pull-parsing.
  112. Reader reader;
  113. StringStream ss(json);
  114. NoOpHandler h;
  115. reader.IterativeParseInit();
  116. while (!reader.IterativeParseComplete()) {
  117. if (!reader.IterativeParseNext<kParseDefaultFlags>(ss, h))
  118. break;
  119. }
  120. EXPECT_FALSE(reader.HasParseError()) << filename;
  121. free(json);
  122. }
  123. }