1 #include <gtest/gtest.h> 2 3 #include "AllocationTestHarness.h" 4 5 #include "osi/include/config.h" 6 7 static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf"; 8 static const char CONFIG_FILE_CONTENT[] = 9 " \n\ 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\ 47 \n\ 48 HiSyncId = 18446744073709551615 \n\ 49 HiSyncId2 = 15001900 \n\ 50 "; 51 52 class ConfigTest : public AllocationTestHarness { 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 } 60 }; 61 62 TEST_F(ConfigTest, config_new_empty) { 63 std::unique_ptr<config_t> config = config_new_empty(); 64 EXPECT_TRUE(config.get() != NULL); 65 } 66 67 TEST_F(ConfigTest, config_new_no_file) { 68 std::unique_ptr<config_t> config = config_new("/meow"); 69 EXPECT_TRUE(config.get() == NULL); 70 } 71 72 TEST_F(ConfigTest, config_new) { 73 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 74 EXPECT_TRUE(config.get() != NULL); 75 } 76 77 TEST_F(ConfigTest, config_new_clone) { 78 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 79 std::unique_ptr<config_t> clone = config_new_clone(*config); 80 81 config_set_string(clone.get(), CONFIG_DEFAULT_SECTION, "first_key", 82 "not_value"); 83 84 std::string one = std::string("one"); 85 EXPECT_STRNE( 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()); 90 } 91 92 TEST_F(ConfigTest, config_has_section) { 93 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 94 EXPECT_TRUE(config_has_section(*config, "DID")); 95 } 96 97 TEST_F(ConfigTest, config_has_key_in_default_section) { 98 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 99 EXPECT_TRUE(config_has_key(*config, CONFIG_DEFAULT_SECTION, "first_key")); 100 EXPECT_STREQ( 101 config_get_string(*config, CONFIG_DEFAULT_SECTION, "first_key", nullptr) 102 ->c_str(), 103 "value"); 104 } 105 106 TEST_F(ConfigTest, config_has_keys) { 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")); 112 } 113 114 TEST_F(ConfigTest, config_no_bad_keys) { 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")); 120 } 121 122 TEST_F(ConfigTest, config_get_int_version) { 123 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 124 EXPECT_EQ(config_get_int(*config, "DID", "version", 0), 0x1436); 125 } 126 127 TEST_F(ConfigTest, config_get_int_default) { 128 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 129 EXPECT_EQ(config_get_int(*config, "DID", "primaryRecord", 123), 123); 130 } 131 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 146 TEST_F(ConfigTest, config_remove_section) { 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")); 151 } 152 153 TEST_F(ConfigTest, config_remove_section_missing) { 154 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 155 EXPECT_FALSE(config_remove_section(config.get(), "not a section")); 156 } 157 158 TEST_F(ConfigTest, config_remove_key) { 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")); 163 } 164 165 TEST_F(ConfigTest, config_remove_key_missing) { 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); 170 } 171 172 TEST_F(ConfigTest, config_save_basic) { 173 std::unique_ptr<config_t> config = config_new(CONFIG_FILE); 174 EXPECT_TRUE(config_save(*config, CONFIG_FILE)); 175 } 176