summaryrefslogtreecommitdiffstats
path: root/osi/test/config_test.cpp
blob: 70e5a2494609425d003955884ccadd30e866af5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <gtest/gtest.h>

#include "AllocationTestHarness.h"

extern "C" {
#include "config.h"
}

static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf";
static const char CONFIG_FILE_CONTENT[] =
"                                                                                    \n\
first_key=value                                                                      \n\
                                                                                     \n\
# Device ID (DID) configuration                                                      \n\
[DID]                                                                                \n\
                                                                                     \n\
# Record Number: 1, 2 or 3 - maximum of 3 records                                    \n\
recordNumber = 1                                                                     \n\
                                                                                     \n\
# Primary Record - true or false (default)                                           \n\
# There can be only one primary record                                               \n\
primaryRecord = true                                                                 \n\
                                                                                     \n\
# Vendor ID '0xFFFF' indicates no Device ID Service Record is present in the device  \n\
# 0x000F = Broadcom Corporation (default)                                            \n\
#vendorId = 0x000F                                                                   \n\
                                                                                     \n\
# Vendor ID Source                                                                   \n\
# 0x0001 = Bluetooth SIG assigned Device ID Vendor ID value (default)                \n\
# 0x0002 = USB Implementer's Forum assigned Device ID Vendor ID value                \n\
#vendorIdSource = 0x0001                                                             \n\
                                                                                     \n\
# Product ID & Product Version                                                       \n\
# Per spec DID v1.3 0xJJMN for version is interpreted as JJ.M.N                      \n\
# JJ: major version number, M: minor version number, N: sub-minor version number     \n\
# For example: 1200, v14.3.6                                                         \n\
productId = 0x1200                                                                   \n\
version = 0x1111                                                                     \n\
                                                                                     \n\
# Optional attributes                                                                \n\
#clientExecutableURL =                                                               \n\
#serviceDescription =                                                                \n\
#documentationURL =                                                                  \n\
                                                                                     \n\
# Additional optional DID records. Bluedroid supports up to 3 records.               \n\
[DID]                                                                                \n\
[DID]                                                                                \n\
version = 0x1436                                                                     \n\
";

class ConfigTest : public AllocationTestHarness {
  protected:
    virtual void SetUp() {
      AllocationTestHarness::SetUp();
      FILE *fp = fopen(CONFIG_FILE, "wt");
      fwrite(CONFIG_FILE_CONTENT, 1, sizeof(CONFIG_FILE_CONTENT), fp);
      fclose(fp);
    }
};

TEST_F(ConfigTest, config_new_empty) {
  config_t *config = config_new_empty();
  EXPECT_TRUE(config != NULL);
  config_free(config);
}

TEST_F(ConfigTest, config_new_no_file) {
  config_t *config = config_new("/meow");
  EXPECT_TRUE(config == NULL);
  config_free(config);
}

TEST_F(ConfigTest, config_new) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config != NULL);
  config_free(config);
}

TEST_F(ConfigTest, config_free_null) {
  config_free(NULL);
}

TEST_F(ConfigTest, config_has_section) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_has_section(config, "DID"));
  config_free(config);
}

TEST_F(ConfigTest, config_has_key_in_default_section) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_has_key(config, CONFIG_DEFAULT_SECTION, "first_key"));
  EXPECT_STREQ(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "meow"), "value");
  config_free(config);
}

TEST_F(ConfigTest, config_has_keys) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_has_key(config, "DID", "recordNumber"));
  EXPECT_TRUE(config_has_key(config, "DID", "primaryRecord"));
  EXPECT_TRUE(config_has_key(config, "DID", "productId"));
  EXPECT_TRUE(config_has_key(config, "DID", "version"));
  config_free(config);
}

TEST_F(ConfigTest, config_no_bad_keys) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_FALSE(config_has_key(config, "DID_BAD", "primaryRecord"));
  EXPECT_FALSE(config_has_key(config, "DID", "primaryRecord_BAD"));
  EXPECT_FALSE(config_has_key(config, CONFIG_DEFAULT_SECTION, "primaryRecord"));
  config_free(config);
}

TEST_F(ConfigTest, config_get_int_version) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_int(config, "DID", "version", 0), 0x1436);
  config_free(config);
}

TEST_F(ConfigTest, config_get_int_default) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_int(config, "DID", "primaryRecord", 123), 123);
  config_free(config);
}

TEST_F(ConfigTest, config_remove_section) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_remove_section(config, "DID"));
  EXPECT_FALSE(config_has_section(config, "DID"));
  EXPECT_FALSE(config_has_key(config, "DID", "productId"));
  config_free(config);
}

TEST_F(ConfigTest, config_remove_section_missing) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_FALSE(config_remove_section(config, "not a section"));
  config_free(config);
}

TEST_F(ConfigTest, config_remove_key) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200);
  EXPECT_TRUE(config_remove_key(config, "DID", "productId"));
  EXPECT_FALSE(config_has_key(config, "DID", "productId"));
  config_free(config);
}

TEST_F(ConfigTest, config_remove_key_missing) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200);
  EXPECT_TRUE(config_remove_key(config, "DID", "productId"));
  EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 999);
  config_free(config);
}

TEST_F(ConfigTest, config_section_begin) {
  config_t *config = config_new(CONFIG_FILE);
  const config_section_node_t *section = config_section_begin(config);
  EXPECT_TRUE(section != NULL);
  const char *section_name = config_section_name(section);
  EXPECT_TRUE(section != NULL);
  EXPECT_TRUE(!strcmp(section_name, CONFIG_DEFAULT_SECTION));
  config_free(config);
}

TEST_F(ConfigTest, config_section_next) {
  config_t *config = config_new(CONFIG_FILE);
  const config_section_node_t *section = config_section_begin(config);
  EXPECT_TRUE(section != NULL);
  section = config_section_next(section);
  EXPECT_TRUE(section != NULL);
  const char *section_name = config_section_name(section);
  EXPECT_TRUE(section != NULL);
  EXPECT_TRUE(!strcmp(section_name, "DID"));
  config_free(config);
}

TEST_F(ConfigTest, config_section_end) {
  config_t *config = config_new(CONFIG_FILE);
  const config_section_node_t * section = config_section_begin(config);
  section = config_section_next(section);
  section = config_section_next(section);
  EXPECT_EQ(section, config_section_end(config));
  config_free(config);
}

TEST_F(ConfigTest, config_save_basic) {
  config_t *config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_save(config, CONFIG_FILE));
  config_free(config);
}