blob: c4113e99406762ef122dd0fb63784359391b392e [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080010namespace aos::testing {
Austin Schuh977a5ed2020-12-02 23:20:04 -080011
12// Tests that Verify works.
13TEST(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
James Kuszmaul1d5d9ec2024-01-14 17:53:23 -080025// Test that the UnpackFlatbuffer builds & works.
26TEST(FlatbufferTest, UnpackFlatbuffer) {
27 const FlatbufferDetachedBuffer<Location> fb =
28 JsonToFlatbuffer<Location>("{\"name\": \"abc\", \"frequency\": 971}");
29
30 LocationT object = UnpackFlatbuffer(&fb.message());
31 EXPECT_EQ("abc", object.name);
32 EXPECT_EQ(971, object.frequency);
33}
34
davidjevans8b9b52f2021-09-17 08:57:30 -070035// Tests the ability to map a flatbuffer on disk to memory
36TEST(FlatbufferMMapTest, Verify) {
37 FlatbufferDetachedBuffer<Configuration> fb =
38 JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
39
40 const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
41 WriteFlatbufferToFile(fb_path, fb);
42
43 FlatbufferMMap<Configuration> fb_mmap(fb_path);
44 EXPECT_TRUE(fb.Verify());
45 EXPECT_TRUE(fb_mmap.Verify());
46 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
47
48 // Verify that copying works
49 {
50 FlatbufferMMap<Configuration> fb_mmap2(fb_path);
51 fb_mmap2 = fb_mmap;
52 EXPECT_TRUE(fb_mmap.Verify());
53 EXPECT_TRUE(fb_mmap2.Verify());
54 ASSERT_EQ(fb_mmap2.message().foo_int(), 3);
55 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
56 }
57 EXPECT_TRUE(fb_mmap.Verify());
58 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
59
60 // Verify that moving works
61 {
62 FlatbufferMMap<Configuration> fb_mmap3(fb_path);
63 fb_mmap3 = std::move(fb_mmap);
64 EXPECT_TRUE(fb_mmap3.Verify());
65 ASSERT_EQ(fb_mmap3.message().foo_int(), 3);
66 }
67}
Austin Schuhe4d1a682021-10-01 15:04:50 -070068
69// Tests the ability to modify a flatbuffer mmaped from on disk in memory
70TEST(FlatbufferMMapTest, Writeable) {
71 FlatbufferDetachedBuffer<Configuration> fb =
72 JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
73
74 const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
75 WriteFlatbufferToFile(fb_path, fb);
76
77 {
78 FlatbufferMMap<Configuration> fb_mmap(fb_path,
79 util::FileOptions::kWriteable);
80 fb_mmap.mutable_message()->mutate_foo_int(5);
81 }
82
83 {
84 FlatbufferMMap<Configuration> fb_mmap(fb_path);
85 EXPECT_EQ(fb_mmap.message().foo_int(), 5);
86 }
87}
88
Stephan Pleinesf63bde82024-01-13 15:59:33 -080089} // namespace aos::testing