blob: da89ec5a91aee0fdcea34ee368db44034279bde0 [file] [log] [blame]
Elliott Hughes1e88c8c2016-09-21 16:53:15 -07001/*
2 * Copyright (C) 2016 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#include "android-base/properties.h"
18
19#include <gtest/gtest.h>
20
21#include <string>
22
23TEST(properties, smoke) {
24 android::base::SetProperty("debug.libbase.property_test", "hello");
25
26 std::string s = android::base::GetProperty("debug.libbase.property_test", "");
27 ASSERT_EQ("hello", s);
28
29 android::base::SetProperty("debug.libbase.property_test", "world");
30 s = android::base::GetProperty("debug.libbase.property_test", "");
31 ASSERT_EQ("world", s);
32
33 s = android::base::GetProperty("this.property.does.not.exist", "");
34 ASSERT_EQ("", s);
35
36 s = android::base::GetProperty("this.property.does.not.exist", "default");
37 ASSERT_EQ("default", s);
38}
39
40TEST(properties, empty) {
41 // Because you can't delete a property, people "delete" them by
42 // setting them to the empty string. In that case we'd want to
43 // keep the default value (like cutils' property_get did).
44 android::base::SetProperty("debug.libbase.property_test", "");
45 std::string s = android::base::GetProperty("debug.libbase.property_test", "default");
46 ASSERT_EQ("default", s);
47}
48
49static void CheckGetBoolProperty(bool expected, const std::string& value, bool default_value) {
50 android::base::SetProperty("debug.libbase.property_test", value.c_str());
51 ASSERT_EQ(expected, android::base::GetBoolProperty("debug.libbase.property_test", default_value));
52}
53
54TEST(properties, GetBoolProperty_true) {
55 CheckGetBoolProperty(true, "1", false);
56 CheckGetBoolProperty(true, "y", false);
57 CheckGetBoolProperty(true, "yes", false);
58 CheckGetBoolProperty(true, "on", false);
59 CheckGetBoolProperty(true, "true", false);
60}
61
62TEST(properties, GetBoolProperty_false) {
63 CheckGetBoolProperty(false, "0", true);
64 CheckGetBoolProperty(false, "n", true);
65 CheckGetBoolProperty(false, "no", true);
66 CheckGetBoolProperty(false, "off", true);
67 CheckGetBoolProperty(false, "false", true);
68}
69
70TEST(properties, GetBoolProperty_default) {
71 CheckGetBoolProperty(true, "burp", true);
72 CheckGetBoolProperty(false, "burp", false);
73}
74
75template <typename T> void CheckGetIntProperty() {
76 // Positive and negative.
77 android::base::SetProperty("debug.libbase.property_test", "-12");
78 EXPECT_EQ(T(-12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
79 android::base::SetProperty("debug.libbase.property_test", "12");
80 EXPECT_EQ(T(12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
81
82 // Default value.
83 android::base::SetProperty("debug.libbase.property_test", "");
84 EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
85
86 // Bounds checks.
87 android::base::SetProperty("debug.libbase.property_test", "0");
88 EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
89 android::base::SetProperty("debug.libbase.property_test", "1");
90 EXPECT_EQ(T(1), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
91 android::base::SetProperty("debug.libbase.property_test", "2");
92 EXPECT_EQ(T(2), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
93 android::base::SetProperty("debug.libbase.property_test", "3");
94 EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
95}
96
97template <typename T> void CheckGetUintProperty() {
98 // Positive.
99 android::base::SetProperty("debug.libbase.property_test", "12");
100 EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 45));
101
102 // Default value.
103 android::base::SetProperty("debug.libbase.property_test", "");
104 EXPECT_EQ(T(45), android::base::GetUintProperty<T>("debug.libbase.property_test", 45));
105
106 // Bounds checks.
107 android::base::SetProperty("debug.libbase.property_test", "12");
108 EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 33, 22));
109 android::base::SetProperty("debug.libbase.property_test", "12");
110 EXPECT_EQ(T(5), android::base::GetUintProperty<T>("debug.libbase.property_test", 5, 10));
111}
112
113TEST(properties, GetIntProperty_int8_t) { CheckGetIntProperty<int8_t>(); }
114TEST(properties, GetIntProperty_int16_t) { CheckGetIntProperty<int16_t>(); }
115TEST(properties, GetIntProperty_int32_t) { CheckGetIntProperty<int32_t>(); }
116TEST(properties, GetIntProperty_int64_t) { CheckGetIntProperty<int64_t>(); }
117
118TEST(properties, GetUintProperty_uint8_t) { CheckGetUintProperty<uint8_t>(); }
119TEST(properties, GetUintProperty_uint16_t) { CheckGetUintProperty<uint16_t>(); }
120TEST(properties, GetUintProperty_uint32_t) { CheckGetUintProperty<uint32_t>(); }
121TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); }