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