Home | History | Annotate | Download | only in unittest
      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 
     15 #include "unittest.h"
     16 
     17 #include "rapidjson/document.h"
     18 
     19 using namespace rapidjson;
     20 
     21 static char* ReadFile(const char* filename, size_t& length) {
     22     const char *paths[] = {
     23         "jsonchecker/%s",
     24         "bin/jsonchecker/%s",
     25         "../bin/jsonchecker/%s",
     26         "../../bin/jsonchecker/%s",
     27         "../../../bin/jsonchecker/%s"
     28     };
     29     char buffer[1024];
     30     FILE *fp = 0;
     31     for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
     32         sprintf(buffer, paths[i], filename);
     33         fp = fopen(buffer, "rb");
     34         if (fp)
     35             break;
     36     }
     37 
     38     if (!fp)
     39         return 0;
     40 
     41     fseek(fp, 0, SEEK_END);
     42     length = (size_t)ftell(fp);
     43     fseek(fp, 0, SEEK_SET);
     44     char* json = (char*)malloc(length + 1);
     45     size_t readLength = fread(json, 1, length, fp);
     46     json[readLength] = '\0';
     47     fclose(fp);
     48     return json;
     49 }
     50 
     51 TEST(JsonChecker, Reader) {
     52     char filename[256];
     53 
     54     // jsonchecker/failXX.json
     55     for (int i = 1; i <= 33; i++) {
     56         if (i == 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
     57             continue;
     58         if (i == 18)    // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
     59             continue;
     60 
     61         sprintf(filename, "fail%d.json", i);
     62         size_t length;
     63         char* json = ReadFile(filename, length);
     64         if (!json) {
     65             printf("jsonchecker file %s not found", filename);
     66             ADD_FAILURE();
     67             continue;
     68         }
     69 
     70         GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
     71         document.Parse((const char*)json);
     72         EXPECT_TRUE(document.HasParseError());
     73 
     74         document.Parse<kParseIterativeFlag>((const char*)json);
     75         EXPECT_TRUE(document.HasParseError());
     76 
     77         free(json);
     78     }
     79 
     80     // passX.json
     81     for (int i = 1; i <= 3; i++) {
     82         sprintf(filename, "pass%d.json", i);
     83         size_t length;
     84         char* json = ReadFile(filename, length);
     85         if (!json) {
     86             printf("jsonchecker file %s not found", filename);
     87             continue;
     88         }
     89 
     90         GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
     91         document.Parse((const char*)json);
     92         EXPECT_FALSE(document.HasParseError());
     93 
     94         document.Parse<kParseIterativeFlag>((const char*)json);
     95         EXPECT_FALSE(document.HasParseError());
     96 
     97         free(json);
     98     }
     99 }
    100