Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 1 | #include "aos/flatbuffers.h" |
| 2 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 3 | #include "absl/strings/str_cat.h" |
Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 4 | #include "aos/json_to_flatbuffer.h" |
| 5 | #include "aos/json_to_flatbuffer_generated.h" |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 6 | #include "aos/testing/tmpdir.h" |
| 7 | #include "gtest/gtest.h" |
Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | // Tests that Verify works. |
| 13 | TEST(FlatbufferTest, Verify) { |
| 14 | FlatbufferDetachedBuffer<Configuration> fb = |
| 15 | JsonToFlatbuffer<Configuration>("{}"); |
| 16 | FlatbufferSpan<Configuration> fb_span(fb); |
| 17 | EXPECT_TRUE(fb.Verify()); |
| 18 | EXPECT_TRUE(fb_span.Verify()); |
| 19 | |
| 20 | // Now confirm it works on an empty flatbuffer. |
| 21 | FlatbufferSpan<Configuration> empty(absl::Span<const uint8_t>(nullptr, 0)); |
| 22 | EXPECT_FALSE(empty.Verify()); |
| 23 | } |
| 24 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 25 | // Tests the ability to map a flatbuffer on disk to memory |
| 26 | TEST(FlatbufferMMapTest, Verify) { |
| 27 | FlatbufferDetachedBuffer<Configuration> fb = |
| 28 | JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}"); |
| 29 | |
| 30 | const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs"); |
| 31 | WriteFlatbufferToFile(fb_path, fb); |
| 32 | |
| 33 | FlatbufferMMap<Configuration> fb_mmap(fb_path); |
| 34 | EXPECT_TRUE(fb.Verify()); |
| 35 | EXPECT_TRUE(fb_mmap.Verify()); |
| 36 | ASSERT_EQ(fb_mmap.message().foo_int(), 3); |
| 37 | |
| 38 | // Verify that copying works |
| 39 | { |
| 40 | FlatbufferMMap<Configuration> fb_mmap2(fb_path); |
| 41 | fb_mmap2 = fb_mmap; |
| 42 | EXPECT_TRUE(fb_mmap.Verify()); |
| 43 | EXPECT_TRUE(fb_mmap2.Verify()); |
| 44 | ASSERT_EQ(fb_mmap2.message().foo_int(), 3); |
| 45 | ASSERT_EQ(fb_mmap.message().foo_int(), 3); |
| 46 | } |
| 47 | EXPECT_TRUE(fb_mmap.Verify()); |
| 48 | ASSERT_EQ(fb_mmap.message().foo_int(), 3); |
| 49 | |
| 50 | // Verify that moving works |
| 51 | { |
| 52 | FlatbufferMMap<Configuration> fb_mmap3(fb_path); |
| 53 | fb_mmap3 = std::move(fb_mmap); |
| 54 | EXPECT_TRUE(fb_mmap3.Verify()); |
| 55 | ASSERT_EQ(fb_mmap3.message().foo_int(), 3); |
| 56 | } |
| 57 | } |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 58 | |
| 59 | // Tests the ability to modify a flatbuffer mmaped from on disk in memory |
| 60 | TEST(FlatbufferMMapTest, Writeable) { |
| 61 | FlatbufferDetachedBuffer<Configuration> fb = |
| 62 | JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}"); |
| 63 | |
| 64 | const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs"); |
| 65 | WriteFlatbufferToFile(fb_path, fb); |
| 66 | |
| 67 | { |
| 68 | FlatbufferMMap<Configuration> fb_mmap(fb_path, |
| 69 | util::FileOptions::kWriteable); |
| 70 | fb_mmap.mutable_message()->mutate_foo_int(5); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | FlatbufferMMap<Configuration> fb_mmap(fb_path); |
| 75 | EXPECT_EQ(fb_mmap.message().foo_int(), 5); |
| 76 | } |
| 77 | } |
| 78 | |
Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 79 | } // namespace testing |
| 80 | } // namespace aos |