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
20 namespace {
21 const char SIMPLE_CONFIG_FILE[] = "/data/local/tmp/test_config.conf";
22 const char SIMPLE_CONFIG[] =
23 "# Simple config file test\n\
24 STRING_VALUE=\"Hello World!\"\n\
25 #COMMENTED_OUT_VALUE=1\n\
26 NUM_VALUE=42\n\
27 BYTES_VALUE={0A:0b:0C:fF:00}\n";
28
29 const char INVALID_CONFIG1[] =
30 "# This is an invalid config\n\
31 # Config values must contain an = sign\n\
32 TEST:1";
33
34 const char INVALID_CONFIG2[] =
35 "# This is an invalid config\n\
36 # Byte arrays must contain at least one value\n\
37 TEST={}";
38
39 const char INVALID_CONFIG3[] =
40 "# This is an invalid config\n\
41 # String values cannot be empty\n\
42 TEST=\"\"";
43
44 const char INVALID_CONFIG4[] =
45 "# This is an invalid config\n\
46 # Multiple config entries with the same key\n\
47 TEST=1\n\
48 TEST=2";
49
50 const char INVALID_CONFIG5[] =
51 "# This is an invalid config\n\
52 # Byte value width incorrect\n\
53 BYTES_VALUE={0A:0b:0C:1:00}\n";
54 } // namespace
55
56 class ConfigTestFromFile : public ::testing::Test {
57 protected:
SetUp()58 void SetUp() override {
59 FILE* fp = fopen(SIMPLE_CONFIG_FILE, "wt");
60 fwrite(SIMPLE_CONFIG, 1, sizeof(SIMPLE_CONFIG), fp);
61 fclose(fp);
62 }
63 };
64
TEST(ConfigTestFromString,test_simple_config)65 TEST(ConfigTestFromString, test_simple_config) {
66 ConfigFile config;
67 config.parseFromString(SIMPLE_CONFIG);
68 EXPECT_FALSE(config.hasKey("UNKNOWN_VALUE"));
69 EXPECT_FALSE(config.hasKey("COMMENTED_OUT_VALUE"));
70 EXPECT_TRUE(config.hasKey("NUM_VALUE"));
71 EXPECT_TRUE(config.hasKey("STRING_VALUE"));
72 EXPECT_TRUE(config.hasKey("BYTES_VALUE"));
73 }
74
TEST(ConfigTestFromString,test_simple_values)75 TEST(ConfigTestFromString, test_simple_values) {
76 ConfigFile config;
77 config.parseFromString(SIMPLE_CONFIG);
78 EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
79 EXPECT_EQ(config.getString("STRING_VALUE"), "Hello World!");
80 auto bytes = config.getBytes("BYTES_VALUE");
81 EXPECT_EQ(bytes.size(), 5u);
82 EXPECT_EQ(bytes[0], 10);
83 EXPECT_EQ(bytes[1], 11);
84 EXPECT_EQ(bytes[2], 12);
85 EXPECT_EQ(bytes[3], 255);
86 EXPECT_EQ(bytes[4], 0);
87 }
88
TEST(ConfigTestFromString,test_invalid_configs)89 TEST(ConfigTestFromString, test_invalid_configs) {
90 ConfigFile config1;
91 EXPECT_DEATH(config1.parseFromString(INVALID_CONFIG1), "");
92 ConfigFile config2;
93 EXPECT_DEATH(config2.parseFromString(INVALID_CONFIG2), "");
94 ConfigFile config3;
95 EXPECT_DEATH(config3.parseFromString(INVALID_CONFIG3), "");
96 ConfigFile config4;
97 EXPECT_DEATH(config4.parseFromString(INVALID_CONFIG4), "");
98 ConfigFile config5;
99 EXPECT_DEATH(config5.parseFromString(INVALID_CONFIG5), "");
100 }
101
TEST(ConfigTestFromString,test_clear)102 TEST(ConfigTestFromString, test_clear) {
103 ConfigFile config;
104 EXPECT_FALSE(config.hasKey("NUM_VALUE"));
105 config.parseFromString(SIMPLE_CONFIG);
106 EXPECT_TRUE(config.hasKey("NUM_VALUE"));
107 EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
108 config.clear();
109 EXPECT_FALSE(config.hasKey("NUM_VALUE"));
110 EXPECT_DEATH(config.getUnsigned("NUM_VALUE"), "");
111 }
112
TEST(ConfigTestFromString,test_isEmpty)113 TEST(ConfigTestFromString, test_isEmpty) {
114 ConfigFile config;
115 EXPECT_TRUE(config.isEmpty());
116 config.parseFromString(SIMPLE_CONFIG);
117 EXPECT_FALSE(config.isEmpty());
118 config.clear();
119 EXPECT_TRUE(config.isEmpty());
120 }
121
TEST_F(ConfigTestFromFile,test_file_based_config)122 TEST_F(ConfigTestFromFile, test_file_based_config) {
123 ConfigFile config;
124 config.parseFromFile(SIMPLE_CONFIG_FILE);
125 EXPECT_FALSE(config.hasKey("UNKNOWN_VALUE"));
126 EXPECT_EQ(config.getUnsigned("NUM_VALUE"), 42u);
127 EXPECT_EQ(config.getString("STRING_VALUE"), "Hello World!");
128 auto bytes = config.getBytes("BYTES_VALUE");
129 EXPECT_EQ(bytes.size(), 5u);
130 EXPECT_EQ(bytes[0], 10);
131 EXPECT_EQ(bytes[1], 11);
132 EXPECT_EQ(bytes[2], 12);
133 EXPECT_EQ(bytes[3], 255);
134 EXPECT_EQ(bytes[4], 0);
135 }
136