blob: f66590bb494be7c6253cc4b75a957b8c944a63e9 [file] [log] [blame]
Igor Murashkind4507e92014-04-08 11:22:42 -07001/*
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"
Elliott Hughesce927182016-02-17 11:54:47 -080018#include <cutils/log.h>
Igor Murashkind4507e92014-04-08 11:22:42 -070019#include <gtest/gtest.h>
20
21#include <cutils/properties.h>
22#include <limits.h>
23#include <string>
24#include <sstream>
25#include <iostream>
26
Elliott Hughes749ae2d2016-06-28 14:48:45 -070027#include <android-base/macros.h>
28
Igor Murashkind4507e92014-04-08 11:22:42 -070029namespace android {
30
31#define STRINGIFY_INNER(x) #x
32#define STRINGIFY(x) STRINGIFY_INNER(x)
Igor Murashkind4507e92014-04-08 11:22:42 -070033#define ASSERT_OK(x) ASSERT_EQ(0, (x))
34#define EXPECT_OK(x) EXPECT_EQ(0, (x))
35
36#define PROPERTY_TEST_KEY "libcutils.test.key"
37#define PROPERTY_TEST_VALUE_DEFAULT "<<<default_value>>>"
38
39template <typename T>
40static std::string HexString(T value) {
41 std::stringstream ss;
42 ss << "0x" << std::hex << std::uppercase << value;
43 return ss.str();
44}
45
46template <typename T>
47static ::testing::AssertionResult AssertEqualHex(const char *mExpr,
48 const char *nExpr,
49 T m,
50 T n) {
51 if (m == n) {
52 return ::testing::AssertionSuccess();
53 }
54
55 return ::testing::AssertionFailure()
56 << mExpr << " and " << nExpr << " (expected: " << HexString(m) <<
57 ", actual: " << HexString(n) << ") are not equal";
58}
59
60class PropertiesTest : public testing::Test {
61public:
62 PropertiesTest() : mValue() {}
63protected:
64 virtual void SetUp() {
65 EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
66 }
67
68 virtual void TearDown() {
69 EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
70 }
71
72 char mValue[PROPERTY_VALUE_MAX];
73
74 template <typename T>
75 static std::string ToString(T value) {
76 std::stringstream ss;
77 ss << value;
78
79 return ss.str();
80 }
81
82 // Return length of property read; value is written into mValue
83 int SetAndGetProperty(const char* value, const char* defaultValue = PROPERTY_TEST_VALUE_DEFAULT) {
84 EXPECT_OK(property_set(PROPERTY_TEST_KEY, value)) << "value: '" << value << "'";
85 return property_get(PROPERTY_TEST_KEY, mValue, defaultValue);
86 }
87
88 void ResetValue(unsigned char c = 0xFF) {
Elliott Hughes749ae2d2016-06-28 14:48:45 -070089 for (size_t i = 0; i < arraysize(mValue); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -070090 mValue[i] = (char) c;
91 }
92 }
93};
94
95TEST_F(PropertiesTest, SetString) {
96
97 // Null key -> unsuccessful set
98 {
99 // Null key -> fails
100 EXPECT_GT(0, property_set(/*key*/NULL, PROPERTY_TEST_VALUE_DEFAULT));
101 }
102
103 // Null value -> returns default value
104 {
105 // Null value -> OK , and it clears the value
106 EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
107 ResetValue();
108
109 // Since the value is null, default value will be returned
James Hawkinsb8980752016-03-01 11:21:53 -0800110 size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
Igor Murashkind4507e92014-04-08 11:22:42 -0700111 EXPECT_EQ(strlen(PROPERTY_TEST_VALUE_DEFAULT), len);
112 EXPECT_STREQ(PROPERTY_TEST_VALUE_DEFAULT, mValue);
113 }
114
115 // Trivial case => get returns what was set
116 {
James Hawkinsb8980752016-03-01 11:21:53 -0800117 size_t len = SetAndGetProperty("hello_world");
Igor Murashkind4507e92014-04-08 11:22:42 -0700118 EXPECT_EQ(strlen("hello_world"), len) << "hello_world key";
119 EXPECT_STREQ("hello_world", mValue);
120 ResetValue();
121 }
122
123 // Set to empty string => get returns default always
124 {
125 const char* EMPTY_STRING_DEFAULT = "EMPTY_STRING";
James Hawkinsb8980752016-03-01 11:21:53 -0800126 size_t len = SetAndGetProperty("", EMPTY_STRING_DEFAULT);
Igor Murashkind4507e92014-04-08 11:22:42 -0700127 EXPECT_EQ(strlen(EMPTY_STRING_DEFAULT), len) << "empty key";
128 EXPECT_STREQ(EMPTY_STRING_DEFAULT, mValue);
129 ResetValue();
130 }
131
132 // Set to max length => get returns what was set
133 {
134 std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
135
136 int len = SetAndGetProperty(maxLengthString.c_str());
137 EXPECT_EQ(PROPERTY_VALUE_MAX-1, len) << "max length key";
138 EXPECT_STREQ(maxLengthString.c_str(), mValue);
139 ResetValue();
140 }
141
142 // Set to max length + 1 => set fails
143 {
144 const char* VALID_TEST_VALUE = "VALID_VALUE";
145 ASSERT_OK(property_set(PROPERTY_TEST_KEY, VALID_TEST_VALUE));
146
147 std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
148
149 // Expect that the value set fails since it's too long
150 EXPECT_GT(0, property_set(PROPERTY_TEST_KEY, oneLongerString.c_str()));
James Hawkinsb8980752016-03-01 11:21:53 -0800151 size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
Igor Murashkind4507e92014-04-08 11:22:42 -0700152
153 EXPECT_EQ(strlen(VALID_TEST_VALUE), len) << "set should've failed";
154 EXPECT_STREQ(VALID_TEST_VALUE, mValue);
155 ResetValue();
156 }
157}
158
159TEST_F(PropertiesTest, GetString) {
160
161 // Try to use a default value that's too long => set fails
162 {
163 ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
164
165 std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
166 std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
167
168 // Expect that the value is truncated since it's too long (by 1)
169 int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
170 EXPECT_EQ(PROPERTY_VALUE_MAX-1, len);
171 EXPECT_STREQ(maxLengthString.c_str(), mValue);
172 ResetValue();
173 }
174}
175
176TEST_F(PropertiesTest, GetBool) {
177 /**
178 * TRUE
179 */
180 const char *valuesTrue[] = { "1", "true", "y", "yes", "on", };
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700181 for (size_t i = 0; i < arraysize(valuesTrue); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -0700182 ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesTrue[i]));
183 bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false);
184 EXPECT_TRUE(val) << "Property should've been TRUE for value: '" << valuesTrue[i] << "'";
185 }
186
187 /**
188 * FALSE
189 */
190 const char *valuesFalse[] = { "0", "false", "n", "no", "off", };
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700191 for (size_t i = 0; i < arraysize(valuesFalse); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -0700192 ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesFalse[i]));
193 bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true);
194 EXPECT_FALSE(val) << "Property shoud've been FALSE For string value: '" << valuesFalse[i] << "'";
195 }
196
197 /**
198 * NEITHER
199 */
200 const char *valuesNeither[] = { "x0", "x1", "2", "-2", "True", "False", "garbage", "", " ",
201 "+1", " 1 ", " true", " true ", " y ", " yes", "yes ",
202 "+0", "-0", "00", " 00 ", " false", "false ",
203 };
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700204 for (size_t i = 0; i < arraysize(valuesNeither); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -0700205 ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesNeither[i]));
206
207 // The default value should always be used
208 bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true);
209 EXPECT_TRUE(val) << "Property should've been NEITHER (true) for string value: '" << valuesNeither[i] << "'";
210
211 val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false);
212 EXPECT_FALSE(val) << "Property should've been NEITHER (false) for string value: '" << valuesNeither[i] << "'";
213 }
214}
215
216TEST_F(PropertiesTest, GetInt64) {
217 const int64_t DEFAULT_VALUE = INT64_C(0xDEADBEEFBEEFDEAD);
218
219 const std::string longMaxString = ToString(INT64_MAX);
220 const std::string longStringOverflow = longMaxString + "0";
221
222 const std::string longMinString = ToString(INT64_MIN);
223 const std::string longStringUnderflow = longMinString + "0";
224
225 const char* setValues[] = {
226 // base 10
227 "1", "2", "12345", "-1", "-2", "-12345",
228 // base 16
229 "0xFF", "0x0FF", "0xC0FFEE",
230 // base 8
231 "0", "01234", "07",
232 // corner cases
233 " 2", "2 ", "+0", "-0", " +0 ", longMaxString.c_str(), longMinString.c_str(),
234 // failing cases
235 NULL, "", " ", " ", "hello", " true ", "y",
236 longStringOverflow.c_str(), longStringUnderflow.c_str(),
237 };
238
239 int64_t getValues[] = {
240 // base 10
241 1, 2, 12345, -1, -2, -12345,
242 // base 16
243 0xFF, 0x0FF, 0xC0FFEE,
244 // base 8
245 0, 01234, 07,
246 // corner cases
247 2, 2, 0, 0, 0, INT64_MAX, INT64_MIN,
248 // failing cases
249 DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE,
250 DEFAULT_VALUE, DEFAULT_VALUE,
251 };
252
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700253 ASSERT_EQ(arraysize(setValues), arraysize(getValues));
Igor Murashkind4507e92014-04-08 11:22:42 -0700254
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700255 for (size_t i = 0; i < arraysize(setValues); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -0700256 ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i]));
257
258 int64_t val = property_get_int64(PROPERTY_TEST_KEY, DEFAULT_VALUE);
259 EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'";
260 }
261}
262
263TEST_F(PropertiesTest, GetInt32) {
264 const int32_t DEFAULT_VALUE = INT32_C(0xDEADBEEF);
265
266 const std::string intMaxString = ToString(INT32_MAX);
267 const std::string intStringOverflow = intMaxString + "0";
268
269 const std::string intMinString = ToString(INT32_MIN);
270 const std::string intStringUnderflow = intMinString + "0";
271
272 const char* setValues[] = {
273 // base 10
274 "1", "2", "12345", "-1", "-2", "-12345",
275 // base 16
276 "0xFF", "0x0FF", "0xC0FFEE", "0Xf00",
277 // base 8
278 "0", "01234", "07",
279 // corner cases
280 " 2", "2 ", "+0", "-0", " +0 ", intMaxString.c_str(), intMinString.c_str(),
281 // failing cases
282 NULL, "", " ", " ", "hello", " true ", "y",
283 intStringOverflow.c_str(), intStringUnderflow.c_str(),
284 };
285
286 int32_t getValues[] = {
287 // base 10
288 1, 2, 12345, -1, -2, -12345,
289 // base 16
290 0xFF, 0x0FF, 0xC0FFEE, 0Xf00,
291 // base 8
292 0, 01234, 07,
293 // corner cases
294 2, 2, 0, 0, 0, INT32_MAX, INT32_MIN,
295 // failing cases
296 DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE, DEFAULT_VALUE,
297 DEFAULT_VALUE, DEFAULT_VALUE,
298 };
299
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700300 ASSERT_EQ(arraysize(setValues), arraysize(getValues));
Igor Murashkind4507e92014-04-08 11:22:42 -0700301
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700302 for (size_t i = 0; i < arraysize(setValues); ++i) {
Igor Murashkind4507e92014-04-08 11:22:42 -0700303 ASSERT_OK(property_set(PROPERTY_TEST_KEY, setValues[i]));
304
305 int32_t val = property_get_int32(PROPERTY_TEST_KEY, DEFAULT_VALUE);
306 EXPECT_PRED_FORMAT2(AssertEqualHex, getValues[i], val) << "Property was set to '" << setValues[i] << "'";
307 }
308}
309
310} // namespace android