• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "flatbuffers/flatbuffers.h"
18 #include "flatbuffers/idl.h"
19 #include "flatbuffers/util.h"
20 #include "flatbuffers/registry.h"
21 #include "flatbuffers/minireflect.h"
22 
23 #include "monster_test_generated.h"
24 #include "namespace_test/namespace_test1_generated.h"
25 #include "namespace_test/namespace_test2_generated.h"
26 #include "union_vector/union_vector_generated.h"
27 
28 #ifndef FLATBUFFERS_CPP98_STL
29   #include <random>
30 #endif
31 
32 #include "flatbuffers/flexbuffers.h"
33 
34 using namespace MyGame::Example;
35 
36 #ifdef __ANDROID__
37   #include <android/log.h>
38   #define TEST_OUTPUT_LINE(...) \
39     __android_log_print(ANDROID_LOG_INFO, "FlatBuffers", __VA_ARGS__)
40   #define FLATBUFFERS_NO_FILE_TESTS
41 #else
42   #define TEST_OUTPUT_LINE(...) \
43     { printf(__VA_ARGS__); printf("\n"); }
44 #endif
45 
46 int testing_fails = 0;
47 
TestFail(const char * expval,const char * val,const char * exp,const char * file,int line)48 void TestFail(const char *expval, const char *val, const char *exp,
49               const char *file, int line) {
50   TEST_OUTPUT_LINE("VALUE: \"%s\"", expval);
51   TEST_OUTPUT_LINE("EXPECTED: \"%s\"", val);
52   TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s", file, line, exp);
53   assert(0);
54   testing_fails++;
55 }
56 
TestEqStr(const char * expval,const char * val,const char * exp,const char * file,int line)57 void TestEqStr(const char *expval, const char *val, const char *exp,
58                const char *file, int line) {
59   if (strcmp(expval, val) != 0) {
60     TestFail(expval, val, exp, file, line);
61   }
62 }
63 
64 template<typename T, typename U>
TestEq(T expval,U val,const char * exp,const char * file,int line)65 void TestEq(T expval, U val, const char *exp, const char *file, int line) {
66   if (U(expval) != val) {
67     TestFail(flatbuffers::NumToString(expval).c_str(),
68              flatbuffers::NumToString(val).c_str(),
69              exp, file, line);
70   }
71 }
72 
73 #define TEST_EQ(exp, val) TestEq(exp,         val,   #exp, __FILE__, __LINE__)
74 #define TEST_NOTNULL(exp) TestEq(exp == NULL, false, #exp, __FILE__, __LINE__)
75 #define TEST_EQ_STR(exp, val) TestEqStr(exp,  val,   #exp, __FILE__, __LINE__)
76 
77 // Include simple random number generator to ensure results will be the
78 // same cross platform.
79 // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
80 uint32_t lcg_seed = 48271;
lcg_rand()81 uint32_t lcg_rand() {
82     return lcg_seed = ((uint64_t)lcg_seed * 279470273UL) % 4294967291UL;
83 }
lcg_reset()84 void lcg_reset() { lcg_seed = 48271; }
85 
86 std::string test_data_path = "tests/";
87 
88 // example of how to build up a serialized buffer algorithmically:
CreateFlatBufferTest(std::string & buffer)89 flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
90   flatbuffers::FlatBufferBuilder builder;
91 
92   auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
93 
94   auto name = builder.CreateString("MyMonster");
95 
96   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
97   auto inventory = builder.CreateVector(inv_data, 10);
98 
99   // Alternatively, create the vector first, and fill in data later:
100   // unsigned char *inv_buf = nullptr;
101   // auto inventory = builder.CreateUninitializedVector<unsigned char>(
102   //                                                              10, &inv_buf);
103   // memcpy(inv_buf, inv_data, 10);
104 
105   Test tests[] = { Test(10, 20), Test(30, 40) };
106   auto testv = builder.CreateVectorOfStructs(tests, 2);
107 
108 
109   #ifndef FLATBUFFERS_CPP98_STL
110     // Create a vector of structures from a lambda.
111     auto testv2 = builder.CreateVectorOfStructs<Test>(
112           2, [&](size_t i, Test* s) -> void {
113             *s = tests[i];
114           });
115   #else
116     // Create a vector of structures using a plain old C++ function.
117     auto testv2 = builder.CreateVectorOfStructs<Test>(
118           2, [](size_t i, Test* s, void *state) -> void {
119             *s = (reinterpret_cast<Test*>(state))[i];
120           }, tests);
121   #endif  // FLATBUFFERS_CPP98_STL
122 
123   // create monster with very few fields set:
124   // (same functionality as CreateMonster below, but sets fields manually)
125   flatbuffers::Offset<Monster> mlocs[3];
126   auto fred = builder.CreateString("Fred");
127   auto barney = builder.CreateString("Barney");
128   auto wilma = builder.CreateString("Wilma");
129   MonsterBuilder mb1(builder);
130   mb1.add_name(fred);
131   mlocs[0] = mb1.Finish();
132   MonsterBuilder mb2(builder);
133   mb2.add_name(barney);
134   mb2.add_hp(1000);
135   mlocs[1] = mb2.Finish();
136   MonsterBuilder mb3(builder);
137   mb3.add_name(wilma);
138   mlocs[2] = mb3.Finish();
139 
140   // Create an array of strings. Also test string pooling, and lambdas.
141   auto vecofstrings =
142       builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(4,
143         [](size_t i, flatbuffers::FlatBufferBuilder *b)
144           -> flatbuffers::Offset<flatbuffers::String> {
145     static const char *names[] = { "bob", "fred", "bob", "fred" };
146     return b->CreateSharedString(names[i]);
147   }, &builder);
148 
149   // Creating vectors of strings in one convenient call.
150   std::vector<std::string> names2;
151   names2.push_back("jane");
152   names2.push_back("mary");
153   auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
154 
155   // Create an array of sorted tables, can be used with binary search when read:
156   auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
157 
158   // Create an array of sorted structs,
159   // can be used with binary search when read:
160   std::vector<Ability> abilities;
161   abilities.push_back(Ability(4, 40));
162   abilities.push_back(Ability(3, 30));
163   abilities.push_back(Ability(2, 20));
164   abilities.push_back(Ability(1, 10));
165   auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
166 
167   // Create a nested FlatBuffer.
168   // Nested FlatBuffers are stored in a ubyte vector, which can be convenient
169   // since they can be memcpy'd around much easier than other FlatBuffer
170   // values. They have little overhead compared to storing the table directly.
171   // As a test, create a mostly empty Monster buffer:
172   flatbuffers::FlatBufferBuilder nested_builder;
173   auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
174                              nested_builder.CreateString("NestedMonster"));
175   FinishMonsterBuffer(nested_builder, nmloc);
176   // Now we can store the buffer in the parent. Note that by default, vectors
177   // are only aligned to their elements or size field, so in this case if the
178   // buffer contains 64-bit elements, they may not be correctly aligned. We fix
179   // that with:
180   builder.ForceVectorAlignment(nested_builder.GetSize(), sizeof(uint8_t),
181                                nested_builder.GetBufferMinAlignment());
182   // If for whatever reason you don't have the nested_builder available, you
183   // can substitute flatbuffers::largest_scalar_t (64-bit) for the alignment, or
184   // the largest force_align value in your schema if you're using it.
185   auto nested_flatbuffer_vector =
186       builder.CreateVector(nested_builder.GetBufferPointer(),
187                            nested_builder.GetSize());
188 
189   // Test a nested FlexBuffer:
190   flexbuffers::Builder flexbuild;
191   flexbuild.Int(1234);
192   flexbuild.Finish();
193   auto flex = builder.CreateVector(flexbuild.GetBuffer());
194 
195   // shortcut for creating monster with all fields set:
196   auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
197                             Any_Monster, mlocs[1].Union(), // Store a union.
198                             testv, vecofstrings, vecoftables, 0,
199                             nested_flatbuffer_vector, 0, false,
200                             0, 0, 0, 0, 0, 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f,
201                             vecofstrings2, vecofstructs, flex, testv2);
202 
203   FinishMonsterBuffer(builder, mloc);
204 
205   #ifdef FLATBUFFERS_TEST_VERBOSE
206   // print byte data for debugging:
207   auto p = builder.GetBufferPointer();
208   for (flatbuffers::uoffset_t i = 0; i < builder.GetSize(); i++)
209     printf("%d ", p[i]);
210   #endif
211 
212   // return the buffer for the caller to use.
213   auto bufferpointer =
214     reinterpret_cast<const char *>(builder.GetBufferPointer());
215   buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
216 
217   return builder.ReleaseBufferPointer();
218 }
219 
220 //  example of accessing a buffer loaded in memory:
AccessFlatBufferTest(const uint8_t * flatbuf,size_t length,bool pooled=true)221 void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
222                           bool pooled = true) {
223 
224   // First, verify the buffers integrity (optional)
225   flatbuffers::Verifier verifier(flatbuf, length);
226   TEST_EQ(VerifyMonsterBuffer(verifier), true);
227 
228   std::vector<uint8_t> test_buff;
229   test_buff.resize(length * 2);
230   std::memcpy(&test_buff[0], flatbuf , length);
231   std::memcpy(&test_buff[length], flatbuf , length);
232 
233   flatbuffers::Verifier verifier1(&test_buff[0], length);
234   TEST_EQ(VerifyMonsterBuffer(verifier1), true);
235   TEST_EQ(verifier1.GetComputedSize(), length);
236 
237   flatbuffers::Verifier verifier2(&test_buff[length], length);
238   TEST_EQ(VerifyMonsterBuffer(verifier2), true);
239   TEST_EQ(verifier2.GetComputedSize(), length);
240 
241   TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
242   TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
243   TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
244 
245   // Access the buffer from the root.
246   auto monster = GetMonster(flatbuf);
247 
248   TEST_EQ(monster->hp(), 80);
249   TEST_EQ(monster->mana(), 150);  // default
250   TEST_EQ_STR(monster->name()->c_str(), "MyMonster");
251   // Can't access the following field, it is deprecated in the schema,
252   // which means accessors are not generated:
253   // monster.friendly()
254 
255   auto pos = monster->pos();
256   TEST_NOTNULL(pos);
257   TEST_EQ(pos->z(), 3);
258   TEST_EQ(pos->test3().a(), 10);
259   TEST_EQ(pos->test3().b(), 20);
260 
261   auto inventory = monster->inventory();
262   TEST_EQ(VectorLength(inventory), 10UL);  // Works even if inventory is null.
263   TEST_NOTNULL(inventory);
264   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
265   for (auto it = inventory->begin(); it != inventory->end(); ++it)
266     TEST_EQ(*it, inv_data[it - inventory->begin()]);
267 
268   TEST_EQ(monster->color(), Color_Blue);
269 
270   // Example of accessing a union:
271   TEST_EQ(monster->test_type(), Any_Monster);  // First make sure which it is.
272   auto monster2 = reinterpret_cast<const Monster *>(monster->test());
273   TEST_NOTNULL(monster2);
274   TEST_EQ_STR(monster2->name()->c_str(), "Fred");
275 
276   // Example of accessing a vector of strings:
277   auto vecofstrings = monster->testarrayofstring();
278   TEST_EQ(vecofstrings->Length(), 4U);
279   TEST_EQ_STR(vecofstrings->Get(0)->c_str(), "bob");
280   TEST_EQ_STR(vecofstrings->Get(1)->c_str(), "fred");
281   if (pooled) {
282     // These should have pointer equality because of string pooling.
283     TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str());
284     TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str());
285   }
286 
287   auto vecofstrings2 = monster->testarrayofstring2();
288   if (vecofstrings2) {
289     TEST_EQ(vecofstrings2->Length(), 2U);
290     TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane");
291     TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary");
292   }
293 
294   // Example of accessing a vector of tables:
295   auto vecoftables = monster->testarrayoftables();
296   TEST_EQ(vecoftables->Length(), 3U);
297   for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
298     TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
299   TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney");
300   TEST_EQ(vecoftables->Get(0)->hp(), 1000);
301   TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred");
302   TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma");
303   TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
304   TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
305   TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
306 
307   // Test accessing a vector of sorted structs
308   auto vecofstructs = monster->testarrayofsortedstruct();
309   if (vecofstructs) {  // not filled in monster_test.bfbs
310     for (flatbuffers::uoffset_t i = 0; i < vecofstructs->size()-1; i++) {
311       auto left = vecofstructs->Get(i);
312       auto right = vecofstructs->Get(i+1);
313       TEST_EQ(true, (left->KeyCompareLessThan(right)));
314     }
315     TEST_NOTNULL(vecofstructs->LookupByKey(3));
316     TEST_EQ(static_cast<const Ability*>(nullptr), vecofstructs->LookupByKey(5));
317   }
318 
319   // Test nested FlatBuffers if available:
320   auto nested_buffer = monster->testnestedflatbuffer();
321   if (nested_buffer) {
322     // nested_buffer is a vector of bytes you can memcpy. However, if you
323     // actually want to access the nested data, this is a convenient
324     // accessor that directly gives you the root table:
325     auto nested_monster = monster->testnestedflatbuffer_nested_root();
326     TEST_EQ_STR(nested_monster->name()->c_str(), "NestedMonster");
327   }
328 
329   // Test flexbuffer if available:
330   auto flex = monster->flex();
331   // flex is a vector of bytes you can memcpy etc.
332   TEST_EQ(flex->size(), 4);  // Encoded FlexBuffer bytes.
333   // However, if you actually want to access the nested data, this is a
334   // convenient accessor that directly gives you the root value:
335   TEST_EQ(monster->flex_flexbuffer_root().AsInt16(), 1234);
336 
337   // Since Flatbuffers uses explicit mechanisms to override the default
338   // compiler alignment, double check that the compiler indeed obeys them:
339   // (Test consists of a short and byte):
340   TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
341   TEST_EQ(sizeof(Test), 4UL);
342 
343   const flatbuffers::Vector<const Test *>* tests_array[] = {
344     monster->test4(),
345     monster->test5(),
346   };
347   for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
348     auto tests = tests_array[i];
349     TEST_NOTNULL(tests);
350     auto test_0 = tests->Get(0);
351     auto test_1 = tests->Get(1);
352     TEST_EQ(test_0->a(), 10);
353     TEST_EQ(test_0->b(), 20);
354     TEST_EQ(test_1->a(), 30);
355     TEST_EQ(test_1->b(), 40);
356     for (auto it = tests->begin(); it != tests->end(); ++it) {
357       TEST_EQ(it->a() == 10 || it->a() == 30, true);  // Just testing iterators.
358     }
359   }
360 
361   // Checking for presence of fields:
362   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
363   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
364 
365   // Obtaining a buffer from a root:
366   TEST_EQ(GetBufferStartFromRootPointer(monster), flatbuf);
367 }
368 
369 // Change a FlatBuffer in-place, after it has been constructed.
MutateFlatBuffersTest(uint8_t * flatbuf,std::size_t length)370 void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
371   // Get non-const pointer to root.
372   auto monster = GetMutableMonster(flatbuf);
373 
374   // Each of these tests mutates, then tests, then set back to the original,
375   // so we can test that the buffer in the end still passes our original test.
376   auto hp_ok = monster->mutate_hp(10);
377   TEST_EQ(hp_ok, true);  // Field was present.
378   TEST_EQ(monster->hp(), 10);
379   // Mutate to default value
380   auto hp_ok_default = monster->mutate_hp(100);
381   TEST_EQ(hp_ok_default, true);  // Field was present.
382   TEST_EQ(monster->hp(), 100);
383   // Test that mutate to default above keeps field valid for further mutations
384   auto hp_ok_2 = monster->mutate_hp(20);
385   TEST_EQ(hp_ok_2, true);
386   TEST_EQ(monster->hp(), 20);
387   monster->mutate_hp(80);
388 
389   // Monster originally at 150 mana (default value)
390   auto mana_default_ok = monster->mutate_mana(150);  // Mutate to default value.
391   TEST_EQ(mana_default_ok, true);  // Mutation should succeed, because default value.
392   TEST_EQ(monster->mana(), 150);
393   auto mana_ok = monster->mutate_mana(10);
394   TEST_EQ(mana_ok, false);  // Field was NOT present, because default value.
395   TEST_EQ(monster->mana(), 150);
396 
397   // Mutate structs.
398   auto pos = monster->mutable_pos();
399   auto test3 = pos->mutable_test3();  // Struct inside a struct.
400   test3.mutate_a(50);                 // Struct fields never fail.
401   TEST_EQ(test3.a(), 50);
402   test3.mutate_a(10);
403 
404   // Mutate vectors.
405   auto inventory = monster->mutable_inventory();
406   inventory->Mutate(9, 100);
407   TEST_EQ(inventory->Get(9), 100);
408   inventory->Mutate(9, 9);
409 
410   auto tables = monster->mutable_testarrayoftables();
411   auto first = tables->GetMutableObject(0);
412   TEST_EQ(first->hp(), 1000);
413   first->mutate_hp(0);
414   TEST_EQ(first->hp(), 0);
415   first->mutate_hp(1000);
416 
417   // Run the verifier and the regular test to make sure we didn't trample on
418   // anything.
419   AccessFlatBufferTest(flatbuf, length);
420 }
421 
422 // Unpack a FlatBuffer into objects.
ObjectFlatBuffersTest(uint8_t * flatbuf)423 void ObjectFlatBuffersTest(uint8_t *flatbuf) {
424   // Optional: we can specify resolver and rehasher functions to turn hashed
425   // strings into object pointers and back, to implement remote references
426   // and such.
427   auto resolver = flatbuffers::resolver_function_t(
428                     [](void **pointer_adr, flatbuffers::hash_value_t hash) {
429     (void)pointer_adr;
430     (void)hash;
431     // Don't actually do anything, leave variable null.
432   });
433   auto rehasher = flatbuffers::rehasher_function_t(
434                     [](void *pointer) -> flatbuffers::hash_value_t {
435     (void)pointer;
436     return 0;
437   });
438 
439   // Turn a buffer into C++ objects.
440   auto monster1 = UnPackMonster(flatbuf, &resolver);
441 
442   // Re-serialize the data.
443   flatbuffers::FlatBufferBuilder fbb1;
444   fbb1.Finish(CreateMonster(fbb1, monster1.get(), &rehasher),
445               MonsterIdentifier());
446 
447   // Unpack again, and re-serialize again.
448   auto monster2 = UnPackMonster(fbb1.GetBufferPointer(), &resolver);
449   flatbuffers::FlatBufferBuilder fbb2;
450   fbb2.Finish(CreateMonster(fbb2, monster2.get(), &rehasher),
451               MonsterIdentifier());
452 
453   // Now we've gone full round-trip, the two buffers should match.
454   auto len1 = fbb1.GetSize();
455   auto len2 = fbb2.GetSize();
456   TEST_EQ(len1, len2);
457   TEST_EQ(memcmp(fbb1.GetBufferPointer(), fbb2.GetBufferPointer(),
458                  len1), 0);
459 
460   // Test it with the original buffer test to make sure all data survived.
461   AccessFlatBufferTest(fbb2.GetBufferPointer(), len2, false);
462 
463   // Test accessing fields, similar to AccessFlatBufferTest above.
464   TEST_EQ(monster2->hp, 80);
465   TEST_EQ(monster2->mana, 150);  // default
466   TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
467 
468   auto &pos = monster2->pos;
469   TEST_NOTNULL(pos);
470   TEST_EQ(pos->z(), 3);
471   TEST_EQ(pos->test3().a(), 10);
472   TEST_EQ(pos->test3().b(), 20);
473 
474   auto &inventory = monster2->inventory;
475   TEST_EQ(inventory.size(), 10UL);
476   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
477   for (auto it = inventory.begin(); it != inventory.end(); ++it)
478     TEST_EQ(*it, inv_data[it - inventory.begin()]);
479 
480   TEST_EQ(monster2->color, Color_Blue);
481 
482   auto monster3 = monster2->test.AsMonster();
483   TEST_NOTNULL(monster3);
484   TEST_EQ_STR(monster3->name.c_str(), "Fred");
485 
486   auto &vecofstrings = monster2->testarrayofstring;
487   TEST_EQ(vecofstrings.size(), 4U);
488   TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
489   TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
490 
491   auto &vecofstrings2 = monster2->testarrayofstring2;
492   TEST_EQ(vecofstrings2.size(), 2U);
493   TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
494   TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
495 
496   auto &vecoftables = monster2->testarrayoftables;
497   TEST_EQ(vecoftables.size(), 3U);
498   TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
499   TEST_EQ(vecoftables[0]->hp, 1000);
500   TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
501   TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
502 
503   auto &tests = monster2->test4;
504   TEST_EQ(tests[0].a(), 10);
505   TEST_EQ(tests[0].b(), 20);
506   TEST_EQ(tests[1].a(), 30);
507   TEST_EQ(tests[1].b(), 40);
508 }
509 
510 // Prefix a FlatBuffer with a size field.
SizePrefixedTest()511 void SizePrefixedTest() {
512   // Create size prefixed buffer.
513   flatbuffers::FlatBufferBuilder fbb;
514   fbb.FinishSizePrefixed(CreateMonster(fbb, 0, 200, 300,
515                                        fbb.CreateString("bob")));
516 
517   // Verify it.
518   flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
519   TEST_EQ(verifier.VerifySizePrefixedBuffer<Monster>(nullptr), true);
520 
521   // Access it.
522   auto m = flatbuffers::GetSizePrefixedRoot<MyGame::Example::Monster>(
523                                                         fbb.GetBufferPointer());
524   TEST_EQ(m->mana(), 200);
525   TEST_EQ(m->hp(), 300);
526   TEST_EQ_STR(m->name()->c_str(), "bob");
527 }
528 
529 
TriviallyCopyableTest()530 void TriviallyCopyableTest() {
531   #if __GNUG__ && __GNUC__ < 5
532     TEST_EQ(__has_trivial_copy(Vec3), true);
533   #else
534     #if __cplusplus >= 201103L
535       TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
536     #endif
537   #endif
538 }
539 
540 
541 // example of parsing text straight into a buffer, and generating
542 // text back from it:
ParseAndGenerateTextTest()543 void ParseAndGenerateTextTest() {
544   // load FlatBuffer schema (.fbs) and JSON from disk
545   std::string schemafile;
546   std::string jsonfile;
547   TEST_EQ(flatbuffers::LoadFile(
548     (test_data_path + "monster_test.fbs").c_str(), false, &schemafile), true);
549   TEST_EQ(flatbuffers::LoadFile(
550     (test_data_path + "monsterdata_test.golden").c_str(), false, &jsonfile),
551     true);
552 
553   // parse schema first, so we can use it to parse the data after
554   flatbuffers::Parser parser;
555   auto include_test_path =
556       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
557   const char *include_directories[] = {
558     test_data_path.c_str(), include_test_path.c_str(), nullptr
559   };
560   TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
561   TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
562 
563   // here, parser.builder_ contains a binary buffer that is the parsed data.
564 
565   // First, verify it, just in case:
566   flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
567                                  parser.builder_.GetSize());
568   TEST_EQ(VerifyMonsterBuffer(verifier), true);
569 
570   AccessFlatBufferTest(parser.builder_.GetBufferPointer(),
571                        parser.builder_.GetSize(), false);
572 
573   // to ensure it is correct, we now generate text back from the binary,
574   // and compare the two:
575   std::string jsongen;
576   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
577   TEST_EQ(result, true);
578   TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
579 
580   // We can also do the above using the convenient Registry that knows about
581   // a set of file_identifiers mapped to schemas.
582   flatbuffers::Registry registry;
583   // Make sure schemas can find their includes.
584   registry.AddIncludeDirectory(test_data_path.c_str());
585   registry.AddIncludeDirectory(include_test_path.c_str());
586   // Call this with many schemas if possible.
587   registry.Register(MonsterIdentifier(),
588                     (test_data_path + "monster_test.fbs").c_str());
589   // Now we got this set up, we can parse by just specifying the identifier,
590   // the correct schema will be loaded on the fly:
591   auto buf = registry.TextToFlatBuffer(jsonfile.c_str(),
592                                        MonsterIdentifier());
593   // If this fails, check registry.lasterror_.
594   TEST_NOTNULL(buf.data());
595   // Test the buffer, to be sure:
596   AccessFlatBufferTest(buf.data(), buf.size(), false);
597   // We can use the registry to turn this back into text, in this case it
598   // will get the file_identifier from the binary:
599   std::string text;
600   auto ok = registry.FlatBufferToText(buf.data(), buf.size(), &text);
601   // If this fails, check registry.lasterror_.
602   TEST_EQ(ok, true);
603   TEST_EQ_STR(text.c_str(), jsonfile.c_str());
604 }
605 
ReflectionTest(uint8_t * flatbuf,size_t length)606 void ReflectionTest(uint8_t *flatbuf, size_t length) {
607   // Load a binary schema.
608   std::string bfbsfile;
609   TEST_EQ(flatbuffers::LoadFile(
610     (test_data_path + "monster_test.bfbs").c_str(), true, &bfbsfile),
611     true);
612 
613   // Verify it, just in case:
614   flatbuffers::Verifier verifier(
615     reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
616   TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
617 
618   // Make sure the schema is what we expect it to be.
619   auto &schema = *reflection::GetSchema(bfbsfile.c_str());
620   auto root_table = schema.root_table();
621   TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
622   auto fields = root_table->fields();
623   auto hp_field_ptr = fields->LookupByKey("hp");
624   TEST_NOTNULL(hp_field_ptr);
625   auto &hp_field = *hp_field_ptr;
626   TEST_EQ_STR(hp_field.name()->c_str(), "hp");
627   TEST_EQ(hp_field.id(), 2);
628   TEST_EQ(hp_field.type()->base_type(), reflection::Short);
629   auto friendly_field_ptr = fields->LookupByKey("friendly");
630   TEST_NOTNULL(friendly_field_ptr);
631   TEST_NOTNULL(friendly_field_ptr->attributes());
632   TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
633 
634   // Make sure the table index is what we expect it to be.
635   auto pos_field_ptr = fields->LookupByKey("pos");
636   TEST_NOTNULL(pos_field_ptr);
637   TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
638   auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
639   TEST_NOTNULL(pos_table_ptr);
640   TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
641 
642   // Now use it to dynamically access a buffer.
643   auto &root = *flatbuffers::GetAnyRoot(flatbuf);
644 
645   // Verify the buffer first using reflection based verification
646   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
647           true);
648 
649   auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
650   TEST_EQ(hp, 80);
651 
652   // Rather than needing to know the type, we can also get the value of
653   // any field as an int64_t/double/string, regardless of what it actually is.
654   auto hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
655   TEST_EQ(hp_int64, 80);
656   auto hp_double = flatbuffers::GetAnyFieldF(root, hp_field);
657   TEST_EQ(hp_double, 80.0);
658   auto hp_string = flatbuffers::GetAnyFieldS(root, hp_field, &schema);
659   TEST_EQ_STR(hp_string.c_str(), "80");
660 
661   // Get struct field through reflection
662   auto pos_struct = flatbuffers::GetFieldStruct(root, *pos_field_ptr);
663   TEST_NOTNULL(pos_struct);
664   TEST_EQ(flatbuffers::GetAnyFieldF(
665     *pos_struct, *pos_table_ptr->fields()->LookupByKey("z")), 3.0f);
666 
667   auto test3_field = pos_table_ptr->fields()->LookupByKey("test3");
668   auto test3_struct = flatbuffers::GetFieldStruct(*pos_struct, *test3_field);
669   TEST_NOTNULL(test3_struct);
670   auto test3_object = schema.objects()->Get(test3_field->type()->index());
671 
672   TEST_EQ(flatbuffers::GetAnyFieldF(
673     *test3_struct, *test3_object->fields()->LookupByKey("a")), 10);
674 
675   // We can also modify it.
676   flatbuffers::SetField<uint16_t>(&root, hp_field, 200);
677   hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
678   TEST_EQ(hp, 200);
679 
680   // We can also set fields generically:
681   flatbuffers::SetAnyFieldI(&root, hp_field, 300);
682   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
683   TEST_EQ(hp_int64, 300);
684   flatbuffers::SetAnyFieldF(&root, hp_field, 300.5);
685   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
686   TEST_EQ(hp_int64, 300);
687   flatbuffers::SetAnyFieldS(&root, hp_field, "300");
688   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
689   TEST_EQ(hp_int64, 300);
690 
691   // Test buffer is valid after the modifications
692   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
693           true);
694 
695   // Reset it, for further tests.
696   flatbuffers::SetField<uint16_t>(&root, hp_field, 80);
697 
698   // More advanced functionality: changing the size of items in-line!
699   // First we put the FlatBuffer inside an std::vector.
700   std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
701   // Find the field we want to modify.
702   auto &name_field = *fields->LookupByKey("name");
703   // Get the root.
704   // This time we wrap the result from GetAnyRoot in a smartpointer that
705   // will keep rroot valid as resizingbuf resizes.
706   auto rroot = flatbuffers::piv(flatbuffers::GetAnyRoot(
707       flatbuffers::vector_data(resizingbuf)), resizingbuf);
708   SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
709             &resizingbuf);
710   // Here resizingbuf has changed, but rroot is still valid.
711   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
712   // Now lets extend a vector by 100 elements (10 -> 110).
713   auto &inventory_field = *fields->LookupByKey("inventory");
714   auto rinventory = flatbuffers::piv(
715                      flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field),
716                      resizingbuf);
717   flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
718                                      &resizingbuf);
719   // rinventory still valid, so lets read from it.
720   TEST_EQ(rinventory->Get(10), 50);
721 
722   // For reflection uses not covered already, there is a more powerful way:
723   // we can simply generate whatever object we want to add/modify in a
724   // FlatBuffer of its own, then add that to an existing FlatBuffer:
725   // As an example, let's add a string to an array of strings.
726   // First, find our field:
727   auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
728   // Find the vector value:
729   auto rtestarrayofstring = flatbuffers::piv(
730          flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
731            **rroot, testarrayofstring_field),
732          resizingbuf);
733   // It's a vector of 2 strings, to which we add one more, initialized to
734   // offset 0.
735   flatbuffers::ResizeVector<flatbuffers::Offset<flatbuffers::String>>(
736         schema, 3, 0, *rtestarrayofstring, &resizingbuf);
737   // Here we just create a buffer that contans a single string, but this
738   // could also be any complex set of tables and other values.
739   flatbuffers::FlatBufferBuilder stringfbb;
740   stringfbb.Finish(stringfbb.CreateString("hank"));
741   // Add the contents of it to our existing FlatBuffer.
742   // We do this last, so the pointer doesn't get invalidated (since it is
743   // at the end of the buffer):
744   auto string_ptr = flatbuffers::AddFlatBuffer(resizingbuf,
745                                                stringfbb.GetBufferPointer(),
746                                                stringfbb.GetSize());
747   // Finally, set the new value in the vector.
748   rtestarrayofstring->MutateOffset(2, string_ptr);
749   TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
750   TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
751   // Test integrity of all resize operations above.
752   flatbuffers::Verifier resize_verifier(
753         reinterpret_cast<const uint8_t *>(
754             flatbuffers::vector_data(resizingbuf)),
755         resizingbuf.size());
756   TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
757 
758   // Test buffer is valid using reflection as well
759   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
760                               flatbuffers::vector_data(resizingbuf),
761                               resizingbuf.size()), true);
762 
763   // As an additional test, also set it on the name field.
764   // Note: unlike the name change above, this just overwrites the offset,
765   // rather than changing the string in-place.
766   SetFieldT(*rroot, name_field, string_ptr);
767   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "hank");
768 
769   // Using reflection, rather than mutating binary FlatBuffers, we can also copy
770   // tables and other things out of other FlatBuffers into a FlatBufferBuilder,
771   // either part or whole.
772   flatbuffers::FlatBufferBuilder fbb;
773   auto root_offset = flatbuffers::CopyTable(fbb, schema, *root_table,
774                                             *flatbuffers::GetAnyRoot(flatbuf),
775                                             true);
776   fbb.Finish(root_offset, MonsterIdentifier());
777   // Test that it was copied correctly:
778   AccessFlatBufferTest(fbb.GetBufferPointer(), fbb.GetSize());
779 
780   // Test buffer is valid using reflection as well
781   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
782                               fbb.GetBufferPointer(), fbb.GetSize()), true);
783 }
784 
MiniReflectFlatBuffersTest(uint8_t * flatbuf)785 void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
786   auto s = flatbuffers::FlatBufferToString(flatbuf, MonsterTypeTable());
787   TEST_EQ_STR(s.c_str(),
788     "{ "
789     "pos: { x: 1.0, y: 2.0, z: 3.0, test1: 0.0, test2: Red, test3: "
790       "{ a: 10, b: 20 } }, "
791     "hp: 80, "
792     "name: \"MyMonster\", "
793     "inventory: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "
794     "test_type: Monster, "
795     "test: { name: \"Fred\" }, "
796     "test4: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
797     "testarrayofstring: [ \"bob\", \"fred\", \"bob\", \"fred\" ], "
798     "testarrayoftables: [ { hp: 1000, name: \"Barney\" }, { name: \"Fred\" }, "
799       "{ name: \"Wilma\" } ], "
800     // TODO(wvo): should really print this nested buffer correctly.
801     "testnestedflatbuffer: [ 20, 0, 0, 0, 77, 79, 78, 83, 12, 0, 12, 0, 0, 0, "
802       "4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
803       "101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
804     "testarrayofstring2: [ \"jane\", \"mary\" ], "
805     "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
806       "{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
807       "{ id: 4, distance: 40 } ], "
808     "flex: [ 210, 4, 5, 2 ], "
809     "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ] "
810     "}");
811 }
812 
813 // Parse a .proto schema, output as .fbs
ParseProtoTest()814 void ParseProtoTest() {
815   // load the .proto and the golden file from disk
816   std::string protofile;
817   std::string goldenfile;
818   TEST_EQ(flatbuffers::LoadFile(
819     (test_data_path + "prototest/test.proto").c_str(), false, &protofile),
820     true);
821   TEST_EQ(flatbuffers::LoadFile(
822     (test_data_path + "prototest/test.golden").c_str(), false, &goldenfile),
823     true);
824 
825   flatbuffers::IDLOptions opts;
826   opts.include_dependence_headers = false;
827   opts.proto_mode = true;
828 
829   // Parse proto.
830   flatbuffers::Parser parser(opts);
831   auto protopath = test_data_path + "prototest/";
832   const char *include_directories[] = { protopath.c_str(), nullptr };
833   TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
834 
835   // Generate fbs.
836   auto fbs = flatbuffers::GenerateFBS(parser, "test");
837 
838   // Ensure generated file is parsable.
839   flatbuffers::Parser parser2;
840   TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
841   TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
842 }
843 
CompareTableFieldValue(flatbuffers::Table * table,flatbuffers::voffset_t voffset,T val)844 template<typename T> void CompareTableFieldValue(flatbuffers::Table *table,
845                                                  flatbuffers::voffset_t voffset,
846                                                  T val) {
847   T read = table->GetField(voffset, static_cast<T>(0));
848   TEST_EQ(read, val);
849 }
850 
851 // Low level stress/fuzz test: serialize/deserialize a variety of
852 // different kinds of data in different combinations
FuzzTest1()853 void FuzzTest1() {
854 
855   // Values we're testing against: chosen to ensure no bits get chopped
856   // off anywhere, and also be different from eachother.
857   const uint8_t  bool_val   = true;
858   const int8_t   char_val   = -127;  // 0x81
859   const uint8_t  uchar_val  = 0xFF;
860   const int16_t  short_val  = -32222; // 0x8222;
861   const uint16_t ushort_val = 0xFEEE;
862   const int32_t  int_val    = 0x83333333;
863   const uint32_t uint_val   = 0xFDDDDDDD;
864   const int64_t  long_val   = 0x8444444444444444LL;
865   const uint64_t ulong_val  = 0xFCCCCCCCCCCCCCCCULL;
866   const float    float_val  = 3.14159f;
867   const double   double_val = 3.14159265359;
868 
869   const int test_values_max = 11;
870   const flatbuffers::voffset_t fields_per_object = 4;
871   const int num_fuzz_objects = 10000;  // The higher, the more thorough :)
872 
873   flatbuffers::FlatBufferBuilder builder;
874 
875   lcg_reset();  // Keep it deterministic.
876 
877   flatbuffers::uoffset_t objects[num_fuzz_objects];
878 
879   // Generate num_fuzz_objects random objects each consisting of
880   // fields_per_object fields, each of a random type.
881   for (int i = 0; i < num_fuzz_objects; i++) {
882     auto start = builder.StartTable();
883     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
884       int choice = lcg_rand() % test_values_max;
885       auto off = flatbuffers::FieldIndexToOffset(f);
886       switch (choice) {
887         case 0:  builder.AddElement<uint8_t >(off, bool_val,   0); break;
888         case 1:  builder.AddElement<int8_t  >(off, char_val,   0); break;
889         case 2:  builder.AddElement<uint8_t >(off, uchar_val,  0); break;
890         case 3:  builder.AddElement<int16_t >(off, short_val,  0); break;
891         case 4:  builder.AddElement<uint16_t>(off, ushort_val, 0); break;
892         case 5:  builder.AddElement<int32_t >(off, int_val,    0); break;
893         case 6:  builder.AddElement<uint32_t>(off, uint_val,   0); break;
894         case 7:  builder.AddElement<int64_t >(off, long_val,   0); break;
895         case 8:  builder.AddElement<uint64_t>(off, ulong_val,  0); break;
896         case 9:  builder.AddElement<float   >(off, float_val,  0); break;
897         case 10: builder.AddElement<double  >(off, double_val, 0); break;
898       }
899     }
900     objects[i] = builder.EndTable(start);
901   }
902   builder.PreAlign<flatbuffers::largest_scalar_t>(0);  // Align whole buffer.
903 
904   lcg_reset();  // Reset.
905 
906   uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
907 
908   // Test that all objects we generated are readable and return the
909   // expected values. We generate random objects in the same order
910   // so this is deterministic.
911   for (int i = 0; i < num_fuzz_objects; i++) {
912     auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
913     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
914       int choice = lcg_rand() % test_values_max;
915       flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
916       switch (choice) {
917         case 0:  CompareTableFieldValue(table, off, bool_val  ); break;
918         case 1:  CompareTableFieldValue(table, off, char_val  ); break;
919         case 2:  CompareTableFieldValue(table, off, uchar_val ); break;
920         case 3:  CompareTableFieldValue(table, off, short_val ); break;
921         case 4:  CompareTableFieldValue(table, off, ushort_val); break;
922         case 5:  CompareTableFieldValue(table, off, int_val   ); break;
923         case 6:  CompareTableFieldValue(table, off, uint_val  ); break;
924         case 7:  CompareTableFieldValue(table, off, long_val  ); break;
925         case 8:  CompareTableFieldValue(table, off, ulong_val ); break;
926         case 9:  CompareTableFieldValue(table, off, float_val ); break;
927         case 10: CompareTableFieldValue(table, off, double_val); break;
928       }
929     }
930   }
931 }
932 
933 // High level stress/fuzz test: generate a big schema and
934 // matching json data in random combinations, then parse both,
935 // generate json back from the binary, and compare with the original.
FuzzTest2()936 void FuzzTest2() {
937   lcg_reset();  // Keep it deterministic.
938 
939   const int num_definitions = 30;
940   const int num_struct_definitions = 5;  // Subset of num_definitions.
941   const int fields_per_definition = 15;
942   const int instances_per_definition = 5;
943   const int deprecation_rate = 10;        // 1 in deprecation_rate fields will
944                                           // be deprecated.
945 
946   std::string schema = "namespace test;\n\n";
947 
948   struct RndDef {
949     std::string instances[instances_per_definition];
950 
951     // Since we're generating schema and corresponding data in tandem,
952     // this convenience function adds strings to both at once.
953     static void Add(RndDef (&definitions_l)[num_definitions],
954                     std::string &schema_l,
955                     const int instances_per_definition_l,
956                     const char *schema_add, const char *instance_add,
957                     int definition) {
958       schema_l += schema_add;
959       for (int i = 0; i < instances_per_definition_l; i++)
960         definitions_l[definition].instances[i] += instance_add;
961     }
962   };
963 
964   #define AddToSchemaAndInstances(schema_add, instance_add) \
965     RndDef::Add(definitions, schema, instances_per_definition, \
966                 schema_add, instance_add, definition)
967 
968   #define Dummy() \
969     RndDef::Add(definitions, schema, instances_per_definition, \
970                 "byte", "1", definition)
971 
972   RndDef definitions[num_definitions];
973 
974   // We are going to generate num_definitions, the first
975   // num_struct_definitions will be structs, the rest tables. For each
976   // generate random fields, some of which may be struct/table types
977   // referring to previously generated structs/tables.
978   // Simultanenously, we generate instances_per_definition JSON data
979   // definitions, which will have identical structure to the schema
980   // being generated. We generate multiple instances such that when creating
981   // hierarchy, we get some variety by picking one randomly.
982   for (int definition = 0; definition < num_definitions; definition++) {
983     std::string definition_name = "D" + flatbuffers::NumToString(definition);
984 
985     bool is_struct = definition < num_struct_definitions;
986 
987     AddToSchemaAndInstances(
988       ((is_struct ? "struct " : "table ") + definition_name + " {\n").c_str(),
989       "{\n");
990 
991     for (int field = 0; field < fields_per_definition; field++) {
992       const bool is_last_field = field == fields_per_definition - 1;
993 
994       // Deprecate 1 in deprecation_rate fields. Only table fields can be
995       // deprecated.
996       // Don't deprecate the last field to avoid dangling commas in JSON.
997       const bool deprecated = !is_struct &&
998                               !is_last_field &&
999                               (lcg_rand() % deprecation_rate == 0);
1000 
1001       std::string field_name = "f" + flatbuffers::NumToString(field);
1002       AddToSchemaAndInstances(("  " + field_name + ":").c_str(),
1003                               deprecated ? "" : (field_name + ": ").c_str());
1004       // Pick random type:
1005       auto base_type = static_cast<flatbuffers::BaseType>(
1006                          lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1));
1007       switch (base_type) {
1008         case flatbuffers::BASE_TYPE_STRING:
1009           if (is_struct) {
1010             Dummy();  // No strings in structs.
1011           } else {
1012             AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
1013           }
1014           break;
1015         case flatbuffers::BASE_TYPE_VECTOR:
1016           if (is_struct) {
1017             Dummy();  // No vectors in structs.
1018           }
1019           else {
1020             AddToSchemaAndInstances("[ubyte]",
1021                                     deprecated ? "" : "[\n0,\n1,\n255\n]");
1022           }
1023           break;
1024         case flatbuffers::BASE_TYPE_NONE:
1025         case flatbuffers::BASE_TYPE_UTYPE:
1026         case flatbuffers::BASE_TYPE_STRUCT:
1027         case flatbuffers::BASE_TYPE_UNION:
1028           if (definition) {
1029             // Pick a random previous definition and random data instance of
1030             // that definition.
1031             int defref = lcg_rand() % definition;
1032             int instance = lcg_rand() % instances_per_definition;
1033             AddToSchemaAndInstances(
1034               ("D" + flatbuffers::NumToString(defref)).c_str(),
1035               deprecated
1036                 ? ""
1037                 : definitions[defref].instances[instance].c_str());
1038           } else {
1039             // If this is the first definition, we have no definition we can
1040             // refer to.
1041             Dummy();
1042           }
1043           break;
1044         case flatbuffers::BASE_TYPE_BOOL:
1045           AddToSchemaAndInstances("bool", deprecated
1046                                   ? ""
1047                                   : (lcg_rand() % 2 ? "true" : "false"));
1048           break;
1049         default:
1050           // All the scalar types.
1051           schema += flatbuffers::kTypeNames[base_type];
1052 
1053           if (!deprecated) {
1054             // We want each instance to use its own random value.
1055             for (int inst = 0; inst < instances_per_definition; inst++)
1056               definitions[definition].instances[inst] +=
1057               flatbuffers::IsFloat(base_type)
1058                   ? flatbuffers::NumToString<double>(lcg_rand() % 128).c_str()
1059                   : flatbuffers::NumToString<int>(lcg_rand() % 128).c_str();
1060           }
1061       }
1062       AddToSchemaAndInstances(
1063         deprecated ? "(deprecated);\n" : ";\n",
1064         deprecated ? "" : is_last_field ? "\n" : ",\n");
1065     }
1066     AddToSchemaAndInstances("}\n\n", "}");
1067   }
1068 
1069   schema += "root_type D" + flatbuffers::NumToString(num_definitions - 1);
1070   schema += ";\n";
1071 
1072   flatbuffers::Parser parser;
1073 
1074   // Will not compare against the original if we don't write defaults
1075   parser.builder_.ForceDefaults(true);
1076 
1077   // Parse the schema, parse the generated data, then generate text back
1078   // from the binary and compare against the original.
1079   TEST_EQ(parser.Parse(schema.c_str()), true);
1080 
1081   const std::string &json =
1082     definitions[num_definitions - 1].instances[0] + "\n";
1083 
1084   TEST_EQ(parser.Parse(json.c_str()), true);
1085 
1086   std::string jsongen;
1087   parser.opts.indent_step = 0;
1088   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1089   TEST_EQ(result, true);
1090 
1091   if (jsongen != json) {
1092     // These strings are larger than a megabyte, so we show the bytes around
1093     // the first bytes that are different rather than the whole string.
1094     size_t len = std::min(json.length(), jsongen.length());
1095     for (size_t i = 0; i < len; i++) {
1096       if (json[i] != jsongen[i]) {
1097         i -= std::min(static_cast<size_t>(10), i); // show some context;
1098         size_t end = std::min(len, i + 20);
1099         for (; i < end; i++)
1100           TEST_OUTPUT_LINE("at %d: found \"%c\", expected \"%c\"\n",
1101                            static_cast<int>(i), jsongen[i], json[i]);
1102         break;
1103       }
1104     }
1105     TEST_NOTNULL(NULL);
1106   }
1107 
1108   #ifdef FLATBUFFERS_TEST_VERBOSE
1109     TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
1110                      static_cast<int>(schema.length() / 1024),
1111                      static_cast<int>(json.length() / 1024));
1112   #endif
1113 }
1114 
1115 // Test that parser errors are actually generated.
TestError(const char * src,const char * error_substr,bool strict_json=false)1116 void TestError(const char *src, const char *error_substr,
1117                bool strict_json = false) {
1118   flatbuffers::IDLOptions opts;
1119   opts.strict_json = strict_json;
1120   flatbuffers::Parser parser(opts);
1121   TEST_EQ(parser.Parse(src), false);  // Must signal error
1122   // Must be the error we're expecting
1123   TEST_NOTNULL(strstr(parser.error_.c_str(), error_substr));
1124 }
1125 
1126 // Test that parsing errors occur as we'd expect.
1127 // Also useful for coverage, making sure these paths are run.
ErrorTest()1128 void ErrorTest() {
1129   // In order they appear in idl_parser.cpp
1130   TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
1131   TestError(".0", "floating point");
1132   TestError("\"\0", "illegal");
1133   TestError("\"\\q", "escape code");
1134   TestError("table ///", "documentation");
1135   TestError("@", "illegal");
1136   TestError("table 1", "expecting");
1137   TestError("table X { Y:[[int]]; }", "nested vector");
1138   TestError("table X { Y:1; }", "illegal type");
1139   TestError("table X { Y:int; Y:int; }", "field already");
1140   TestError("table Y {} table X { Y:int; }", "same as table");
1141   TestError("struct X { Y:string; }", "only scalar");
1142   TestError("struct X { Y:int (deprecated); }", "deprecate");
1143   TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
1144             "missing type field");
1145   TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
1146             "type id");
1147   TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
1148   TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
1149   TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
1150             true);
1151   TestError("struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
1152             "{ V:{ Y:1 } }", "wrong number");
1153   TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
1154             "unknown enum value");
1155   TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
1156   TestError("enum X:byte { Y } enum X {", "enum already");
1157   TestError("enum X:float {}", "underlying");
1158   TestError("enum X:byte { Y, Y }", "value already");
1159   TestError("enum X:byte { Y=2, Z=1 }", "ascending");
1160   TestError("union X { Y = 256 }", "must fit");
1161   TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out");
1162   TestError("table X { Y:int; } table X {", "datatype already");
1163   TestError("struct X (force_align: 7) { Y:int; }", "force_align");
1164   TestError("{}", "no root");
1165   TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "one json");
1166   TestError("root_type X;", "unknown root");
1167   TestError("struct X { Y:int; } root_type X;", "a table");
1168   TestError("union X { Y }", "referenced");
1169   TestError("union Z { X } struct X { Y:int; }", "only tables");
1170   TestError("table X { Y:[int]; YLength:int; }", "clash");
1171   TestError("table X { Y:string = 1; }", "scalar");
1172   TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
1173 }
1174 
TestValue(const char * json,const char * type_name)1175 template<typename T> T TestValue(const char *json, const char *type_name) {
1176   flatbuffers::Parser parser;
1177 
1178   // Simple schema.
1179   TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(type_name) +
1180                                    "; } root_type X;").c_str()), true);
1181 
1182   TEST_EQ(parser.Parse(json), true);
1183   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1184                 parser.builder_.GetBufferPointer());
1185   return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
1186 }
1187 
FloatCompare(float a,float b)1188 bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
1189 
1190 // Additional parser testing not covered elsewhere.
ValueTest()1191 void ValueTest() {
1192   // Test scientific notation numbers.
1193   TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }","float"),
1194                        (float)3.14159), true);
1195 
1196   // Test conversion functions.
1197   TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }","float"), -1),
1198           true);
1199 
1200   // Test negative hex constant.
1201   TEST_EQ(TestValue<int>("{ Y:-0x80 }","int"), -128);
1202 
1203   // Make sure we do unsigned 64bit correctly.
1204   TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }","ulong"),
1205                               12335089644688340133ULL);
1206 }
1207 
NestedListTest()1208 void NestedListTest() {
1209   flatbuffers::Parser parser1;
1210   TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
1211                         "root_type T;"
1212                         "{ F:[ [10,20], [30,40]] }"), true);
1213 }
1214 
EnumStringsTest()1215 void EnumStringsTest() {
1216   flatbuffers::Parser parser1;
1217   TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
1218                         "root_type T;"
1219                         "{ F:[ A, B, \"C\", \"A B C\" ] }"), true);
1220   flatbuffers::Parser parser2;
1221   TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
1222                         "root_type T;"
1223                         "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"), true);
1224 }
1225 
IntegerOutOfRangeTest()1226 void IntegerOutOfRangeTest() {
1227   TestError("table T { F:byte; } root_type T; { F:128 }",
1228             "constant does not fit");
1229   TestError("table T { F:byte; } root_type T; { F:-129 }",
1230             "constant does not fit");
1231   TestError("table T { F:ubyte; } root_type T; { F:256 }",
1232             "constant does not fit");
1233   TestError("table T { F:ubyte; } root_type T; { F:-1 }",
1234             "constant does not fit");
1235   TestError("table T { F:short; } root_type T; { F:32768 }",
1236             "constant does not fit");
1237   TestError("table T { F:short; } root_type T; { F:-32769 }",
1238             "constant does not fit");
1239   TestError("table T { F:ushort; } root_type T; { F:65536 }",
1240             "constant does not fit");
1241   TestError("table T { F:ushort; } root_type T; { F:-1 }",
1242             "constant does not fit");
1243   TestError("table T { F:int; } root_type T; { F:2147483648 }",
1244             "constant does not fit");
1245   TestError("table T { F:int; } root_type T; { F:-2147483649 }",
1246             "constant does not fit");
1247   TestError("table T { F:uint; } root_type T; { F:4294967296 }",
1248             "constant does not fit");
1249   TestError("table T { F:uint; } root_type T; { F:-1 }",
1250             "constant does not fit");
1251 }
1252 
IntegerBoundaryTest()1253 void IntegerBoundaryTest() {
1254   TEST_EQ(TestValue<int8_t>("{ Y:127 }","byte"), 127);
1255   TEST_EQ(TestValue<int8_t>("{ Y:-128 }","byte"), -128);
1256   TEST_EQ(TestValue<uint8_t>("{ Y:255 }","ubyte"), 255);
1257   TEST_EQ(TestValue<uint8_t>("{ Y:0 }","ubyte"), 0);
1258   TEST_EQ(TestValue<int16_t>("{ Y:32767 }","short"), 32767);
1259   TEST_EQ(TestValue<int16_t>("{ Y:-32768 }","short"), -32768);
1260   TEST_EQ(TestValue<uint16_t>("{ Y:65535 }","ushort"), 65535);
1261   TEST_EQ(TestValue<uint16_t>("{ Y:0 }","ushort"), 0);
1262   TEST_EQ(TestValue<int32_t>("{ Y:2147483647 }","int"), 2147483647);
1263   TEST_EQ(TestValue<int32_t>("{ Y:-2147483648 }","int"), (-2147483647 - 1));
1264   TEST_EQ(TestValue<uint32_t>("{ Y:4294967295 }","uint"), 4294967295);
1265   TEST_EQ(TestValue<uint32_t>("{ Y:0 }","uint"), 0);
1266   TEST_EQ(TestValue<int64_t>("{ Y:9223372036854775807 }","long"), 9223372036854775807);
1267   TEST_EQ(TestValue<int64_t>("{ Y:-9223372036854775808 }","long"), (-9223372036854775807 - 1));
1268   TEST_EQ(TestValue<uint64_t>("{ Y:18446744073709551615 }","ulong"), 18446744073709551615U);
1269   TEST_EQ(TestValue<uint64_t>("{ Y:0 }","ulong"), 0);
1270 }
1271 
UnicodeTest()1272 void UnicodeTest() {
1273   flatbuffers::Parser parser;
1274   // Without setting allow_non_utf8 = true, we treat \x sequences as byte sequences
1275   // which are then validated as UTF-8.
1276   TEST_EQ(parser.Parse("table T { F:string; }"
1277                        "root_type T;"
1278                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1279                        "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD83D\\uDE0E\" }"),
1280           true);
1281   std::string jsongen;
1282   parser.opts.indent_step = -1;
1283   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1284   TEST_EQ(result, true);
1285   TEST_EQ_STR(jsongen.c_str(),
1286             "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1287             "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
1288 }
1289 
UnicodeTestAllowNonUTF8()1290 void UnicodeTestAllowNonUTF8() {
1291   flatbuffers::Parser parser;
1292   parser.opts.allow_non_utf8 = true;
1293   TEST_EQ(parser.Parse("table T { F:string; }"
1294                        "root_type T;"
1295                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1296                        "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"), true);
1297   std::string jsongen;
1298   parser.opts.indent_step = -1;
1299   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1300   TEST_EQ(result, true);
1301   TEST_EQ_STR(jsongen.c_str(),
1302             "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1303             "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
1304 }
1305 
UnicodeTestGenerateTextFailsOnNonUTF8()1306 void UnicodeTestGenerateTextFailsOnNonUTF8() {
1307   flatbuffers::Parser parser;
1308   // Allow non-UTF-8 initially to model what happens when we load a binary flatbuffer from disk
1309   // which contains non-UTF-8 strings.
1310   parser.opts.allow_non_utf8 = true;
1311   TEST_EQ(parser.Parse("table T { F:string; }"
1312                        "root_type T;"
1313                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1314                        "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"), true);
1315   std::string jsongen;
1316   parser.opts.indent_step = -1;
1317   // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates failure.
1318   parser.opts.allow_non_utf8 = false;
1319   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1320   TEST_EQ(result, false);
1321 }
1322 
UnicodeSurrogatesTest()1323 void UnicodeSurrogatesTest() {
1324   flatbuffers::Parser parser;
1325 
1326   TEST_EQ(
1327     parser.Parse(
1328       "table T { F:string (id: 0); }"
1329       "root_type T;"
1330       "{ F:\"\\uD83D\\uDCA9\"}"), true);
1331   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1332     parser.builder_.GetBufferPointer());
1333   auto string = root->GetPointer<flatbuffers::String *>(
1334     flatbuffers::FieldIndexToOffset(0));
1335   TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
1336 }
1337 
UnicodeInvalidSurrogatesTest()1338 void UnicodeInvalidSurrogatesTest() {
1339   TestError(
1340     "table T { F:string; }"
1341     "root_type T;"
1342     "{ F:\"\\uD800\"}", "unpaired high surrogate");
1343   TestError(
1344     "table T { F:string; }"
1345     "root_type T;"
1346     "{ F:\"\\uD800abcd\"}", "unpaired high surrogate");
1347   TestError(
1348     "table T { F:string; }"
1349     "root_type T;"
1350     "{ F:\"\\uD800\\n\"}", "unpaired high surrogate");
1351   TestError(
1352     "table T { F:string; }"
1353     "root_type T;"
1354     "{ F:\"\\uD800\\uD800\"}", "multiple high surrogates");
1355   TestError(
1356     "table T { F:string; }"
1357     "root_type T;"
1358     "{ F:\"\\uDC00\"}", "unpaired low surrogate");
1359 }
1360 
InvalidUTF8Test()1361 void InvalidUTF8Test() {
1362   // "1 byte" pattern, under min length of 2 bytes
1363   TestError(
1364     "table T { F:string; }"
1365     "root_type T;"
1366     "{ F:\"\x80\"}", "illegal UTF-8 sequence");
1367   // 2 byte pattern, string too short
1368   TestError(
1369     "table T { F:string; }"
1370     "root_type T;"
1371     "{ F:\"\xDF\"}", "illegal UTF-8 sequence");
1372   // 3 byte pattern, string too short
1373   TestError(
1374     "table T { F:string; }"
1375     "root_type T;"
1376     "{ F:\"\xEF\xBF\"}", "illegal UTF-8 sequence");
1377   // 4 byte pattern, string too short
1378   TestError(
1379     "table T { F:string; }"
1380     "root_type T;"
1381     "{ F:\"\xF7\xBF\xBF\"}", "illegal UTF-8 sequence");
1382   // "5 byte" pattern, string too short
1383   TestError(
1384     "table T { F:string; }"
1385     "root_type T;"
1386     "{ F:\"\xFB\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1387   // "6 byte" pattern, string too short
1388   TestError(
1389     "table T { F:string; }"
1390     "root_type T;"
1391     "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1392   // "7 byte" pattern, string too short
1393   TestError(
1394     "table T { F:string; }"
1395     "root_type T;"
1396     "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1397   // "5 byte" pattern, over max length of 4 bytes
1398   TestError(
1399     "table T { F:string; }"
1400     "root_type T;"
1401     "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1402   // "6 byte" pattern, over max length of 4 bytes
1403   TestError(
1404     "table T { F:string; }"
1405     "root_type T;"
1406     "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1407   // "7 byte" pattern, over max length of 4 bytes
1408   TestError(
1409     "table T { F:string; }"
1410     "root_type T;"
1411     "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}", "illegal UTF-8 sequence");
1412 
1413   // Three invalid encodings for U+000A (\n, aka NEWLINE)
1414   TestError(
1415     "table T { F:string; }"
1416     "root_type T;"
1417     "{ F:\"\xC0\x8A\"}", "illegal UTF-8 sequence");
1418   TestError(
1419     "table T { F:string; }"
1420     "root_type T;"
1421     "{ F:\"\xE0\x80\x8A\"}", "illegal UTF-8 sequence");
1422   TestError(
1423     "table T { F:string; }"
1424     "root_type T;"
1425     "{ F:\"\xF0\x80\x80\x8A\"}", "illegal UTF-8 sequence");
1426 
1427   // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
1428   TestError(
1429     "table T { F:string; }"
1430     "root_type T;"
1431     "{ F:\"\xE0\x81\xA9\"}", "illegal UTF-8 sequence");
1432   TestError(
1433     "table T { F:string; }"
1434     "root_type T;"
1435     "{ F:\"\xF0\x80\x81\xA9\"}", "illegal UTF-8 sequence");
1436 
1437   // Invalid encoding for U+20AC (EURO SYMBOL)
1438   TestError(
1439     "table T { F:string; }"
1440     "root_type T;"
1441     "{ F:\"\xF0\x82\x82\xAC\"}", "illegal UTF-8 sequence");
1442 
1443   // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in UTF-8
1444   TestError(
1445     "table T { F:string; }"
1446     "root_type T;"
1447     // U+10400 "encoded" as U+D801 U+DC00
1448     "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}", "illegal UTF-8 sequence");
1449 }
1450 
UnknownFieldsTest()1451 void UnknownFieldsTest() {
1452   flatbuffers::IDLOptions opts;
1453   opts.skip_unexpected_fields_in_json = true;
1454   flatbuffers::Parser parser(opts);
1455 
1456   TEST_EQ(parser.Parse("table T { str:string; i:int;}"
1457                        "root_type T;"
1458                        "{ str:\"test\","
1459                        "unknown_string:\"test\","
1460                        "\"unknown_string\":\"test\","
1461                        "unknown_int:10,"
1462                        "unknown_float:1.0,"
1463                        "unknown_array: [ 1, 2, 3, 4],"
1464                        "unknown_object: { i: 10 },"
1465                        "\"unknown_object\": { \"i\": 10 },"
1466                        "i:10}"), true);
1467 
1468   std::string jsongen;
1469   parser.opts.indent_step = -1;
1470   auto result = GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1471   TEST_EQ(result, true);
1472   TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
1473 }
1474 
ParseUnionTest()1475 void ParseUnionTest() {
1476   // Unions must be parseable with the type field following the object.
1477   flatbuffers::Parser parser;
1478   TEST_EQ(parser.Parse("table T { A:int; }"
1479                        "union U { T }"
1480                        "table V { X:U; }"
1481                        "root_type V;"
1482                        "{ X:{ A:1 }, X_type: T }"), true);
1483   // Unions must be parsable with prefixed namespace.
1484   flatbuffers::Parser parser2;
1485   TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
1486                         "table B { e:U; } root_type B;"
1487                         "{ e_type: N_A, e: {} }"), true);
1488 }
1489 
UnionVectorTest()1490 void UnionVectorTest() {
1491   // load FlatBuffer fbs schema.
1492   // TODO: load a JSON file with such a vector when JSON support is ready.
1493   std::string schemafile;
1494   TEST_EQ(flatbuffers::LoadFile(
1495     (test_data_path + "union_vector/union_vector.fbs").c_str(), false,
1496     &schemafile), true);
1497 
1498   // parse schema.
1499   flatbuffers::IDLOptions idl_opts;
1500   idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kCpp;
1501   flatbuffers::Parser parser(idl_opts);
1502   TEST_EQ(parser.Parse(schemafile.c_str()), true);
1503 
1504   flatbuffers::FlatBufferBuilder fbb;
1505 
1506   // union types.
1507   std::vector<uint8_t> types;
1508   types.push_back(static_cast<uint8_t>(Character_Belle));
1509   types.push_back(static_cast<uint8_t>(Character_MuLan));
1510   types.push_back(static_cast<uint8_t>(Character_BookFan));
1511   types.push_back(static_cast<uint8_t>(Character_Other));
1512   types.push_back(static_cast<uint8_t>(Character_Unused));
1513 
1514   // union values.
1515   std::vector<flatbuffers::Offset<void>> characters;
1516   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
1517   characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
1518   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
1519   characters.push_back(fbb.CreateString("Other").Union());
1520   characters.push_back(fbb.CreateString("Unused").Union());
1521 
1522   // create Movie.
1523   const auto movie_offset =
1524       CreateMovie(fbb,
1525                   Character_Rapunzel,
1526                   fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
1527                   fbb.CreateVector(types),
1528                   fbb.CreateVector(characters));
1529   FinishMovieBuffer(fbb, movie_offset);
1530   auto buf = fbb.GetBufferPointer();
1531 
1532   flatbuffers::Verifier verifier(buf, fbb.GetSize());
1533   TEST_EQ(VerifyMovieBuffer(verifier), true);
1534 
1535   auto flat_movie = GetMovie(buf);
1536 
1537   auto TestMovie = [](const Movie *movie) {
1538     TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
1539 
1540     auto cts = movie->characters_type();
1541     TEST_EQ(movie->characters_type()->size(), 5);
1542     TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
1543     TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
1544     TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
1545     TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
1546     TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
1547 
1548     auto rapunzel = movie->main_character_as_Rapunzel();
1549     TEST_EQ(rapunzel->hair_length(), 6);
1550 
1551     auto cs = movie->characters();
1552     TEST_EQ(cs->size(), 5);
1553     auto belle = cs->GetAs<BookReader>(0);
1554     TEST_EQ(belle->books_read(), 7);
1555     auto mu_lan = cs->GetAs<Attacker>(1);
1556     TEST_EQ(mu_lan->sword_attack_damage(), 5);
1557     auto book_fan = cs->GetAs<BookReader>(2);
1558     TEST_EQ(book_fan->books_read(), 2);
1559     auto other = cs->GetAsString(3);
1560     TEST_EQ_STR(other->c_str(), "Other");
1561     auto unused = cs->GetAsString(4);
1562     TEST_EQ_STR(unused->c_str(), "Unused");
1563   };
1564 
1565   TestMovie(flat_movie);
1566 
1567   auto movie_object = flat_movie->UnPack();
1568   TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
1569   TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
1570   TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
1571   TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
1572   TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
1573   TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
1574 
1575   fbb.Clear();
1576   fbb.Finish(Movie::Pack(fbb, movie_object));
1577 
1578   delete movie_object;
1579 
1580   auto repacked_movie = GetMovie(fbb.GetBufferPointer());
1581 
1582   TestMovie(repacked_movie);
1583 
1584   auto s = flatbuffers::FlatBufferToString(fbb.GetBufferPointer(),
1585                                            MovieTypeTable());
1586   TEST_EQ_STR(s.c_str(),
1587     "{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
1588     "characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
1589     "characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
1590     "{ books_read: 2 }, \"Other\", \"Unused\" ] }");
1591 }
1592 
ConformTest()1593 void ConformTest() {
1594   flatbuffers::Parser parser;
1595   TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
1596 
1597   auto test_conform = [](flatbuffers::Parser &parser1,
1598                          const char *test, const char *expected_err) {
1599     flatbuffers::Parser parser2;
1600     TEST_EQ(parser2.Parse(test), true);
1601     auto err = parser2.ConformTo(parser1);
1602     TEST_NOTNULL(strstr(err.c_str(), expected_err));
1603   };
1604 
1605   test_conform(parser, "table T { A:byte; }", "types differ for field");
1606   test_conform(parser, "table T { B:int; A:int; }", "offsets differ for field");
1607   test_conform(parser, "table T { A:int = 1; }", "defaults differ for field");
1608   test_conform(parser, "table T { B:float; }",
1609                "field renamed to different type");
1610   test_conform(parser, "enum E:byte { B, A }", "values differ for enum");
1611 }
1612 
ParseProtoBufAsciiTest()1613 void ParseProtoBufAsciiTest() {
1614   // We can put the parser in a mode where it will accept JSON that looks more
1615   // like Protobuf ASCII, for users that have data in that format.
1616   // This uses no "" for field names (which we already support by default,
1617   // omits `,`, `:` before `{` and a couple of other features.
1618   flatbuffers::Parser parser;
1619   parser.opts.protobuf_ascii_alike = true;
1620   TEST_EQ(parser.Parse(
1621             "table S { B:int; } table T { A:[int]; C:S; } root_type T;"), true);
1622   TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
1623   // Similarly, in text output, it should omit these.
1624   std::string text;
1625   auto ok = flatbuffers::GenerateText(parser,
1626                                       parser.builder_.GetBufferPointer(),
1627                                       &text);
1628   TEST_EQ(ok, true);
1629   TEST_EQ_STR(text.c_str(),
1630               "{\n  A [\n    1\n    2\n  ]\n  C {\n    B: 2\n  }\n}\n");
1631 }
1632 
FlexBuffersTest()1633 void FlexBuffersTest() {
1634   flexbuffers::Builder slb(512,
1635                            flexbuffers::BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
1636 
1637   // Write the equivalent of:
1638   // { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ], foo: 100, bool: true, mymap: { foo: "Fred" } }
1639   #ifndef FLATBUFFERS_CPP98_STL
1640     // It's possible to do this without std::function support as well.
1641     slb.Map([&]() {
1642        slb.Vector("vec", [&]() {
1643         slb += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
1644         slb += "Fred";
1645         slb.IndirectFloat(4.0f);
1646         uint8_t blob[] = { 77 };
1647         slb.Blob(blob, 1);
1648         slb += false;
1649       });
1650       int ints[] = { 1, 2, 3 };
1651       slb.Vector("bar", ints, 3);
1652       slb.FixedTypedVector("bar3", ints, 3);
1653       bool bools[] = {true, false, true, false};
1654       slb.Vector("bools", bools, 4);
1655       slb.Bool("bool", true);
1656       slb.Double("foo", 100);
1657       slb.Map("mymap", [&]() {
1658         slb.String("foo", "Fred");  // Testing key and string reuse.
1659       });
1660     });
1661     slb.Finish();
1662   #else
1663     // It's possible to do this without std::function support as well.
1664     slb.Map([](flexbuffers::Builder& slb2) {
1665        slb2.Vector("vec", [](flexbuffers::Builder& slb3) {
1666         slb3 += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
1667         slb3 += "Fred";
1668         slb3.IndirectFloat(4.0f);
1669         uint8_t blob[] = { 77 };
1670         slb3.Blob(blob, 1);
1671         slb3 += false;
1672       }, slb2);
1673       int ints[] = { 1, 2, 3 };
1674       slb2.Vector("bar", ints, 3);
1675       slb2.FixedTypedVector("bar3", ints, 3);
1676       slb2.Bool("bool", true);
1677       slb2.Double("foo", 100);
1678       slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
1679         slb3.String("foo", "Fred");  // Testing key and string reuse.
1680       }, slb2);
1681     }, slb);
1682     slb.Finish();
1683   #endif  // FLATBUFFERS_CPP98_STL
1684 
1685   #ifdef FLATBUFFERS_TEST_VERBOSE
1686     for (size_t i = 0; i < slb.GetBuffer().size(); i++)
1687       printf("%d ", flatbuffers::vector_data(slb.GetBuffer())[i]);
1688     printf("\n");
1689   #endif
1690 
1691   auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
1692   TEST_EQ(map.size(), 7);
1693   auto vec = map["vec"].AsVector();
1694   TEST_EQ(vec.size(), 5);
1695   TEST_EQ(vec[0].AsInt64(), -100);
1696   TEST_EQ_STR(vec[1].AsString().c_str(), "Fred");
1697   TEST_EQ(vec[1].AsInt64(), 0);  // Number parsing failed.
1698   TEST_EQ(vec[2].AsDouble(), 4.0);
1699   TEST_EQ(vec[2].AsString().IsTheEmptyString(), true);  // Wrong Type.
1700   TEST_EQ_STR(vec[2].AsString().c_str(), "");  // This still works though.
1701   TEST_EQ_STR(vec[2].ToString().c_str(), "4.0");  // Or have it converted.
1702 
1703   // Few tests for templated version of As.
1704   TEST_EQ(vec[0].As<int64_t>(), -100);
1705   TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred");
1706   TEST_EQ(vec[1].As<int64_t>(), 0);  // Number parsing failed.
1707   TEST_EQ(vec[2].As<double>(), 4.0);
1708 
1709   // Test that the blob can be accessed.
1710   TEST_EQ(vec[3].IsBlob(), true);
1711   auto blob = vec[3].AsBlob();
1712   TEST_EQ(blob.size(), 1);
1713   TEST_EQ(blob.data()[0], 77);
1714   TEST_EQ(vec[4].IsBool(), true);  // Check if type is a bool
1715   TEST_EQ(vec[4].AsBool(), false);  // Check if value is false
1716   auto tvec = map["bar"].AsTypedVector();
1717   TEST_EQ(tvec.size(), 3);
1718   TEST_EQ(tvec[2].AsInt8(), 3);
1719   auto tvec3 = map["bar3"].AsFixedTypedVector();
1720   TEST_EQ(tvec3.size(), 3);
1721   TEST_EQ(tvec3[2].AsInt8(), 3);
1722   TEST_EQ(map["bool"].AsBool(), true);
1723   auto tvecb = map["bools"].AsTypedVector();
1724   TEST_EQ(tvecb.ElementType(), flexbuffers::TYPE_BOOL);
1725   TEST_EQ(map["foo"].AsUInt8(), 100);
1726   TEST_EQ(map["unknown"].IsNull(), true);
1727   auto mymap = map["mymap"].AsMap();
1728   // These should be equal by pointer equality, since key and value are shared.
1729   TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey());
1730   TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
1731   // We can mutate values in the buffer.
1732   TEST_EQ(vec[0].MutateInt(-99), true);
1733   TEST_EQ(vec[0].AsInt64(), -99);
1734   TEST_EQ(vec[1].MutateString("John"), true);  // Size must match.
1735   TEST_EQ_STR(vec[1].AsString().c_str(), "John");
1736   TEST_EQ(vec[1].MutateString("Alfred"), false);  // Too long.
1737   TEST_EQ(vec[2].MutateFloat(2.0f), true);
1738   TEST_EQ(vec[2].AsFloat(), 2.0f);
1739   TEST_EQ(vec[2].MutateFloat(3.14159), false);  // Double does not fit in float.
1740   TEST_EQ(vec[4].AsBool(), false); // Is false before change
1741   TEST_EQ(vec[4].MutateBool(true), true); // Can change a bool
1742   TEST_EQ(vec[4].AsBool(), true); // Changed bool is now true
1743 
1744   // Parse from JSON:
1745   flatbuffers::Parser parser;
1746   slb.Clear();
1747   auto jsontest = "{ a: [ 123, 456.0 ], b: \"hello\", c: true, d: false }";
1748   TEST_EQ(parser.ParseFlexBuffer(jsontest, nullptr, &slb),
1749           true);
1750   auto jroot = flexbuffers::GetRoot(slb.GetBuffer());
1751   auto jmap = jroot.AsMap();
1752   auto jvec = jmap["a"].AsVector();
1753   TEST_EQ(jvec[0].AsInt64(), 123);
1754   TEST_EQ(jvec[1].AsDouble(), 456.0);
1755   TEST_EQ_STR(jmap["b"].AsString().c_str(), "hello");
1756   TEST_EQ(jmap["c"].IsBool(), true); // Parsed correctly to a bool
1757   TEST_EQ(jmap["c"].AsBool(), true); // Parsed correctly to true
1758   TEST_EQ(jmap["d"].IsBool(), true); // Parsed correctly to a bool
1759   TEST_EQ(jmap["d"].AsBool(), false); // Parsed correctly to false
1760   // And from FlexBuffer back to JSON:
1761   auto jsonback = jroot.ToString();
1762   TEST_EQ_STR(jsontest, jsonback.c_str());
1763 }
1764 
TypeAliasesTest()1765 void TypeAliasesTest()
1766 {
1767   flatbuffers::FlatBufferBuilder builder;
1768 
1769   builder.Finish(CreateTypeAliases(
1770       builder,
1771       flatbuffers::numeric_limits<int8_t>::min(),
1772       flatbuffers::numeric_limits<uint8_t>::max(),
1773       flatbuffers::numeric_limits<int16_t>::min(),
1774       flatbuffers::numeric_limits<uint16_t>::max(),
1775       flatbuffers::numeric_limits<int32_t>::min(),
1776       flatbuffers::numeric_limits<uint32_t>::max(),
1777       flatbuffers::numeric_limits<int64_t>::min(),
1778       flatbuffers::numeric_limits<uint64_t>::max(),
1779       2.3f, 2.3));
1780 
1781   auto p = builder.GetBufferPointer();
1782   auto ta = flatbuffers::GetRoot<TypeAliases>(p);
1783 
1784   TEST_EQ(ta->i8(), flatbuffers::numeric_limits<int8_t>::min());
1785   TEST_EQ(ta->u8(), flatbuffers::numeric_limits<uint8_t>::max());
1786   TEST_EQ(ta->i16(), flatbuffers::numeric_limits<int16_t>::min());
1787   TEST_EQ(ta->u16(), flatbuffers::numeric_limits<uint16_t>::max());
1788   TEST_EQ(ta->i32(), flatbuffers::numeric_limits<int32_t>::min());
1789   TEST_EQ(ta->u32(), flatbuffers::numeric_limits<uint32_t>::max());
1790   TEST_EQ(ta->i64(), flatbuffers::numeric_limits<int64_t>::min());
1791   TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
1792   TEST_EQ(ta->f32(), 2.3f);
1793   TEST_EQ(ta->f64(), 2.3);
1794   TEST_EQ(sizeof(ta->i8()), 1);
1795   TEST_EQ(sizeof(ta->i16()), 2);
1796   TEST_EQ(sizeof(ta->i32()), 4);
1797   TEST_EQ(sizeof(ta->i64()), 8);
1798   TEST_EQ(sizeof(ta->u8()), 1);
1799   TEST_EQ(sizeof(ta->u16()), 2);
1800   TEST_EQ(sizeof(ta->u32()), 4);
1801   TEST_EQ(sizeof(ta->u64()), 8);
1802   TEST_EQ(sizeof(ta->f32()), 4);
1803   TEST_EQ(sizeof(ta->f64()), 8);
1804 }
1805 
EndianSwapTest()1806 void EndianSwapTest() {
1807   TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)),
1808           0x3412);
1809   TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
1810           0x78563412);
1811   TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
1812           0xEFCDAB9078563412);
1813   TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
1814 }
1815 
main(int,const char * [])1816 int main(int /*argc*/, const char * /*argv*/[]) {
1817   #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
1818       defined(_MSC_VER) && defined(_DEBUG)
1819     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
1820       // For more thorough checking:
1821       //| _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF
1822     );
1823   #endif
1824 
1825   // Run our various test suites:
1826 
1827   std::string rawbuf;
1828   auto flatbuf1 = CreateFlatBufferTest(rawbuf);
1829   #if !defined(FLATBUFFERS_CPP98_STL)
1830     auto flatbuf = std::move(flatbuf1);  // Test move assignment.
1831   #else
1832     auto &flatbuf = flatbuf1;
1833   #endif // !defined(FLATBUFFERS_CPP98_STL)
1834 
1835   TriviallyCopyableTest();
1836 
1837   AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
1838                        rawbuf.length());
1839   AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
1840 
1841   MutateFlatBuffersTest(flatbuf.data(), flatbuf.size());
1842 
1843   ObjectFlatBuffersTest(flatbuf.data());
1844 
1845   MiniReflectFlatBuffersTest(flatbuf.data());
1846 
1847   SizePrefixedTest();
1848 
1849   #ifndef FLATBUFFERS_NO_FILE_TESTS
1850     #ifdef FLATBUFFERS_TEST_PATH_PREFIX
1851       test_data_path = FLATBUFFERS_STRING(FLATBUFFERS_TEST_PATH_PREFIX) +
1852                        test_data_path;
1853     #endif
1854     ParseAndGenerateTextTest();
1855     ReflectionTest(flatbuf.data(), flatbuf.size());
1856     ParseProtoTest();
1857     UnionVectorTest();
1858   #endif
1859 
1860   FuzzTest1();
1861   FuzzTest2();
1862 
1863   ErrorTest();
1864   ValueTest();
1865   EnumStringsTest();
1866   IntegerOutOfRangeTest();
1867   IntegerBoundaryTest();
1868   UnicodeTest();
1869   UnicodeTestAllowNonUTF8();
1870   UnicodeTestGenerateTextFailsOnNonUTF8();
1871   UnicodeSurrogatesTest();
1872   UnicodeInvalidSurrogatesTest();
1873   InvalidUTF8Test();
1874   UnknownFieldsTest();
1875   ParseUnionTest();
1876   ConformTest();
1877   ParseProtoBufAsciiTest();
1878   TypeAliasesTest();
1879   EndianSwapTest();
1880 
1881   FlexBuffersTest();
1882 
1883   if (!testing_fails) {
1884     TEST_OUTPUT_LINE("ALL TESTS PASSED");
1885     return 0;
1886   } else {
1887     TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
1888     return 1;
1889   }
1890 }
1891