xref: /openbmc/entity-manager/test/test_entity-manager.cpp (revision 9d2ef081ee572b506d1b17308979809c79fdc4ef)
1 #include "Utils.hpp"
2 
3 #include <boost/container/flat_map.hpp>
4 #include <nlohmann/json.hpp>
5 
6 #include <regex>
7 #include <string>
8 #include <variant>
9 
10 #include "gtest/gtest.h"
11 
12 using namespace std::string_literals;
13 
14 TEST(TemplateCharReplace, replaceOneInt)
15 {
16     nlohmann::json j = {{"foo", "$bus"}};
17     auto it = j.begin();
18     boost::container::flat_map<std::string, BasicVariantType> data;
19     data["BUS"] = 23;
20 
21     templateCharReplace(it, data, 0);
22 
23     nlohmann::json expected = 23;
24     EXPECT_EQ(expected, j["foo"]);
25 }
26 
27 TEST(TemplateCharReplace, replaceOneStr)
28 {
29     nlohmann::json j = {{"foo", "$TEST"}};
30     auto it = j.begin();
31     boost::container::flat_map<std::string, BasicVariantType> data;
32     data["TEST"] = std::string("Test");
33 
34     templateCharReplace(it, data, 0);
35 
36     nlohmann::json expected = "Test";
37     EXPECT_EQ(expected, j["foo"]);
38 }
39 
40 TEST(TemplateCharReplace, replaceSecondStr)
41 {
42     nlohmann::json j = {{"foo", "the $TEST"}};
43     auto it = j.begin();
44     boost::container::flat_map<std::string, BasicVariantType> data;
45     data["TEST"] = std::string("Test");
46 
47     templateCharReplace(it, data, 0);
48 
49     nlohmann::json expected = "the Test";
50     EXPECT_EQ(expected, j["foo"]);
51 }
52 
53 TEST(TemplateCharReplace, replaceMiddleStr)
54 {
55     nlohmann::json j = {{"foo", "the $TEST worked"}};
56     auto it = j.begin();
57     boost::container::flat_map<std::string, BasicVariantType> data;
58     data["TEST"] = std::string("Test");
59 
60     templateCharReplace(it, data, 0);
61 
62     nlohmann::json expected = "the Test worked";
63     EXPECT_EQ(expected, j["foo"]);
64 }
65 
66 TEST(TemplateCharReplace, replaceLastStr)
67 {
68     nlohmann::json j = {{"foo", "the Test $TEST"}};
69     auto it = j.begin();
70     boost::container::flat_map<std::string, BasicVariantType> data;
71     data["TEST"] = 23;
72 
73     templateCharReplace(it, data, 0);
74 
75     nlohmann::json expected = "the Test 23";
76     EXPECT_EQ(expected, j["foo"]);
77 }
78 
79 TEST(TemplateCharReplace, increment)
80 {
81     nlohmann::json j = {{"foo", "3 plus 1 equals $TEST + 1"}};
82     auto it = j.begin();
83     boost::container::flat_map<std::string, BasicVariantType> data;
84     data["TEST"] = 3;
85 
86     templateCharReplace(it, data, 0);
87 
88     nlohmann::json expected = "3 plus 1 equals 4";
89     EXPECT_EQ(expected, j["foo"]);
90 }
91 
92 TEST(TemplateCharReplace, decrement)
93 {
94     nlohmann::json j = {{"foo", "3 minus 1 equals $TEST - 1 !"}};
95     auto it = j.begin();
96     boost::container::flat_map<std::string, BasicVariantType> data;
97     data["TEST"] = 3;
98 
99     templateCharReplace(it, data, 0);
100 
101     nlohmann::json expected = "3 minus 1 equals 2 !";
102     EXPECT_EQ(expected, j["foo"]);
103 }
104 
105 TEST(TemplateCharReplace, modulus)
106 {
107     nlohmann::json j = {{"foo", "3 mod 2 equals $TEST % 2"}};
108     auto it = j.begin();
109     boost::container::flat_map<std::string, BasicVariantType> data;
110     data["TEST"] = 3;
111 
112     templateCharReplace(it, data, 0);
113 
114     nlohmann::json expected = "3 mod 2 equals 1";
115     EXPECT_EQ(expected, j["foo"]);
116 }
117 
118 TEST(TemplateCharReplace, multiply)
119 {
120     nlohmann::json j = {{"foo", "3 * 2 equals $TEST * 2"}};
121     auto it = j.begin();
122     boost::container::flat_map<std::string, BasicVariantType> data;
123     data["TEST"] = 3;
124 
125     templateCharReplace(it, data, 0);
126 
127     nlohmann::json expected = "3 * 2 equals 6";
128     EXPECT_EQ(expected, j["foo"]);
129 }
130 
131 TEST(TemplateCharReplace, divide)
132 {
133     nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2"}};
134     auto it = j.begin();
135     boost::container::flat_map<std::string, BasicVariantType> data;
136     data["TEST"] = 4;
137 
138     templateCharReplace(it, data, 0);
139 
140     nlohmann::json expected = "4 / 2 equals 2";
141     EXPECT_EQ(expected, j["foo"]);
142 }
143 
144 TEST(TemplateCharReplace, multiMath)
145 {
146     nlohmann::json j = {{"foo", "4 * 2 % 6 equals $TEST * 2 % 6"}};
147     auto it = j.begin();
148     boost::container::flat_map<std::string, BasicVariantType> data;
149     data["TEST"] = 4;
150 
151     templateCharReplace(it, data, 0);
152 
153     nlohmann::json expected = "4 * 2 % 6 equals 2";
154     EXPECT_EQ(expected, j["foo"]);
155 }
156 
157 TEST(TemplateCharReplace, twoReplacements)
158 {
159     nlohmann::json j = {{"foo", "$FOO $BAR"}};
160     auto it = j.begin();
161     boost::container::flat_map<std::string, BasicVariantType> data;
162     data["FOO"] = std::string("foo");
163     data["BAR"] = std::string("bar");
164 
165     templateCharReplace(it, data, 0);
166 
167     nlohmann::json expected = "foo bar";
168     EXPECT_EQ(expected, j["foo"]);
169 }
170 
171 TEST(TemplateCharReplace, twoReplacementsWithMath)
172 {
173     nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2 $BAR"}};
174     auto it = j.begin();
175     boost::container::flat_map<std::string, BasicVariantType> data;
176     data["TEST"] = 4;
177     data["BAR"] = std::string("bar");
178 
179     templateCharReplace(it, data, 0);
180 
181     nlohmann::json expected = "4 / 2 equals 2 bar";
182     EXPECT_EQ(expected, j["foo"]);
183 }
184 
185 TEST(TemplateCharReplace, hexAndWrongCase)
186 {
187     nlohmann::json j = {{"Address", "0x54"},
188                         {"Bus", 15},
189                         {"Name", "$bus sensor 0"},
190                         {"Type", "SomeType"}};
191 
192     boost::container::flat_map<std::string, BasicVariantType> data;
193     data["BUS"] = 15;
194 
195     for (auto it = j.begin(); it != j.end(); it++)
196     {
197         templateCharReplace(it, data, 0);
198     }
199     nlohmann::json expected = {{"Address", 84},
200                                {"Bus", 15},
201                                {"Name", "15 sensor 0"},
202                                {"Type", "SomeType"}};
203     EXPECT_EQ(expected, j);
204 }
205 
206 TEST(TemplateCharReplace, replaceSecondAsInt)
207 {
208     nlohmann::json j = {{"foo", "twelve is $TEST"}};
209     auto it = j.begin();
210     boost::container::flat_map<std::string, BasicVariantType> data;
211     data["test"] = 12;
212 
213     templateCharReplace(it, data, 0);
214 
215     nlohmann::json expected = "twelve is 12";
216     EXPECT_EQ(expected, j["foo"]);
217 }
218 
219 TEST(TemplateCharReplace, singleHex)
220 {
221     nlohmann::json j = {{"foo", "0x54"}};
222     auto it = j.begin();
223     boost::container::flat_map<std::string, BasicVariantType> data;
224 
225     templateCharReplace(it, data, 0);
226 
227     nlohmann::json expected = 84;
228     EXPECT_EQ(expected, j["foo"]);
229 }
230 
231 TEST(MatchProbe, stringEqString)
232 {
233     nlohmann::json j = R"("foo")"_json;
234     BasicVariantType v = "foo"s;
235     EXPECT_TRUE(matchProbe(j, v));
236 }
237 
238 TEST(MatchProbe, stringRegexEqString)
239 {
240     nlohmann::json j = R"("foo*")"_json;
241     BasicVariantType v = "foobar"s;
242     EXPECT_TRUE(matchProbe(j, v));
243 }
244 
245 TEST(MatchProbe, stringNeqString)
246 {
247     nlohmann::json j = R"("foobar")"_json;
248     BasicVariantType v = "foo"s;
249     EXPECT_FALSE(matchProbe(j, v));
250 }
251 
252 TEST(MatchProbe, stringRegexError)
253 {
254     nlohmann::json j = R"("foo[")"_json;
255     BasicVariantType v = "foobar"s;
256     EXPECT_THROW(matchProbe(j, v), std::regex_error);
257 }
258 
259 TEST(MatchProbe, stringZeroEqFalse)
260 {
261     nlohmann::json j = R"("0")"_json;
262     BasicVariantType v = false;
263     EXPECT_TRUE(matchProbe(j, v));
264 }
265 
266 TEST(MatchProbe, stringOneEqTrue)
267 {
268     nlohmann::json j = R"("1")"_json;
269     BasicVariantType v = true;
270     EXPECT_TRUE(matchProbe(j, v));
271 }
272 
273 TEST(MatchProbe, stringElevenNeqTrue)
274 {
275     nlohmann::json j = R"("11")"_json;
276     BasicVariantType v = true;
277     EXPECT_FALSE(matchProbe(j, v));
278 }
279 
280 TEST(MatchProbe, stringFalseNeqFalse)
281 {
282     nlohmann::json j = R"("false")"_json;
283     BasicVariantType v = false;
284     EXPECT_FALSE(matchProbe(j, v));
285 }
286 
287 TEST(MatchProbe, stringTrueNeqTrue)
288 {
289     nlohmann::json j = R"("true")"_json;
290     BasicVariantType v = true;
291     EXPECT_FALSE(matchProbe(j, v));
292 }
293 
294 TEST(MatchProbe, stringFalseNeqTrue)
295 {
296     nlohmann::json j = R"("false")"_json;
297     BasicVariantType v = true;
298     EXPECT_FALSE(matchProbe(j, v));
299 }
300 
301 TEST(MatchProbe, stringEqUint8)
302 {
303     nlohmann::json j = R"("255")"_json;
304     BasicVariantType v = uint8_t(255);
305     EXPECT_TRUE(matchProbe(j, v));
306 }
307 
308 TEST(MatchProbe, stringNeqUint8Overflow)
309 {
310     nlohmann::json j = R"("65535")"_json;
311     BasicVariantType v = uint8_t(255);
312     EXPECT_FALSE(matchProbe(j, v));
313 }
314 
315 TEST(MatchProbe, stringFalseNeqUint8Zero)
316 {
317     nlohmann::json j = R"("false")"_json;
318     BasicVariantType v = uint8_t(0);
319     EXPECT_FALSE(matchProbe(j, v));
320 }
321 
322 TEST(MatchProbe, stringTrueNeqUint8Zero)
323 {
324     nlohmann::json j = R"("true")"_json;
325     BasicVariantType v = uint8_t(1);
326     EXPECT_FALSE(matchProbe(j, v));
327 }
328 
329 TEST(MatchProbe, stringEqUint32)
330 {
331     nlohmann::json j = R"("11")"_json;
332     BasicVariantType v = uint32_t(11);
333     EXPECT_TRUE(matchProbe(j, v));
334 }
335 
336 TEST(MatchProbe, stringNeqUint32)
337 {
338     nlohmann::json j = R"("12")"_json;
339     BasicVariantType v = uint32_t(11);
340     EXPECT_FALSE(matchProbe(j, v));
341 }
342 
343 TEST(MatchProbe, stringEqInt32)
344 {
345     nlohmann::json j = R"("-11")"_json;
346     BasicVariantType v = int32_t(-11);
347     EXPECT_TRUE(matchProbe(j, v));
348 }
349 
350 TEST(MatchProbe, stringNeqInt32)
351 {
352     nlohmann::json j = R"("-12")"_json;
353     BasicVariantType v = int32_t(-11);
354     EXPECT_FALSE(matchProbe(j, v));
355 }
356 
357 TEST(MatchProbe, stringRegexEqInt32)
358 {
359     nlohmann::json j = R"("1*4")"_json;
360     BasicVariantType v = int32_t(124);
361     EXPECT_TRUE(matchProbe(j, v));
362 }
363 
364 TEST(MatchProbe, stringNeqUint64)
365 {
366     nlohmann::json j = R"("foo")"_json;
367     BasicVariantType v = uint64_t(65535);
368     EXPECT_FALSE(matchProbe(j, v));
369 }
370 
371 TEST(MatchProbe, stringEqDouble)
372 {
373     nlohmann::json j = R"("123.4")"_json;
374     BasicVariantType v = double(123.4);
375     EXPECT_TRUE(matchProbe(j, v));
376 }
377 
378 TEST(MatchProbe, stringNeqDouble)
379 {
380     nlohmann::json j = R"("-123.4")"_json;
381     BasicVariantType v = double(123.4);
382     EXPECT_FALSE(matchProbe(j, v));
383 }
384 
385 TEST(MatchProbe, stringNeqEmpty)
386 {
387     nlohmann::json j = R"("-123.4")"_json;
388     BasicVariantType v;
389     EXPECT_FALSE(matchProbe(j, v));
390 }
391