1#include <gtest/gtest.h>
2
3extern "C" {
4#include "config.h"
5}
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 ::testing::Test {
50  protected:
51    virtual void SetUp() {
52      FILE *fp = fopen(CONFIG_FILE, "wt");
53      fwrite(CONFIG_FILE_CONTENT, 1, sizeof(CONFIG_FILE_CONTENT), fp);
54      fclose(fp);
55    }
56};
57
58TEST_F(ConfigTest, config_new_no_file) {
59  config_t *config = config_new("/meow");
60  EXPECT_TRUE(config == NULL);
61}
62
63TEST_F(ConfigTest, config_new) {
64  config_t *config = config_new(CONFIG_FILE);
65  EXPECT_TRUE(config != NULL);
66  config_free(config);
67}
68
69TEST_F(ConfigTest, config_free_null) {
70  config_free(NULL);
71}
72
73TEST_F(ConfigTest, config_has_section) {
74  config_t *config = config_new(CONFIG_FILE);
75  EXPECT_TRUE(config_has_section(config, "DID"));
76  config_free(config);
77}
78
79TEST_F(ConfigTest, config_has_key_in_default_section) {
80  config_t *config = config_new(CONFIG_FILE);
81  EXPECT_TRUE(config_has_key(config, CONFIG_DEFAULT_SECTION, "first_key"));
82  EXPECT_STREQ(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "meow"), "value");
83  config_free(config);
84}
85
86TEST_F(ConfigTest, config_has_keys) {
87  config_t *config = config_new(CONFIG_FILE);
88  EXPECT_TRUE(config_has_key(config, "DID", "recordNumber"));
89  EXPECT_TRUE(config_has_key(config, "DID", "primaryRecord"));
90  EXPECT_TRUE(config_has_key(config, "DID", "productId"));
91  EXPECT_TRUE(config_has_key(config, "DID", "version"));
92  config_free(config);
93}
94
95TEST_F(ConfigTest, config_no_bad_keys) {
96  config_t *config = config_new(CONFIG_FILE);
97  EXPECT_FALSE(config_has_key(config, "DID_BAD", "primaryRecord"));
98  EXPECT_FALSE(config_has_key(config, "DID", "primaryRecord_BAD"));
99  EXPECT_FALSE(config_has_key(config, CONFIG_DEFAULT_SECTION, "primaryRecord"));
100  config_free(config);
101}
102
103TEST_F(ConfigTest, config_get_int_version) {
104  config_t *config = config_new(CONFIG_FILE);
105  EXPECT_EQ(config_get_int(config, "DID", "version", 0), 0x1436);
106  config_free(config);
107}
108
109TEST_F(ConfigTest, config_get_int_default) {
110  config_t *config = config_new(CONFIG_FILE);
111  EXPECT_EQ(config_get_int(config, "DID", "primaryRecord", 123), 123);
112  config_free(config);
113}
114