Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Properties_test" |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 18 | |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 19 | #include <limits.h> |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 20 | |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <sstream> |
| 23 | #include <string> |
| 24 | |
| 25 | #include <android/log.h> |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | #include <gtest/gtest.h> |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 29 | |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | #define STRINGIFY_INNER(x) #x |
| 33 | #define STRINGIFY(x) STRINGIFY_INNER(x) |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 34 | #define ASSERT_OK(x) ASSERT_EQ(0, (x)) |
| 35 | #define EXPECT_OK(x) EXPECT_EQ(0, (x)) |
| 36 | |
| 37 | #define PROPERTY_TEST_KEY "libcutils.test.key" |
| 38 | #define PROPERTY_TEST_VALUE_DEFAULT "<<<default_value>>>" |
| 39 | |
| 40 | template <typename T> |
| 41 | static std::string HexString(T value) { |
| 42 | std::stringstream ss; |
| 43 | ss << "0x" << std::hex << std::uppercase << value; |
| 44 | return ss.str(); |
| 45 | } |
| 46 | |
| 47 | template <typename T> |
| 48 | static ::testing::AssertionResult AssertEqualHex(const char *mExpr, |
| 49 | const char *nExpr, |
| 50 | T m, |
| 51 | T n) { |
| 52 | if (m == n) { |
| 53 | return ::testing::AssertionSuccess(); |
| 54 | } |
| 55 | |
| 56 | return ::testing::AssertionFailure() |
| 57 | << mExpr << " and " << nExpr << " (expected: " << HexString(m) << |
| 58 | ", actual: " << HexString(n) << ") are not equal"; |
| 59 | } |
| 60 | |
| 61 | class PropertiesTest : public testing::Test { |
| 62 | public: |
| 63 | PropertiesTest() : mValue() {} |
| 64 | protected: |
| 65 | virtual void SetUp() { |
| 66 | EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL)); |
| 67 | } |
| 68 | |
| 69 | virtual void TearDown() { |
| 70 | EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL)); |
| 71 | } |
| 72 | |
| 73 | char mValue[PROPERTY_VALUE_MAX]; |
| 74 | |
| 75 | template <typename T> |
| 76 | static std::string ToString(T value) { |
| 77 | std::stringstream ss; |
| 78 | ss << value; |
| 79 | |
| 80 | return ss.str(); |
| 81 | } |
| 82 | |
| 83 | // Return length of property read; value is written into mValue |
| 84 | int SetAndGetProperty(const char* value, const char* defaultValue = PROPERTY_TEST_VALUE_DEFAULT) { |
| 85 | EXPECT_OK(property_set(PROPERTY_TEST_KEY, value)) << "value: '" << value << "'"; |
| 86 | return property_get(PROPERTY_TEST_KEY, mValue, defaultValue); |
| 87 | } |
| 88 | |
| 89 | void ResetValue(unsigned char c = 0xFF) { |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 90 | for (size_t i = 0; i < arraysize(mValue); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 91 | mValue[i] = (char) c; |
| 92 | } |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | TEST_F(PropertiesTest, SetString) { |
| 97 | |
| 98 | // Null key -> unsuccessful set |
| 99 | { |
| 100 | // Null key -> fails |
| 101 | EXPECT_GT(0, property_set(/*key*/NULL, PROPERTY_TEST_VALUE_DEFAULT)); |
| 102 | } |
| 103 | |
| 104 | // Null value -> returns default value |
| 105 | { |
| 106 | // Null value -> OK , and it clears the value |
| 107 | EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL)); |
| 108 | ResetValue(); |
| 109 | |
| 110 | // Since the value is null, default value will be returned |
James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 111 | size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 112 | EXPECT_EQ(strlen(PROPERTY_TEST_VALUE_DEFAULT), len); |
| 113 | EXPECT_STREQ(PROPERTY_TEST_VALUE_DEFAULT, mValue); |
| 114 | } |
| 115 | |
| 116 | // Trivial case => get returns what was set |
| 117 | { |
James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 118 | size_t len = SetAndGetProperty("hello_world"); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 119 | EXPECT_EQ(strlen("hello_world"), len) << "hello_world key"; |
| 120 | EXPECT_STREQ("hello_world", mValue); |
| 121 | ResetValue(); |
| 122 | } |
| 123 | |
| 124 | // Set to empty string => get returns default always |
| 125 | { |
| 126 | const char* EMPTY_STRING_DEFAULT = "EMPTY_STRING"; |
James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 127 | size_t len = SetAndGetProperty("", EMPTY_STRING_DEFAULT); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 128 | EXPECT_EQ(strlen(EMPTY_STRING_DEFAULT), len) << "empty key"; |
| 129 | EXPECT_STREQ(EMPTY_STRING_DEFAULT, mValue); |
| 130 | ResetValue(); |
| 131 | } |
| 132 | |
| 133 | // Set to max length => get returns what was set |
| 134 | { |
| 135 | std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a'); |
| 136 | |
| 137 | int len = SetAndGetProperty(maxLengthString.c_str()); |
| 138 | EXPECT_EQ(PROPERTY_VALUE_MAX-1, len) << "max length key"; |
| 139 | EXPECT_STREQ(maxLengthString.c_str(), mValue); |
| 140 | ResetValue(); |
| 141 | } |
| 142 | |
| 143 | // Set to max length + 1 => set fails |
| 144 | { |
| 145 | const char* VALID_TEST_VALUE = "VALID_VALUE"; |
| 146 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, VALID_TEST_VALUE)); |
| 147 | |
| 148 | std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a'); |
| 149 | |
| 150 | // Expect that the value set fails since it's too long |
| 151 | EXPECT_GT(0, property_set(PROPERTY_TEST_KEY, oneLongerString.c_str())); |
James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 152 | size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 153 | |
| 154 | EXPECT_EQ(strlen(VALID_TEST_VALUE), len) << "set should've failed"; |
| 155 | EXPECT_STREQ(VALID_TEST_VALUE, mValue); |
| 156 | ResetValue(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | TEST_F(PropertiesTest, GetString) { |
| 161 | |
| 162 | // Try to use a default value that's too long => set fails |
| 163 | { |
| 164 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, "")); |
| 165 | |
| 166 | std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a'); |
| 167 | std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a'); |
| 168 | |
| 169 | // Expect that the value is truncated since it's too long (by 1) |
| 170 | int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str()); |
| 171 | EXPECT_EQ(PROPERTY_VALUE_MAX-1, len); |
| 172 | EXPECT_STREQ(maxLengthString.c_str(), mValue); |
| 173 | ResetValue(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | TEST_F(PropertiesTest, GetBool) { |
| 178 | /** |
| 179 | * TRUE |
| 180 | */ |
| 181 | const char *valuesTrue[] = { "1", "true", "y", "yes", "on", }; |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 182 | for (size_t i = 0; i < arraysize(valuesTrue); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 183 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesTrue[i])); |
| 184 | bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false); |
| 185 | EXPECT_TRUE(val) << "Property should've been TRUE for value: '" << valuesTrue[i] << "'"; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * FALSE |
| 190 | */ |
| 191 | const char *valuesFalse[] = { "0", "false", "n", "no", "off", }; |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 192 | for (size_t i = 0; i < arraysize(valuesFalse); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 193 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesFalse[i])); |
| 194 | bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true); |
| 195 | EXPECT_FALSE(val) << "Property shoud've been FALSE For string value: '" << valuesFalse[i] << "'"; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * NEITHER |
| 200 | */ |
| 201 | const char *valuesNeither[] = { "x0", "x1", "2", "-2", "True", "False", "garbage", "", " ", |
| 202 | "+1", " 1 ", " true", " true ", " y ", " yes", "yes ", |
| 203 | "+0", "-0", "00", " 00 ", " false", "false ", |
| 204 | }; |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 205 | for (size_t i = 0; i < arraysize(valuesNeither); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 206 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesNeither[i])); |
| 207 | |
| 208 | // The default value should always be used |
| 209 | bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true); |
| 210 | EXPECT_TRUE(val) << "Property should've been NEITHER (true) for string value: '" << valuesNeither[i] << "'"; |
| 211 | |
| 212 | val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false); |
| 213 | EXPECT_FALSE(val) << "Property should've been NEITHER (false) for string value: '" << valuesNeither[i] << "'"; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | TEST_F(PropertiesTest, GetInt64) { |
| 218 | const int64_t DEFAULT_VALUE = INT64_C(0xDEADBEEFBEEFDEAD); |
| 219 | |
| 220 | const std::string longMaxString = ToString(INT64_MAX); |
| 221 | const std::string longStringOverflow = longMaxString + "0"; |
| 222 | |
| 223 | const std::string longMinString = ToString(INT64_MIN); |
| 224 | const std::string longStringUnderflow = longMinString + "0"; |
| 225 | |
| 226 | const char* setValues[] = { |
| 227 | // base 10 |
| 228 | "1", "2", "12345", "-1", "-2", "-12345", |
| 229 | // base 16 |
| 230 | "0xFF", "0x0FF", "0xC0FFEE", |
| 231 | // base 8 |
| 232 | "0", "01234", "07", |
| 233 | // corner cases |
| 234 | " 2", "2 ", "+0", "-0", " +0 ", longMaxString.c_str(), longMinString.c_str(), |
| 235 | // failing cases |
| 236 | NULL, "", " ", " ", "hello", " true ", "y", |
| 237 | longStringOverflow.c_str(), longStringUnderflow.c_str(), |
| 238 | }; |
| 239 | |
| 240 | int64_t getValues[] = { |
| 241 | // base 10 |
| 242 | 1, 2, 12345, -1, -2, -12345, |
| 243 | // base 16 |
| 244 | 0xFF, 0x0FF, 0xC0FFEE, |
| 245 | // base 8 |
| 246 | 0, 01234, 07, |
| 247 | // corner cases |
| 248 | 2, 2, 0, 0, 0, INT64_MAX, INT64_MIN, |
| 249 | // failing cases |
| 250 | DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, |
| 251 | DEFAULT_VALUE, DEFAULT_VALUE, |
| 252 | }; |
| 253 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 254 | ASSERT_EQ(arraysize(setValues), arraysize(getValues)); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 255 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 256 | for (size_t i = 0; i < arraysize(setValues); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 257 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i])); |
| 258 | |
| 259 | int64_t val = property_get_int64(PROPERTY_TEST_KEY, DEFAULT_VALUE); |
| 260 | EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'"; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | TEST_F(PropertiesTest, GetInt32) { |
| 265 | const int32_t DEFAULT_VALUE = INT32_C(0xDEADBEEF); |
| 266 | |
| 267 | const std::string intMaxString = ToString(INT32_MAX); |
| 268 | const std::string intStringOverflow = intMaxString + "0"; |
| 269 | |
| 270 | const std::string intMinString = ToString(INT32_MIN); |
| 271 | const std::string intStringUnderflow = intMinString + "0"; |
| 272 | |
| 273 | const char* setValues[] = { |
| 274 | // base 10 |
| 275 | "1", "2", "12345", "-1", "-2", "-12345", |
| 276 | // base 16 |
| 277 | "0xFF", "0x0FF", "0xC0FFEE", "0Xf00", |
| 278 | // base 8 |
| 279 | "0", "01234", "07", |
| 280 | // corner cases |
| 281 | " 2", "2 ", "+0", "-0", " +0 ", intMaxString.c_str(), intMinString.c_str(), |
| 282 | // failing cases |
| 283 | NULL, "", " ", " ", "hello", " true ", "y", |
| 284 | intStringOverflow.c_str(), intStringUnderflow.c_str(), |
| 285 | }; |
| 286 | |
| 287 | int32_t getValues[] = { |
| 288 | // base 10 |
| 289 | 1, 2, 12345, -1, -2, -12345, |
| 290 | // base 16 |
| 291 | 0xFF, 0x0FF, 0xC0FFEE, 0Xf00, |
| 292 | // base 8 |
| 293 | 0, 01234, 07, |
| 294 | // corner cases |
| 295 | 2, 2, 0, 0, 0, INT32_MAX, INT32_MIN, |
| 296 | // failing cases |
| 297 | DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, |
| 298 | DEFAULT_VALUE, DEFAULT_VALUE, |
| 299 | }; |
| 300 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 301 | ASSERT_EQ(arraysize(setValues), arraysize(getValues)); |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 302 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 303 | for (size_t i = 0; i < arraysize(setValues); ++i) { |
Igor Murashkin | d4507e9 | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 304 | ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i])); |
| 305 | |
| 306 | int32_t val = property_get_int32(PROPERTY_TEST_KEY, DEFAULT_VALUE); |
| 307 | EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'"; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | } // namespace android |