Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
| 2 | |
Zach Johnson | ee2aa45 | 2014-08-26 20:16:03 -0700 | [diff] [blame] | 3 | #include "AllocationTestHarness.h" |
| 4 | |
Pavlin Radoslavov | c196f21 | 2015-09-23 20:39:53 -0700 | [diff] [blame] | 5 | #include "osi/include/config.h" |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 6 | |
| 7 | static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf"; |
| 8 | static const char CONFIG_FILE_CONTENT[] = |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 9 | " \n\ |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 10 | first_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\ |
| 16 | recordNumber = 1 \n\ |
| 17 | \n\ |
| 18 | # Primary Record - true or false (default) \n\ |
| 19 | # There can be only one primary record \n\ |
| 20 | primaryRecord = 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\ |
| 35 | productId = 0x1200 \n\ |
| 36 | version = 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\ |
| 46 | version = 0x1436 \n\ |
Jakub Pawlowski | 1c5e31a | 2018-02-15 10:09:03 -0800 | [diff] [blame] | 47 | \n\ |
| 48 | HiSyncId = 18446744073709551615 \n\ |
| 49 | HiSyncId2 = 15001900 \n\ |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 50 | "; |
| 51 | |
Zach Johnson | ee2aa45 | 2014-08-26 20:16:03 -0700 | [diff] [blame] | 52 | class ConfigTest : public AllocationTestHarness { |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 53 | 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 Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Sharvil Nanavati | 7f280eb | 2014-08-15 15:04:46 -0700 | [diff] [blame] | 62 | TEST_F(ConfigTest, config_new_empty) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 63 | std::unique_ptr<config_t> config = config_new_empty(); |
| 64 | EXPECT_TRUE(config.get() != NULL); |
Sharvil Nanavati | 7f280eb | 2014-08-15 15:04:46 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 67 | TEST_F(ConfigTest, config_new_no_file) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 68 | std::unique_ptr<config_t> config = config_new("/meow"); |
| 69 | EXPECT_TRUE(config.get() == NULL); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(ConfigTest, config_new) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 73 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 74 | EXPECT_TRUE(config.get() != NULL); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Marie Janssen | 4cef24d | 2016-04-05 11:36:15 -0700 | [diff] [blame] | 77 | TEST_F(ConfigTest, config_new_clone) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 78 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 79 | std::unique_ptr<config_t> clone = config_new_clone(*config); |
Marie Janssen | d9ebc23 | 2016-03-25 13:37:13 -0700 | [diff] [blame] | 80 | |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 81 | config_set_string(clone.get(), CONFIG_DEFAULT_SECTION, "first_key", |
| 82 | "not_value"); |
Marie Janssen | d9ebc23 | 2016-03-25 13:37:13 -0700 | [diff] [blame] | 83 | |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 84 | std::string one = std::string("one"); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 85 | EXPECT_STRNE( |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 86 | 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 Janssen | d9ebc23 | 2016-03-25 13:37:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 92 | TEST_F(ConfigTest, config_has_section) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 93 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 94 | EXPECT_TRUE(config_has_section(*config, "DID")); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST_F(ConfigTest, config_has_key_in_default_section) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 98 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 99 | EXPECT_TRUE(config_has_key(*config, CONFIG_DEFAULT_SECTION, "first_key")); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 100 | EXPECT_STREQ( |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 101 | config_get_string(*config, CONFIG_DEFAULT_SECTION, "first_key", nullptr) |
| 102 | ->c_str(), |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 103 | "value"); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(ConfigTest, config_has_keys) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 107 | 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 Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST_F(ConfigTest, config_no_bad_keys) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 115 | 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 Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST_F(ConfigTest, config_get_int_version) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 123 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 124 | EXPECT_EQ(config_get_int(*config, "DID", "version", 0), 0x1436); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(ConfigTest, config_get_int_default) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 128 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 129 | EXPECT_EQ(config_get_int(*config, "DID", "primaryRecord", 123), 123); |
Sharvil Nanavati | d1c453f | 2014-05-04 21:33:52 -0700 | [diff] [blame] | 130 | } |
Sharvil Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 131 | |
Jakub Pawlowski | 1c5e31a | 2018-02-15 10:09:03 -0800 | [diff] [blame] | 132 | TEST_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 | |
| 140 | TEST_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 Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 146 | TEST_F(ConfigTest, config_remove_section) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 147 | 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 Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(ConfigTest, config_remove_section_missing) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 154 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 155 | EXPECT_FALSE(config_remove_section(config.get(), "not a section")); |
Sharvil Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST_F(ConfigTest, config_remove_key) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 159 | 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 Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(ConfigTest, config_remove_key_missing) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 166 | 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 Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_F(ConfigTest, config_save_basic) { |
Jakub Pawlowski | f3fb316 | 2017-10-06 00:04:27 -0700 | [diff] [blame] | 173 | std::unique_ptr<config_t> config = config_new(CONFIG_FILE); |
| 174 | EXPECT_TRUE(config_save(*config, CONFIG_FILE)); |
Sharvil Nanavati | 36c1c09 | 2014-07-27 21:12:20 -0700 | [diff] [blame] | 175 | } |