• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // JsonSerializer_unittests.cpp: Unit tests for the JSON based serializer
7 //
8 
9 #if !defined(ANGLE_HAS_RAPIDJSON)
10 #    error RapidJSON must be available to build this file.
11 #endif  // !defined(ANGLE_HAS_RAPIDJSON)
12 
13 #include "JsonSerializer.h"
14 
15 #include <gtest/gtest.h>
16 
17 class JsonSerializerTest : public ::testing::Test
18 {
19   protected:
20     void SetUp() override;
21     void check(const std::string &expect);
22 
23     angle::JsonSerializer js;
24 };
25 
26 // Test writing one integer value
TEST_F(JsonSerializerTest,NamedIntValue1)27 TEST_F(JsonSerializerTest, NamedIntValue1)
28 {
29     js.addScalar("test1", 1);
30 
31     const std::string expect =
32         R"({
33     "context": {
34         "test1": 1
35     }
36 })";
37     check(expect);
38 }
39 
40 // Test writing one long value
TEST_F(JsonSerializerTest,NamedLongValue)41 TEST_F(JsonSerializerTest, NamedLongValue)
42 {
43     long v = -12;
44     js.addScalar("test1", v);
45 
46     const std::string expect =
47         R"({
48     "context": {
49         "test1": -12
50     }
51 })";
52     check(expect);
53 }
54 
55 // Test writing one unsigned long value
TEST_F(JsonSerializerTest,NamedULongValue)56 TEST_F(JsonSerializerTest, NamedULongValue)
57 {
58     unsigned long v = 12;
59     js.addScalar("test1", v);
60 
61     const std::string expect =
62         R"({
63     "context": {
64         "test1": 12
65     }
66 })";
67     check(expect);
68 }
69 
70 // Test writing another integer value
TEST_F(JsonSerializerTest,NamedIntValue2)71 TEST_F(JsonSerializerTest, NamedIntValue2)
72 {
73     js.addScalar("test2", 2);
74 
75     const std::string expect =
76         R"({
77     "context": {
78         "test2": 2
79     }
80 })";
81 
82     check(expect);
83 }
84 
85 // Test writing one string value
TEST_F(JsonSerializerTest,NamedStringValue)86 TEST_F(JsonSerializerTest, NamedStringValue)
87 {
88     js.addCString("test2", "value");
89 
90     const std::string expect =
91         R"({
92     "context": {
93         "test2": "value"
94     }
95 })";
96 
97     check(expect);
98 }
99 
100 // Test writing one byte array
101 // Since he serialiter is only used for testing we don't store
102 // the whole byte array, but only it's SHA1 checksum
TEST_F(JsonSerializerTest,ByteArrayValue)103 TEST_F(JsonSerializerTest, ByteArrayValue)
104 {
105     const uint8_t value[5] = {10, 0, 0xcc, 0xff, 0xaa};
106     js.addBlob("test2", value, 5);
107 
108     const std::string expect =
109         R"({
110     "context": {
111         "test2-hash": "SHA1:4315724B1AB1EB2C0128E8E9DAD6D76254BA711D",
112         "test2-raw[0-4]": [
113             10,
114             0,
115             204,
116             255,
117             170
118         ]
119     }
120 })";
121 
122     check(expect);
123 }
124 
125 // Test writing one vector of integer values
TEST_F(JsonSerializerTest,IntVectorValue)126 TEST_F(JsonSerializerTest, IntVectorValue)
127 {
128     std::vector<int> v = {0, 1, -1};
129 
130     js.addVector("test2", v);
131 
132     const std::string expect =
133         R"({
134     "context": {
135         "test2": [
136             0,
137             1,
138             -1
139         ]
140     }
141 })";
142 
143     check(expect);
144 }
145 
146 // Test writing one vector of integer values
TEST_F(JsonSerializerTest,IntVectorAsBlobValue)147 TEST_F(JsonSerializerTest, IntVectorAsBlobValue)
148 {
149     std::vector<int> v = {0, 1, -1};
150 
151     js.addVectorAsHash("test2", v);
152     const std::string expect =
153         R"({
154     "context": {
155         "test2-hash": "SHA1:6216A439C16A113E2F1E53AB63FB88877D3597F5",
156         "test2-raw[0-11]": [
157             0,
158             0,
159             0,
160             0,
161             1,
162             0,
163             0,
164             0,
165             255,
166             255,
167             255,
168             255
169         ]
170     }
171 })";
172     check(expect);
173 }
174 
175 // Test unsorted input gets sorted
TEST_F(JsonSerializerTest,SortValues1)176 TEST_F(JsonSerializerTest, SortValues1)
177 {
178     js.addScalar("b", 1.0);
179     js.addScalar("a", 2.0);
180     const std::string expect =
181         R"({
182     "context": {
183         "a": 2.0,
184         "b": 1.0
185     }
186 })";
187     check(expect);
188 }
189 
190 // Test writing one vector of short integer values
TEST_F(JsonSerializerTest,ShortVectorAsBlobValue)191 TEST_F(JsonSerializerTest, ShortVectorAsBlobValue)
192 {
193     std::vector<short> v = {0, 1, -1};
194 
195     js.addVectorAsHash("test2", v);
196     const std::string expect =
197         R"({
198     "context": {
199         "test2-hash": "SHA1:0BA7C0DE700CE0F8018D084B8CF447B150A9465D",
200         "test2-raw[0-5]": [
201             0,
202             0,
203             1,
204             0,
205             255,
206             255
207         ]
208     }
209 })";
210     check(expect);
211 }
212 
213 // Test adding the same key twice
TEST_F(JsonSerializerTest,KeyUsedTwice)214 TEST_F(JsonSerializerTest, KeyUsedTwice)
215 {
216     js.addScalar("a", 1.0);
217     js.addScalar("a", 1.0);
218 
219     const std::string expect =
220         R"({
221     "context": {
222         "a": 1.0,
223         "a": 1.0
224     }
225 })";
226 
227     check(expect);
228 }
229 
230 // Test writing boolean values
TEST_F(JsonSerializerTest,NamedBoolValues)231 TEST_F(JsonSerializerTest, NamedBoolValues)
232 {
233     js.addScalar("test_false", false);
234     js.addScalar("test_true", true);
235 
236     const std::string expect =
237         R"({
238     "context": {
239         "test_false": false,
240         "test_true": true
241     }
242 })";
243 
244     check(expect);
245 }
246 
247 // test writing two values in a sub-group
TEST_F(JsonSerializerTest,GroupedIntValue)248 TEST_F(JsonSerializerTest, GroupedIntValue)
249 {
250     js.startGroup("group");
251     js.addScalar("test1", 1);
252     js.addScalar("test2", 2);
253     js.endGroup();
254 
255     const std::string expect =
256         R"({
257     "context": {
258         "group": {
259             "test1": 1,
260             "test2": 2
261         }
262     }
263 })";
264 
265     check(expect);
266 }
267 
SetUp()268 void JsonSerializerTest::SetUp()
269 {
270     js.startGroup("context");
271 }
272 
check(const std::string & expect)273 void JsonSerializerTest::check(const std::string &expect)
274 {
275     js.endGroup();
276     EXPECT_EQ(js.data(), expect);
277     EXPECT_EQ(js.length(), expect.length());
278     std::vector<uint8_t> expectAsUbyte(expect.begin(), expect.end());
279     EXPECT_EQ(js.getData(), expectAsUbyte);
280 }
281