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