• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <gtest/gtest.h>
17 
18 #include <config.h>
19 #include <filesystem>
20 
21 namespace {
22 const std::filesystem::path kConfigFile =
23     std::filesystem::temp_directory_path() / "test_config.conf";
24 const char* SIMPLE_CONFIG_FILE = kConfigFile.c_str();
25 const char SIMPLE_CONFIG[] =
26     "# Simple config file test\n\
27 STRING_VALUE=\"Hello World!\"\n\
28 #COMMENTED_OUT_VALUE=1\n\
29 NUM_VALUE=42\n\
30 BYTES_VALUE={0A:0b:0C:fF:00}\n";
31 
32 const char INVALID_CONFIG1[] =
33     "# This is an invalid config\n\
34 # Config values must contain an = sign\n\
35 TEST:1";
36 
37 const char INVALID_CONFIG2[] =
38     "# This is an invalid config\n\
39 # Byte arrays must contain at least one value\n\
40 TEST={}";
41 
42 const char INVALID_CONFIG3[] =
43     "# This is an invalid config\n\
44 # String values cannot be empty\n\
45 TEST=\"\"";
46 
47 const char INVALID_CONFIG4[] =
48     "# This is an invalid config\n\
49 # Multiple config entries with the same key\n\
50 TEST=1\n\
51 TEST=2";
52 
53 const char INVALID_CONFIG5[] =
54     "# This is an invalid config\n\
55 # Byte value width incorrect\n\
56 BYTES_VALUE={0A:0b:0C:1:00}\n";
57 }  // namespace
58 
59 class ConfigTestFromFile : public ::testing::Test {
60  protected:
SetUp()61   void SetUp() override {
62     FILE* fp = fopen(SIMPLE_CONFIG_FILE, "wt");
63     fwrite(SIMPLE_CONFIG, 1, sizeof(SIMPLE_CONFIG), fp);
64     fclose(fp);
65   }
TearDown()66   void TearDown() override {
67     std::filesystem::remove(kConfigFile);
68   }
69 };
70 
TEST(ConfigTestFromString,test_simple_config)71 TEST(ConfigTestFromString, test_simple_config) {
72   ConfigFile config;
73   config.parseFromString(SIMPLE_CONFIG);
74   EXPECT_FALSE(config.hasKey("UNKNOWN_VALUE"));
75   EXPECT_FALSE(config.hasKey("COMMENTED_OUT_VALUE"));
76   EXPECT_TRUE(config.hasKey("NUM_VALUE"));
77   EXPECT_TRUE(config.hasKey("STRING_VALUE"));
78   EXPECT_TRUE(config.hasKey("BYTES_VALUE"));
79 }
80 
TEST(ConfigTestFromString,test_simple_values)81 TEST(ConfigTestFromString, test_simple_values) {
82   ConfigFile config;
83   config.parseFromString(SIMPLE_CONFIG);
84   EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
85   EXPECT_EQ(config.getString("STRING_VALUE"), "Hello World!");
86   auto bytes = config.getBytes("BYTES_VALUE");
87   EXPECT_EQ(bytes.size(), 5u);
88   EXPECT_EQ(bytes[0], 10);
89   EXPECT_EQ(bytes[1], 11);
90   EXPECT_EQ(bytes[2], 12);
91   EXPECT_EQ(bytes[3], 255);
92   EXPECT_EQ(bytes[4], 0);
93 }
94 
TEST(ConfigTestFromString,test_invalid_configs)95 TEST(ConfigTestFromString, test_invalid_configs) {
96   ConfigFile config1;
97   EXPECT_DEATH(config1.parseFromString(INVALID_CONFIG1), "");
98   ConfigFile config2;
99   EXPECT_DEATH(config2.parseFromString(INVALID_CONFIG2), "");
100   ConfigFile config3;
101   EXPECT_DEATH(config3.parseFromString(INVALID_CONFIG3), "");
102   ConfigFile config4;
103   EXPECT_DEATH(config4.parseFromString(INVALID_CONFIG4), "");
104   ConfigFile config5;
105   EXPECT_DEATH(config5.parseFromString(INVALID_CONFIG5), "");
106 }
107 
TEST(ConfigTestFromString,test_clear)108 TEST(ConfigTestFromString, test_clear) {
109   ConfigFile config;
110   EXPECT_FALSE(config.hasKey("NUM_VALUE"));
111   config.parseFromString(SIMPLE_CONFIG);
112   EXPECT_TRUE(config.hasKey("NUM_VALUE"));
113   EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
114   config.clear();
115   EXPECT_FALSE(config.hasKey("NUM_VALUE"));
116   EXPECT_DEATH(config.getUnsigned("NUM_VALUE"), "");
117 }
118 
TEST(ConfigTestFromString,test_isEmpty)119 TEST(ConfigTestFromString, test_isEmpty) {
120   ConfigFile config;
121   EXPECT_TRUE(config.isEmpty());
122   config.parseFromString(SIMPLE_CONFIG);
123   EXPECT_FALSE(config.isEmpty());
124   config.clear();
125   EXPECT_TRUE(config.isEmpty());
126 }
127 
TEST_F(ConfigTestFromFile,test_file_based_config)128 TEST_F(ConfigTestFromFile, test_file_based_config) {
129   ConfigFile config;
130   config.parseFromFile(SIMPLE_CONFIG_FILE);
131   EXPECT_FALSE(config.hasKey("UNKNOWN_VALUE"));
132   EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
133   EXPECT_EQ(config.getString("STRING_VALUE"), "Hello World!");
134   auto bytes = config.getBytes("BYTES_VALUE");
135   EXPECT_EQ(bytes.size(), 5u);
136   EXPECT_EQ(bytes[0], 10);
137   EXPECT_EQ(bytes[1], 11);
138   EXPECT_EQ(bytes[2], 12);
139   EXPECT_EQ(bytes[3], 255);
140   EXPECT_EQ(bytes[4], 0);
141 }
142