1 #include <gtest/gtest.h>
2
3 #include "AllocationTestHarness.h"
4
5 extern "C" {
6 #include "osi/include/config.h"
7 }
8
9 static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf";
10 static const char CONFIG_FILE_CONTENT[] =
11 " \n\
12 first_key=value \n\
13 \n\
14 # Device ID (DID) configuration \n\
15 [DID] \n\
16 \n\
17 # Record Number: 1, 2 or 3 - maximum of 3 records \n\
18 recordNumber = 1 \n\
19 \n\
20 # Primary Record - true or false (default) \n\
21 # There can be only one primary record \n\
22 primaryRecord = true \n\
23 \n\
24 # Vendor ID '0xFFFF' indicates no Device ID Service Record is present in the device \n\
25 # 0x000F = Broadcom Corporation (default) \n\
26 #vendorId = 0x000F \n\
27 \n\
28 # Vendor ID Source \n\
29 # 0x0001 = Bluetooth SIG assigned Device ID Vendor ID value (default) \n\
30 # 0x0002 = USB Implementer's Forum assigned Device ID Vendor ID value \n\
31 #vendorIdSource = 0x0001 \n\
32 \n\
33 # Product ID & Product Version \n\
34 # Per spec DID v1.3 0xJJMN for version is interpreted as JJ.M.N \n\
35 # JJ: major version number, M: minor version number, N: sub-minor version number \n\
36 # For example: 1200, v14.3.6 \n\
37 productId = 0x1200 \n\
38 version = 0x1111 \n\
39 \n\
40 # Optional attributes \n\
41 #clientExecutableURL = \n\
42 #serviceDescription = \n\
43 #documentationURL = \n\
44 \n\
45 # Additional optional DID records. Bluedroid supports up to 3 records. \n\
46 [DID] \n\
47 [DID] \n\
48 version = 0x1436 \n\
49 ";
50
51 class ConfigTest : public AllocationTestHarness {
52 protected:
SetUp()53 virtual void SetUp() {
54 AllocationTestHarness::SetUp();
55 FILE *fp = fopen(CONFIG_FILE, "wt");
56 fwrite(CONFIG_FILE_CONTENT, 1, sizeof(CONFIG_FILE_CONTENT), fp);
57 fclose(fp);
58 }
59 };
60
TEST_F(ConfigTest,config_new_empty)61 TEST_F(ConfigTest, config_new_empty) {
62 config_t *config = config_new_empty();
63 EXPECT_TRUE(config != NULL);
64 config_free(config);
65 }
66
TEST_F(ConfigTest,config_new_no_file)67 TEST_F(ConfigTest, config_new_no_file) {
68 config_t *config = config_new("/meow");
69 EXPECT_TRUE(config == NULL);
70 config_free(config);
71 }
72
TEST_F(ConfigTest,config_new)73 TEST_F(ConfigTest, config_new) {
74 config_t *config = config_new(CONFIG_FILE);
75 EXPECT_TRUE(config != NULL);
76 config_free(config);
77 }
78
TEST_F(ConfigTest,config_free_null)79 TEST_F(ConfigTest, config_free_null) {
80 config_free(NULL);
81 }
82
TEST_F(ConfigTest,config_new_clone)83 TEST_F(ConfigTest, config_new_clone) {
84 config_t *config = config_new(CONFIG_FILE);
85 config_t *clone = config_new_clone(config);
86
87 config_set_string(clone, CONFIG_DEFAULT_SECTION, "first_key", "not_value");
88
89 EXPECT_STRNE(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "one"),
90 config_get_string(clone, CONFIG_DEFAULT_SECTION, "first_key", "one"));
91
92 config_free(config);
93 config_free(clone);
94 }
95
TEST_F(ConfigTest,config_has_section)96 TEST_F(ConfigTest, config_has_section) {
97 config_t *config = config_new(CONFIG_FILE);
98 EXPECT_TRUE(config_has_section(config, "DID"));
99 config_free(config);
100 }
101
TEST_F(ConfigTest,config_has_key_in_default_section)102 TEST_F(ConfigTest, config_has_key_in_default_section) {
103 config_t *config = config_new(CONFIG_FILE);
104 EXPECT_TRUE(config_has_key(config, CONFIG_DEFAULT_SECTION, "first_key"));
105 EXPECT_STREQ(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "meow"), "value");
106 config_free(config);
107 }
108
TEST_F(ConfigTest,config_has_keys)109 TEST_F(ConfigTest, config_has_keys) {
110 config_t *config = config_new(CONFIG_FILE);
111 EXPECT_TRUE(config_has_key(config, "DID", "recordNumber"));
112 EXPECT_TRUE(config_has_key(config, "DID", "primaryRecord"));
113 EXPECT_TRUE(config_has_key(config, "DID", "productId"));
114 EXPECT_TRUE(config_has_key(config, "DID", "version"));
115 config_free(config);
116 }
117
TEST_F(ConfigTest,config_no_bad_keys)118 TEST_F(ConfigTest, config_no_bad_keys) {
119 config_t *config = config_new(CONFIG_FILE);
120 EXPECT_FALSE(config_has_key(config, "DID_BAD", "primaryRecord"));
121 EXPECT_FALSE(config_has_key(config, "DID", "primaryRecord_BAD"));
122 EXPECT_FALSE(config_has_key(config, CONFIG_DEFAULT_SECTION, "primaryRecord"));
123 config_free(config);
124 }
125
TEST_F(ConfigTest,config_get_int_version)126 TEST_F(ConfigTest, config_get_int_version) {
127 config_t *config = config_new(CONFIG_FILE);
128 EXPECT_EQ(config_get_int(config, "DID", "version", 0), 0x1436);
129 config_free(config);
130 }
131
TEST_F(ConfigTest,config_get_int_default)132 TEST_F(ConfigTest, config_get_int_default) {
133 config_t *config = config_new(CONFIG_FILE);
134 EXPECT_EQ(config_get_int(config, "DID", "primaryRecord", 123), 123);
135 config_free(config);
136 }
137
TEST_F(ConfigTest,config_remove_section)138 TEST_F(ConfigTest, config_remove_section) {
139 config_t *config = config_new(CONFIG_FILE);
140 EXPECT_TRUE(config_remove_section(config, "DID"));
141 EXPECT_FALSE(config_has_section(config, "DID"));
142 EXPECT_FALSE(config_has_key(config, "DID", "productId"));
143 config_free(config);
144 }
145
TEST_F(ConfigTest,config_remove_section_missing)146 TEST_F(ConfigTest, config_remove_section_missing) {
147 config_t *config = config_new(CONFIG_FILE);
148 EXPECT_FALSE(config_remove_section(config, "not a section"));
149 config_free(config);
150 }
151
TEST_F(ConfigTest,config_remove_key)152 TEST_F(ConfigTest, config_remove_key) {
153 config_t *config = config_new(CONFIG_FILE);
154 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200);
155 EXPECT_TRUE(config_remove_key(config, "DID", "productId"));
156 EXPECT_FALSE(config_has_key(config, "DID", "productId"));
157 config_free(config);
158 }
159
TEST_F(ConfigTest,config_remove_key_missing)160 TEST_F(ConfigTest, config_remove_key_missing) {
161 config_t *config = config_new(CONFIG_FILE);
162 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200);
163 EXPECT_TRUE(config_remove_key(config, "DID", "productId"));
164 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 999);
165 config_free(config);
166 }
167
TEST_F(ConfigTest,config_section_begin)168 TEST_F(ConfigTest, config_section_begin) {
169 config_t *config = config_new(CONFIG_FILE);
170 const config_section_node_t *section = config_section_begin(config);
171 EXPECT_TRUE(section != NULL);
172 const char *section_name = config_section_name(section);
173 EXPECT_TRUE(section != NULL);
174 EXPECT_TRUE(!strcmp(section_name, CONFIG_DEFAULT_SECTION));
175 config_free(config);
176 }
177
TEST_F(ConfigTest,config_section_next)178 TEST_F(ConfigTest, config_section_next) {
179 config_t *config = config_new(CONFIG_FILE);
180 const config_section_node_t *section = config_section_begin(config);
181 EXPECT_TRUE(section != NULL);
182 section = config_section_next(section);
183 EXPECT_TRUE(section != NULL);
184 const char *section_name = config_section_name(section);
185 EXPECT_TRUE(section != NULL);
186 EXPECT_TRUE(!strcmp(section_name, "DID"));
187 config_free(config);
188 }
189
TEST_F(ConfigTest,config_section_end)190 TEST_F(ConfigTest, config_section_end) {
191 config_t *config = config_new(CONFIG_FILE);
192 const config_section_node_t * section = config_section_begin(config);
193 section = config_section_next(section);
194 section = config_section_next(section);
195 EXPECT_EQ(section, config_section_end(config));
196 config_free(config);
197 }
198
TEST_F(ConfigTest,config_save_basic)199 TEST_F(ConfigTest, config_save_basic) {
200 config_t *config = config_new(CONFIG_FILE);
201 EXPECT_TRUE(config_save(config, CONFIG_FILE));
202 config_free(config);
203 }
204