1 #include <gtest/gtest.h>
3 #include "AllocationTestHarness.h"
6 #include "osi/include/config.h"
9 static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf";
10 static const char CONFIG_FILE_CONTENT[] =
14 # Device ID (DID) configuration \n\
17 # Record Number: 1, 2 or 3 - maximum of 3 records \n\
20 # Primary Record - true or false (default) \n\
21 # There can be only one primary record \n\
22 primaryRecord = true \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\
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\
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\
40 # Optional attributes \n\
41 #clientExecutableURL = \n\
42 #serviceDescription = \n\
43 #documentationURL = \n\
45 # Additional optional DID records. Bluedroid supports up to 3 records. \n\
51 class ConfigTest : public AllocationTestHarness {
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);
61 TEST_F(ConfigTest, config_new_empty) {
62 config_t *config = config_new_empty();
63 EXPECT_TRUE(config != NULL);
67 TEST_F(ConfigTest, config_new_no_file) {
68 config_t *config = config_new("/meow");
69 EXPECT_TRUE(config == NULL);
73 TEST_F(ConfigTest, config_new) {
74 config_t *config = config_new(CONFIG_FILE);
75 EXPECT_TRUE(config != NULL);
79 TEST_F(ConfigTest, config_free_null) {
83 TEST_F(ConfigTest, config_new_clone) {
84 config_t *config = config_new(CONFIG_FILE);
85 config_t *clone = config_new_clone(config);
87 config_set_string(clone, CONFIG_DEFAULT_SECTION, "first_key", "not_value");
89 EXPECT_STRNE(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "one"),
90 config_get_string(clone, CONFIG_DEFAULT_SECTION, "first_key", "one"));
96 TEST_F(ConfigTest, config_has_section) {
97 config_t *config = config_new(CONFIG_FILE);
98 EXPECT_TRUE(config_has_section(config, "DID"));
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");
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"));
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"));
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);
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);
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"));
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"));
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"));
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);
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));
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"));
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));
199 TEST_F(ConfigTest, config_save_basic) {
200 config_t *config = config_new(CONFIG_FILE);
201 EXPECT_TRUE(config_save(config, CONFIG_FILE));