libcutils: Use strnlen for default property values
Add unit tests to test the corner cases. Test: unit tests pass before and after the change. Change-Id: Idafeb8354cd6c7db2a68cd398dafe153453a3940
This commit is contained in:
parent
22c0962ab9
commit
e67abec514
2 changed files with 53 additions and 7 deletions
|
|
@ -119,10 +119,7 @@ int property_get(const char *key, char *value, const char *default_value) {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
if (default_value) {
|
if (default_value) {
|
||||||
len = strlen(default_value);
|
len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
|
||||||
if (len >= PROPERTY_VALUE_MAX) {
|
|
||||||
len = PROPERTY_VALUE_MAX - 1;
|
|
||||||
}
|
|
||||||
memcpy(value, default_value, len);
|
memcpy(value, default_value, len);
|
||||||
value[len] = '\0';
|
value[len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -159,19 +159,68 @@ TEST_F(PropertiesTest, SetString) {
|
||||||
|
|
||||||
TEST_F(PropertiesTest, GetString) {
|
TEST_F(PropertiesTest, GetString) {
|
||||||
|
|
||||||
// Try to use a default value that's too long => set fails
|
// Try to use a default value that's too long => get truncates the value
|
||||||
{
|
{
|
||||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||||
|
|
||||||
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
|
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'a');
|
||||||
std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
|
std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
|
||||||
|
|
||||||
// Expect that the value is truncated since it's too long (by 1)
|
// Expect that the value is truncated since it's too long (by 1)
|
||||||
int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
|
int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
|
||||||
EXPECT_EQ(PROPERTY_VALUE_MAX-1, len);
|
EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
|
||||||
EXPECT_STREQ(maxLengthString.c_str(), mValue);
|
EXPECT_STREQ(maxLengthString.c_str(), mValue);
|
||||||
ResetValue();
|
ResetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to use a default value that's the max length => get succeeds
|
||||||
|
{
|
||||||
|
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||||
|
|
||||||
|
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'b');
|
||||||
|
|
||||||
|
// Expect that the value matches maxLengthString
|
||||||
|
int len = property_get(PROPERTY_TEST_KEY, mValue, maxLengthString.c_str());
|
||||||
|
EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
|
||||||
|
EXPECT_STREQ(maxLengthString.c_str(), mValue);
|
||||||
|
ResetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use a default value of length one => get succeeds
|
||||||
|
{
|
||||||
|
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||||
|
|
||||||
|
std::string oneCharString = std::string(1, 'c');
|
||||||
|
|
||||||
|
// Expect that the value matches oneCharString
|
||||||
|
int len = property_get(PROPERTY_TEST_KEY, mValue, oneCharString.c_str());
|
||||||
|
EXPECT_EQ(1, len);
|
||||||
|
EXPECT_STREQ(oneCharString.c_str(), mValue);
|
||||||
|
ResetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use a default value of length zero => get succeeds
|
||||||
|
{
|
||||||
|
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||||
|
|
||||||
|
std::string zeroCharString = std::string(0, 'd');
|
||||||
|
|
||||||
|
// Expect that the value matches oneCharString
|
||||||
|
int len = property_get(PROPERTY_TEST_KEY, mValue, zeroCharString.c_str());
|
||||||
|
EXPECT_EQ(0, len);
|
||||||
|
EXPECT_STREQ(zeroCharString.c_str(), mValue);
|
||||||
|
ResetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use a NULL default value => get returns 0
|
||||||
|
{
|
||||||
|
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||||
|
|
||||||
|
// Expect a return value of 0
|
||||||
|
int len = property_get(PROPERTY_TEST_KEY, mValue, NULL);
|
||||||
|
EXPECT_EQ(0, len);
|
||||||
|
ResetValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PropertiesTest, GetBool) {
|
TEST_F(PropertiesTest, GetBool) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue