-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainerTest.cpp
243 lines (202 loc) · 7.44 KB
/
ContainerTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (C) 2015-2018 The ViaDuck Project
*
* This file is part of Commons.
*
* Commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Commons is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Commons. If not, see <http://www.gnu.org/licenses/>.
*/
#include <flatbuffers/test/VarMsg.h>
#include <flatbuffers/test/EnumMsg.h>
#include <flatbuffers/test/sometest.h>
#include <flatbuffers/test/TestLegacy.h>
#include <flatbuffers/test/TestFuture.h>
#include "ContainerTest.h"
#include "custom_assert.h"
void generate(sometest &test) {
test.version(0xBEEFDEAD);
test.first(0xfe);
test.second(0xDEAD);
test.buf().write("\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB", 10, 0);
test.third(0x171);
test.this_is_a_cool_property(0x1337);
}
void verify(sometest &test) {
ASSERT_EQ(0xBEEFDEAD, test.version());
ASSERT_EQ(0xfe, test.first());
ASSERT_EQ(0xDEAD, test.second());
ASSERT_EQ(10u, test.buf().size());
EXPECT_ARRAY_EQ(const uint8_t, "\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB\xAB", test.buf().const_data(), 10);
ASSERT_EQ(0x171, test.third());
ASSERT_EQ(0x1337u, test.this_is_a_cool_property());
}
void verify_reset(sometest &test) {
ASSERT_EQ(0u, test.version());
ASSERT_EQ(0u, test.first());
ASSERT_EQ(0u, test.second());
ASSERT_EQ(0u, test.buf().size());
ASSERT_EQ(0u, test.third());
ASSERT_EQ(0u, test.this_is_a_cool_property());
}
TEST_F(ContainerTest, SimpleWrite) {
sometest c, d;
generate(c);
verify(c);
// serialize and deserialize
Buffer test;
c.serialize(test);
ASSERT_TRUE(d.deserialize(test));
verify(d);
}
TEST_F(ContainerTest, Malformed) {
sometest test;
generate(test);
Buffer out;
test.serialize(out);
// manipulate size indicator to be bigger than actual buffer
uint32_t size1 = flatbuffers::GetPrefixedSize(out.const_data()) + 5;
out.write(&size1, sizeof(uint32_t), 0);
ASSERT_FALSE(test.deserialize(out));
verify_reset(test);
// append garbage to buffer, now size indicator fits buffer
out.append("asdfg", 5);
ASSERT_TRUE(test.deserialize(out));
verify(test);
// garbage in vtable
out.write("asdfg", 5, 4);
ASSERT_FALSE(test.deserialize(out));
verify_reset(test);
// serialize valid buffer bigger than max_size
out.clear();
for (uint32_t i = 0; i < 200; i++)
test.buf().append("asdfghjkll", 10);
test.serialize(out);
// buffer bigger than max_size should not deserialize
ASSERT_GE(test.buf().size(), 2000u);
ASSERT_FALSE(test.deserialize(out));
verify_reset(test);
// serialize zero byte buffer
Buffer out2(0);
uint32_t size = 0;
out2.write(&size, sizeof(uint32_t), 0);
ASSERT_FALSE(test.deserialize(out2));
verify_reset(test);
}
TEST_F(ContainerTest, Serialize) {
nlohmann::json testJson = {
{"a", 1234},
{"b", 1234},
{"c", {
{"q1234/#´12", "kasjkaslj"},
{"a", "123"},
}},
};
VarMsg msg, omsg;
msg.testFuture().first(5);
msg.testFuture2().second(8);
msg.testFutureEx().first(22);
msg.testFutureEx2().second(42);
msg.thisIsACoolProperty(123);
msg.bufVar().append("abc", 3);
msg.bufVar2().append("defgh", 5);
msg.bufFixed().append("12345678901", 11);
msg.someVector({1, 2, 3, 4});
msg.someVectorOfBytes({Buffer("123", 3), Buffer("456", 3)});
msg.someJson() = testJson;
Buffer out;
msg.serialize(out);
ASSERT_TRUE(omsg.deserialize(out));
EXPECT_EQ(5u, omsg.testFuture().first());
EXPECT_EQ(8u, omsg.testFuture2().second());
EXPECT_EQ(22u, omsg.testFutureEx().first());
EXPECT_EQ(42u, omsg.testFutureEx2().second());
EXPECT_EQ(123u, omsg.thisIsACoolProperty());
EXPECT_EQ(3u, omsg.bufVar().size());
EXPECT_ARRAY_EQ(const uint8_t, "abc", omsg.bufVar().const_data(), 3);
EXPECT_EQ(5u, omsg.bufVar2().size());
EXPECT_ARRAY_EQ(const uint8_t, "defgh", omsg.bufVar2().const_data(), 5);
EXPECT_EQ(4u, omsg.someVector().size());
EXPECT_EQ(1u, omsg.someVector()[0]);
EXPECT_EQ(2u, omsg.someVector()[1]);
EXPECT_EQ(3u, omsg.someVector()[2]);
EXPECT_EQ(4u, omsg.someVector()[3]);
EXPECT_EQ(Buffer("123", 3), omsg.someVectorOfBytes()[0]);
EXPECT_EQ(Buffer("456", 3), omsg.someVectorOfBytes()[1]);
EXPECT_EQ(testJson, omsg.someJson());
}
TEST_F(ContainerTest, Evolve) {
Buffer test, testData;
testData.append("asdf", 4);
// construct and serialize a legacy data object
TestLegacy legacy(TestField(0), 123, 45, TestEnum::VALUE_BLA, 678, testData, 901, {1, 2, 3});
legacy.serialize(test);
// deserialize into a future object having deprecated flags
TestFuture future;
ASSERT_TRUE(future.deserialize(test));
// check the leftover fields
EXPECT_EQ(123u, future.first());
EXPECT_EQ(45u, future.second());
EXPECT_EQ(4u, future.fourth().size());
EXPECT_ARRAY_EQ(const uint8_t, "asdf", future.fourth().const_data(), 4);
EXPECT_EQ(901u, future.fifth());
EXPECT_EQ(TestEnum::VALUE_1, future.newEnum());
EXPECT_EQ(0u, future.newField().value());
}
TEST_F(ContainerTest, Bit) {
TestField field;
// put squeeze values, expect bitfield value
field.squeezed_one(123);
EXPECT_EQ(123, field.squeezed_one());
field.squeezed_two(3);
EXPECT_EQ(3, field.squeezed_two());
EXPECT_EQ(0x1807Bu, field.value());
// put bitfield value, expect squeeze values
field.value(0x123412);
EXPECT_EQ(0x3412, field.squeezed_one());
EXPECT_EQ(0x24, field.squeezed_two());
}
TEST_F(ContainerTest, BitField) {
VarMsg msg;
TestField field = msg.testField();
EXPECT_EQ(15u, msg.testField().squeezed_one_width());
EXPECT_EQ(12u, msg.testField().squeezed_two_width());
EXPECT_EQ(0u, msg.testField().squeezed_one());
EXPECT_EQ(0u, msg.testField().squeezed_two());
field.squeezed_one(1337);
msg.testField(field);
EXPECT_EQ(1337u, msg.testField().squeezed_one());
EXPECT_EQ(0u, msg.testField().squeezed_two());
field.squeezed_two(15);
msg.testField(field);
EXPECT_EQ(1337u, msg.testField().squeezed_one());
EXPECT_EQ(15u, msg.testField().squeezed_two());
field.squeezed_two(0);
msg.testField(field);
EXPECT_EQ(1337u, msg.testField().squeezed_one());
EXPECT_EQ(0u, msg.testField().squeezed_two());
field.squeezed_one(987);
msg.testField(field);
EXPECT_EQ(987u, msg.testField().squeezed_one());
EXPECT_EQ(0u, msg.testField().squeezed_two());
}
TEST_F(ContainerTest, Enum) {
EnumMsg msg;
msg.myTestEnum(TestEnum::NO_STRICT_NAMING_ONLY_CPP_LIMITATIONS_APPLY);
EXPECT_EQ(TestEnum::NO_STRICT_NAMING_ONLY_CPP_LIMITATIONS_APPLY, msg.myTestEnum());
msg.myTestEnum(TestEnum::VALUE_INVALID);
EXPECT_EQ(TestEnum::VALUE_INVALID, msg.myTestEnum());
msg.myTestEnum(static_cast<TestEnum>(static_cast<uint16_t>(TestEnum::VALUE_INVALID)+2));
EXPECT_EQ(TestEnum::VALUE_INVALID, msg.myTestEnum());
msg.myTestEnum(TestEnum::VALUE_X55);
EXPECT_EQ(TestEnum::VALUE_X55, msg.myTestEnum());
}