blob: b0f1ac2971a689de17d28899cc2546b12ca79398 [file] [log] [blame]
Kun Yi4dc76482019-03-05 16:06:19 -08001#include "parse_config.hpp"
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
6using json = nlohmann::json;
7using namespace conf;
8
9TEST(ParseConfigTest, ExceptionWhenMissingRequiredFields)
10{
11 auto j = R"(
12 {
13 "blobBaseId": "/test/"
14 }
15 )"_json;
16
17 BinaryBlobConfig config;
18
19 EXPECT_THROW(parseFromConfigFile(j, config), std::exception);
20}
21
22TEST(ParseConfigTest, TestSimpleConfig)
23{
24 auto j = R"(
25 {
26 "blobBaseId": "/test/",
27 "sysFilePath": "/sys/fake/path",
28 "offsetBytes": 32,
Willy Tu7f107802023-11-06 23:05:25 -080029 "maxSizeBytes": 2,
30 "aliasBlobBaseId": "/test2/",
31 "migrateToAlias": true
Kun Yi4dc76482019-03-05 16:06:19 -080032 }
33 )"_json;
34
35 BinaryBlobConfig config;
36
37 EXPECT_NO_THROW(parseFromConfigFile(j, config));
38 EXPECT_EQ(config.blobBaseId, "/test/");
39 EXPECT_EQ(config.sysFilePath, "/sys/fake/path");
40 EXPECT_EQ(config.offsetBytes, 32);
41 EXPECT_EQ(config.maxSizeBytes, 2);
Willy Tu7f107802023-11-06 23:05:25 -080042 EXPECT_EQ(config.aliasBlobBaseId, "/test2/");
43 EXPECT_TRUE(config.migrateToAlias);
Kun Yi4dc76482019-03-05 16:06:19 -080044}
45
46TEST(ParseConfigTest, TestConfigArray)
47{
48 auto j = R"(
49 [{
50 "blobBaseId": "/test/",
51 "sysFilePath": "/sys/fake/path",
52 "offsetBytes": 32,
53 "maxSizeBytes": 32
54 },
55 {
56 "blobBaseId": "/test/",
57 "sysFilePath": "/another/path"
Willy Tu2b6683f2022-02-20 01:45:42 -080058 },
59 {
60 "blobBaseId": "/test/",
61 "sysFilePath": "/another/path",
62 "offsetBytes": 32
63 },
64 {
65 "blobBaseId": "/test/",
66 "sysFilePath": "/another/path",
67 "maxSizeBytes": 32
Kun Yi4dc76482019-03-05 16:06:19 -080068 }]
69 )"_json;
70
71 for (auto& element : j)
72 {
73 BinaryBlobConfig config;
74
75 EXPECT_NO_THROW(parseFromConfigFile(element, config));
76 EXPECT_EQ(config.blobBaseId, "/test/");
Willy Tu2b6683f2022-02-20 01:45:42 -080077 EXPECT_TRUE(config.offsetBytes == std::nullopt ||
78 *config.offsetBytes == 32);
79 EXPECT_TRUE(config.maxSizeBytes == std::nullopt ||
80 *config.maxSizeBytes == 32);
Kun Yi4dc76482019-03-05 16:06:19 -080081 }
82}