vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / shared / JsonEndToEndTest.cpp
blob4f7d9fe6bb44008e15afd4d2b08fed606d635e21
1 /*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
3 * Distributed under the terms of the MIT License.
4 */
5 #include "JsonEndToEndTest.h"
7 #include <AutoDeleter.h>
9 #include <Json.h>
10 #include <JsonTextWriter.h>
12 #include <cppunit/TestCaller.h>
13 #include <cppunit/TestSuite.h>
15 #include "JsonSamples.h"
18 using namespace BPrivate;
21 JsonEndToEndTest::JsonEndToEndTest()
26 JsonEndToEndTest::~JsonEndToEndTest()
31 void
32 JsonEndToEndTest::TestParseAndWrite(const char* input, const char* expectedOutput)
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);
42 // ----------------------
43 BPrivate::BJson::Parse(inputData, listener);
44 // ----------------------
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)));
56 void
57 JsonEndToEndTest::TestNullA()
59 TestParseAndWrite(JSON_SAMPLE_NULL_A_IN,
60 JSON_SAMPLE_NULL_A_EXPECTED_OUT);
64 void
65 JsonEndToEndTest::TestTrueA()
67 TestParseAndWrite(JSON_SAMPLE_TRUE_A_IN,
68 JSON_SAMPLE_TRUE_A_EXPECTED_OUT);
72 void
73 JsonEndToEndTest::TestFalseA()
75 TestParseAndWrite(JSON_SAMPLE_FALSE_A_IN,
76 JSON_SAMPLE_FALSE_A_EXPECTED_OUT);
80 void
81 JsonEndToEndTest::TestNumberA()
83 TestParseAndWrite(JSON_SAMPLE_NUMBER_A_IN,
84 JSON_SAMPLE_NUMBER_A_EXPECTED_OUT);
88 void
89 JsonEndToEndTest::TestStringA()
91 TestParseAndWrite(JSON_SAMPLE_STRING_A_IN,
92 JSON_SAMPLE_STRING_A_EXPECTED_OUT);
96 void
97 JsonEndToEndTest::TestStringB()
99 TestParseAndWrite(JSON_SAMPLE_STRING_B_IN,
100 JSON_SAMPLE_STRING_B_EXPECTED_OUT);
104 /* In this test, there are some UTF-8 characters. */
106 void
107 JsonEndToEndTest::TestStringA2()
109 TestParseAndWrite(JSON_SAMPLE_STRING_A2_IN,
110 JSON_SAMPLE_STRING_A_EXPECTED_OUT);
114 void
115 JsonEndToEndTest::TestArrayA()
117 TestParseAndWrite(JSON_SAMPLE_ARRAY_A_IN, JSON_SAMPLE_ARRAY_A_EXPECTED_OUT);
121 void
122 JsonEndToEndTest::TestArrayB()
124 TestParseAndWrite(JSON_SAMPLE_ARRAY_B_IN, JSON_SAMPLE_ARRAY_B_EXPECTED_OUT);
128 void
129 JsonEndToEndTest::TestObjectA()
131 TestParseAndWrite(JSON_SAMPLE_OBJECT_A_IN,
132 JSON_SAMPLE_OBJECT_A_EXPECTED_OUT);
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
141 void
142 JsonEndToEndTest::TestUnterminated(const char *input)
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);
152 // ----------------------
153 BPrivate::BJson::Parse(inputData, listener);
154 // ----------------------
156 CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, listener->ErrorStatus());
160 void
161 JsonEndToEndTest::TestStringUnterminated()
163 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_STRING);
166 void
167 JsonEndToEndTest::TestArrayUnterminated()
169 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_ARRAY);
172 void
173 JsonEndToEndTest::TestObjectUnterminated()
175 TestUnterminated(JSON_SAMPLE_BROKEN_UNTERMINATED_OBJECT);
179 /*static*/ void
180 JsonEndToEndTest::AddTests(BTestSuite& parent)
182 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("JsonEndToEndTest");
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));
214 parent.addTest("JsonEndToEndTest", &suite);