blob: 333d5d7e4f66db5f726e1fdedd127be9a2b4ba38 [file] [log] [blame]
Austin Schuh977a5ed2020-12-02 23:20:04 -08001#include "aos/flatbuffers.h"
2
davidjevans8b9b52f2021-09-17 08:57:30 -07003#include "absl/strings/str_cat.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07004#include "gtest/gtest.h"
5
Austin Schuh977a5ed2020-12-02 23:20:04 -08006#include "aos/json_to_flatbuffer.h"
7#include "aos/json_to_flatbuffer_generated.h"
davidjevans8b9b52f2021-09-17 08:57:30 -07008#include "aos/testing/tmpdir.h"
Austin Schuh977a5ed2020-12-02 23:20:04 -08009
10namespace aos {
11namespace testing {
12
13// Tests that Verify works.
14TEST(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
James Kuszmaul1d5d9ec2024-01-14 17:53:23 -080026// Test that the UnpackFlatbuffer builds & works.
27TEST(FlatbufferTest, UnpackFlatbuffer) {
28 const FlatbufferDetachedBuffer<Location> fb =
29 JsonToFlatbuffer<Location>("{\"name\": \"abc\", \"frequency\": 971}");
30
31 LocationT object = UnpackFlatbuffer(&fb.message());
32 EXPECT_EQ("abc", object.name);
33 EXPECT_EQ(971, object.frequency);
34}
35
davidjevans8b9b52f2021-09-17 08:57:30 -070036// Tests the ability to map a flatbuffer on disk to memory
37TEST(FlatbufferMMapTest, Verify) {
38 FlatbufferDetachedBuffer<Configuration> fb =
39 JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
40
41 const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
42 WriteFlatbufferToFile(fb_path, fb);
43
44 FlatbufferMMap<Configuration> fb_mmap(fb_path);
45 EXPECT_TRUE(fb.Verify());
46 EXPECT_TRUE(fb_mmap.Verify());
47 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
48
49 // Verify that copying works
50 {
51 FlatbufferMMap<Configuration> fb_mmap2(fb_path);
52 fb_mmap2 = fb_mmap;
53 EXPECT_TRUE(fb_mmap.Verify());
54 EXPECT_TRUE(fb_mmap2.Verify());
55 ASSERT_EQ(fb_mmap2.message().foo_int(), 3);
56 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
57 }
58 EXPECT_TRUE(fb_mmap.Verify());
59 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
60
61 // Verify that moving works
62 {
63 FlatbufferMMap<Configuration> fb_mmap3(fb_path);
64 fb_mmap3 = std::move(fb_mmap);
65 EXPECT_TRUE(fb_mmap3.Verify());
66 ASSERT_EQ(fb_mmap3.message().foo_int(), 3);
67 }
68}
Austin Schuhe4d1a682021-10-01 15:04:50 -070069
70// Tests the ability to modify a flatbuffer mmaped from on disk in memory
71TEST(FlatbufferMMapTest, Writeable) {
72 FlatbufferDetachedBuffer<Configuration> fb =
73 JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
74
75 const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
76 WriteFlatbufferToFile(fb_path, fb);
77
78 {
79 FlatbufferMMap<Configuration> fb_mmap(fb_path,
80 util::FileOptions::kWriteable);
81 fb_mmap.mutable_message()->mutate_foo_int(5);
82 }
83
84 {
85 FlatbufferMMap<Configuration> fb_mmap(fb_path);
86 EXPECT_EQ(fb_mmap.message().foo_int(), 5);
87 }
88}
89
Austin Schuh977a5ed2020-12-02 23:20:04 -080090} // namespace testing
91} // namespace aos