config_test.cc revision ee2aa45def216a3c4d6a23481fa96f1b02a5de8c
1#include <gtest/gtest.h> 2 3#include "AllocationTestHarness.h" 4 5extern "C" { 6#include "config.h" 7} 8 9static const char CONFIG_FILE[] = "/data/local/tmp/config_test.conf"; 10static const char CONFIG_FILE_CONTENT[] = 11" \n\ 12first_key=value \n\ 13 \n\ 14# Device ID (DID) configuration \n\ 15[DID] \n\ 16 \n\ 17# Record Number: 1, 2 or 3 - maximum of 3 records \n\ 18recordNumber = 1 \n\ 19 \n\ 20# Primary Record - true or false (default) \n\ 21# There can be only one primary record \n\ 22primaryRecord = true \n\ 23 \n\ 24# Vendor ID '0xFFFF' indicates no Device ID Service Record is present in the device \n\ 25# 0x000F = Broadcom Corporation (default) \n\ 26#vendorId = 0x000F \n\ 27 \n\ 28# Vendor ID Source \n\ 29# 0x0001 = Bluetooth SIG assigned Device ID Vendor ID value (default) \n\ 30# 0x0002 = USB Implementer's Forum assigned Device ID Vendor ID value \n\ 31#vendorIdSource = 0x0001 \n\ 32 \n\ 33# Product ID & Product Version \n\ 34# Per spec DID v1.3 0xJJMN for version is interpreted as JJ.M.N \n\ 35# JJ: major version number, M: minor version number, N: sub-minor version number \n\ 36# For example: 1200, v14.3.6 \n\ 37productId = 0x1200 \n\ 38version = 0x1111 \n\ 39 \n\ 40# Optional attributes \n\ 41#clientExecutableURL = \n\ 42#serviceDescription = \n\ 43#documentationURL = \n\ 44 \n\ 45# Additional optional DID records. Bluedroid supports up to 3 records. \n\ 46[DID] \n\ 47[DID] \n\ 48version = 0x1436 \n\ 49"; 50 51class ConfigTest : public AllocationTestHarness { 52 protected: 53 virtual void SetUp() { 54 AllocationTestHarness::SetUp(); 55 FILE *fp = fopen(CONFIG_FILE, "wt"); 56 fwrite(CONFIG_FILE_CONTENT, 1, sizeof(CONFIG_FILE_CONTENT), fp); 57 fclose(fp); 58 } 59}; 60 61TEST_F(ConfigTest, config_new_empty) { 62 config_t *config = config_new_empty(); 63 EXPECT_TRUE(config != NULL); 64 config_free(config); 65} 66 67TEST_F(ConfigTest, config_new_no_file) { 68 config_t *config = config_new("/meow"); 69 EXPECT_TRUE(config == NULL); 70 config_free(config); 71} 72 73TEST_F(ConfigTest, config_new) { 74 config_t *config = config_new(CONFIG_FILE); 75 EXPECT_TRUE(config != NULL); 76 config_free(config); 77} 78 79TEST_F(ConfigTest, config_free_null) { 80 config_free(NULL); 81} 82 83TEST_F(ConfigTest, config_has_section) { 84 config_t *config = config_new(CONFIG_FILE); 85 EXPECT_TRUE(config_has_section(config, "DID")); 86 config_free(config); 87} 88 89TEST_F(ConfigTest, config_has_key_in_default_section) { 90 config_t *config = config_new(CONFIG_FILE); 91 EXPECT_TRUE(config_has_key(config, CONFIG_DEFAULT_SECTION, "first_key")); 92 EXPECT_STREQ(config_get_string(config, CONFIG_DEFAULT_SECTION, "first_key", "meow"), "value"); 93 config_free(config); 94} 95 96TEST_F(ConfigTest, config_has_keys) { 97 config_t *config = config_new(CONFIG_FILE); 98 EXPECT_TRUE(config_has_key(config, "DID", "recordNumber")); 99 EXPECT_TRUE(config_has_key(config, "DID", "primaryRecord")); 100 EXPECT_TRUE(config_has_key(config, "DID", "productId")); 101 EXPECT_TRUE(config_has_key(config, "DID", "version")); 102 config_free(config); 103} 104 105TEST_F(ConfigTest, config_no_bad_keys) { 106 config_t *config = config_new(CONFIG_FILE); 107 EXPECT_FALSE(config_has_key(config, "DID_BAD", "primaryRecord")); 108 EXPECT_FALSE(config_has_key(config, "DID", "primaryRecord_BAD")); 109 EXPECT_FALSE(config_has_key(config, CONFIG_DEFAULT_SECTION, "primaryRecord")); 110 config_free(config); 111} 112 113TEST_F(ConfigTest, config_get_int_version) { 114 config_t *config = config_new(CONFIG_FILE); 115 EXPECT_EQ(config_get_int(config, "DID", "version", 0), 0x1436); 116 config_free(config); 117} 118 119TEST_F(ConfigTest, config_get_int_default) { 120 config_t *config = config_new(CONFIG_FILE); 121 EXPECT_EQ(config_get_int(config, "DID", "primaryRecord", 123), 123); 122 config_free(config); 123} 124 125TEST_F(ConfigTest, config_remove_section) { 126 config_t *config = config_new(CONFIG_FILE); 127 EXPECT_TRUE(config_remove_section(config, "DID")); 128 EXPECT_FALSE(config_has_section(config, "DID")); 129 EXPECT_FALSE(config_has_key(config, "DID", "productId")); 130 config_free(config); 131} 132 133TEST_F(ConfigTest, config_remove_section_missing) { 134 config_t *config = config_new(CONFIG_FILE); 135 EXPECT_FALSE(config_remove_section(config, "not a section")); 136 config_free(config); 137} 138 139TEST_F(ConfigTest, config_remove_key) { 140 config_t *config = config_new(CONFIG_FILE); 141 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200); 142 EXPECT_TRUE(config_remove_key(config, "DID", "productId")); 143 EXPECT_FALSE(config_has_key(config, "DID", "productId")); 144 config_free(config); 145} 146 147TEST_F(ConfigTest, config_remove_key_missing) { 148 config_t *config = config_new(CONFIG_FILE); 149 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 0x1200); 150 EXPECT_TRUE(config_remove_key(config, "DID", "productId")); 151 EXPECT_EQ(config_get_int(config, "DID", "productId", 999), 999); 152 config_free(config); 153} 154 155TEST_F(ConfigTest, config_section_begin) { 156 config_t *config = config_new(CONFIG_FILE); 157 const config_section_node_t *section = config_section_begin(config); 158 EXPECT_TRUE(section != NULL); 159 const char *section_name = config_section_name(section); 160 EXPECT_TRUE(section != NULL); 161 EXPECT_TRUE(!strcmp(section_name, CONFIG_DEFAULT_SECTION)); 162 config_free(config); 163} 164 165TEST_F(ConfigTest, config_section_next) { 166 config_t *config = config_new(CONFIG_FILE); 167 const config_section_node_t *section = config_section_begin(config); 168 EXPECT_TRUE(section != NULL); 169 section = config_section_next(section); 170 EXPECT_TRUE(section != NULL); 171 const char *section_name = config_section_name(section); 172 EXPECT_TRUE(section != NULL); 173 EXPECT_TRUE(!strcmp(section_name, "DID")); 174 config_free(config); 175} 176 177TEST_F(ConfigTest, config_section_end) { 178 config_t *config = config_new(CONFIG_FILE); 179 const config_section_node_t * section = config_section_begin(config); 180 section = config_section_next(section); 181 section = config_section_next(section); 182 EXPECT_EQ(section, config_section_end(config)); 183 config_free(config); 184} 185 186TEST_F(ConfigTest, config_save_basic) { 187 config_t *config = config_new(CONFIG_FILE); 188 EXPECT_TRUE(config_save(config, CONFIG_FILE)); 189 config_free(config); 190} 191