blob: 58ce81807f982ec41ac52c0ffdfd00ead74eecb0 [file] [log] [blame]
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -07001#include <gtest/gtest.h>
2
Zach Johnsonee2aa452014-08-26 20:16:03 -07003#include "AllocationTestHarness.h"
4
Pavlin Radoslavovc196f212015-09-23 20:39:53 -07005#include "osi/include/config.h"
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -07006
7static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf";
8static const char CONFIG_FILE_CONTENT[] =
Myles Watsonb55040c2016-10-19 13:15:34 -07009 " \n\
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070010first_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\
Jakub Pawlowski1c5e31a2018-02-15 10:09:03 -080047 \n\
48HiSyncId = 18446744073709551615 \n\
49HiSyncId2 = 15001900 \n\
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070050";
51
Zach Johnsonee2aa452014-08-26 20:16:03 -070052class ConfigTest : public AllocationTestHarness {
Myles Watsonb55040c2016-10-19 13:15:34 -070053 protected:
54 virtual void SetUp() {
55 AllocationTestHarness::SetUp();
56 FILE* fp = fopen(CONFIG_FILE, "wt");
57 fwrite(CONFIG_FILE_CONTENT, 1, sizeof(CONFIG_FILE_CONTENT), fp);
58 fclose(fp);
59 }
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070060};
61
Sharvil Nanavati7f280eb2014-08-15 15:04:46 -070062TEST_F(ConfigTest, config_new_empty) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070063 std::unique_ptr<config_t> config = config_new_empty();
64 EXPECT_TRUE(config.get() != NULL);
Sharvil Nanavati7f280eb2014-08-15 15:04:46 -070065}
66
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070067TEST_F(ConfigTest, config_new_no_file) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070068 std::unique_ptr<config_t> config = config_new("/meow");
69 EXPECT_TRUE(config.get() == NULL);
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070070}
71
72TEST_F(ConfigTest, config_new) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070073 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
74 EXPECT_TRUE(config.get() != NULL);
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070075}
76
Marie Janssen4cef24d2016-04-05 11:36:15 -070077TEST_F(ConfigTest, config_new_clone) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070078 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
79 std::unique_ptr<config_t> clone = config_new_clone(*config);
Marie Janssend9ebc232016-03-25 13:37:13 -070080
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070081 config_set_string(clone.get(), CONFIG_DEFAULT_SECTION, "first_key",
82 "not_value");
Marie Janssend9ebc232016-03-25 13:37:13 -070083
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070084 std::string one = std::string("one");
Myles Watsonb55040c2016-10-19 13:15:34 -070085 EXPECT_STRNE(
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070086 config_get_string(*config, CONFIG_DEFAULT_SECTION, "first_key", &one)
87 ->c_str(),
88 config_get_string(*clone, CONFIG_DEFAULT_SECTION, "first_key", &one)
89 ->c_str());
Marie Janssend9ebc232016-03-25 13:37:13 -070090}
91
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070092TEST_F(ConfigTest, config_has_section) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070093 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
94 EXPECT_TRUE(config_has_section(*config, "DID"));
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -070095}
96
97TEST_F(ConfigTest, config_has_key_in_default_section) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -070098 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
99 EXPECT_TRUE(config_has_key(*config, CONFIG_DEFAULT_SECTION, "first_key"));
Myles Watsonb55040c2016-10-19 13:15:34 -0700100 EXPECT_STREQ(
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700101 config_get_string(*config, CONFIG_DEFAULT_SECTION, "first_key", nullptr)
102 ->c_str(),
Myles Watsonb55040c2016-10-19 13:15:34 -0700103 "value");
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -0700104}
105
106TEST_F(ConfigTest, config_has_keys) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700107 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
108 EXPECT_TRUE(config_has_key(*config, "DID", "recordNumber"));
109 EXPECT_TRUE(config_has_key(*config, "DID", "primaryRecord"));
110 EXPECT_TRUE(config_has_key(*config, "DID", "productId"));
111 EXPECT_TRUE(config_has_key(*config, "DID", "version"));
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -0700112}
113
114TEST_F(ConfigTest, config_no_bad_keys) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700115 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
116 EXPECT_FALSE(config_has_key(*config, "DID_BAD", "primaryRecord"));
117 EXPECT_FALSE(config_has_key(*config, "DID", "primaryRecord_BAD"));
118 EXPECT_FALSE(
119 config_has_key(*config, CONFIG_DEFAULT_SECTION, "primaryRecord"));
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -0700120}
121
122TEST_F(ConfigTest, config_get_int_version) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700123 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
124 EXPECT_EQ(config_get_int(*config, "DID", "version", 0), 0x1436);
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -0700125}
126
127TEST_F(ConfigTest, config_get_int_default) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700128 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
129 EXPECT_EQ(config_get_int(*config, "DID", "primaryRecord", 123), 123);
Sharvil Nanavatid1c453f2014-05-04 21:33:52 -0700130}
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700131
Jakub Pawlowski1c5e31a2018-02-15 10:09:03 -0800132TEST_F(ConfigTest, config_get_uint64) {
133 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
134 EXPECT_EQ(config_get_uint64(*config, "DID", "HiSyncId", 0),
135 0xFFFFFFFFFFFFFFFF);
136 EXPECT_EQ(config_get_uint64(*config, "DID", "HiSyncId2", 0),
137 uint64_t(15001900));
138}
139
140TEST_F(ConfigTest, config_get_uint64_default) {
141 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
142 EXPECT_EQ(config_get_uint64(*config, "DID", "primaryRecord", 123),
143 uint64_t(123));
144}
145
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700146TEST_F(ConfigTest, config_remove_section) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700147 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
148 EXPECT_TRUE(config_remove_section(config.get(), "DID"));
149 EXPECT_FALSE(config_has_section(*config, "DID"));
150 EXPECT_FALSE(config_has_key(*config, "DID", "productId"));
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700151}
152
153TEST_F(ConfigTest, config_remove_section_missing) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700154 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
155 EXPECT_FALSE(config_remove_section(config.get(), "not a section"));
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700156}
157
158TEST_F(ConfigTest, config_remove_key) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700159 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
160 EXPECT_EQ(config_get_int(*config, "DID", "productId", 999), 0x1200);
161 EXPECT_TRUE(config_remove_key(config.get(), "DID", "productId"));
162 EXPECT_FALSE(config_has_key(*config, "DID", "productId"));
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700163}
164
165TEST_F(ConfigTest, config_remove_key_missing) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700166 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
167 EXPECT_EQ(config_get_int(*config, "DID", "productId", 999), 0x1200);
168 EXPECT_TRUE(config_remove_key(config.get(), "DID", "productId"));
169 EXPECT_EQ(config_get_int(*config, "DID", "productId", 999), 999);
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700170}
171
172TEST_F(ConfigTest, config_save_basic) {
Jakub Pawlowskif3fb3162017-10-06 00:04:27 -0700173 std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
174 EXPECT_TRUE(config_save(*config, CONFIG_FILE));
Sharvil Nanavati36c1c092014-07-27 21:12:20 -0700175}