1 #include "bej_dictionary.h"
2 
3 #include <array>
4 #include <string_view>
5 #include <tuple>
6 
7 #include <gmock/gmock-matchers.h>
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10 
11 namespace libbej
12 {
13 
14 /**
15  * @brief A valid dictionary.
16  */
17 constexpr std::array<uint8_t, 279> dummySimpleDict{
18     {0x0,  0x0,  0xc,  0x0,  0x0,  0xf0, 0xf0, 0xf1, 0x17, 0x1,  0x0,  0x0,
19      0x0,  0x0,  0x0,  0x16, 0x0,  0x5,  0x0,  0xc,  0x84, 0x0,  0x14, 0x0,
20      0x0,  0x48, 0x0,  0x1,  0x0,  0x13, 0x90, 0x0,  0x56, 0x1,  0x0,  0x0,
21      0x0,  0x0,  0x0,  0x3,  0xa3, 0x0,  0x74, 0x2,  0x0,  0x0,  0x0,  0x0,
22      0x0,  0x16, 0xa6, 0x0,  0x34, 0x3,  0x0,  0x0,  0x0,  0x0,  0x0,  0x16,
23      0xbc, 0x0,  0x64, 0x4,  0x0,  0x0,  0x0,  0x0,  0x0,  0x13, 0xd2, 0x0,
24      0x0,  0x0,  0x0,  0x52, 0x0,  0x2,  0x0,  0x0,  0x0,  0x0,  0x74, 0x0,
25      0x0,  0x0,  0x0,  0x0,  0x0,  0xf,  0xe5, 0x0,  0x46, 0x1,  0x0,  0x66,
26      0x0,  0x3,  0x0,  0xb,  0xf4, 0x0,  0x50, 0x0,  0x0,  0x0,  0x0,  0x0,
27      0x0,  0x9,  0xff, 0x0,  0x50, 0x1,  0x0,  0x0,  0x0,  0x0,  0x0,  0x7,
28      0x8,  0x1,  0x50, 0x2,  0x0,  0x0,  0x0,  0x0,  0x0,  0x7,  0xf,  0x1,
29      0x44, 0x75, 0x6d, 0x6d, 0x79, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x0,
30      0x43, 0x68, 0x69, 0x6c, 0x64, 0x41, 0x72, 0x72, 0x61, 0x79, 0x50, 0x72,
31      0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x0,  0x49, 0x64, 0x0,  0x53, 0x61,
32      0x6d, 0x70, 0x6c, 0x65, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x50,
33      0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x0,  0x53, 0x61, 0x6d, 0x70,
34      0x6c, 0x65, 0x49, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x50, 0x72, 0x6f,
35      0x70, 0x65, 0x72, 0x74, 0x79, 0x0,  0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65,
36      0x52, 0x65, 0x61, 0x6c, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79,
37      0x0,  0x41, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x42, 0x6f, 0x6f, 0x6c,
38      0x65, 0x61, 0x6e, 0x0,  0x4c, 0x69, 0x6e, 0x6b, 0x53, 0x74, 0x61, 0x74,
39      0x75, 0x73, 0x0,  0x4c, 0x69, 0x6e, 0x6b, 0x44, 0x6f, 0x77, 0x6e, 0x0,
40      0x4c, 0x69, 0x6e, 0x6b, 0x55, 0x70, 0x0,  0x4e, 0x6f, 0x4c, 0x69, 0x6e,
41      0x6b, 0x0,  0x0}};
42 
43 /**
44  * @brief Property names and sequence numbers in dummySimpleDict.
45  * Order here is the same as the order in the dummySimpleDict.
46  */
47 constexpr std::array<std::tuple<std::string_view, int>, 12> propertyNameSeq{{
48     {"DummySimple", 0},
49     {"ChildArrayProperty", 0},
50     {"Id", 1},
51     {"SampleEnabledProperty", 2},
52     {"SampleIntegerProperty", 3},
53     {"SampleRealProperty", 4},
54     {"", 0},
55     {"AnotherBoolean", 0},
56     {"LinkStatus", 1},
57     {"LinkDown", 0},
58     {"LinkUp", 1},
59     {"NoLink", 2},
60 }};
61 
TEST(BejDictionaryTest,PropertyHeadOffsetTest)62 TEST(BejDictionaryTest, PropertyHeadOffsetTest)
63 {
64     EXPECT_THAT(bejDictGetPropertyHeadOffset(), sizeof(BejDictionaryHeader));
65 }
66 
TEST(BejDictionaryTest,AnnotationPropertyHeadOffsetTest)67 TEST(BejDictionaryTest, AnnotationPropertyHeadOffsetTest)
68 {
69     EXPECT_THAT(bejDictGetFirstAnnotatedPropertyOffset(),
70                 sizeof(BejDictionaryHeader) + sizeof(BejDictionaryProperty));
71 }
72 
TEST(BejDictionaryTest,ValidPropertyTest)73 TEST(BejDictionaryTest, ValidPropertyTest)
74 {
75     const struct BejDictionaryHeader* header =
76         (const struct BejDictionaryHeader*)dummySimpleDict.data();
77     uint16_t propHead = bejDictGetPropertyHeadOffset();
78     // Read each property in the dictionary and verify that the property name is
79     // correct.
80     for (uint16_t index = 0; index < header->entryCount; ++index)
81     {
82         uint16_t offset = propHead + sizeof(BejDictionaryProperty) * index;
83         const struct BejDictionaryProperty* property;
84         EXPECT_THAT(bejDictGetProperty(dummySimpleDict.data(), offset,
85                                        std::get<1>(propertyNameSeq[index]),
86                                        &property),
87                     0);
88         EXPECT_THAT(
89             bejDictGetPropertyName(dummySimpleDict.data(), property->nameOffset,
90                                    property->nameLength),
91             std::get<0>(propertyNameSeq[index]));
92     }
93 }
94 
TEST(BejDictionaryTest,invalidPropertyOffsetTest)95 TEST(BejDictionaryTest, invalidPropertyOffsetTest)
96 {
97     const struct BejDictionaryProperty* property;
98     EXPECT_THAT(bejDictGetProperty(dummySimpleDict.data(), /*offset=*/0,
99                                    /*sequenceNumber=*/0, &property),
100                 bejErrorInvalidPropertyOffset);
101     uint16_t propHead = bejDictGetPropertyHeadOffset();
102     EXPECT_THAT(bejDictGetProperty(dummySimpleDict.data(), propHead + 1,
103                                    /*sequenceNumber=*/0, &property),
104                 bejErrorInvalidPropertyOffset);
105 }
106 
107 } // namespace libbej
108