jsoncheckertest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. TEST(JsonChecker, Reader) {
  45. char filename[256];
  46. // jsonchecker/failXX.json
  47. for (int i = 1; i <= 33; i++) {
  48. if (i == 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
  49. continue;
  50. if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
  51. continue;
  52. sprintf(filename, "fail%d.json", i);
  53. size_t length;
  54. char* json = ReadFile(filename, length);
  55. if (!json) {
  56. printf("jsonchecker file %s not found", filename);
  57. ADD_FAILURE();
  58. continue;
  59. }
  60. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  61. document.Parse(json);
  62. EXPECT_TRUE(document.HasParseError());
  63. document.Parse<kParseIterativeFlag>(json);
  64. EXPECT_TRUE(document.HasParseError());
  65. free(json);
  66. }
  67. // passX.json
  68. for (int i = 1; i <= 3; i++) {
  69. sprintf(filename, "pass%d.json", i);
  70. size_t length;
  71. char* json = ReadFile(filename, length);
  72. if (!json) {
  73. printf("jsonchecker file %s not found", filename);
  74. continue;
  75. }
  76. GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
  77. document.Parse(json);
  78. EXPECT_FALSE(document.HasParseError());
  79. document.Parse<kParseIterativeFlag>(json);
  80. EXPECT_FALSE(document.HasParseError());
  81. free(json);
  82. }
  83. }