1 /* 2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz> 3 * Distributed under the terms of the MIT License. 4 */ 5 #include "JsonEndToEndTest.h" 6 7 #include <AutoDeleter.h> 8 9 #include <Json.h> 10 #include <JsonTextWriter.h> 11 12 #include <cppunit/TestCaller.h> 13 #include <cppunit/TestSuite.h> 14 15 #include "JsonSamples.h" 16 17 18 using namespace BPrivate; 19 20 21 JsonEndToEndTest::JsonEndToEndTest() 22 { 23 } 24 25 26 JsonEndToEndTest::~JsonEndToEndTest() 27 { 28 } 29 30 31 void 32 JsonEndToEndTest::TestParseAndWrite(const char* input, const char* expectedOutput) 33 { 34 BDataIO* inputData = new BMemoryIO(input, strlen(input)); 35 ObjectDeleter<BDataIO> inputDataDeleter(inputData); 36 BMallocIO* outputData = new BMallocIO(); 37 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 38 BPrivate::BJsonTextWriter* listener 39 = new BJsonTextWriter(outputData); 40 ObjectDeleter<BPrivate::BJsonTextWriter> listenerDeleter(listener); 41 42 // ---------------------- 43 BPrivate::BJson::Parse(inputData, listener); 44 // ---------------------- 45 46 CPPUNIT_ASSERT_EQUAL(B_OK, listener->ErrorStatus()); 47 fprintf(stderr, "in >%s<\n", input); 48 fprintf(stderr, "expected out >%s<\n", expectedOutput); 49 fprintf(stderr, "actual out >%s<\n", (char*)outputData->Buffer()); 50 CPPUNIT_ASSERT_MESSAGE("expected did no equal actual output", 51 0 == strncmp(expectedOutput, (char*)outputData->Buffer(), 52 strlen(expectedOutput))); 53 } 54 55 56 void 57 JsonEndToEndTest::TestNullA() 58 { 59 TestParseAndWrite(JSON_SAMPLE_NULL_A_IN, 60 JSON_SAMPLE_NULL_A_EXPECTED_OUT); 61 } 62 63 64 void 65 JsonEndToEndTest::TestTrueA() 66 { 67 TestParseAndWrite(JSON_SAMPLE_TRUE_A_IN, 68 JSON_SAMPLE_TRUE_A_EXPECTED_OUT); 69 } 70 71 72 void 73 JsonEndToEndTest::TestFalseA() 74 { 75 TestParseAndWrite(JSON_SAMPLE_FALSE_A_IN, 76 JSON_SAMPLE_FALSE_A_EXPECTED_OUT); 77 } 78 79 80 void 81 JsonEndToEndTest::TestNumberA() 82 { 83 TestParseAndWrite(JSON_SAMPLE_NUMBER_A_IN, 84 JSON_SAMPLE_NUMBER_A_EXPECTED_OUT); 85 } 86 87 88 void 89 JsonEndToEndTest::TestStringA() 90 { 91 TestParseAndWrite(JSON_SAMPLE_STRING_A_IN, 92 JSON_SAMPLE_STRING_A_EXPECTED_OUT); 93 } 94 95 96 void 97 JsonEndToEndTest::TestStringB() 98 { 99 TestParseAndWrite(JSON_SAMPLE_STRING_B_IN, 100 JSON_SAMPLE_STRING_B_EXPECTED_OUT); 101 } 102 103 104 /* In this test, there are some UTF-8 characters. */ 105 106 void 107 JsonEndToEndTest::TestStringA2() 108 { 109 TestParseAndWrite(JSON_SAMPLE_STRING_A2_IN, 110 JSON_SAMPLE_STRING_A_EXPECTED_OUT); 111 } 112 113 114 void 115 JsonEndToEndTest::TestArrayA() 116 { 117 TestParseAndWrite(JSON_SAMPLE_ARRAY_A_IN, JSON_SAMPLE_ARRAY_A_EXPECTED_OUT); 118 } 119 120 121 void 122 JsonEndToEndTest::TestArrayB() 123 { 124 TestParseAndWrite(JSON_SAMPLE_ARRAY_B_IN, JSON_SAMPLE_ARRAY_B_EXPECTED_OUT); 125 } 126 127 128 void 129 JsonEndToEndTest::TestObjectA() 130 { 131 TestParseAndWrite(JSON_SAMPLE_OBJECT_A_IN, 132 JSON_SAMPLE_OBJECT_A_EXPECTED_OUT); 133 } 134 135 136 /*! This method will test an element being unterminated; such an object that 137 is missing the terminating "}" symbol or a string that has no closing 138 quote. This is tested here because the writer 139 */ 140 141 void 142 JsonEndToEndTest::TestUnterminated(const char *input) 143 { 144 BDataIO* inputData = new BMemoryIO(input, strlen(input)); 145 ObjectDeleter<BDataIO> inputDataDeleter(inputData); 146 BMallocIO* outputData = new BMallocIO(); 147 ObjectDeleter<BMallocIO> outputDataDeleter(outputData); 148 BPrivate::BJsonTextWriter* listener 149 = new BJsonTextWriter(outputData); 150 ObjectDeleter<BPrivate::BJsonTextWriter> listenerDeleter(listener); 151 152 // ---------------------- 153 BPrivate::BJson::Parse(inputData, listener); 154 // ---------------------- 155 156 CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, listener->ErrorStatus()); 157 } 158 159 160 void 161 JsonEndToEndTest::TestStringUnterminated() 162 { 163 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_STRING); 164 } 165 166 void 167 JsonEndToEndTest::TestArrayUnterminated() 168 { 169 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_ARRAY); 170 } 171 172 void 173 JsonEndToEndTest::TestObjectUnterminated() 174 { 175 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_OBJECT); 176 } 177 178 179 /*static*/ void 180 JsonEndToEndTest::AddTests(BTestSuite& parent) 181 { 182 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("JsonEndToEndTest"); 183 184 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 185 "JsonEndToEndTest::TestNullA", &JsonEndToEndTest::TestNullA)); 186 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 187 "JsonEndToEndTest::TestTrueA", &JsonEndToEndTest::TestTrueA)); 188 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 189 "JsonEndToEndTest::TestFalseA", &JsonEndToEndTest::TestFalseA)); 190 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 191 "JsonEndToEndTest::TestNumberA", &JsonEndToEndTest::TestNumberA)); 192 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 193 "JsonEndToEndTest::TestStringA", &JsonEndToEndTest::TestStringA)); 194 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 195 "JsonEndToEndTest::TestStringA2", &JsonEndToEndTest::TestStringA2)); 196 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 197 "JsonEndToEndTest::TestStringB", &JsonEndToEndTest::TestStringB)); 198 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 199 "JsonEndToEndTest::TestArrayA", &JsonEndToEndTest::TestArrayA)); 200 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 201 "JsonEndToEndTest::TestArrayB", &JsonEndToEndTest::TestArrayB)); 202 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 203 "JsonEndToEndTest::TestObjectA", &JsonEndToEndTest::TestObjectA)); 204 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 205 "JsonEndToEndTest::TestStringUnterminated", 206 &JsonEndToEndTest::TestStringUnterminated)); 207 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 208 "JsonEndToEndTest::TestArrayUnterminated", 209 &JsonEndToEndTest::TestArrayUnterminated)); 210 suite.addTest(new CppUnit::TestCaller<JsonEndToEndTest>( 211 "JsonEndToEndTest::TestObjectUnterminated", 212 &JsonEndToEndTest::TestObjectUnterminated)); 213 214 parent.addTest("JsonEndToEndTest", &suite); 215 } 216