11f6b57a5SAndrew Lindesay /*
21f6b57a5SAndrew Lindesay * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
31f6b57a5SAndrew Lindesay * Distributed under the terms of the MIT License.
41f6b57a5SAndrew Lindesay */
51f6b57a5SAndrew Lindesay #include "JsonToMessageTest.h"
61f6b57a5SAndrew Lindesay
71f6b57a5SAndrew Lindesay #include <Json.h>
81f6b57a5SAndrew Lindesay
91f6b57a5SAndrew Lindesay #include <cppunit/TestCaller.h>
101f6b57a5SAndrew Lindesay #include <cppunit/TestSuite.h>
111f6b57a5SAndrew Lindesay
121f6b57a5SAndrew Lindesay #include "JsonSamples.h"
131f6b57a5SAndrew Lindesay
141f6b57a5SAndrew Lindesay
151f6b57a5SAndrew Lindesay #define LOOPS_TEST_OBJECT_A_FOR_PERFORMANCE 100000
161f6b57a5SAndrew Lindesay
171f6b57a5SAndrew Lindesay using namespace BPrivate;
181f6b57a5SAndrew Lindesay
191f6b57a5SAndrew Lindesay
JsonToMessageTest()201f6b57a5SAndrew Lindesay JsonToMessageTest::JsonToMessageTest()
211f6b57a5SAndrew Lindesay {
221f6b57a5SAndrew Lindesay }
231f6b57a5SAndrew Lindesay
241f6b57a5SAndrew Lindesay
~JsonToMessageTest()251f6b57a5SAndrew Lindesay JsonToMessageTest::~JsonToMessageTest()
261f6b57a5SAndrew Lindesay {
271f6b57a5SAndrew Lindesay }
281f6b57a5SAndrew Lindesay
291f6b57a5SAndrew Lindesay
301f6b57a5SAndrew Lindesay void
TestArrayA()311f6b57a5SAndrew Lindesay JsonToMessageTest::TestArrayA()
321f6b57a5SAndrew Lindesay {
331f6b57a5SAndrew Lindesay BMessage message;
341f6b57a5SAndrew Lindesay BMessage subMessage;
351f6b57a5SAndrew Lindesay BString stringValue;
361f6b57a5SAndrew Lindesay double doubleValue;
371f6b57a5SAndrew Lindesay
381f6b57a5SAndrew Lindesay // ----------------------
391f6b57a5SAndrew Lindesay BJson::Parse(JSON_SAMPLE_ARRAY_A_IN, message);
401f6b57a5SAndrew Lindesay // ----------------------
411f6b57a5SAndrew Lindesay
421f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [0]", B_OK,
431f6b57a5SAndrew Lindesay message.FindString("0", &stringValue));
441f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [0]", BString("1234"), stringValue);
451f6b57a5SAndrew Lindesay
461f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [1]", B_OK,
471f6b57a5SAndrew Lindesay message.FindDouble("1", &doubleValue));
481f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [1]", 4567, doubleValue);
491f6b57a5SAndrew Lindesay
501f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2]", B_OK,
511f6b57a5SAndrew Lindesay message.FindMessage("2", &subMessage));
521f6b57a5SAndrew Lindesay
531f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.0]", B_OK,
541f6b57a5SAndrew Lindesay subMessage.FindString("0", &stringValue));
551f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.0]", BString("A"), stringValue);
561f6b57a5SAndrew Lindesay
571f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.1]", B_OK,
581f6b57a5SAndrew Lindesay subMessage.FindString("1", &stringValue));
591f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.1]", BString("b"), stringValue);
601f6b57a5SAndrew Lindesay
611f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.2]", B_OK,
621f6b57a5SAndrew Lindesay subMessage.FindString("2", &stringValue));
631f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.2]", BString("C\xc3\xa9zanne"),
641f6b57a5SAndrew Lindesay stringValue);
651f6b57a5SAndrew Lindesay }
661f6b57a5SAndrew Lindesay
671f6b57a5SAndrew Lindesay
681f6b57a5SAndrew Lindesay void
TestArrayB()691f6b57a5SAndrew Lindesay JsonToMessageTest::TestArrayB()
701f6b57a5SAndrew Lindesay {
711f6b57a5SAndrew Lindesay BMessage message;
721f6b57a5SAndrew Lindesay BMessage subMessage;
731f6b57a5SAndrew Lindesay BMessage subSubMessage;
741f6b57a5SAndrew Lindesay BString stringValue;
751f6b57a5SAndrew Lindesay
761f6b57a5SAndrew Lindesay // ----------------------
771f6b57a5SAndrew Lindesay BJson::Parse(JSON_SAMPLE_ARRAY_B_IN, message);
781f6b57a5SAndrew Lindesay // ----------------------
791f6b57a5SAndrew Lindesay
801f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [0]", B_OK,
811f6b57a5SAndrew Lindesay message.FindString("0", &stringValue));
821f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [0]", BString("Whirinaki"), stringValue);
831f6b57a5SAndrew Lindesay
841f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [1]", B_OK,
851f6b57a5SAndrew Lindesay message.FindString("1", &stringValue));
861f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [1]", BString("Wellington"), stringValue);
871f6b57a5SAndrew Lindesay
881f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2]", B_OK,
891f6b57a5SAndrew Lindesay message.FindMessage("2", &subMessage));
901f6b57a5SAndrew Lindesay
911f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.0]", B_OK,
921f6b57a5SAndrew Lindesay subMessage.FindMessage("key", &subSubMessage));
931f6b57a5SAndrew Lindesay
941f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.0.0]", B_OK,
951f6b57a5SAndrew Lindesay subSubMessage.FindString("0", &stringValue));
961f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.0.0]", BString("value"), stringValue);
971f6b57a5SAndrew Lindesay }
981f6b57a5SAndrew Lindesay
991f6b57a5SAndrew Lindesay
1001f6b57a5SAndrew Lindesay void
TestObjectA()1011f6b57a5SAndrew Lindesay JsonToMessageTest::TestObjectA()
1021f6b57a5SAndrew Lindesay {
1031f6b57a5SAndrew Lindesay BMessage message;
1041f6b57a5SAndrew Lindesay BMessage subMessage;
1051f6b57a5SAndrew Lindesay BString stringValue;
1061f6b57a5SAndrew Lindesay
1071f6b57a5SAndrew Lindesay // ----------------------
1081f6b57a5SAndrew Lindesay BJson::Parse(JSON_SAMPLE_OBJECT_A_IN, message);
1091f6b57a5SAndrew Lindesay // ----------------------
1101f6b57a5SAndrew Lindesay
1111f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindString("weather", &stringValue));
1121f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("raining"), stringValue);
1131f6b57a5SAndrew Lindesay
1141f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindString("humidity", &stringValue));
1151f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("too-high"), stringValue);
1161f6b57a5SAndrew Lindesay
1171f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("daysOfWeek", &subMessage));
1181f6b57a5SAndrew Lindesay
1191f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindString("0", &stringValue));
1201f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("MON"), stringValue);
1211f6b57a5SAndrew Lindesay
1221f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindString("1", &stringValue));
1231f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("TUE"), stringValue);
1241f6b57a5SAndrew Lindesay
1251f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindString("2", &stringValue));
1261f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("WED"), stringValue);
1271f6b57a5SAndrew Lindesay
1281f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindString("3", &stringValue));
1291f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("THR"), stringValue);
1301f6b57a5SAndrew Lindesay
1311f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindString("4", &stringValue));
1321f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("FRI"), stringValue);
1331f6b57a5SAndrew Lindesay }
1341f6b57a5SAndrew Lindesay
1351f6b57a5SAndrew Lindesay
1361f6b57a5SAndrew Lindesay /*! This is not a real test, but is a convenient point at which to implement a
1371f6b57a5SAndrew Lindesay performance test.
1381f6b57a5SAndrew Lindesay */
1391f6b57a5SAndrew Lindesay
1401f6b57a5SAndrew Lindesay void
TestObjectAForPerformance()1411f6b57a5SAndrew Lindesay JsonToMessageTest::TestObjectAForPerformance()
1421f6b57a5SAndrew Lindesay {
1431f6b57a5SAndrew Lindesay int i;
1441f6b57a5SAndrew Lindesay
1451f6b57a5SAndrew Lindesay for (i=0; i < LOOPS_TEST_OBJECT_A_FOR_PERFORMANCE; i++)
1461f6b57a5SAndrew Lindesay TestObjectA();
1471f6b57a5SAndrew Lindesay }
1481f6b57a5SAndrew Lindesay
1491f6b57a5SAndrew Lindesay
1501f6b57a5SAndrew Lindesay void
TestObjectB()1511f6b57a5SAndrew Lindesay JsonToMessageTest::TestObjectB()
1521f6b57a5SAndrew Lindesay {
1531f6b57a5SAndrew Lindesay BMessage message;
1541f6b57a5SAndrew Lindesay bool boolValue;
1551f6b57a5SAndrew Lindesay void *ptrValue; // this is how NULL is represented.
1561f6b57a5SAndrew Lindesay
1571f6b57a5SAndrew Lindesay // ----------------------
1581f6b57a5SAndrew Lindesay BJson::Parse(JSON_SAMPLE_OBJECT_B_IN, message);
1591f6b57a5SAndrew Lindesay // ----------------------
1601f6b57a5SAndrew Lindesay
1611f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindBool("testTrue", &boolValue));
1621f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(true, boolValue);
1631f6b57a5SAndrew Lindesay
1641f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindBool("testFalse", &boolValue));
1651f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(false, boolValue);
1661f6b57a5SAndrew Lindesay
1671f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindPointer("testNull", &ptrValue));
168*221b77b6SJérôme Duval CPPUNIT_ASSERT_EQUAL(0, (addr_t)ptrValue);
1691f6b57a5SAndrew Lindesay }
1701f6b57a5SAndrew Lindesay
1711f6b57a5SAndrew Lindesay
1721f6b57a5SAndrew Lindesay void
TestObjectC()1731f6b57a5SAndrew Lindesay JsonToMessageTest::TestObjectC()
1741f6b57a5SAndrew Lindesay {
1751f6b57a5SAndrew Lindesay BMessage message;
1761f6b57a5SAndrew Lindesay BMessage subMessage;
1771f6b57a5SAndrew Lindesay BString stringValue;
1781f6b57a5SAndrew Lindesay
1791f6b57a5SAndrew Lindesay // ----------------------
1801f6b57a5SAndrew Lindesay BJson::Parse(JSON_SAMPLE_OBJECT_C_IN, message);
1811f6b57a5SAndrew Lindesay // ----------------------
1821f6b57a5SAndrew Lindesay
1831f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, message.FindString("key", &stringValue));
1841f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString("value"), stringValue);
1851f6b57a5SAndrew Lindesay }
1861f6b57a5SAndrew Lindesay
1871f6b57a5SAndrew Lindesay
1881f6b57a5SAndrew Lindesay void
TestUnterminatedObject()1891f6b57a5SAndrew Lindesay JsonToMessageTest::TestUnterminatedObject()
1901f6b57a5SAndrew Lindesay {
1911f6b57a5SAndrew Lindesay BMessage message;
1921f6b57a5SAndrew Lindesay
1931f6b57a5SAndrew Lindesay // ----------------------
1941f6b57a5SAndrew Lindesay status_t result = BJson::Parse(JSON_SAMPLE_BROKEN_UNTERMINATED_OBJECT,
1951f6b57a5SAndrew Lindesay message);
1961f6b57a5SAndrew Lindesay // ----------------------
1971f6b57a5SAndrew Lindesay
1981f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, result);
1991f6b57a5SAndrew Lindesay }
2001f6b57a5SAndrew Lindesay
2011f6b57a5SAndrew Lindesay
2021f6b57a5SAndrew Lindesay void
TestUnterminatedArray()2031f6b57a5SAndrew Lindesay JsonToMessageTest::TestUnterminatedArray()
2041f6b57a5SAndrew Lindesay {
2051f6b57a5SAndrew Lindesay BMessage message;
2061f6b57a5SAndrew Lindesay
2071f6b57a5SAndrew Lindesay // ----------------------
2081f6b57a5SAndrew Lindesay status_t result = BJson::Parse(JSON_SAMPLE_BROKEN_UNTERMINATED_ARRAY,
2091f6b57a5SAndrew Lindesay message);
2101f6b57a5SAndrew Lindesay // ----------------------
2111f6b57a5SAndrew Lindesay
2121f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, result);
2131f6b57a5SAndrew Lindesay }
2141f6b57a5SAndrew Lindesay
2151f6b57a5SAndrew Lindesay
TestHaikuDepotFetchBatch()2161f6b57a5SAndrew Lindesay void JsonToMessageTest::TestHaikuDepotFetchBatch()
2171f6b57a5SAndrew Lindesay {
2181f6b57a5SAndrew Lindesay const unsigned char input[] = JSON_SAMPLE_HDS_FETCH_BATCH_PKGS;
2191f6b57a5SAndrew Lindesay BMessage message;
2201f6b57a5SAndrew Lindesay BMessage resultMessage;
2211f6b57a5SAndrew Lindesay BMessage pkgsMessage;
2221f6b57a5SAndrew Lindesay BMessage pkgMessage1;
2231f6b57a5SAndrew Lindesay double modifyTimestampDouble;
2241f6b57a5SAndrew Lindesay double expectedModifyTimestampDouble = 1488785331631.0;
2251f6b57a5SAndrew Lindesay
2261f6b57a5SAndrew Lindesay // ----------------------
2271f6b57a5SAndrew Lindesay status_t result = BJson::Parse((char*)input,
2281f6b57a5SAndrew Lindesay message);
2291f6b57a5SAndrew Lindesay // ----------------------
2301f6b57a5SAndrew Lindesay
2311f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, result);
2321f6b57a5SAndrew Lindesay
2331f6b57a5SAndrew Lindesay // this is quite a large test input so a random sample "thing to
2341f6b57a5SAndrew Lindesay // check" is chosen to indicate that the parse was successful.
2351f6b57a5SAndrew Lindesay
2361f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("result", B_OK,
2371f6b57a5SAndrew Lindesay message.FindMessage("result", &resultMessage));
2381f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("pkgs", B_OK,
2391f6b57a5SAndrew Lindesay resultMessage.FindMessage("pkgs", &pkgsMessage));
2401f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("1", B_OK,
2411f6b57a5SAndrew Lindesay pkgsMessage.FindMessage("1", &pkgMessage1));
2421f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL_MESSAGE("modifyTimestamp", B_OK,
2431f6b57a5SAndrew Lindesay pkgMessage1.FindDouble("modifyTimestamp", &modifyTimestampDouble));
2441f6b57a5SAndrew Lindesay
2451f6b57a5SAndrew Lindesay CPPUNIT_ASSERT_EQUAL(expectedModifyTimestampDouble, modifyTimestampDouble);
2461f6b57a5SAndrew Lindesay }
2471f6b57a5SAndrew Lindesay
2481f6b57a5SAndrew Lindesay
2491f6b57a5SAndrew Lindesay /*static*/ void
AddTests(BTestSuite & parent)2501f6b57a5SAndrew Lindesay JsonToMessageTest::AddTests(BTestSuite& parent)
2511f6b57a5SAndrew Lindesay {
2521f6b57a5SAndrew Lindesay CppUnit::TestSuite& suite = *new CppUnit::TestSuite(
2531f6b57a5SAndrew Lindesay "JsonToMessageTest");
2541f6b57a5SAndrew Lindesay
2551f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2561f6b57a5SAndrew Lindesay "JsonToMessageTest::TestArrayA",
2571f6b57a5SAndrew Lindesay &JsonToMessageTest::TestArrayA));
2581f6b57a5SAndrew Lindesay
2591f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2601f6b57a5SAndrew Lindesay "JsonToMessageTest::TestArrayB",
2611f6b57a5SAndrew Lindesay &JsonToMessageTest::TestArrayB));
2621f6b57a5SAndrew Lindesay
2631f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2641f6b57a5SAndrew Lindesay "JsonToMessageTest::TestObjectA",
2651f6b57a5SAndrew Lindesay &JsonToMessageTest::TestObjectA));
2661f6b57a5SAndrew Lindesay
2671f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2681f6b57a5SAndrew Lindesay "JsonToMessageTest::TestObjectB",
2691f6b57a5SAndrew Lindesay &JsonToMessageTest::TestObjectB));
2701f6b57a5SAndrew Lindesay
2711f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2721f6b57a5SAndrew Lindesay "JsonToMessageTest::TestObjectC",
2731f6b57a5SAndrew Lindesay &JsonToMessageTest::TestObjectC));
2741f6b57a5SAndrew Lindesay
2751f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2761f6b57a5SAndrew Lindesay "JsonToMessageTest::TestUnterminatedObject",
2771f6b57a5SAndrew Lindesay &JsonToMessageTest::TestUnterminatedObject));
2781f6b57a5SAndrew Lindesay
2791f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2801f6b57a5SAndrew Lindesay "JsonToMessageTest::TestUnterminatedArray",
2811f6b57a5SAndrew Lindesay &JsonToMessageTest::TestUnterminatedArray));
2821f6b57a5SAndrew Lindesay
2831f6b57a5SAndrew Lindesay suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2841f6b57a5SAndrew Lindesay "JsonToMessageTest::TestHaikuDepotFetchBatch",
2851f6b57a5SAndrew Lindesay &JsonToMessageTest::TestHaikuDepotFetchBatch));
2861f6b57a5SAndrew Lindesay
2871f6b57a5SAndrew Lindesay // suite.addTest(new CppUnit::TestCaller<JsonToMessageTest>(
2881f6b57a5SAndrew Lindesay // "JsonToMessageTest::TestObjectAForPerformance",
2891f6b57a5SAndrew Lindesay // &JsonToMessageTest::TestObjectAForPerformance));
2901f6b57a5SAndrew Lindesay
2911f6b57a5SAndrew Lindesay parent.addTest("JsonToMessageTest", &suite);
2921f6b57a5SAndrew Lindesay }
293