• 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 #include <cmath>
17 #include "flatbuffers/flatbuffers.h"
18 #include "flatbuffers/idl.h"
19 #include "flatbuffers/minireflect.h"
20 #include "flatbuffers/registry.h"
21 #include "flatbuffers/util.h"
22 
23 // clang-format off
24 #ifdef FLATBUFFERS_CPP98_STL
25   #include "flatbuffers/stl_emulation.h"
26   namespace std {
27     using flatbuffers::unique_ptr;
28   }
29 #endif
30 // clang-format on
31 
32 #include "monster_test_generated.h"
33 #include "namespace_test/namespace_test1_generated.h"
34 #include "namespace_test/namespace_test2_generated.h"
35 #include "union_vector/union_vector_generated.h"
36 #include "monster_extra_generated.h"
37 #include "test_assert.h"
38 
39 #include "flatbuffers/flexbuffers.h"
40 
41 using namespace MyGame::Example;
42 
43 void FlatBufferBuilderTest();
44 
45 // Include simple random number generator to ensure results will be the
46 // same cross platform.
47 // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
48 uint32_t lcg_seed = 48271;
lcg_rand()49 uint32_t lcg_rand() {
50   return lcg_seed = (static_cast<uint64_t>(lcg_seed) * 279470273UL) % 4294967291UL;
51 }
lcg_reset()52 void lcg_reset() { lcg_seed = 48271; }
53 
54 std::string test_data_path =
55 #ifdef BAZEL_TEST_DATA_PATH
56     "../com_github_google_flatbuffers/tests/";
57 #else
58     "tests/";
59 #endif
60 
61 // example of how to build up a serialized buffer algorithmically:
CreateFlatBufferTest(std::string & buffer)62 flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
63   flatbuffers::FlatBufferBuilder builder;
64 
65   auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
66 
67   auto name = builder.CreateString("MyMonster");
68 
69   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
70   auto inventory = builder.CreateVector(inv_data, 10);
71 
72   // Alternatively, create the vector first, and fill in data later:
73   // unsigned char *inv_buf = nullptr;
74   // auto inventory = builder.CreateUninitializedVector<unsigned char>(
75   //                                                              10, &inv_buf);
76   // memcpy(inv_buf, inv_data, 10);
77 
78   Test tests[] = { Test(10, 20), Test(30, 40) };
79   auto testv = builder.CreateVectorOfStructs(tests, 2);
80 
81   // clang-format off
82   #ifndef FLATBUFFERS_CPP98_STL
83     // Create a vector of structures from a lambda.
84     auto testv2 = builder.CreateVectorOfStructs<Test>(
85           2, [&](size_t i, Test* s) -> void {
86             *s = tests[i];
87           });
88   #else
89     // Create a vector of structures using a plain old C++ function.
90     auto testv2 = builder.CreateVectorOfStructs<Test>(
91           2, [](size_t i, Test* s, void *state) -> void {
92             *s = (reinterpret_cast<Test*>(state))[i];
93           }, tests);
94   #endif  // FLATBUFFERS_CPP98_STL
95   // clang-format on
96 
97   // create monster with very few fields set:
98   // (same functionality as CreateMonster below, but sets fields manually)
99   flatbuffers::Offset<Monster> mlocs[3];
100   auto fred = builder.CreateString("Fred");
101   auto barney = builder.CreateString("Barney");
102   auto wilma = builder.CreateString("Wilma");
103   MonsterBuilder mb1(builder);
104   mb1.add_name(fred);
105   mlocs[0] = mb1.Finish();
106   MonsterBuilder mb2(builder);
107   mb2.add_name(barney);
108   mb2.add_hp(1000);
109   mlocs[1] = mb2.Finish();
110   MonsterBuilder mb3(builder);
111   mb3.add_name(wilma);
112   mlocs[2] = mb3.Finish();
113 
114   // Create an array of strings. Also test string pooling, and lambdas.
115   auto vecofstrings =
116       builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(
117           4,
118           [](size_t i, flatbuffers::FlatBufferBuilder *b)
119               -> flatbuffers::Offset<flatbuffers::String> {
120             static const char *names[] = { "bob", "fred", "bob", "fred" };
121             return b->CreateSharedString(names[i]);
122           },
123           &builder);
124 
125   // Creating vectors of strings in one convenient call.
126   std::vector<std::string> names2;
127   names2.push_back("jane");
128   names2.push_back("mary");
129   auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
130 
131   // Create an array of sorted tables, can be used with binary search when read:
132   auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
133 
134   // Create an array of sorted structs,
135   // can be used with binary search when read:
136   std::vector<Ability> abilities;
137   abilities.push_back(Ability(4, 40));
138   abilities.push_back(Ability(3, 30));
139   abilities.push_back(Ability(2, 20));
140   abilities.push_back(Ability(1, 10));
141   auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
142 
143   // Create a nested FlatBuffer.
144   // Nested FlatBuffers are stored in a ubyte vector, which can be convenient
145   // since they can be memcpy'd around much easier than other FlatBuffer
146   // values. They have little overhead compared to storing the table directly.
147   // As a test, create a mostly empty Monster buffer:
148   flatbuffers::FlatBufferBuilder nested_builder;
149   auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
150                              nested_builder.CreateString("NestedMonster"));
151   FinishMonsterBuffer(nested_builder, nmloc);
152   // Now we can store the buffer in the parent. Note that by default, vectors
153   // are only aligned to their elements or size field, so in this case if the
154   // buffer contains 64-bit elements, they may not be correctly aligned. We fix
155   // that with:
156   builder.ForceVectorAlignment(nested_builder.GetSize(), sizeof(uint8_t),
157                                nested_builder.GetBufferMinAlignment());
158   // If for whatever reason you don't have the nested_builder available, you
159   // can substitute flatbuffers::largest_scalar_t (64-bit) for the alignment, or
160   // the largest force_align value in your schema if you're using it.
161   auto nested_flatbuffer_vector = builder.CreateVector(
162       nested_builder.GetBufferPointer(), nested_builder.GetSize());
163 
164   // Test a nested FlexBuffer:
165   flexbuffers::Builder flexbuild;
166   flexbuild.Int(1234);
167   flexbuild.Finish();
168   auto flex = builder.CreateVector(flexbuild.GetBuffer());
169 
170   // Test vector of enums.
171   Color colors[] = { Color_Blue, Color_Green };
172   // We use this special creation function because we have an array of
173   // pre-C++11 (enum class) enums whose size likely is int, yet its declared
174   // type in the schema is byte.
175   auto vecofcolors = builder.CreateVectorScalarCast<int8_t, Color>(colors, 2);
176 
177   // shortcut for creating monster with all fields set:
178   auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
179                             Any_Monster, mlocs[1].Union(),  // Store a union.
180                             testv, vecofstrings, vecoftables, 0,
181                             nested_flatbuffer_vector, 0, false, 0, 0, 0, 0, 0,
182                             0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
183                             vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0,
184                             0, 0, 0, AnyUniqueAliases_NONE, 0,
185                             AnyAmbiguousAliases_NONE, 0, vecofcolors);
186 
187   FinishMonsterBuffer(builder, mloc);
188 
189   // clang-format off
190   #ifdef FLATBUFFERS_TEST_VERBOSE
191   // print byte data for debugging:
192   auto p = builder.GetBufferPointer();
193   for (flatbuffers::uoffset_t i = 0; i < builder.GetSize(); i++)
194     printf("%d ", p[i]);
195   #endif
196   // clang-format on
197 
198   // return the buffer for the caller to use.
199   auto bufferpointer =
200       reinterpret_cast<const char *>(builder.GetBufferPointer());
201   buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
202 
203   return builder.Release();
204 }
205 
206 //  example of accessing a buffer loaded in memory:
AccessFlatBufferTest(const uint8_t * flatbuf,size_t length,bool pooled=true)207 void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
208                           bool pooled = true) {
209   // First, verify the buffers integrity (optional)
210   flatbuffers::Verifier verifier(flatbuf, length);
211   TEST_EQ(VerifyMonsterBuffer(verifier), true);
212 
213   // clang-format off
214   #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
215     std::vector<uint8_t> test_buff;
216     test_buff.resize(length * 2);
217     std::memcpy(&test_buff[0], flatbuf, length);
218     std::memcpy(&test_buff[length], flatbuf, length);
219 
220     flatbuffers::Verifier verifier1(&test_buff[0], length);
221     TEST_EQ(VerifyMonsterBuffer(verifier1), true);
222     TEST_EQ(verifier1.GetComputedSize(), length);
223 
224     flatbuffers::Verifier verifier2(&test_buff[length], length);
225     TEST_EQ(VerifyMonsterBuffer(verifier2), true);
226     TEST_EQ(verifier2.GetComputedSize(), length);
227   #endif
228   // clang-format on
229 
230   TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
231   TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
232   TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
233 
234   // Access the buffer from the root.
235   auto monster = GetMonster(flatbuf);
236 
237   TEST_EQ(monster->hp(), 80);
238   TEST_EQ(monster->mana(), 150);  // default
239   TEST_EQ_STR(monster->name()->c_str(), "MyMonster");
240   // Can't access the following field, it is deprecated in the schema,
241   // which means accessors are not generated:
242   // monster.friendly()
243 
244   auto pos = monster->pos();
245   TEST_NOTNULL(pos);
246   TEST_EQ(pos->z(), 3);
247   TEST_EQ(pos->test3().a(), 10);
248   TEST_EQ(pos->test3().b(), 20);
249 
250   auto inventory = monster->inventory();
251   TEST_EQ(VectorLength(inventory), 10UL);  // Works even if inventory is null.
252   TEST_NOTNULL(inventory);
253   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
254   // Check compatibilty of iterators with STL.
255   std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
256   for (auto it = inventory->begin(); it != inventory->end(); ++it) {
257     auto indx = it - inventory->begin();
258     TEST_EQ(*it, inv_vec.at(indx));  // Use bounds-check.
259     TEST_EQ(*it, inv_data[indx]);
260   }
261 
262   for (auto it = inventory->cbegin(); it != inventory->cend(); ++it) {
263     auto indx = it - inventory->cbegin();
264     TEST_EQ(*it, inv_vec.at(indx));  // Use bounds-check.
265     TEST_EQ(*it, inv_data[indx]);
266   }
267 
268   for (auto it = inventory->rbegin(); it != inventory->rend(); ++it) {
269     auto indx = inventory->rend() - it;
270     TEST_EQ(*it, inv_vec.at(indx));  // Use bounds-check.
271     TEST_EQ(*it, inv_data[indx]);
272   }
273 
274   for (auto it = inventory->crbegin(); it != inventory->crend(); ++it) {
275     auto indx = inventory->crend() - it;
276     TEST_EQ(*it, inv_vec.at(indx));  // Use bounds-check.
277     TEST_EQ(*it, inv_data[indx]);
278   }
279 
280   TEST_EQ(monster->color(), Color_Blue);
281 
282   // Example of accessing a union:
283   TEST_EQ(monster->test_type(), Any_Monster);  // First make sure which it is.
284   auto monster2 = reinterpret_cast<const Monster *>(monster->test());
285   TEST_NOTNULL(monster2);
286   TEST_EQ_STR(monster2->name()->c_str(), "Fred");
287 
288   // Example of accessing a vector of strings:
289   auto vecofstrings = monster->testarrayofstring();
290   TEST_EQ(vecofstrings->size(), 4U);
291   TEST_EQ_STR(vecofstrings->Get(0)->c_str(), "bob");
292   TEST_EQ_STR(vecofstrings->Get(1)->c_str(), "fred");
293   if (pooled) {
294     // These should have pointer equality because of string pooling.
295     TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str());
296     TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str());
297   }
298 
299   auto vecofstrings2 = monster->testarrayofstring2();
300   if (vecofstrings2) {
301     TEST_EQ(vecofstrings2->size(), 2U);
302     TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane");
303     TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary");
304   }
305 
306   // Example of accessing a vector of tables:
307   auto vecoftables = monster->testarrayoftables();
308   TEST_EQ(vecoftables->size(), 3U);
309   for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
310     TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
311   TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney");
312   TEST_EQ(vecoftables->Get(0)->hp(), 1000);
313   TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred");
314   TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma");
315   TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
316   TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
317   TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
318 
319   // Test accessing a vector of sorted structs
320   auto vecofstructs = monster->testarrayofsortedstruct();
321   if (vecofstructs) {  // not filled in monster_test.bfbs
322     for (flatbuffers::uoffset_t i = 0; i < vecofstructs->size() - 1; i++) {
323       auto left = vecofstructs->Get(i);
324       auto right = vecofstructs->Get(i + 1);
325       TEST_EQ(true, (left->KeyCompareLessThan(right)));
326     }
327     TEST_NOTNULL(vecofstructs->LookupByKey(3));
328     TEST_EQ(static_cast<const Ability *>(nullptr),
329             vecofstructs->LookupByKey(5));
330   }
331 
332   // Test nested FlatBuffers if available:
333   auto nested_buffer = monster->testnestedflatbuffer();
334   if (nested_buffer) {
335     // nested_buffer is a vector of bytes you can memcpy. However, if you
336     // actually want to access the nested data, this is a convenient
337     // accessor that directly gives you the root table:
338     auto nested_monster = monster->testnestedflatbuffer_nested_root();
339     TEST_EQ_STR(nested_monster->name()->c_str(), "NestedMonster");
340   }
341 
342   // Test flexbuffer if available:
343   auto flex = monster->flex();
344   // flex is a vector of bytes you can memcpy etc.
345   TEST_EQ(flex->size(), 4);  // Encoded FlexBuffer bytes.
346   // However, if you actually want to access the nested data, this is a
347   // convenient accessor that directly gives you the root value:
348   TEST_EQ(monster->flex_flexbuffer_root().AsInt16(), 1234);
349 
350   // Test vector of enums:
351   auto colors = monster->vector_of_enums();
352   if (colors) {
353     TEST_EQ(colors->size(), 2);
354     TEST_EQ(colors->Get(0), Color_Blue);
355     TEST_EQ(colors->Get(1), Color_Green);
356   }
357 
358   // Since Flatbuffers uses explicit mechanisms to override the default
359   // compiler alignment, double check that the compiler indeed obeys them:
360   // (Test consists of a short and byte):
361   TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
362   TEST_EQ(sizeof(Test), 4UL);
363 
364   const flatbuffers::Vector<const Test *> *tests_array[] = {
365     monster->test4(),
366     monster->test5(),
367   };
368   for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
369     auto tests = tests_array[i];
370     TEST_NOTNULL(tests);
371     auto test_0 = tests->Get(0);
372     auto test_1 = tests->Get(1);
373     TEST_EQ(test_0->a(), 10);
374     TEST_EQ(test_0->b(), 20);
375     TEST_EQ(test_1->a(), 30);
376     TEST_EQ(test_1->b(), 40);
377     for (auto it = tests->begin(); it != tests->end(); ++it) {
378       TEST_EQ(it->a() == 10 || it->a() == 30, true);  // Just testing iterators.
379     }
380   }
381 
382   // Checking for presence of fields:
383   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
384   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
385 
386   // Obtaining a buffer from a root:
387   TEST_EQ(GetBufferStartFromRootPointer(monster), flatbuf);
388 }
389 
390 // Change a FlatBuffer in-place, after it has been constructed.
MutateFlatBuffersTest(uint8_t * flatbuf,std::size_t length)391 void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
392   // Get non-const pointer to root.
393   auto monster = GetMutableMonster(flatbuf);
394 
395   // Each of these tests mutates, then tests, then set back to the original,
396   // so we can test that the buffer in the end still passes our original test.
397   auto hp_ok = monster->mutate_hp(10);
398   TEST_EQ(hp_ok, true);  // Field was present.
399   TEST_EQ(monster->hp(), 10);
400   // Mutate to default value
401   auto hp_ok_default = monster->mutate_hp(100);
402   TEST_EQ(hp_ok_default, true);  // Field was present.
403   TEST_EQ(monster->hp(), 100);
404   // Test that mutate to default above keeps field valid for further mutations
405   auto hp_ok_2 = monster->mutate_hp(20);
406   TEST_EQ(hp_ok_2, true);
407   TEST_EQ(monster->hp(), 20);
408   monster->mutate_hp(80);
409 
410   // Monster originally at 150 mana (default value)
411   auto mana_default_ok = monster->mutate_mana(150);  // Mutate to default value.
412   TEST_EQ(mana_default_ok,
413           true);  // Mutation should succeed, because default value.
414   TEST_EQ(monster->mana(), 150);
415   auto mana_ok = monster->mutate_mana(10);
416   TEST_EQ(mana_ok, false);  // Field was NOT present, because default value.
417   TEST_EQ(monster->mana(), 150);
418 
419   // Mutate structs.
420   auto pos = monster->mutable_pos();
421   auto test3 = pos->mutable_test3();  // Struct inside a struct.
422   test3.mutate_a(50);                 // Struct fields never fail.
423   TEST_EQ(test3.a(), 50);
424   test3.mutate_a(10);
425 
426   // Mutate vectors.
427   auto inventory = monster->mutable_inventory();
428   inventory->Mutate(9, 100);
429   TEST_EQ(inventory->Get(9), 100);
430   inventory->Mutate(9, 9);
431 
432   auto tables = monster->mutable_testarrayoftables();
433   auto first = tables->GetMutableObject(0);
434   TEST_EQ(first->hp(), 1000);
435   first->mutate_hp(0);
436   TEST_EQ(first->hp(), 0);
437   first->mutate_hp(1000);
438 
439   // Run the verifier and the regular test to make sure we didn't trample on
440   // anything.
441   AccessFlatBufferTest(flatbuf, length);
442 }
443 
444 // Unpack a FlatBuffer into objects.
ObjectFlatBuffersTest(uint8_t * flatbuf)445 void ObjectFlatBuffersTest(uint8_t *flatbuf) {
446   // Optional: we can specify resolver and rehasher functions to turn hashed
447   // strings into object pointers and back, to implement remote references
448   // and such.
449   auto resolver = flatbuffers::resolver_function_t(
450       [](void **pointer_adr, flatbuffers::hash_value_t hash) {
451         (void)pointer_adr;
452         (void)hash;
453         // Don't actually do anything, leave variable null.
454       });
455   auto rehasher = flatbuffers::rehasher_function_t(
456       [](void *pointer) -> flatbuffers::hash_value_t {
457         (void)pointer;
458         return 0;
459       });
460 
461   // Turn a buffer into C++ objects.
462   auto monster1 = UnPackMonster(flatbuf, &resolver);
463 
464   // Re-serialize the data.
465   flatbuffers::FlatBufferBuilder fbb1;
466   fbb1.Finish(CreateMonster(fbb1, monster1.get(), &rehasher),
467               MonsterIdentifier());
468 
469   // Unpack again, and re-serialize again.
470   auto monster2 = UnPackMonster(fbb1.GetBufferPointer(), &resolver);
471   flatbuffers::FlatBufferBuilder fbb2;
472   fbb2.Finish(CreateMonster(fbb2, monster2.get(), &rehasher),
473               MonsterIdentifier());
474 
475   // Now we've gone full round-trip, the two buffers should match.
476   auto len1 = fbb1.GetSize();
477   auto len2 = fbb2.GetSize();
478   TEST_EQ(len1, len2);
479   TEST_EQ(memcmp(fbb1.GetBufferPointer(), fbb2.GetBufferPointer(), len1), 0);
480 
481   // Test it with the original buffer test to make sure all data survived.
482   AccessFlatBufferTest(fbb2.GetBufferPointer(), len2, false);
483 
484   // Test accessing fields, similar to AccessFlatBufferTest above.
485   TEST_EQ(monster2->hp, 80);
486   TEST_EQ(monster2->mana, 150);  // default
487   TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
488 
489   auto &pos = monster2->pos;
490   TEST_NOTNULL(pos);
491   TEST_EQ(pos->z(), 3);
492   TEST_EQ(pos->test3().a(), 10);
493   TEST_EQ(pos->test3().b(), 20);
494 
495   auto &inventory = monster2->inventory;
496   TEST_EQ(inventory.size(), 10UL);
497   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
498   for (auto it = inventory.begin(); it != inventory.end(); ++it)
499     TEST_EQ(*it, inv_data[it - inventory.begin()]);
500 
501   TEST_EQ(monster2->color, Color_Blue);
502 
503   auto monster3 = monster2->test.AsMonster();
504   TEST_NOTNULL(monster3);
505   TEST_EQ_STR(monster3->name.c_str(), "Fred");
506 
507   auto &vecofstrings = monster2->testarrayofstring;
508   TEST_EQ(vecofstrings.size(), 4U);
509   TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
510   TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
511 
512   auto &vecofstrings2 = monster2->testarrayofstring2;
513   TEST_EQ(vecofstrings2.size(), 2U);
514   TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
515   TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
516 
517   auto &vecoftables = monster2->testarrayoftables;
518   TEST_EQ(vecoftables.size(), 3U);
519   TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
520   TEST_EQ(vecoftables[0]->hp, 1000);
521   TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
522   TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
523 
524   auto &tests = monster2->test4;
525   TEST_EQ(tests[0].a(), 10);
526   TEST_EQ(tests[0].b(), 20);
527   TEST_EQ(tests[1].a(), 30);
528   TEST_EQ(tests[1].b(), 40);
529 }
530 
531 // Prefix a FlatBuffer with a size field.
SizePrefixedTest()532 void SizePrefixedTest() {
533   // Create size prefixed buffer.
534   flatbuffers::FlatBufferBuilder fbb;
535   FinishSizePrefixedMonsterBuffer(
536       fbb,
537       CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
538 
539   // Verify it.
540   flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
541   TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true);
542 
543   // Access it.
544   auto m = GetSizePrefixedMonster(fbb.GetBufferPointer());
545   TEST_EQ(m->mana(), 200);
546   TEST_EQ(m->hp(), 300);
547   TEST_EQ_STR(m->name()->c_str(), "bob");
548 }
549 
TriviallyCopyableTest()550 void TriviallyCopyableTest() {
551   // clang-format off
552   #if __GNUG__ && __GNUC__ < 5
553     TEST_EQ(__has_trivial_copy(Vec3), true);
554   #else
555     #if __cplusplus >= 201103L
556       TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
557     #endif
558   #endif
559   // clang-format on
560 }
561 
562 // Check stringify of an default enum value to json
JsonDefaultTest()563 void JsonDefaultTest() {
564   // load FlatBuffer schema (.fbs) from disk
565   std::string schemafile;
566   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
567                                 false, &schemafile), true);
568   // parse schema first, so we can use it to parse the data after
569   flatbuffers::Parser parser;
570   auto include_test_path =
571       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
572   const char *include_directories[] = { test_data_path.c_str(),
573                                         include_test_path.c_str(), nullptr };
574 
575   TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
576   // create incomplete monster and store to json
577   parser.opts.output_default_scalars_in_json = true;
578   parser.opts.output_enum_identifiers = true;
579   flatbuffers::FlatBufferBuilder builder;
580   auto name = builder.CreateString("default_enum");
581   MonsterBuilder color_monster(builder);
582   color_monster.add_name(name);
583   FinishMonsterBuffer(builder, color_monster.Finish());
584   std::string jsongen;
585   auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
586   TEST_EQ(result, true);
587   // default value of the "color" field is Blue
588   TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true);
589   // default value of the "testf" field is 3.14159
590   TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
591 }
592 
593 // example of parsing text straight into a buffer, and generating
594 // text back from it:
ParseAndGenerateTextTest(bool binary)595 void ParseAndGenerateTextTest(bool binary) {
596   // load FlatBuffer schema (.fbs) and JSON from disk
597   std::string schemafile;
598   std::string jsonfile;
599   TEST_EQ(flatbuffers::LoadFile(
600               (test_data_path + "monster_test." + (binary ? "bfbs" : "fbs"))
601                   .c_str(),
602               binary, &schemafile),
603           true);
604   TEST_EQ(flatbuffers::LoadFile(
605               (test_data_path + "monsterdata_test.golden").c_str(), false,
606               &jsonfile),
607           true);
608 
609   auto include_test_path =
610     flatbuffers::ConCatPathFileName(test_data_path, "include_test");
611   const char *include_directories[] = { test_data_path.c_str(),
612                                         include_test_path.c_str(), nullptr };
613 
614   // parse schema first, so we can use it to parse the data after
615   flatbuffers::Parser parser;
616   if (binary) {
617     flatbuffers::Verifier verifier(
618         reinterpret_cast<const uint8_t *>(schemafile.c_str()),
619         schemafile.size());
620     TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
621     //auto schema = reflection::GetSchema(schemafile.c_str());
622     TEST_EQ(parser.Deserialize((const uint8_t *)schemafile.c_str(), schemafile.size()), true);
623   } else {
624     TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
625   }
626   TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
627 
628   // here, parser.builder_ contains a binary buffer that is the parsed data.
629 
630   // First, verify it, just in case:
631   flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
632                                  parser.builder_.GetSize());
633   TEST_EQ(VerifyMonsterBuffer(verifier), true);
634 
635   AccessFlatBufferTest(parser.builder_.GetBufferPointer(),
636                        parser.builder_.GetSize(), false);
637 
638   // to ensure it is correct, we now generate text back from the binary,
639   // and compare the two:
640   std::string jsongen;
641   auto result =
642       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
643   TEST_EQ(result, true);
644   TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
645 
646   // We can also do the above using the convenient Registry that knows about
647   // a set of file_identifiers mapped to schemas.
648   flatbuffers::Registry registry;
649   // Make sure schemas can find their includes.
650   registry.AddIncludeDirectory(test_data_path.c_str());
651   registry.AddIncludeDirectory(include_test_path.c_str());
652   // Call this with many schemas if possible.
653   registry.Register(MonsterIdentifier(),
654                     (test_data_path + "monster_test.fbs").c_str());
655   // Now we got this set up, we can parse by just specifying the identifier,
656   // the correct schema will be loaded on the fly:
657   auto buf = registry.TextToFlatBuffer(jsonfile.c_str(), MonsterIdentifier());
658   // If this fails, check registry.lasterror_.
659   TEST_NOTNULL(buf.data());
660   // Test the buffer, to be sure:
661   AccessFlatBufferTest(buf.data(), buf.size(), false);
662   // We can use the registry to turn this back into text, in this case it
663   // will get the file_identifier from the binary:
664   std::string text;
665   auto ok = registry.FlatBufferToText(buf.data(), buf.size(), &text);
666   // If this fails, check registry.lasterror_.
667   TEST_EQ(ok, true);
668   TEST_EQ_STR(text.c_str(), jsonfile.c_str());
669 
670   // Generate text for UTF-8 strings without escapes.
671   std::string jsonfile_utf8;
672   TEST_EQ(flatbuffers::LoadFile((test_data_path + "unicode_test.json").c_str(),
673                                 false, &jsonfile_utf8),
674           true);
675   TEST_EQ(parser.Parse(jsonfile_utf8.c_str(), include_directories), true);
676   // To ensure it is correct, generate utf-8 text back from the binary.
677   std::string jsongen_utf8;
678   // request natural printing for utf-8 strings
679   parser.opts.natural_utf8 = true;
680   parser.opts.strict_json = true;
681   TEST_EQ(
682       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen_utf8),
683       true);
684   TEST_EQ_STR(jsongen_utf8.c_str(), jsonfile_utf8.c_str());
685 }
686 
ReflectionTest(uint8_t * flatbuf,size_t length)687 void ReflectionTest(uint8_t *flatbuf, size_t length) {
688   // Load a binary schema.
689   std::string bfbsfile;
690   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
691                                 true, &bfbsfile),
692           true);
693 
694   // Verify it, just in case:
695   flatbuffers::Verifier verifier(
696       reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
697   TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
698 
699   // Make sure the schema is what we expect it to be.
700   auto &schema = *reflection::GetSchema(bfbsfile.c_str());
701   auto root_table = schema.root_table();
702   TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
703   auto fields = root_table->fields();
704   auto hp_field_ptr = fields->LookupByKey("hp");
705   TEST_NOTNULL(hp_field_ptr);
706   auto &hp_field = *hp_field_ptr;
707   TEST_EQ_STR(hp_field.name()->c_str(), "hp");
708   TEST_EQ(hp_field.id(), 2);
709   TEST_EQ(hp_field.type()->base_type(), reflection::Short);
710   auto friendly_field_ptr = fields->LookupByKey("friendly");
711   TEST_NOTNULL(friendly_field_ptr);
712   TEST_NOTNULL(friendly_field_ptr->attributes());
713   TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
714 
715   // Make sure the table index is what we expect it to be.
716   auto pos_field_ptr = fields->LookupByKey("pos");
717   TEST_NOTNULL(pos_field_ptr);
718   TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
719   auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
720   TEST_NOTNULL(pos_table_ptr);
721   TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
722 
723   // Now use it to dynamically access a buffer.
724   auto &root = *flatbuffers::GetAnyRoot(flatbuf);
725 
726   // Verify the buffer first using reflection based verification
727   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
728           true);
729 
730   auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
731   TEST_EQ(hp, 80);
732 
733   // Rather than needing to know the type, we can also get the value of
734   // any field as an int64_t/double/string, regardless of what it actually is.
735   auto hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
736   TEST_EQ(hp_int64, 80);
737   auto hp_double = flatbuffers::GetAnyFieldF(root, hp_field);
738   TEST_EQ(hp_double, 80.0);
739   auto hp_string = flatbuffers::GetAnyFieldS(root, hp_field, &schema);
740   TEST_EQ_STR(hp_string.c_str(), "80");
741 
742   // Get struct field through reflection
743   auto pos_struct = flatbuffers::GetFieldStruct(root, *pos_field_ptr);
744   TEST_NOTNULL(pos_struct);
745   TEST_EQ(flatbuffers::GetAnyFieldF(*pos_struct,
746                                     *pos_table_ptr->fields()->LookupByKey("z")),
747           3.0f);
748 
749   auto test3_field = pos_table_ptr->fields()->LookupByKey("test3");
750   auto test3_struct = flatbuffers::GetFieldStruct(*pos_struct, *test3_field);
751   TEST_NOTNULL(test3_struct);
752   auto test3_object = schema.objects()->Get(test3_field->type()->index());
753 
754   TEST_EQ(flatbuffers::GetAnyFieldF(*test3_struct,
755                                     *test3_object->fields()->LookupByKey("a")),
756           10);
757 
758   // We can also modify it.
759   flatbuffers::SetField<uint16_t>(&root, hp_field, 200);
760   hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
761   TEST_EQ(hp, 200);
762 
763   // We can also set fields generically:
764   flatbuffers::SetAnyFieldI(&root, hp_field, 300);
765   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
766   TEST_EQ(hp_int64, 300);
767   flatbuffers::SetAnyFieldF(&root, hp_field, 300.5);
768   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
769   TEST_EQ(hp_int64, 300);
770   flatbuffers::SetAnyFieldS(&root, hp_field, "300");
771   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
772   TEST_EQ(hp_int64, 300);
773 
774   // Test buffer is valid after the modifications
775   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
776           true);
777 
778   // Reset it, for further tests.
779   flatbuffers::SetField<uint16_t>(&root, hp_field, 80);
780 
781   // More advanced functionality: changing the size of items in-line!
782   // First we put the FlatBuffer inside an std::vector.
783   std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
784   // Find the field we want to modify.
785   auto &name_field = *fields->LookupByKey("name");
786   // Get the root.
787   // This time we wrap the result from GetAnyRoot in a smartpointer that
788   // will keep rroot valid as resizingbuf resizes.
789   auto rroot = flatbuffers::piv(
790       flatbuffers::GetAnyRoot(flatbuffers::vector_data(resizingbuf)),
791       resizingbuf);
792   SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
793             &resizingbuf);
794   // Here resizingbuf has changed, but rroot is still valid.
795   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
796   // Now lets extend a vector by 100 elements (10 -> 110).
797   auto &inventory_field = *fields->LookupByKey("inventory");
798   auto rinventory = flatbuffers::piv(
799       flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
800   flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
801                                      &resizingbuf);
802   // rinventory still valid, so lets read from it.
803   TEST_EQ(rinventory->Get(10), 50);
804 
805   // For reflection uses not covered already, there is a more powerful way:
806   // we can simply generate whatever object we want to add/modify in a
807   // FlatBuffer of its own, then add that to an existing FlatBuffer:
808   // As an example, let's add a string to an array of strings.
809   // First, find our field:
810   auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
811   // Find the vector value:
812   auto rtestarrayofstring = flatbuffers::piv(
813       flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
814           **rroot, testarrayofstring_field),
815       resizingbuf);
816   // It's a vector of 2 strings, to which we add one more, initialized to
817   // offset 0.
818   flatbuffers::ResizeVector<flatbuffers::Offset<flatbuffers::String>>(
819       schema, 3, 0, *rtestarrayofstring, &resizingbuf);
820   // Here we just create a buffer that contans a single string, but this
821   // could also be any complex set of tables and other values.
822   flatbuffers::FlatBufferBuilder stringfbb;
823   stringfbb.Finish(stringfbb.CreateString("hank"));
824   // Add the contents of it to our existing FlatBuffer.
825   // We do this last, so the pointer doesn't get invalidated (since it is
826   // at the end of the buffer):
827   auto string_ptr = flatbuffers::AddFlatBuffer(
828       resizingbuf, stringfbb.GetBufferPointer(), stringfbb.GetSize());
829   // Finally, set the new value in the vector.
830   rtestarrayofstring->MutateOffset(2, string_ptr);
831   TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
832   TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
833   // Test integrity of all resize operations above.
834   flatbuffers::Verifier resize_verifier(
835       reinterpret_cast<const uint8_t *>(flatbuffers::vector_data(resizingbuf)),
836       resizingbuf.size());
837   TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
838 
839   // Test buffer is valid using reflection as well
840   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
841                               flatbuffers::vector_data(resizingbuf),
842                               resizingbuf.size()),
843           true);
844 
845   // As an additional test, also set it on the name field.
846   // Note: unlike the name change above, this just overwrites the offset,
847   // rather than changing the string in-place.
848   SetFieldT(*rroot, name_field, string_ptr);
849   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "hank");
850 
851   // Using reflection, rather than mutating binary FlatBuffers, we can also copy
852   // tables and other things out of other FlatBuffers into a FlatBufferBuilder,
853   // either part or whole.
854   flatbuffers::FlatBufferBuilder fbb;
855   auto root_offset = flatbuffers::CopyTable(
856       fbb, schema, *root_table, *flatbuffers::GetAnyRoot(flatbuf), true);
857   fbb.Finish(root_offset, MonsterIdentifier());
858   // Test that it was copied correctly:
859   AccessFlatBufferTest(fbb.GetBufferPointer(), fbb.GetSize());
860 
861   // Test buffer is valid using reflection as well
862   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
863                               fbb.GetBufferPointer(), fbb.GetSize()),
864           true);
865 }
866 
MiniReflectFlatBuffersTest(uint8_t * flatbuf)867 void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
868   auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
869   TEST_EQ_STR(
870       s.c_str(),
871       "{ "
872       "pos: { x: 1.0, y: 2.0, z: 3.0, test1: 0.0, test2: Red, test3: "
873       "{ a: 10, b: 20 } }, "
874       "hp: 80, "
875       "name: \"MyMonster\", "
876       "inventory: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "
877       "test_type: Monster, "
878       "test: { name: \"Fred\" }, "
879       "test4: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
880       "testarrayofstring: [ \"bob\", \"fred\", \"bob\", \"fred\" ], "
881       "testarrayoftables: [ { hp: 1000, name: \"Barney\" }, { name: \"Fred\" "
882       "}, "
883       "{ name: \"Wilma\" } ], "
884       // TODO(wvo): should really print this nested buffer correctly.
885       "testnestedflatbuffer: [ 20, 0, 0, 0, 77, 79, 78, 83, 12, 0, 12, 0, 0, "
886       "0, "
887       "4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
888       "101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
889       "testarrayofstring2: [ \"jane\", \"mary\" ], "
890       "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
891       "{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
892       "{ id: 4, distance: 40 } ], "
893       "flex: [ 210, 4, 5, 2 ], "
894       "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
895       "vector_of_enums: [ Blue, Green ] "
896       "}");
897 }
898 
899 // Parse a .proto schema, output as .fbs
ParseProtoTest()900 void ParseProtoTest() {
901   // load the .proto and the golden file from disk
902   std::string protofile;
903   std::string goldenfile;
904   std::string goldenunionfile;
905   TEST_EQ(
906       flatbuffers::LoadFile((test_data_path + "prototest/test.proto").c_str(),
907                             false, &protofile),
908       true);
909   TEST_EQ(
910       flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
911                             false, &goldenfile),
912       true);
913   TEST_EQ(
914       flatbuffers::LoadFile((test_data_path +
915                             "prototest/test_union.golden").c_str(),
916                             false, &goldenunionfile),
917       true);
918 
919   flatbuffers::IDLOptions opts;
920   opts.include_dependence_headers = false;
921   opts.proto_mode = true;
922 
923   // Parse proto.
924   flatbuffers::Parser parser(opts);
925   auto protopath = test_data_path + "prototest/";
926   const char *include_directories[] = { protopath.c_str(), nullptr };
927   TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
928 
929   // Generate fbs.
930   auto fbs = flatbuffers::GenerateFBS(parser, "test");
931 
932   // Ensure generated file is parsable.
933   flatbuffers::Parser parser2;
934   TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
935   TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
936 
937   // Parse proto with --oneof-union option.
938   opts.proto_oneof_union = true;
939   flatbuffers::Parser parser3(opts);
940   TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
941 
942   // Generate fbs.
943   auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
944 
945   // Ensure generated file is parsable.
946   flatbuffers::Parser parser4;
947   TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
948   TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
949 }
950 
951 template<typename T>
CompareTableFieldValue(flatbuffers::Table * table,flatbuffers::voffset_t voffset,T val)952 void CompareTableFieldValue(flatbuffers::Table *table,
953                             flatbuffers::voffset_t voffset, T val) {
954   T read = table->GetField(voffset, static_cast<T>(0));
955   TEST_EQ(read, val);
956 }
957 
958 // Low level stress/fuzz test: serialize/deserialize a variety of
959 // different kinds of data in different combinations
FuzzTest1()960 void FuzzTest1() {
961   // Values we're testing against: chosen to ensure no bits get chopped
962   // off anywhere, and also be different from eachother.
963   const uint8_t bool_val = true;
964   const int8_t char_val = -127;  // 0x81
965   const uint8_t uchar_val = 0xFF;
966   const int16_t short_val = -32222;  // 0x8222;
967   const uint16_t ushort_val = 0xFEEE;
968   const int32_t int_val = 0x83333333;
969   const uint32_t uint_val = 0xFDDDDDDD;
970   const int64_t long_val = 0x8444444444444444LL;
971   const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCCULL;
972   const float float_val = 3.14159f;
973   const double double_val = 3.14159265359;
974 
975   const int test_values_max = 11;
976   const flatbuffers::voffset_t fields_per_object = 4;
977   const int num_fuzz_objects = 10000;  // The higher, the more thorough :)
978 
979   flatbuffers::FlatBufferBuilder builder;
980 
981   lcg_reset();  // Keep it deterministic.
982 
983   flatbuffers::uoffset_t objects[num_fuzz_objects];
984 
985   // Generate num_fuzz_objects random objects each consisting of
986   // fields_per_object fields, each of a random type.
987   for (int i = 0; i < num_fuzz_objects; i++) {
988     auto start = builder.StartTable();
989     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
990       int choice = lcg_rand() % test_values_max;
991       auto off = flatbuffers::FieldIndexToOffset(f);
992       switch (choice) {
993         case 0: builder.AddElement<uint8_t>(off, bool_val, 0); break;
994         case 1: builder.AddElement<int8_t>(off, char_val, 0); break;
995         case 2: builder.AddElement<uint8_t>(off, uchar_val, 0); break;
996         case 3: builder.AddElement<int16_t>(off, short_val, 0); break;
997         case 4: builder.AddElement<uint16_t>(off, ushort_val, 0); break;
998         case 5: builder.AddElement<int32_t>(off, int_val, 0); break;
999         case 6: builder.AddElement<uint32_t>(off, uint_val, 0); break;
1000         case 7: builder.AddElement<int64_t>(off, long_val, 0); break;
1001         case 8: builder.AddElement<uint64_t>(off, ulong_val, 0); break;
1002         case 9: builder.AddElement<float>(off, float_val, 0); break;
1003         case 10: builder.AddElement<double>(off, double_val, 0); break;
1004       }
1005     }
1006     objects[i] = builder.EndTable(start);
1007   }
1008   builder.PreAlign<flatbuffers::largest_scalar_t>(0);  // Align whole buffer.
1009 
1010   lcg_reset();  // Reset.
1011 
1012   uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
1013 
1014   // Test that all objects we generated are readable and return the
1015   // expected values. We generate random objects in the same order
1016   // so this is deterministic.
1017   for (int i = 0; i < num_fuzz_objects; i++) {
1018     auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
1019     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
1020       int choice = lcg_rand() % test_values_max;
1021       flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
1022       switch (choice) {
1023         case 0: CompareTableFieldValue(table, off, bool_val); break;
1024         case 1: CompareTableFieldValue(table, off, char_val); break;
1025         case 2: CompareTableFieldValue(table, off, uchar_val); break;
1026         case 3: CompareTableFieldValue(table, off, short_val); break;
1027         case 4: CompareTableFieldValue(table, off, ushort_val); break;
1028         case 5: CompareTableFieldValue(table, off, int_val); break;
1029         case 6: CompareTableFieldValue(table, off, uint_val); break;
1030         case 7: CompareTableFieldValue(table, off, long_val); break;
1031         case 8: CompareTableFieldValue(table, off, ulong_val); break;
1032         case 9: CompareTableFieldValue(table, off, float_val); break;
1033         case 10: CompareTableFieldValue(table, off, double_val); break;
1034       }
1035     }
1036   }
1037 }
1038 
1039 // High level stress/fuzz test: generate a big schema and
1040 // matching json data in random combinations, then parse both,
1041 // generate json back from the binary, and compare with the original.
FuzzTest2()1042 void FuzzTest2() {
1043   lcg_reset();  // Keep it deterministic.
1044 
1045   const int num_definitions = 30;
1046   const int num_struct_definitions = 5;  // Subset of num_definitions.
1047   const int fields_per_definition = 15;
1048   const int instances_per_definition = 5;
1049   const int deprecation_rate = 10;  // 1 in deprecation_rate fields will
1050                                     // be deprecated.
1051 
1052   std::string schema = "namespace test;\n\n";
1053 
1054   struct RndDef {
1055     std::string instances[instances_per_definition];
1056 
1057     // Since we're generating schema and corresponding data in tandem,
1058     // this convenience function adds strings to both at once.
1059     static void Add(RndDef (&definitions_l)[num_definitions],
1060                     std::string &schema_l, const int instances_per_definition_l,
1061                     const char *schema_add, const char *instance_add,
1062                     int definition) {
1063       schema_l += schema_add;
1064       for (int i = 0; i < instances_per_definition_l; i++)
1065         definitions_l[definition].instances[i] += instance_add;
1066     }
1067   };
1068 
1069   // clang-format off
1070   #define AddToSchemaAndInstances(schema_add, instance_add) \
1071     RndDef::Add(definitions, schema, instances_per_definition, \
1072                 schema_add, instance_add, definition)
1073 
1074   #define Dummy() \
1075     RndDef::Add(definitions, schema, instances_per_definition, \
1076                 "byte", "1", definition)
1077   // clang-format on
1078 
1079   RndDef definitions[num_definitions];
1080 
1081   // We are going to generate num_definitions, the first
1082   // num_struct_definitions will be structs, the rest tables. For each
1083   // generate random fields, some of which may be struct/table types
1084   // referring to previously generated structs/tables.
1085   // Simultanenously, we generate instances_per_definition JSON data
1086   // definitions, which will have identical structure to the schema
1087   // being generated. We generate multiple instances such that when creating
1088   // hierarchy, we get some variety by picking one randomly.
1089   for (int definition = 0; definition < num_definitions; definition++) {
1090     std::string definition_name = "D" + flatbuffers::NumToString(definition);
1091 
1092     bool is_struct = definition < num_struct_definitions;
1093 
1094     AddToSchemaAndInstances(
1095         ((is_struct ? "struct " : "table ") + definition_name + " {\n").c_str(),
1096         "{\n");
1097 
1098     for (int field = 0; field < fields_per_definition; field++) {
1099       const bool is_last_field = field == fields_per_definition - 1;
1100 
1101       // Deprecate 1 in deprecation_rate fields. Only table fields can be
1102       // deprecated.
1103       // Don't deprecate the last field to avoid dangling commas in JSON.
1104       const bool deprecated =
1105           !is_struct && !is_last_field && (lcg_rand() % deprecation_rate == 0);
1106 
1107       std::string field_name = "f" + flatbuffers::NumToString(field);
1108       AddToSchemaAndInstances(("  " + field_name + ":").c_str(),
1109                               deprecated ? "" : (field_name + ": ").c_str());
1110       // Pick random type:
1111       auto base_type = static_cast<flatbuffers::BaseType>(
1112           lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1));
1113       switch (base_type) {
1114         case flatbuffers::BASE_TYPE_STRING:
1115           if (is_struct) {
1116             Dummy();  // No strings in structs.
1117           } else {
1118             AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
1119           }
1120           break;
1121         case flatbuffers::BASE_TYPE_VECTOR:
1122           if (is_struct) {
1123             Dummy();  // No vectors in structs.
1124           } else {
1125             AddToSchemaAndInstances("[ubyte]",
1126                                     deprecated ? "" : "[\n0,\n1,\n255\n]");
1127           }
1128           break;
1129         case flatbuffers::BASE_TYPE_NONE:
1130         case flatbuffers::BASE_TYPE_UTYPE:
1131         case flatbuffers::BASE_TYPE_STRUCT:
1132         case flatbuffers::BASE_TYPE_UNION:
1133           if (definition) {
1134             // Pick a random previous definition and random data instance of
1135             // that definition.
1136             int defref = lcg_rand() % definition;
1137             int instance = lcg_rand() % instances_per_definition;
1138             AddToSchemaAndInstances(
1139                 ("D" + flatbuffers::NumToString(defref)).c_str(),
1140                 deprecated ? ""
1141                            : definitions[defref].instances[instance].c_str());
1142           } else {
1143             // If this is the first definition, we have no definition we can
1144             // refer to.
1145             Dummy();
1146           }
1147           break;
1148         case flatbuffers::BASE_TYPE_BOOL:
1149           AddToSchemaAndInstances(
1150               "bool", deprecated ? "" : (lcg_rand() % 2 ? "true" : "false"));
1151           break;
1152         default:
1153           // All the scalar types.
1154           schema += flatbuffers::kTypeNames[base_type];
1155 
1156           if (!deprecated) {
1157             // We want each instance to use its own random value.
1158             for (int inst = 0; inst < instances_per_definition; inst++)
1159               definitions[definition].instances[inst] +=
1160                   flatbuffers::IsFloat(base_type)
1161                       ? flatbuffers::NumToString<double>(lcg_rand() % 128)
1162                             .c_str()
1163                       : flatbuffers::NumToString<int>(lcg_rand() % 128).c_str();
1164           }
1165       }
1166       AddToSchemaAndInstances(deprecated ? "(deprecated);\n" : ";\n",
1167                               deprecated ? "" : is_last_field ? "\n" : ",\n");
1168     }
1169     AddToSchemaAndInstances("}\n\n", "}");
1170   }
1171 
1172   schema += "root_type D" + flatbuffers::NumToString(num_definitions - 1);
1173   schema += ";\n";
1174 
1175   flatbuffers::Parser parser;
1176 
1177   // Will not compare against the original if we don't write defaults
1178   parser.builder_.ForceDefaults(true);
1179 
1180   // Parse the schema, parse the generated data, then generate text back
1181   // from the binary and compare against the original.
1182   TEST_EQ(parser.Parse(schema.c_str()), true);
1183 
1184   const std::string &json =
1185       definitions[num_definitions - 1].instances[0] + "\n";
1186 
1187   TEST_EQ(parser.Parse(json.c_str()), true);
1188 
1189   std::string jsongen;
1190   parser.opts.indent_step = 0;
1191   auto result =
1192       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1193   TEST_EQ(result, true);
1194 
1195   if (jsongen != json) {
1196     // These strings are larger than a megabyte, so we show the bytes around
1197     // the first bytes that are different rather than the whole string.
1198     size_t len = std::min(json.length(), jsongen.length());
1199     for (size_t i = 0; i < len; i++) {
1200       if (json[i] != jsongen[i]) {
1201         i -= std::min(static_cast<size_t>(10), i);  // show some context;
1202         size_t end = std::min(len, i + 20);
1203         for (; i < end; i++)
1204           TEST_OUTPUT_LINE("at %d: found \"%c\", expected \"%c\"\n",
1205                            static_cast<int>(i), jsongen[i], json[i]);
1206         break;
1207       }
1208     }
1209     TEST_NOTNULL(NULL);
1210   }
1211 
1212   // clang-format off
1213   #ifdef FLATBUFFERS_TEST_VERBOSE
1214     TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
1215                      static_cast<int>(schema.length() / 1024),
1216                      static_cast<int>(json.length() / 1024));
1217   #endif
1218   // clang-format on
1219 }
1220 
1221 // Test that parser errors are actually generated.
TestError_(const char * src,const char * error_substr,bool strict_json,const char * file,int line,const char * func)1222 void TestError_(const char *src, const char *error_substr, bool strict_json,
1223                 const char *file, int line, const char *func) {
1224   flatbuffers::IDLOptions opts;
1225   opts.strict_json = strict_json;
1226   flatbuffers::Parser parser(opts);
1227   if (parser.Parse(src)) {
1228     TestFail("true", "false",
1229              ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1230              func);
1231   } else if (!strstr(parser.error_.c_str(), error_substr)) {
1232     TestFail(parser.error_.c_str(), error_substr,
1233              ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1234              func);
1235   }
1236 }
1237 
TestError_(const char * src,const char * error_substr,const char * file,int line,const char * func)1238 void TestError_(const char *src, const char *error_substr, const char *file,
1239                 int line, const char *func) {
1240   TestError_(src, error_substr, false, file, line, func);
1241 }
1242 
1243 #ifdef _WIN32
1244 #  define TestError(src, ...) \
1245     TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
1246 #else
1247 #  define TestError(src, ...) \
1248     TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1249 #endif
1250 
1251 // Test that parsing errors occur as we'd expect.
1252 // Also useful for coverage, making sure these paths are run.
ErrorTest()1253 void ErrorTest() {
1254   // In order they appear in idl_parser.cpp
1255   TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
1256   TestError("\"\0", "illegal");
1257   TestError("\"\\q", "escape code");
1258   TestError("table ///", "documentation");
1259   TestError("@", "illegal");
1260   TestError("table 1", "expecting");
1261   TestError("table X { Y:[[int]]; }", "nested vector");
1262   TestError("table X { Y:1; }", "illegal type");
1263   TestError("table X { Y:int; Y:int; }", "field already");
1264   TestError("table Y {} table X { Y:int; }", "same as table");
1265   TestError("struct X { Y:string; }", "only scalar");
1266   TestError("table X { Y:string = \"\"; }", "default values");
1267   TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
1268   TestError("struct X { Y:int (deprecated); }", "deprecate");
1269   TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
1270             "missing type field");
1271   TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
1272             "type id");
1273   TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
1274   TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
1275   TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
1276             true);
1277   TestError(
1278       "struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
1279       "{ V:{ Y:1 } }",
1280       "wrong number");
1281   TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
1282             "unknown enum value");
1283   TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
1284   TestError("enum X:byte { Y } enum X {", "enum already");
1285   TestError("enum X:float {}", "underlying");
1286   TestError("enum X:byte { Y, Y }", "value already");
1287   TestError("enum X:byte { Y=2, Z=1 }", "ascending");
1288   TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out");
1289   TestError("table X { Y:int; } table X {", "datatype already");
1290   TestError("struct X (force_align: 7) { Y:int; }", "force_align");
1291   TestError("struct X {}", "size 0");
1292   TestError("{}", "no root");
1293   TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "end of file");
1294   TestError("table X { Y:byte; } root_type X; { Y:1 } table Y{ Z:int }",
1295             "end of file");
1296   TestError("root_type X;", "unknown root");
1297   TestError("struct X { Y:int; } root_type X;", "a table");
1298   TestError("union X { Y }", "referenced");
1299   TestError("union Z { X } struct X { Y:int; }", "only tables");
1300   TestError("table X { Y:[int]; YLength:int; }", "clash");
1301   TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
1302   // float to integer conversion is forbidden
1303   TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float");
1304   TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float");
1305   TestError("enum X:bool { Y = true }", "must be integral");
1306 }
1307 
TestValue(const char * json,const char * type_name)1308 template<typename T> T TestValue(const char *json, const char *type_name) {
1309   flatbuffers::Parser parser;
1310   parser.builder_.ForceDefaults(true);  // return defaults
1311   auto check_default = json ? false : true;
1312   if (check_default) { parser.opts.output_default_scalars_in_json = true; }
1313   // Simple schema.
1314   std::string schema =
1315       "table X { Y:" + std::string(type_name) + "; } root_type X;";
1316   TEST_EQ(parser.Parse(schema.c_str()), true);
1317 
1318   auto done = parser.Parse(check_default ? "{}" : json);
1319   TEST_EQ_STR(parser.error_.c_str(), "");
1320   TEST_EQ(done, true);
1321 
1322   // Check with print.
1323   std::string print_back;
1324   parser.opts.indent_step = -1;
1325   TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back),
1326           true);
1327   // restore value from its default
1328   if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); }
1329 
1330   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1331       parser.builder_.GetBufferPointer());
1332   return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
1333 }
1334 
FloatCompare(float a,float b)1335 bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
1336 
1337 // Additional parser testing not covered elsewhere.
ValueTest()1338 void ValueTest() {
1339   // Test scientific notation numbers.
1340   TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"),
1341                        3.14159f),
1342           true);
1343   // number in string
1344   TEST_EQ(FloatCompare(TestValue<float>("{ Y:\"0.0314159e+2\" }", "float"),
1345                        3.14159f),
1346           true);
1347 
1348   // Test conversion functions.
1349   TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }", "float"), -1),
1350           true);
1351 
1352   // int embedded to string
1353   TEST_EQ(TestValue<int>("{ Y:\"-876\" }", "int=-123"), -876);
1354   TEST_EQ(TestValue<int>("{ Y:\"876\" }", "int=-123"), 876);
1355 
1356   // Test negative hex constant.
1357   TEST_EQ(TestValue<int>("{ Y:-0x8ea0 }", "int=-0x8ea0"), -36512);
1358   TEST_EQ(TestValue<int>(nullptr, "int=-0x8ea0"), -36512);
1359 
1360   // positive hex constant
1361   TEST_EQ(TestValue<int>("{ Y:0x1abcdef }", "int=0x1"), 0x1abcdef);
1362   // with optional '+' sign
1363   TEST_EQ(TestValue<int>("{ Y:+0x1abcdef }", "int=+0x1"), 0x1abcdef);
1364   // hex in string
1365   TEST_EQ(TestValue<int>("{ Y:\"0x1abcdef\" }", "int=+0x1"), 0x1abcdef);
1366 
1367   // Make sure we do unsigned 64bit correctly.
1368   TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }", "ulong"),
1369           12335089644688340133ULL);
1370 
1371   // bool in string
1372   TEST_EQ(TestValue<bool>("{ Y:\"false\" }", "bool=true"), false);
1373   TEST_EQ(TestValue<bool>("{ Y:\"true\" }", "bool=\"true\""), true);
1374   TEST_EQ(TestValue<bool>("{ Y:'false' }", "bool=true"), false);
1375   TEST_EQ(TestValue<bool>("{ Y:'true' }", "bool=\"true\""), true);
1376 
1377   // check comments before and after json object
1378   TEST_EQ(TestValue<int>("/*before*/ { Y:1 } /*after*/", "int"), 1);
1379   TEST_EQ(TestValue<int>("//before \n { Y:1 } //after", "int"), 1);
1380 
1381 }
1382 
NestedListTest()1383 void NestedListTest() {
1384   flatbuffers::Parser parser1;
1385   TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
1386                         "root_type T;"
1387                         "{ F:[ [10,20], [30,40]] }"),
1388           true);
1389 }
1390 
EnumStringsTest()1391 void EnumStringsTest() {
1392   flatbuffers::Parser parser1;
1393   TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
1394                         "root_type T;"
1395                         "{ F:[ A, B, \"C\", \"A B C\" ] }"),
1396           true);
1397   flatbuffers::Parser parser2;
1398   TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
1399                         "root_type T;"
1400                         "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"),
1401           true);
1402 }
1403 
EnumNamesTest()1404 void EnumNamesTest() {
1405   TEST_EQ_STR("Red", EnumNameColor(Color_Red));
1406   TEST_EQ_STR("Green", EnumNameColor(Color_Green));
1407   TEST_EQ_STR("Blue", EnumNameColor(Color_Blue));
1408   // Check that Color to string don't crash while decode a mixture of Colors.
1409   // 1) Example::Color enum is enum with unfixed underlying type.
1410   // 2) Valid enum range: [0; 2^(ceil(log2(Color_ANY))) - 1].
1411   // Consequence: A value is out of this range will lead to UB (since C++17).
1412   // For details see C++17 standard or explanation on the SO:
1413   // stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class
1414   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(0)));
1415   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY-1)));
1416   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY+1)));
1417 }
1418 
EnumOutOfRangeTest()1419 void EnumOutOfRangeTest() {
1420   TestError("enum X:byte { Y = 128 }", "enum value does not fit");
1421   TestError("enum X:byte { Y = -129 }", "enum value does not fit");
1422   TestError("enum X:byte { Y = 127, Z }", "enum value does not fit");
1423   TestError("enum X:ubyte { Y = -1 }", "enum value does not fit");
1424   TestError("enum X:ubyte { Y = 256 }", "enum value does not fit");
1425   // Unions begin with an implicit "NONE = 0".
1426   TestError("table Y{} union X { Y = -1 }",
1427             "enum values must be specified in ascending order");
1428   TestError("table Y{} union X { Y = 256 }", "enum value does not fit");
1429   TestError("table Y{} union X { Y = 255, Z:Y }", "enum value does not fit");
1430   TestError("enum X:int { Y = -2147483649 }", "enum value does not fit");
1431   TestError("enum X:int { Y = 2147483648 }", "enum value does not fit");
1432   TestError("enum X:uint { Y = -1 }", "enum value does not fit");
1433   TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit");
1434   TestError("enum X:long { Y = 9223372036854775808 }", "constant does not fit");
1435   TestError("enum X:long { Y = 9223372036854775807, Z }", "enum value overflows");
1436   TestError("enum X:ulong { Y = -1 }", "enum value does not fit");
1437   // TODO: these are perfectly valid constants that shouldn't fail
1438   TestError("enum X:ulong { Y = 13835058055282163712 }", "constant does not fit");
1439   TestError("enum X:ulong { Y = 18446744073709551615 }", "constant does not fit");
1440 }
1441 
IntegerOutOfRangeTest()1442 void IntegerOutOfRangeTest() {
1443   TestError("table T { F:byte; } root_type T; { F:128 }",
1444             "constant does not fit");
1445   TestError("table T { F:byte; } root_type T; { F:-129 }",
1446             "constant does not fit");
1447   TestError("table T { F:ubyte; } root_type T; { F:256 }",
1448             "constant does not fit");
1449   TestError("table T { F:ubyte; } root_type T; { F:-1 }",
1450             "constant does not fit");
1451   TestError("table T { F:short; } root_type T; { F:32768 }",
1452             "constant does not fit");
1453   TestError("table T { F:short; } root_type T; { F:-32769 }",
1454             "constant does not fit");
1455   TestError("table T { F:ushort; } root_type T; { F:65536 }",
1456             "constant does not fit");
1457   TestError("table T { F:ushort; } root_type T; { F:-1 }",
1458             "constant does not fit");
1459   TestError("table T { F:int; } root_type T; { F:2147483648 }",
1460             "constant does not fit");
1461   TestError("table T { F:int; } root_type T; { F:-2147483649 }",
1462             "constant does not fit");
1463   TestError("table T { F:uint; } root_type T; { F:4294967296 }",
1464             "constant does not fit");
1465   TestError("table T { F:uint; } root_type T; { F:-1 }",
1466             "constant does not fit");
1467   // Check fixed width aliases
1468   TestError("table X { Y:uint8; } root_type X; { Y: -1 }", "does not fit");
1469   TestError("table X { Y:uint8; } root_type X; { Y: 256 }", "does not fit");
1470   TestError("table X { Y:uint16; } root_type X; { Y: -1 }", "does not fit");
1471   TestError("table X { Y:uint16; } root_type X; { Y: 65536 }", "does not fit");
1472   TestError("table X { Y:uint32; } root_type X; { Y: -1 }", "");
1473   TestError("table X { Y:uint32; } root_type X; { Y: 4294967296 }",
1474             "does not fit");
1475   TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1476   TestError("table X { Y:uint64; } root_type X; { Y: -9223372036854775809 }",
1477             "does not fit");
1478   TestError("table X { Y:uint64; } root_type X; { Y: 18446744073709551616 }",
1479             "does not fit");
1480 
1481   TestError("table X { Y:int8; } root_type X; { Y: -129 }", "does not fit");
1482   TestError("table X { Y:int8; } root_type X; { Y: 128 }", "does not fit");
1483   TestError("table X { Y:int16; } root_type X; { Y: -32769 }", "does not fit");
1484   TestError("table X { Y:int16; } root_type X; { Y: 32768 }", "does not fit");
1485   TestError("table X { Y:int32; } root_type X; { Y: -2147483649 }", "");
1486   TestError("table X { Y:int32; } root_type X; { Y: 2147483648 }",
1487             "does not fit");
1488   TestError("table X { Y:int64; } root_type X; { Y: -9223372036854775809 }",
1489             "does not fit");
1490   TestError("table X { Y:int64; } root_type X; { Y: 9223372036854775808 }",
1491             "does not fit");
1492   // check out-of-int64 as int8
1493   TestError("table X { Y:int8; } root_type X; { Y: -9223372036854775809 }",
1494             "does not fit");
1495   TestError("table X { Y:int8; } root_type X; { Y: 9223372036854775808 }",
1496             "does not fit");
1497 
1498   // Check default values
1499   TestError("table X { Y:int64=-9223372036854775809; } root_type X; {}",
1500             "does not fit");
1501   TestError("table X { Y:int64= 9223372036854775808; } root_type X; {}",
1502             "does not fit");
1503   TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1504   TestError("table X { Y:uint64=-9223372036854775809; } root_type X; {}",
1505             "does not fit");
1506   TestError("table X { Y:uint64= 18446744073709551616; } root_type X; {}",
1507             "does not fit");
1508 }
1509 
IntegerBoundaryTest()1510 void IntegerBoundaryTest() {
1511   TEST_EQ(TestValue<int8_t>("{ Y:127 }", "byte"), 127);
1512   TEST_EQ(TestValue<int8_t>("{ Y:-128 }", "byte"), -128);
1513   TEST_EQ(TestValue<uint8_t>("{ Y:255 }", "ubyte"), 255);
1514   TEST_EQ(TestValue<uint8_t>("{ Y:0 }", "ubyte"), 0);
1515   TEST_EQ(TestValue<int16_t>("{ Y:32767 }", "short"), 32767);
1516   TEST_EQ(TestValue<int16_t>("{ Y:-32768 }", "short"), -32768);
1517   TEST_EQ(TestValue<uint16_t>("{ Y:65535 }", "ushort"), 65535);
1518   TEST_EQ(TestValue<uint16_t>("{ Y:0 }", "ushort"), 0);
1519   TEST_EQ(TestValue<int32_t>("{ Y:2147483647 }", "int"), 2147483647);
1520   TEST_EQ(TestValue<int32_t>("{ Y:-2147483648 }", "int"), (-2147483647 - 1));
1521   TEST_EQ(TestValue<uint32_t>("{ Y:4294967295 }", "uint"), 4294967295);
1522   TEST_EQ(TestValue<uint32_t>("{ Y:0 }", "uint"), 0);
1523   TEST_EQ(TestValue<int64_t>("{ Y:9223372036854775807 }", "long"),
1524           9223372036854775807);
1525   TEST_EQ(TestValue<int64_t>("{ Y:-9223372036854775808 }", "long"),
1526           (-9223372036854775807 - 1));
1527   TEST_EQ(TestValue<uint64_t>("{ Y:18446744073709551615 }", "ulong"),
1528           18446744073709551615U);
1529   TEST_EQ(TestValue<uint64_t>("{ Y:0 }", "ulong"), 0);
1530   TEST_EQ(TestValue<uint64_t>("{ Y: 18446744073709551615 }", "uint64"),
1531           18446744073709551615ULL);
1532   // check that the default works
1533   TEST_EQ(TestValue<uint64_t>(nullptr, "uint64 = 18446744073709551615"),
1534           18446744073709551615ULL);
1535 }
1536 
ValidFloatTest()1537 void ValidFloatTest() {
1538   const auto infinityf = flatbuffers::numeric_limits<float>::infinity();
1539   const auto infinityd = flatbuffers::numeric_limits<double>::infinity();
1540   // check rounding to infinity
1541   TEST_EQ(TestValue<float>("{ Y:+3.4029e+38 }", "float"), +infinityf);
1542   TEST_EQ(TestValue<float>("{ Y:-3.4029e+38 }", "float"), -infinityf);
1543   TEST_EQ(TestValue<double>("{ Y:+1.7977e+308 }", "double"), +infinityd);
1544   TEST_EQ(TestValue<double>("{ Y:-1.7977e+308 }", "double"), -infinityd);
1545 
1546   TEST_EQ(
1547       FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"), 3.14159f),
1548       true);
1549   // float in string
1550   TEST_EQ(FloatCompare(TestValue<float>("{ Y:\" 0.0314159e+2  \" }", "float"),
1551                        3.14159f),
1552           true);
1553 
1554   TEST_EQ(TestValue<float>("{ Y:1 }", "float"), 1.0f);
1555   TEST_EQ(TestValue<float>("{ Y:1.0 }", "float"), 1.0f);
1556   TEST_EQ(TestValue<float>("{ Y:1. }", "float"), 1.0f);
1557   TEST_EQ(TestValue<float>("{ Y:+1. }", "float"), 1.0f);
1558   TEST_EQ(TestValue<float>("{ Y:-1. }", "float"), -1.0f);
1559   TEST_EQ(TestValue<float>("{ Y:1.e0 }", "float"), 1.0f);
1560   TEST_EQ(TestValue<float>("{ Y:1.e+0 }", "float"), 1.0f);
1561   TEST_EQ(TestValue<float>("{ Y:1.e-0 }", "float"), 1.0f);
1562   TEST_EQ(TestValue<float>("{ Y:0.125 }", "float"), 0.125f);
1563   TEST_EQ(TestValue<float>("{ Y:.125 }", "float"), 0.125f);
1564   TEST_EQ(TestValue<float>("{ Y:-.125 }", "float"), -0.125f);
1565   TEST_EQ(TestValue<float>("{ Y:+.125 }", "float"), +0.125f);
1566   TEST_EQ(TestValue<float>("{ Y:5 }", "float"), 5.0f);
1567   TEST_EQ(TestValue<float>("{ Y:\"5\" }", "float"), 5.0f);
1568 
1569   #if defined(FLATBUFFERS_HAS_NEW_STRTOD)
1570   // Old MSVC versions may have problem with this check.
1571   // https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
1572   TEST_EQ(TestValue<double>("{ Y:6.9294956446009195e15 }", "double"),
1573     6929495644600920.0);
1574   // check nan's
1575   TEST_EQ(std::isnan(TestValue<double>("{ Y:nan }", "double")), true);
1576   TEST_EQ(std::isnan(TestValue<float>("{ Y:nan }", "float")), true);
1577   TEST_EQ(std::isnan(TestValue<float>("{ Y:\"nan\" }", "float")), true);
1578   TEST_EQ(std::isnan(TestValue<float>("{ Y:+nan }", "float")), true);
1579   TEST_EQ(std::isnan(TestValue<float>("{ Y:-nan }", "float")), true);
1580   TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true);
1581   TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true);
1582   // check inf
1583   TEST_EQ(TestValue<float>("{ Y:inf }", "float"), infinityf);
1584   TEST_EQ(TestValue<float>("{ Y:\"inf\" }", "float"), infinityf);
1585   TEST_EQ(TestValue<float>("{ Y:+inf }", "float"), infinityf);
1586   TEST_EQ(TestValue<float>("{ Y:-inf }", "float"), -infinityf);
1587   TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinityf);
1588   TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinityf);
1589   TestValue<double>(
1590       "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1591       "3.0e2] }",
1592       "[double]");
1593   TestValue<float>(
1594       "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1595       "3.0e2] }",
1596       "[float]");
1597 
1598   // Test binary format of float point.
1599   // https://en.cppreference.com/w/cpp/language/floating_literal
1600   // 0x11.12p-1 = (1*16^1 + 2*16^0 + 3*16^-1 + 4*16^-2) * 2^-1 =
1601   TEST_EQ(TestValue<double>("{ Y:0x12.34p-1 }", "double"), 9.1015625);
1602   // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
1603   TEST_EQ(TestValue<float>("{ Y:-0x0.2p0 }", "float"), -0.125f);
1604   TEST_EQ(TestValue<float>("{ Y:-0x.2p1 }", "float"), -0.25f);
1605   TEST_EQ(TestValue<float>("{ Y:0x1.2p3 }", "float"), 9.0f);
1606   TEST_EQ(TestValue<float>("{ Y:0x10.1p0 }", "float"), 16.0625f);
1607   TEST_EQ(TestValue<double>("{ Y:0x1.2p3 }", "double"), 9.0);
1608   TEST_EQ(TestValue<double>("{ Y:0x10.1p0 }", "double"), 16.0625);
1609   TEST_EQ(TestValue<double>("{ Y:0xC.68p+2 }", "double"), 49.625);
1610   TestValue<double>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[double]");
1611   TestValue<float>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[float]");
1612 
1613 #else   // FLATBUFFERS_HAS_NEW_STRTOD
1614   TEST_OUTPUT_LINE("FLATBUFFERS_HAS_NEW_STRTOD tests skipped");
1615 #endif  // FLATBUFFERS_HAS_NEW_STRTOD
1616 }
1617 
InvalidFloatTest()1618 void InvalidFloatTest() {
1619   auto invalid_msg = "invalid number";
1620   auto comma_msg = "expecting: ,";
1621   TestError("table T { F:float; } root_type T; { F:1,0 }", "");
1622   TestError("table T { F:float; } root_type T; { F:. }", "");
1623   TestError("table T { F:float; } root_type T; { F:- }", invalid_msg);
1624   TestError("table T { F:float; } root_type T; { F:+ }", invalid_msg);
1625   TestError("table T { F:float; } root_type T; { F:-. }", invalid_msg);
1626   TestError("table T { F:float; } root_type T; { F:+. }", invalid_msg);
1627   TestError("table T { F:float; } root_type T; { F:.e }", "");
1628   TestError("table T { F:float; } root_type T; { F:-e }", invalid_msg);
1629   TestError("table T { F:float; } root_type T; { F:+e }", invalid_msg);
1630   TestError("table T { F:float; } root_type T; { F:-.e }", invalid_msg);
1631   TestError("table T { F:float; } root_type T; { F:+.e }", invalid_msg);
1632   TestError("table T { F:float; } root_type T; { F:-e1 }", invalid_msg);
1633   TestError("table T { F:float; } root_type T; { F:+e1 }", invalid_msg);
1634   TestError("table T { F:float; } root_type T; { F:1.0e+ }", invalid_msg);
1635   TestError("table T { F:float; } root_type T; { F:1.0e- }", invalid_msg);
1636   // exponent pP is mandatory for hex-float
1637   TestError("table T { F:float; } root_type T; { F:0x0 }", invalid_msg);
1638   TestError("table T { F:float; } root_type T; { F:-0x. }", invalid_msg);
1639   TestError("table T { F:float; } root_type T; { F:0x. }", invalid_msg);
1640   // eE not exponent in hex-float!
1641   TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1642   TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1643   TestError("table T { F:float; } root_type T; { F:0x0.0p }", invalid_msg);
1644   TestError("table T { F:float; } root_type T; { F:0x0.0p+ }", invalid_msg);
1645   TestError("table T { F:float; } root_type T; { F:0x0.0p- }", invalid_msg);
1646   TestError("table T { F:float; } root_type T; { F:0x0.0pa1 }", invalid_msg);
1647   TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1648   TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1649   TestError("table T { F:float; } root_type T; { F:0x0.0e+0 }", invalid_msg);
1650   TestError("table T { F:float; } root_type T; { F:0x0.0e-0 }", invalid_msg);
1651   TestError("table T { F:float; } root_type T; { F:0x0.0ep+ }", invalid_msg);
1652   TestError("table T { F:float; } root_type T; { F:0x0.0ep- }", invalid_msg);
1653   TestError("table T { F:float; } root_type T; { F:1.2.3 }", invalid_msg);
1654   TestError("table T { F:float; } root_type T; { F:1.2.e3 }", invalid_msg);
1655   TestError("table T { F:float; } root_type T; { F:1.2e.3 }", invalid_msg);
1656   TestError("table T { F:float; } root_type T; { F:1.2e0.3 }", invalid_msg);
1657   TestError("table T { F:float; } root_type T; { F:1.2e3. }", invalid_msg);
1658   TestError("table T { F:float; } root_type T; { F:1.2e3.0 }", invalid_msg);
1659   TestError("table T { F:float; } root_type T; { F:+-1.0 }", invalid_msg);
1660   TestError("table T { F:float; } root_type T; { F:1.0e+-1 }", invalid_msg);
1661   TestError("table T { F:float; } root_type T; { F:\"1.0e+-1\" }", invalid_msg);
1662   TestError("table T { F:float; } root_type T; { F:1.e0e }", comma_msg);
1663   TestError("table T { F:float; } root_type T; { F:0x1.p0e }", comma_msg);
1664   TestError("table T { F:float; } root_type T; { F:\" 0x10 \" }", invalid_msg);
1665   // floats in string
1666   TestError("table T { F:float; } root_type T; { F:\"1,2.\" }", invalid_msg);
1667   TestError("table T { F:float; } root_type T; { F:\"1.2e3.\" }", invalid_msg);
1668   TestError("table T { F:float; } root_type T; { F:\"0x1.p0e\" }", invalid_msg);
1669   TestError("table T { F:float; } root_type T; { F:\"0x1.0\" }", invalid_msg);
1670   TestError("table T { F:float; } root_type T; { F:\" 0x1.0\" }", invalid_msg);
1671   TestError("table T { F:float; } root_type T; { F:\"+ 0\" }", invalid_msg);
1672   // disable escapes for "number-in-string"
1673   TestError("table T { F:float; } root_type T; { F:\"\\f1.2e3.\" }", "invalid");
1674   TestError("table T { F:float; } root_type T; { F:\"\\t1.2e3.\" }", "invalid");
1675   TestError("table T { F:float; } root_type T; { F:\"\\n1.2e3.\" }", "invalid");
1676   TestError("table T { F:float; } root_type T; { F:\"\\r1.2e3.\" }", "invalid");
1677   TestError("table T { F:float; } root_type T; { F:\"4\\x005\" }", "invalid");
1678   TestError("table T { F:float; } root_type T; { F:\"\'12\'\" }", invalid_msg);
1679   // null is not a number constant!
1680   TestError("table T { F:float; } root_type T; { F:\"null\" }", invalid_msg);
1681   TestError("table T { F:float; } root_type T; { F:null }", invalid_msg);
1682 }
1683 
1684 template<typename T>
NumericUtilsTestInteger(const char * lower,const char * upper)1685 void NumericUtilsTestInteger(const char *lower, const char *upper) {
1686   T x;
1687   TEST_EQ(flatbuffers::StringToNumber("1q", &x), false);
1688   TEST_EQ(x, 0);
1689   TEST_EQ(flatbuffers::StringToNumber(upper, &x), false);
1690   TEST_EQ(x, flatbuffers::numeric_limits<T>::max());
1691   TEST_EQ(flatbuffers::StringToNumber(lower, &x), false);
1692   auto expval = flatbuffers::is_unsigned<T>::value
1693                     ? flatbuffers::numeric_limits<T>::max()
1694                     : flatbuffers::numeric_limits<T>::lowest();
1695   TEST_EQ(x, expval);
1696 }
1697 
1698 template<typename T>
NumericUtilsTestFloat(const char * lower,const char * upper)1699 void NumericUtilsTestFloat(const char *lower, const char *upper) {
1700   T f;
1701   TEST_EQ(flatbuffers::StringToNumber("", &f), false);
1702   TEST_EQ(flatbuffers::StringToNumber("1q", &f), false);
1703   TEST_EQ(f, 0);
1704   TEST_EQ(flatbuffers::StringToNumber(upper, &f), true);
1705   TEST_EQ(f, +flatbuffers::numeric_limits<T>::infinity());
1706   TEST_EQ(flatbuffers::StringToNumber(lower, &f), true);
1707   TEST_EQ(f, -flatbuffers::numeric_limits<T>::infinity());
1708 }
1709 
NumericUtilsTest()1710 void NumericUtilsTest() {
1711   NumericUtilsTestInteger<uint64_t>("-1", "18446744073709551616");
1712   NumericUtilsTestInteger<uint8_t>("-1", "256");
1713   NumericUtilsTestInteger<int64_t>("-9223372036854775809",
1714                                    "9223372036854775808");
1715   NumericUtilsTestInteger<int8_t>("-129", "128");
1716   NumericUtilsTestFloat<float>("-3.4029e+38", "+3.4029e+38");
1717   NumericUtilsTestFloat<float>("-1.7977e+308", "+1.7977e+308");
1718 }
1719 
IsAsciiUtilsTest()1720 void IsAsciiUtilsTest() {
1721   char c = -128;
1722   for (int cnt = 0; cnt < 256; cnt++) {
1723     auto alpha = (('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z'));
1724     auto dec = (('0' <= c) && (c <= '9'));
1725     auto hex = (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'));
1726     TEST_EQ(flatbuffers::is_alpha(c), alpha);
1727     TEST_EQ(flatbuffers::is_alnum(c), alpha || dec);
1728     TEST_EQ(flatbuffers::is_digit(c), dec);
1729     TEST_EQ(flatbuffers::is_xdigit(c), dec || hex);
1730     c += 1;
1731   }
1732 }
1733 
UnicodeTest()1734 void UnicodeTest() {
1735   flatbuffers::Parser parser;
1736   // Without setting allow_non_utf8 = true, we treat \x sequences as byte
1737   // sequences which are then validated as UTF-8.
1738   TEST_EQ(parser.Parse("table T { F:string; }"
1739                        "root_type T;"
1740                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1741                        "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8"
1742                        "3D\\uDE0E\" }"),
1743           true);
1744   std::string jsongen;
1745   parser.opts.indent_step = -1;
1746   auto result =
1747       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1748   TEST_EQ(result, true);
1749   TEST_EQ_STR(jsongen.c_str(),
1750               "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1751               "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
1752 }
1753 
UnicodeTestAllowNonUTF8()1754 void UnicodeTestAllowNonUTF8() {
1755   flatbuffers::Parser parser;
1756   parser.opts.allow_non_utf8 = true;
1757   TEST_EQ(
1758       parser.Parse(
1759           "table T { F:string; }"
1760           "root_type T;"
1761           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1762           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1763       true);
1764   std::string jsongen;
1765   parser.opts.indent_step = -1;
1766   auto result =
1767       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1768   TEST_EQ(result, true);
1769   TEST_EQ_STR(
1770       jsongen.c_str(),
1771       "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1772       "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
1773 }
1774 
UnicodeTestGenerateTextFailsOnNonUTF8()1775 void UnicodeTestGenerateTextFailsOnNonUTF8() {
1776   flatbuffers::Parser parser;
1777   // Allow non-UTF-8 initially to model what happens when we load a binary
1778   // flatbuffer from disk which contains non-UTF-8 strings.
1779   parser.opts.allow_non_utf8 = true;
1780   TEST_EQ(
1781       parser.Parse(
1782           "table T { F:string; }"
1783           "root_type T;"
1784           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1785           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1786       true);
1787   std::string jsongen;
1788   parser.opts.indent_step = -1;
1789   // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates
1790   // failure.
1791   parser.opts.allow_non_utf8 = false;
1792   auto result =
1793       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1794   TEST_EQ(result, false);
1795 }
1796 
UnicodeSurrogatesTest()1797 void UnicodeSurrogatesTest() {
1798   flatbuffers::Parser parser;
1799 
1800   TEST_EQ(parser.Parse("table T { F:string (id: 0); }"
1801                        "root_type T;"
1802                        "{ F:\"\\uD83D\\uDCA9\"}"),
1803           true);
1804   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1805       parser.builder_.GetBufferPointer());
1806   auto string = root->GetPointer<flatbuffers::String *>(
1807       flatbuffers::FieldIndexToOffset(0));
1808   TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
1809 }
1810 
UnicodeInvalidSurrogatesTest()1811 void UnicodeInvalidSurrogatesTest() {
1812   TestError(
1813       "table T { F:string; }"
1814       "root_type T;"
1815       "{ F:\"\\uD800\"}",
1816       "unpaired high surrogate");
1817   TestError(
1818       "table T { F:string; }"
1819       "root_type T;"
1820       "{ F:\"\\uD800abcd\"}",
1821       "unpaired high surrogate");
1822   TestError(
1823       "table T { F:string; }"
1824       "root_type T;"
1825       "{ F:\"\\uD800\\n\"}",
1826       "unpaired high surrogate");
1827   TestError(
1828       "table T { F:string; }"
1829       "root_type T;"
1830       "{ F:\"\\uD800\\uD800\"}",
1831       "multiple high surrogates");
1832   TestError(
1833       "table T { F:string; }"
1834       "root_type T;"
1835       "{ F:\"\\uDC00\"}",
1836       "unpaired low surrogate");
1837 }
1838 
InvalidUTF8Test()1839 void InvalidUTF8Test() {
1840   // "1 byte" pattern, under min length of 2 bytes
1841   TestError(
1842       "table T { F:string; }"
1843       "root_type T;"
1844       "{ F:\"\x80\"}",
1845       "illegal UTF-8 sequence");
1846   // 2 byte pattern, string too short
1847   TestError(
1848       "table T { F:string; }"
1849       "root_type T;"
1850       "{ F:\"\xDF\"}",
1851       "illegal UTF-8 sequence");
1852   // 3 byte pattern, string too short
1853   TestError(
1854       "table T { F:string; }"
1855       "root_type T;"
1856       "{ F:\"\xEF\xBF\"}",
1857       "illegal UTF-8 sequence");
1858   // 4 byte pattern, string too short
1859   TestError(
1860       "table T { F:string; }"
1861       "root_type T;"
1862       "{ F:\"\xF7\xBF\xBF\"}",
1863       "illegal UTF-8 sequence");
1864   // "5 byte" pattern, string too short
1865   TestError(
1866       "table T { F:string; }"
1867       "root_type T;"
1868       "{ F:\"\xFB\xBF\xBF\xBF\"}",
1869       "illegal UTF-8 sequence");
1870   // "6 byte" pattern, string too short
1871   TestError(
1872       "table T { F:string; }"
1873       "root_type T;"
1874       "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}",
1875       "illegal UTF-8 sequence");
1876   // "7 byte" pattern, string too short
1877   TestError(
1878       "table T { F:string; }"
1879       "root_type T;"
1880       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}",
1881       "illegal UTF-8 sequence");
1882   // "5 byte" pattern, over max length of 4 bytes
1883   TestError(
1884       "table T { F:string; }"
1885       "root_type T;"
1886       "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}",
1887       "illegal UTF-8 sequence");
1888   // "6 byte" pattern, over max length of 4 bytes
1889   TestError(
1890       "table T { F:string; }"
1891       "root_type T;"
1892       "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}",
1893       "illegal UTF-8 sequence");
1894   // "7 byte" pattern, over max length of 4 bytes
1895   TestError(
1896       "table T { F:string; }"
1897       "root_type T;"
1898       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}",
1899       "illegal UTF-8 sequence");
1900 
1901   // Three invalid encodings for U+000A (\n, aka NEWLINE)
1902   TestError(
1903       "table T { F:string; }"
1904       "root_type T;"
1905       "{ F:\"\xC0\x8A\"}",
1906       "illegal UTF-8 sequence");
1907   TestError(
1908       "table T { F:string; }"
1909       "root_type T;"
1910       "{ F:\"\xE0\x80\x8A\"}",
1911       "illegal UTF-8 sequence");
1912   TestError(
1913       "table T { F:string; }"
1914       "root_type T;"
1915       "{ F:\"\xF0\x80\x80\x8A\"}",
1916       "illegal UTF-8 sequence");
1917 
1918   // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
1919   TestError(
1920       "table T { F:string; }"
1921       "root_type T;"
1922       "{ F:\"\xE0\x81\xA9\"}",
1923       "illegal UTF-8 sequence");
1924   TestError(
1925       "table T { F:string; }"
1926       "root_type T;"
1927       "{ F:\"\xF0\x80\x81\xA9\"}",
1928       "illegal UTF-8 sequence");
1929 
1930   // Invalid encoding for U+20AC (EURO SYMBOL)
1931   TestError(
1932       "table T { F:string; }"
1933       "root_type T;"
1934       "{ F:\"\xF0\x82\x82\xAC\"}",
1935       "illegal UTF-8 sequence");
1936 
1937   // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in
1938   // UTF-8
1939   TestError(
1940       "table T { F:string; }"
1941       "root_type T;"
1942       // U+10400 "encoded" as U+D801 U+DC00
1943       "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}",
1944       "illegal UTF-8 sequence");
1945 
1946   // Check independence of identifier from locale.
1947   std::string locale_ident;
1948   locale_ident += "table T { F";
1949   locale_ident += static_cast<char>(-32); // unsigned 0xE0
1950   locale_ident += " :string; }";
1951   locale_ident += "root_type T;";
1952   locale_ident += "{}";
1953   TestError(locale_ident.c_str(), "");
1954 }
1955 
UnknownFieldsTest()1956 void UnknownFieldsTest() {
1957   flatbuffers::IDLOptions opts;
1958   opts.skip_unexpected_fields_in_json = true;
1959   flatbuffers::Parser parser(opts);
1960 
1961   TEST_EQ(parser.Parse("table T { str:string; i:int;}"
1962                        "root_type T;"
1963                        "{ str:\"test\","
1964                        "unknown_string:\"test\","
1965                        "\"unknown_string\":\"test\","
1966                        "unknown_int:10,"
1967                        "unknown_float:1.0,"
1968                        "unknown_array: [ 1, 2, 3, 4],"
1969                        "unknown_object: { i: 10 },"
1970                        "\"unknown_object\": { \"i\": 10 },"
1971                        "i:10}"),
1972           true);
1973 
1974   std::string jsongen;
1975   parser.opts.indent_step = -1;
1976   auto result =
1977       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1978   TEST_EQ(result, true);
1979   TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
1980 }
1981 
ParseUnionTest()1982 void ParseUnionTest() {
1983   // Unions must be parseable with the type field following the object.
1984   flatbuffers::Parser parser;
1985   TEST_EQ(parser.Parse("table T { A:int; }"
1986                        "union U { T }"
1987                        "table V { X:U; }"
1988                        "root_type V;"
1989                        "{ X:{ A:1 }, X_type: T }"),
1990           true);
1991   // Unions must be parsable with prefixed namespace.
1992   flatbuffers::Parser parser2;
1993   TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
1994                         "table B { e:U; } root_type B;"
1995                         "{ e_type: N_A, e: {} }"),
1996           true);
1997 }
1998 
InvalidNestedFlatbufferTest()1999 void InvalidNestedFlatbufferTest() {
2000   // First, load and parse FlatBuffer schema (.fbs)
2001   std::string schemafile;
2002   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
2003                                 false, &schemafile),
2004           true);
2005   auto include_test_path =
2006       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
2007   const char *include_directories[] = { test_data_path.c_str(),
2008                                         include_test_path.c_str(), nullptr };
2009   flatbuffers::Parser parser1;
2010   TEST_EQ(parser1.Parse(schemafile.c_str(), include_directories), true);
2011 
2012   // "color" inside nested flatbuffer contains invalid enum value
2013   TEST_EQ(parser1.Parse("{ name: \"Bender\", testnestedflatbuffer: { name: "
2014                         "\"Leela\", color: \"nonexistent\"}}"),
2015           false);
2016   // Check that Parser is destroyed correctly after parsing invalid json
2017 }
2018 
UnionVectorTest()2019 void UnionVectorTest() {
2020   // load FlatBuffer fbs schema.
2021   // TODO: load a JSON file with such a vector when JSON support is ready.
2022   std::string schemafile;
2023   TEST_EQ(flatbuffers::LoadFile(
2024               (test_data_path + "union_vector/union_vector.fbs").c_str(), false,
2025               &schemafile),
2026           true);
2027 
2028   // parse schema.
2029   flatbuffers::IDLOptions idl_opts;
2030   idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kCpp;
2031   flatbuffers::Parser parser(idl_opts);
2032   TEST_EQ(parser.Parse(schemafile.c_str()), true);
2033 
2034   flatbuffers::FlatBufferBuilder fbb;
2035 
2036   // union types.
2037   std::vector<uint8_t> types;
2038   types.push_back(static_cast<uint8_t>(Character_Belle));
2039   types.push_back(static_cast<uint8_t>(Character_MuLan));
2040   types.push_back(static_cast<uint8_t>(Character_BookFan));
2041   types.push_back(static_cast<uint8_t>(Character_Other));
2042   types.push_back(static_cast<uint8_t>(Character_Unused));
2043 
2044   // union values.
2045   std::vector<flatbuffers::Offset<void>> characters;
2046   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
2047   characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
2048   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
2049   characters.push_back(fbb.CreateString("Other").Union());
2050   characters.push_back(fbb.CreateString("Unused").Union());
2051 
2052   // create Movie.
2053   const auto movie_offset =
2054       CreateMovie(fbb, Character_Rapunzel,
2055                   fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
2056                   fbb.CreateVector(types), fbb.CreateVector(characters));
2057   FinishMovieBuffer(fbb, movie_offset);
2058   auto buf = fbb.GetBufferPointer();
2059 
2060   flatbuffers::Verifier verifier(buf, fbb.GetSize());
2061   TEST_EQ(VerifyMovieBuffer(verifier), true);
2062 
2063   auto flat_movie = GetMovie(buf);
2064 
2065   auto TestMovie = [](const Movie *movie) {
2066     TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
2067 
2068     auto cts = movie->characters_type();
2069     TEST_EQ(movie->characters_type()->size(), 5);
2070     TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
2071     TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
2072     TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
2073     TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
2074     TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
2075 
2076     auto rapunzel = movie->main_character_as_Rapunzel();
2077     TEST_NOTNULL(rapunzel);
2078     TEST_EQ(rapunzel->hair_length(), 6);
2079 
2080     auto cs = movie->characters();
2081     TEST_EQ(cs->size(), 5);
2082     auto belle = cs->GetAs<BookReader>(0);
2083     TEST_EQ(belle->books_read(), 7);
2084     auto mu_lan = cs->GetAs<Attacker>(1);
2085     TEST_EQ(mu_lan->sword_attack_damage(), 5);
2086     auto book_fan = cs->GetAs<BookReader>(2);
2087     TEST_EQ(book_fan->books_read(), 2);
2088     auto other = cs->GetAsString(3);
2089     TEST_EQ_STR(other->c_str(), "Other");
2090     auto unused = cs->GetAsString(4);
2091     TEST_EQ_STR(unused->c_str(), "Unused");
2092   };
2093 
2094   TestMovie(flat_movie);
2095 
2096   auto movie_object = flat_movie->UnPack();
2097   TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
2098   TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
2099   TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
2100   TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
2101   TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
2102   TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
2103 
2104   fbb.Clear();
2105   fbb.Finish(Movie::Pack(fbb, movie_object));
2106 
2107   delete movie_object;
2108 
2109   auto repacked_movie = GetMovie(fbb.GetBufferPointer());
2110 
2111   TestMovie(repacked_movie);
2112 
2113   auto s =
2114       flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable());
2115   TEST_EQ_STR(
2116       s.c_str(),
2117       "{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
2118       "characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
2119       "characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
2120       "{ books_read: 2 }, \"Other\", \"Unused\" ] }");
2121 
2122 
2123   flatbuffers::ToStringVisitor visitor("\n", true, "  ");
2124   IterateFlatBuffer(fbb.GetBufferPointer(), MovieTypeTable(), &visitor);
2125   TEST_EQ_STR(
2126       visitor.s.c_str(),
2127       "{\n"
2128       "  \"main_character_type\": \"Rapunzel\",\n"
2129       "  \"main_character\": {\n"
2130       "    \"hair_length\": 6\n"
2131       "  },\n"
2132       "  \"characters_type\": [\n"
2133       "    \"Belle\",\n"
2134       "    \"MuLan\",\n"
2135       "    \"BookFan\",\n"
2136       "    \"Other\",\n"
2137       "    \"Unused\"\n"
2138       "  ],\n"
2139       "  \"characters\": [\n"
2140       "    {\n"
2141       "      \"books_read\": 7\n"
2142       "    },\n"
2143       "    {\n"
2144       "      \"sword_attack_damage\": 5\n"
2145       "    },\n"
2146       "    {\n"
2147       "      \"books_read\": 2\n"
2148       "    },\n"
2149       "    \"Other\",\n"
2150       "    \"Unused\"\n"
2151       "  ]\n"
2152       "}");
2153 }
2154 
ConformTest()2155 void ConformTest() {
2156   flatbuffers::Parser parser;
2157   TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
2158 
2159   auto test_conform = [](flatbuffers::Parser &parser1, const char *test,
2160                          const char *expected_err) {
2161     flatbuffers::Parser parser2;
2162     TEST_EQ(parser2.Parse(test), true);
2163     auto err = parser2.ConformTo(parser1);
2164     TEST_NOTNULL(strstr(err.c_str(), expected_err));
2165   };
2166 
2167   test_conform(parser, "table T { A:byte; }", "types differ for field");
2168   test_conform(parser, "table T { B:int; A:int; }", "offsets differ for field");
2169   test_conform(parser, "table T { A:int = 1; }", "defaults differ for field");
2170   test_conform(parser, "table T { B:float; }",
2171                "field renamed to different type");
2172   test_conform(parser, "enum E:byte { B, A }", "values differ for enum");
2173 }
2174 
ParseProtoBufAsciiTest()2175 void ParseProtoBufAsciiTest() {
2176   // We can put the parser in a mode where it will accept JSON that looks more
2177   // like Protobuf ASCII, for users that have data in that format.
2178   // This uses no "" for field names (which we already support by default,
2179   // omits `,`, `:` before `{` and a couple of other features.
2180   flatbuffers::Parser parser;
2181   parser.opts.protobuf_ascii_alike = true;
2182   TEST_EQ(
2183       parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
2184       true);
2185   TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
2186   // Similarly, in text output, it should omit these.
2187   std::string text;
2188   auto ok = flatbuffers::GenerateText(
2189       parser, parser.builder_.GetBufferPointer(), &text);
2190   TEST_EQ(ok, true);
2191   TEST_EQ_STR(text.c_str(),
2192               "{\n  A [\n    1\n    2\n  ]\n  C {\n    B: 2\n  }\n}\n");
2193 }
2194 
FlexBuffersTest()2195 void FlexBuffersTest() {
2196   flexbuffers::Builder slb(512,
2197                            flexbuffers::BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
2198 
2199   // Write the equivalent of:
2200   // { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ],
2201   // foo: 100, bool: true, mymap: { foo: "Fred" } }
2202   // clang-format off
2203   #ifndef FLATBUFFERS_CPP98_STL
2204     // It's possible to do this without std::function support as well.
2205     slb.Map([&]() {
2206        slb.Vector("vec", [&]() {
2207         slb += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
2208         slb += "Fred";
2209         slb.IndirectFloat(4.0f);
2210         uint8_t blob[] = { 77 };
2211         slb.Blob(blob, 1);
2212         slb += false;
2213       });
2214       int ints[] = { 1, 2, 3 };
2215       slb.Vector("bar", ints, 3);
2216       slb.FixedTypedVector("bar3", ints, 3);
2217       bool bools[] = {true, false, true, false};
2218       slb.Vector("bools", bools, 4);
2219       slb.Bool("bool", true);
2220       slb.Double("foo", 100);
2221       slb.Map("mymap", [&]() {
2222         slb.String("foo", "Fred");  // Testing key and string reuse.
2223       });
2224     });
2225     slb.Finish();
2226   #else
2227     // It's possible to do this without std::function support as well.
2228     slb.Map([](flexbuffers::Builder& slb2) {
2229        slb2.Vector("vec", [](flexbuffers::Builder& slb3) {
2230         slb3 += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
2231         slb3 += "Fred";
2232         slb3.IndirectFloat(4.0f);
2233         uint8_t blob[] = { 77 };
2234         slb3.Blob(blob, 1);
2235         slb3 += false;
2236       }, slb2);
2237       int ints[] = { 1, 2, 3 };
2238       slb2.Vector("bar", ints, 3);
2239       slb2.FixedTypedVector("bar3", ints, 3);
2240       slb2.Bool("bool", true);
2241       slb2.Double("foo", 100);
2242       slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
2243         slb3.String("foo", "Fred");  // Testing key and string reuse.
2244       }, slb2);
2245     }, slb);
2246     slb.Finish();
2247   #endif  // FLATBUFFERS_CPP98_STL
2248 
2249   #ifdef FLATBUFFERS_TEST_VERBOSE
2250     for (size_t i = 0; i < slb.GetBuffer().size(); i++)
2251       printf("%d ", flatbuffers::vector_data(slb.GetBuffer())[i]);
2252     printf("\n");
2253   #endif
2254   // clang-format on
2255 
2256   auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
2257   TEST_EQ(map.size(), 7);
2258   auto vec = map["vec"].AsVector();
2259   TEST_EQ(vec.size(), 5);
2260   TEST_EQ(vec[0].AsInt64(), -100);
2261   TEST_EQ_STR(vec[1].AsString().c_str(), "Fred");
2262   TEST_EQ(vec[1].AsInt64(), 0);  // Number parsing failed.
2263   TEST_EQ(vec[2].AsDouble(), 4.0);
2264   TEST_EQ(vec[2].AsString().IsTheEmptyString(), true);  // Wrong Type.
2265   TEST_EQ_STR(vec[2].AsString().c_str(), "");     // This still works though.
2266   TEST_EQ_STR(vec[2].ToString().c_str(), "4.0");  // Or have it converted.
2267 
2268   // Few tests for templated version of As.
2269   TEST_EQ(vec[0].As<int64_t>(), -100);
2270   TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred");
2271   TEST_EQ(vec[1].As<int64_t>(), 0);  // Number parsing failed.
2272   TEST_EQ(vec[2].As<double>(), 4.0);
2273 
2274   // Test that the blob can be accessed.
2275   TEST_EQ(vec[3].IsBlob(), true);
2276   auto blob = vec[3].AsBlob();
2277   TEST_EQ(blob.size(), 1);
2278   TEST_EQ(blob.data()[0], 77);
2279   TEST_EQ(vec[4].IsBool(), true);   // Check if type is a bool
2280   TEST_EQ(vec[4].AsBool(), false);  // Check if value is false
2281   auto tvec = map["bar"].AsTypedVector();
2282   TEST_EQ(tvec.size(), 3);
2283   TEST_EQ(tvec[2].AsInt8(), 3);
2284   auto tvec3 = map["bar3"].AsFixedTypedVector();
2285   TEST_EQ(tvec3.size(), 3);
2286   TEST_EQ(tvec3[2].AsInt8(), 3);
2287   TEST_EQ(map["bool"].AsBool(), true);
2288   auto tvecb = map["bools"].AsTypedVector();
2289   TEST_EQ(tvecb.ElementType(), flexbuffers::FBT_BOOL);
2290   TEST_EQ(map["foo"].AsUInt8(), 100);
2291   TEST_EQ(map["unknown"].IsNull(), true);
2292   auto mymap = map["mymap"].AsMap();
2293   // These should be equal by pointer equality, since key and value are shared.
2294   TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey());
2295   TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
2296   // We can mutate values in the buffer.
2297   TEST_EQ(vec[0].MutateInt(-99), true);
2298   TEST_EQ(vec[0].AsInt64(), -99);
2299   TEST_EQ(vec[1].MutateString("John"), true);  // Size must match.
2300   TEST_EQ_STR(vec[1].AsString().c_str(), "John");
2301   TEST_EQ(vec[1].MutateString("Alfred"), false);  // Too long.
2302   TEST_EQ(vec[2].MutateFloat(2.0f), true);
2303   TEST_EQ(vec[2].AsFloat(), 2.0f);
2304   TEST_EQ(vec[2].MutateFloat(3.14159), false);  // Double does not fit in float.
2305   TEST_EQ(vec[4].AsBool(), false);              // Is false before change
2306   TEST_EQ(vec[4].MutateBool(true), true);       // Can change a bool
2307   TEST_EQ(vec[4].AsBool(), true);               // Changed bool is now true
2308 
2309   // Parse from JSON:
2310   flatbuffers::Parser parser;
2311   slb.Clear();
2312   auto jsontest = "{ a: [ 123, 456.0 ], b: \"hello\", c: true, d: false }";
2313   TEST_EQ(parser.ParseFlexBuffer(jsontest, nullptr, &slb), true);
2314   auto jroot = flexbuffers::GetRoot(slb.GetBuffer());
2315   auto jmap = jroot.AsMap();
2316   auto jvec = jmap["a"].AsVector();
2317   TEST_EQ(jvec[0].AsInt64(), 123);
2318   TEST_EQ(jvec[1].AsDouble(), 456.0);
2319   TEST_EQ_STR(jmap["b"].AsString().c_str(), "hello");
2320   TEST_EQ(jmap["c"].IsBool(), true);   // Parsed correctly to a bool
2321   TEST_EQ(jmap["c"].AsBool(), true);   // Parsed correctly to true
2322   TEST_EQ(jmap["d"].IsBool(), true);   // Parsed correctly to a bool
2323   TEST_EQ(jmap["d"].AsBool(), false);  // Parsed correctly to false
2324   // And from FlexBuffer back to JSON:
2325   auto jsonback = jroot.ToString();
2326   TEST_EQ_STR(jsontest, jsonback.c_str());
2327 }
2328 
TypeAliasesTest()2329 void TypeAliasesTest() {
2330   flatbuffers::FlatBufferBuilder builder;
2331 
2332   builder.Finish(CreateTypeAliases(
2333       builder, flatbuffers::numeric_limits<int8_t>::min(),
2334       flatbuffers::numeric_limits<uint8_t>::max(),
2335       flatbuffers::numeric_limits<int16_t>::min(),
2336       flatbuffers::numeric_limits<uint16_t>::max(),
2337       flatbuffers::numeric_limits<int32_t>::min(),
2338       flatbuffers::numeric_limits<uint32_t>::max(),
2339       flatbuffers::numeric_limits<int64_t>::min(),
2340       flatbuffers::numeric_limits<uint64_t>::max(), 2.3f, 2.3));
2341 
2342   auto p = builder.GetBufferPointer();
2343   auto ta = flatbuffers::GetRoot<TypeAliases>(p);
2344 
2345   TEST_EQ(ta->i8(), flatbuffers::numeric_limits<int8_t>::min());
2346   TEST_EQ(ta->u8(), flatbuffers::numeric_limits<uint8_t>::max());
2347   TEST_EQ(ta->i16(), flatbuffers::numeric_limits<int16_t>::min());
2348   TEST_EQ(ta->u16(), flatbuffers::numeric_limits<uint16_t>::max());
2349   TEST_EQ(ta->i32(), flatbuffers::numeric_limits<int32_t>::min());
2350   TEST_EQ(ta->u32(), flatbuffers::numeric_limits<uint32_t>::max());
2351   TEST_EQ(ta->i64(), flatbuffers::numeric_limits<int64_t>::min());
2352   TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
2353   TEST_EQ(ta->f32(), 2.3f);
2354   TEST_EQ(ta->f64(), 2.3);
2355   using namespace flatbuffers; // is_same
2356   static_assert(is_same<decltype(ta->i8()), int8_t>::value, "invalid type");
2357   static_assert(is_same<decltype(ta->i16()), int16_t>::value, "invalid type");
2358   static_assert(is_same<decltype(ta->i32()), int32_t>::value, "invalid type");
2359   static_assert(is_same<decltype(ta->i64()), int64_t>::value, "invalid type");
2360   static_assert(is_same<decltype(ta->u8()), uint8_t>::value, "invalid type");
2361   static_assert(is_same<decltype(ta->u16()), uint16_t>::value, "invalid type");
2362   static_assert(is_same<decltype(ta->u32()), uint32_t>::value, "invalid type");
2363   static_assert(is_same<decltype(ta->u64()), uint64_t>::value, "invalid type");
2364   static_assert(is_same<decltype(ta->f32()), float>::value, "invalid type");
2365   static_assert(is_same<decltype(ta->f64()), double>::value, "invalid type");
2366 }
2367 
EndianSwapTest()2368 void EndianSwapTest() {
2369   TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)), 0x3412);
2370   TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
2371           0x78563412);
2372   TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
2373           0xEFCDAB9078563412);
2374   TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
2375 }
2376 
UninitializedVectorTest()2377 void UninitializedVectorTest() {
2378   flatbuffers::FlatBufferBuilder builder;
2379 
2380   Test *buf = nullptr;
2381   auto vector_offset = builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
2382   TEST_NOTNULL(buf);
2383   buf[0] = Test(10, 20);
2384   buf[1] = Test(30, 40);
2385 
2386   auto required_name = builder.CreateString("myMonster");
2387   auto monster_builder = MonsterBuilder(builder);
2388   monster_builder.add_name(required_name); // required field mandated for monster.
2389   monster_builder.add_test4(vector_offset);
2390   builder.Finish(monster_builder.Finish());
2391 
2392   auto p = builder.GetBufferPointer();
2393   auto uvt = flatbuffers::GetRoot<Monster>(p);
2394   TEST_NOTNULL(uvt);
2395   auto vec = uvt->test4();
2396   TEST_NOTNULL(vec);
2397   auto test_0 = vec->Get(0);
2398   auto test_1 = vec->Get(1);
2399   TEST_EQ(test_0->a(), 10);
2400   TEST_EQ(test_0->b(), 20);
2401   TEST_EQ(test_1->a(), 30);
2402   TEST_EQ(test_1->b(), 40);
2403 }
2404 
EqualOperatorTest()2405 void EqualOperatorTest() {
2406   MonsterT a;
2407   MonsterT b;
2408   TEST_EQ(b == a, true);
2409 
2410   b.mana = 33;
2411   TEST_EQ(b == a, false);
2412   b.mana = 150;
2413   TEST_EQ(b == a, true);
2414 
2415   b.inventory.push_back(3);
2416   TEST_EQ(b == a, false);
2417   b.inventory.clear();
2418   TEST_EQ(b == a, true);
2419 
2420   b.test.type = Any_Monster;
2421   TEST_EQ(b == a, false);
2422 }
2423 
2424 // For testing any binaries, e.g. from fuzzing.
LoadVerifyBinaryTest()2425 void LoadVerifyBinaryTest() {
2426   std::string binary;
2427   if (flatbuffers::LoadFile((test_data_path +
2428                              "fuzzer/your-filename-here").c_str(),
2429                             true, &binary)) {
2430     flatbuffers::Verifier verifier(
2431           reinterpret_cast<const uint8_t *>(binary.data()), binary.size());
2432     TEST_EQ(VerifyMonsterBuffer(verifier), true);
2433   }
2434 }
2435 
CreateSharedStringTest()2436 void CreateSharedStringTest() {
2437   flatbuffers::FlatBufferBuilder builder;
2438   const auto one1 = builder.CreateSharedString("one");
2439   const auto two = builder.CreateSharedString("two");
2440   const auto one2 = builder.CreateSharedString("one");
2441   TEST_EQ(one1.o, one2.o);
2442   const auto onetwo = builder.CreateSharedString("onetwo");
2443   TEST_EQ(onetwo.o != one1.o, true);
2444   TEST_EQ(onetwo.o != two.o, true);
2445 
2446   // Support for embedded nulls
2447   const char chars_b[] = {'a', '\0', 'b'};
2448   const char chars_c[] = {'a', '\0', 'c'};
2449   const auto null_b1 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2450   const auto null_c = builder.CreateSharedString(chars_c, sizeof(chars_c));
2451   const auto null_b2 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2452   TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
2453   TEST_EQ(null_b1.o, null_b2.o);
2454 
2455   // Put the strings into an array for round trip verification.
2456   const flatbuffers::Offset<flatbuffers::String> array[7] = { one1, two, one2, onetwo, null_b1, null_c, null_b2 };
2457   const auto vector_offset = builder.CreateVector(array, flatbuffers::uoffset_t(7));
2458   MonsterBuilder monster_builder(builder);
2459   monster_builder.add_name(two);
2460   monster_builder.add_testarrayofstring(vector_offset);
2461   builder.Finish(monster_builder.Finish());
2462 
2463   // Read the Monster back.
2464   const auto *monster = flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
2465   TEST_EQ_STR(monster->name()->c_str(), "two");
2466   const auto *testarrayofstring = monster->testarrayofstring();
2467   TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
2468   const auto &a = *testarrayofstring;
2469   TEST_EQ_STR(a[0]->c_str(), "one");
2470   TEST_EQ_STR(a[1]->c_str(), "two");
2471   TEST_EQ_STR(a[2]->c_str(), "one");
2472   TEST_EQ_STR(a[3]->c_str(), "onetwo");
2473   TEST_EQ(a[4]->str(), (std::string(chars_b, sizeof(chars_b))));
2474   TEST_EQ(a[5]->str(), (std::string(chars_c, sizeof(chars_c))));
2475   TEST_EQ(a[6]->str(), (std::string(chars_b, sizeof(chars_b))));
2476 
2477   // Make sure String::operator< works, too, since it is related to StringOffsetCompare.
2478   TEST_EQ((*a[0]) < (*a[1]), true);
2479   TEST_EQ((*a[1]) < (*a[0]), false);
2480   TEST_EQ((*a[1]) < (*a[2]), false);
2481   TEST_EQ((*a[2]) < (*a[1]), true);
2482   TEST_EQ((*a[4]) < (*a[3]), true);
2483   TEST_EQ((*a[5]) < (*a[4]), false);
2484   TEST_EQ((*a[5]) < (*a[4]), false);
2485   TEST_EQ((*a[6]) < (*a[5]), true);
2486 }
2487 
FlatBufferTests()2488 int FlatBufferTests() {
2489   // clang-format off
2490 
2491   // Run our various test suites:
2492 
2493   std::string rawbuf;
2494   auto flatbuf1 = CreateFlatBufferTest(rawbuf);
2495   #if !defined(FLATBUFFERS_CPP98_STL)
2496     auto flatbuf = std::move(flatbuf1);  // Test move assignment.
2497   #else
2498     auto &flatbuf = flatbuf1;
2499   #endif // !defined(FLATBUFFERS_CPP98_STL)
2500 
2501   TriviallyCopyableTest();
2502 
2503   AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
2504                        rawbuf.length());
2505   AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
2506 
2507   MutateFlatBuffersTest(flatbuf.data(), flatbuf.size());
2508 
2509   ObjectFlatBuffersTest(flatbuf.data());
2510 
2511   MiniReflectFlatBuffersTest(flatbuf.data());
2512 
2513   SizePrefixedTest();
2514 
2515   #ifndef FLATBUFFERS_NO_FILE_TESTS
2516     #ifdef FLATBUFFERS_TEST_PATH_PREFIX
2517       test_data_path = FLATBUFFERS_STRING(FLATBUFFERS_TEST_PATH_PREFIX) +
2518                        test_data_path;
2519     #endif
2520     ParseAndGenerateTextTest(false);
2521     ParseAndGenerateTextTest(true);
2522     ReflectionTest(flatbuf.data(), flatbuf.size());
2523     ParseProtoTest();
2524     UnionVectorTest();
2525     LoadVerifyBinaryTest();
2526   #endif
2527   // clang-format on
2528 
2529   FuzzTest1();
2530   FuzzTest2();
2531 
2532   ErrorTest();
2533   ValueTest();
2534   EnumStringsTest();
2535   EnumNamesTest();
2536   EnumOutOfRangeTest();
2537   IntegerOutOfRangeTest();
2538   IntegerBoundaryTest();
2539   UnicodeTest();
2540   UnicodeTestAllowNonUTF8();
2541   UnicodeTestGenerateTextFailsOnNonUTF8();
2542   UnicodeSurrogatesTest();
2543   UnicodeInvalidSurrogatesTest();
2544   InvalidUTF8Test();
2545   UnknownFieldsTest();
2546   ParseUnionTest();
2547   InvalidNestedFlatbufferTest();
2548   ConformTest();
2549   ParseProtoBufAsciiTest();
2550   TypeAliasesTest();
2551   EndianSwapTest();
2552   CreateSharedStringTest();
2553   JsonDefaultTest();
2554   FlexBuffersTest();
2555   UninitializedVectorTest();
2556   EqualOperatorTest();
2557   NumericUtilsTest();
2558   IsAsciiUtilsTest();
2559   ValidFloatTest();
2560   InvalidFloatTest();
2561   return 0;
2562 }
2563 
main(int,const char * [])2564 int main(int /*argc*/, const char * /*argv*/ []) {
2565   InitTestEngine();
2566 
2567   std::string req_locale;
2568   if (flatbuffers::ReadEnvironmentVariable("FLATBUFFERS_TEST_LOCALE",
2569                                           &req_locale)) {
2570     TEST_OUTPUT_LINE("The environment variable FLATBUFFERS_TEST_LOCALE=%s",
2571                      req_locale.c_str());
2572     req_locale = flatbuffers::RemoveStringQuotes(req_locale);
2573     std::string the_locale;
2574     TEST_ASSERT_FUNC(
2575         flatbuffers::SetGlobalTestLocale(req_locale.c_str(), &the_locale));
2576     TEST_OUTPUT_LINE("The global C-locale changed: %s", the_locale.c_str());
2577   }
2578 
2579   FlatBufferTests();
2580   FlatBufferBuilderTest();
2581 
2582   if (!testing_fails) {
2583     TEST_OUTPUT_LINE("ALL TESTS PASSED");
2584   } else {
2585     TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
2586   }
2587   return CloseTestEngine();
2588 }
2589