1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2014 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "config.h"
19 
20 #include <gtest/gtest.h>
21 #include <iostream>
22 #include <JSON_checker.h>
23 
24 class DeprecatedInterfaceValidatorTest : public ::testing::Test {
25 };
26 
27 #define CHECK_JSON(X) checkUTF8JSON((const unsigned char *)X, sizeof(X) - 1)
28 
TEST_F(DeprecatedInterfaceValidatorTest, SimpleJsonChecksOk)29 TEST_F(DeprecatedInterfaceValidatorTest, SimpleJsonChecksOk) {
30     EXPECT_TRUE(CHECK_JSON("{\"test\": 12}"));
31 }
32 
TEST_F(DeprecatedInterfaceValidatorTest, DeepJsonChecksOk)33 TEST_F(DeprecatedInterfaceValidatorTest, DeepJsonChecksOk) {
34     EXPECT_TRUE(CHECK_JSON(
35               "{\"test\": [[[[[[[[[[[[[[[[[[[[[[12]]]]]]]]]]]]]]]]]]]]]]}"));
36 }
37 
TEST_F(DeprecatedInterfaceValidatorTest, BadDeepJsonIsNotOK)38 TEST_F(DeprecatedInterfaceValidatorTest, BadDeepJsonIsNotOK) {
39     EXPECT_FALSE(CHECK_JSON(
40                  "{\"test\": [[[[[[[[[[[[[[[[[[[[[[12]]]]]]]]]]]]]]]]]]]]]]]]}"));
41 }
42 
TEST_F(DeprecatedInterfaceValidatorTest, BadJsonStartingWithBraceIsNotOk)43 TEST_F(DeprecatedInterfaceValidatorTest, BadJsonStartingWithBraceIsNotOk) {
44     EXPECT_FALSE(CHECK_JSON("{bad stuff}"));
45 }
46 
TEST_F(DeprecatedInterfaceValidatorTest, BareValuesAreOk)47 TEST_F(DeprecatedInterfaceValidatorTest, BareValuesAreOk) {
48     EXPECT_TRUE(CHECK_JSON("null"));
49 }
50 
TEST_F(DeprecatedInterfaceValidatorTest, BareNumbersAreOk)51 TEST_F(DeprecatedInterfaceValidatorTest, BareNumbersAreOk) {
52     EXPECT_TRUE(CHECK_JSON("99"));
53 }
TEST_F(DeprecatedInterfaceValidatorTest, BadUtf8IsNotOk)54 TEST_F(DeprecatedInterfaceValidatorTest, BadUtf8IsNotOk) {
55     EXPECT_FALSE(CHECK_JSON("{\"test\xFF\": 12}"));
56 }
57 
TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk)58 TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk) {
59     // MB-15778: Regression test for memory leaks.
60     unsigned char mb15778[] = {'"', 0xff, 0};
61     EXPECT_FALSE(CHECK_JSON(mb15778));
62 }
TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk2)63 TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk2) {
64     // MB-15778: Regression test for memory leaks.
65     unsigned char mb15778[] = {'"', 'a', 0xff, 0};
66     EXPECT_FALSE(CHECK_JSON(mb15778));
67 }
68 
TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk3)69 TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk3) {
70     // MB-15778: Regression test for memory leaks.
71     unsigned char mb15778[] = {'"', '1', '2', 0xfe, 0};
72     EXPECT_FALSE(CHECK_JSON(mb15778));
73 }
TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk4)74 TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk4) {
75     // MB-15778: Regression test for memory leaks.
76     unsigned char mb15778[] = {'"', '1', '2', 0xfd, 0};
77     EXPECT_FALSE(CHECK_JSON(mb15778));
78 }
TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk5)79 TEST_F(DeprecatedInterfaceValidatorTest, MB15778BadUtf8IsNotOk5) {
80     // MB-15778: Regression test for memory leaks.
81     unsigned char mb15778[] = {'{', '"', 'k', '"', ':', '"', 0xfc, 0};
82     EXPECT_FALSE(CHECK_JSON(mb15778));
83 }
84 
85 class ValidatorTest : public ::testing::Test {
86 protected:
87     JSON_checker::Validator validator;
88 };
89 
TEST_F(ValidatorTest, SimpleValidatorTest)90 TEST_F(ValidatorTest, SimpleValidatorTest) {
91     const uint8_t value1[] = "{\"test\": 12}";
92     EXPECT_TRUE(validator.validate(value1, sizeof(value1) - 1));
93     EXPECT_FALSE(validator.validate(value1, sizeof(value1) - 3));
94     EXPECT_TRUE(validator.validate(value1, sizeof(value1) - 1));
95 }
96 
TEST_F(ValidatorTest, ByteArrayValidatorTest)97 TEST_F(ValidatorTest, ByteArrayValidatorTest) {
98     const uint8_t value1[] = "{\"test\": 12}";
99     std::vector<uint8_t> data;
100     data.resize(sizeof(value1) - 1);
101     memcpy(data.data(), value1, sizeof(value1) - 1);
102     EXPECT_TRUE(validator.validate(data));
103     data.resize(data.size() - 1);
104     EXPECT_FALSE(validator.validate(data));
105     data.emplace_back('}');
106     EXPECT_TRUE(validator.validate(data));
107 }
108 
TEST_F(ValidatorTest, StringValidatorTest)109 TEST_F(ValidatorTest, StringValidatorTest) {
110     std::string value("{\"test\": 12}");
111     EXPECT_TRUE(validator.validate(value));
112     value.append("}");
113     EXPECT_FALSE(validator.validate(value));
114     value.resize(value.length() - 1);
115     EXPECT_TRUE(validator.validate(value));
116 }
117