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