1 /*
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 <unistd.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include <atomic>
24 #include <chrono>
25 #include <string>
26 #include <thread>
27 
28 #include <android-base/stringprintf.h>
29 
30 #if !defined(_WIN32)
31 using namespace std::literals;
32 #endif
33 
TEST(properties,smoke)34 TEST(properties, smoke) {
35   android::base::SetProperty("debug.libbase.property_test", "hello");
36 
37   std::string s = android::base::GetProperty("debug.libbase.property_test", "");
38   ASSERT_EQ("hello", s);
39 
40   android::base::SetProperty("debug.libbase.property_test", "world");
41   s = android::base::GetProperty("debug.libbase.property_test", "");
42   ASSERT_EQ("world", s);
43 
44   s = android::base::GetProperty("this.property.does.not.exist", "");
45   ASSERT_EQ("", s);
46 
47   s = android::base::GetProperty("this.property.does.not.exist", "default");
48   ASSERT_EQ("default", s);
49 }
50 
TEST(properties,too_long)51 TEST(properties, too_long) {
52 #if !defined(_WIN32)
53   if (getuid() != 0) {
54     GTEST_SKIP() << "Skipping test, must be run as root.";
55   }
56 #endif
57   // Properties have a fixed limit on the size of their value.
58   std::string key("debug.libbase.property_too_long");
59   std::string value(92, 'a');
60   ASSERT_FALSE(android::base::SetProperty(key, value));
61   ASSERT_EQ("missing", android::base::GetProperty(key, "missing"));
62 
63   // Except for "ro." properties, which can have arbitrarily-long values.
64   key = "ro." + key + std::to_string(time(nullptr));
65   ASSERT_TRUE(android::base::SetProperty(key, value));
66   ASSERT_EQ(value, android::base::GetProperty(key, "missing"));
67   // ...because you can't change them.
68   ASSERT_FALSE(android::base::SetProperty(key, "hello"));
69   ASSERT_EQ(value, android::base::GetProperty(key, "missing"));
70 }
71 
TEST(properties,empty_key)72 TEST(properties, empty_key) {
73   ASSERT_FALSE(android::base::SetProperty("", "hello"));
74   ASSERT_EQ("default", android::base::GetProperty("", "default"));
75 }
76 
TEST(properties,empty_value)77 TEST(properties, empty_value) {
78   // Because you can't delete a property, people "delete" them by
79   // setting them to the empty string. In that case we'd want to
80   // keep the default value (like cutils' property_get did).
81   ASSERT_TRUE(android::base::SetProperty("debug.libbase.property_empty_value", ""));
82   ASSERT_EQ("default", android::base::GetProperty("debug.libbase.property_empty_value", "default"));
83 }
84 
CheckGetBoolProperty(bool expected,const std::string & value,bool default_value)85 static void CheckGetBoolProperty(bool expected, const std::string& value, bool default_value) {
86   android::base::SetProperty("debug.libbase.property_test", value.c_str());
87   ASSERT_EQ(expected, android::base::GetBoolProperty("debug.libbase.property_test", default_value));
88 }
89 
TEST(properties,GetBoolProperty_true)90 TEST(properties, GetBoolProperty_true) {
91   CheckGetBoolProperty(true, "1", false);
92   CheckGetBoolProperty(true, "y", false);
93   CheckGetBoolProperty(true, "yes", false);
94   CheckGetBoolProperty(true, "on", false);
95   CheckGetBoolProperty(true, "true", false);
96 }
97 
TEST(properties,GetBoolProperty_false)98 TEST(properties, GetBoolProperty_false) {
99   CheckGetBoolProperty(false, "0", true);
100   CheckGetBoolProperty(false, "n", true);
101   CheckGetBoolProperty(false, "no", true);
102   CheckGetBoolProperty(false, "off", true);
103   CheckGetBoolProperty(false, "false", true);
104 }
105 
TEST(properties,GetBoolProperty_default)106 TEST(properties, GetBoolProperty_default) {
107   CheckGetBoolProperty(true, "burp", true);
108   CheckGetBoolProperty(false, "burp", false);
109 }
110 
CheckGetIntProperty()111 template <typename T> void CheckGetIntProperty() {
112   // Positive and negative.
113   android::base::SetProperty("debug.libbase.property_test", "-12");
114   EXPECT_EQ(T(-12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
115   android::base::SetProperty("debug.libbase.property_test", "12");
116   EXPECT_EQ(T(12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
117 
118   // Default value.
119   android::base::SetProperty("debug.libbase.property_test", "");
120   EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45));
121 
122   // Bounds checks.
123   android::base::SetProperty("debug.libbase.property_test", "0");
124   EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
125   android::base::SetProperty("debug.libbase.property_test", "1");
126   EXPECT_EQ(T(1), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
127   android::base::SetProperty("debug.libbase.property_test", "2");
128   EXPECT_EQ(T(2), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
129   android::base::SetProperty("debug.libbase.property_test", "3");
130   EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2));
131 }
132 
CheckGetUintProperty()133 template <typename T> void CheckGetUintProperty() {
134   // Positive.
135   android::base::SetProperty("debug.libbase.property_test", "12");
136   EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 45));
137 
138   // Default value.
139   android::base::SetProperty("debug.libbase.property_test", "");
140   EXPECT_EQ(T(45), android::base::GetUintProperty<T>("debug.libbase.property_test", 45));
141 
142   // Bounds checks.
143   android::base::SetProperty("debug.libbase.property_test", "12");
144   EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 33, 22));
145   android::base::SetProperty("debug.libbase.property_test", "12");
146   EXPECT_EQ(T(5), android::base::GetUintProperty<T>("debug.libbase.property_test", 5, 10));
147 }
148 
TEST(properties,GetIntProperty_int8_t)149 TEST(properties, GetIntProperty_int8_t) { CheckGetIntProperty<int8_t>(); }
TEST(properties,GetIntProperty_int16_t)150 TEST(properties, GetIntProperty_int16_t) { CheckGetIntProperty<int16_t>(); }
TEST(properties,GetIntProperty_int32_t)151 TEST(properties, GetIntProperty_int32_t) { CheckGetIntProperty<int32_t>(); }
TEST(properties,GetIntProperty_int64_t)152 TEST(properties, GetIntProperty_int64_t) { CheckGetIntProperty<int64_t>(); }
153 
TEST(properties,GetUintProperty_uint8_t)154 TEST(properties, GetUintProperty_uint8_t) { CheckGetUintProperty<uint8_t>(); }
TEST(properties,GetUintProperty_uint16_t)155 TEST(properties, GetUintProperty_uint16_t) { CheckGetUintProperty<uint16_t>(); }
TEST(properties,GetUintProperty_uint32_t)156 TEST(properties, GetUintProperty_uint32_t) { CheckGetUintProperty<uint32_t>(); }
TEST(properties,GetUintProperty_uint64_t)157 TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); }
158 
TEST(properties,WaitForProperty)159 TEST(properties, WaitForProperty) {
160 #if defined(__BIONIC__)
161   std::atomic<bool> flag{false};
162   std::thread thread([&]() {
163     std::this_thread::sleep_for(100ms);
164     android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
165     while (!flag) std::this_thread::yield();
166     android::base::SetProperty("debug.libbase.WaitForProperty_test", "b");
167   });
168 
169   ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "a", 1s));
170   flag = true;
171   ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", 1s));
172   thread.join();
173 #else
174   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
175 #endif
176 }
177 
TEST(properties,WaitForProperty_timeout)178 TEST(properties, WaitForProperty_timeout) {
179 #if defined(__BIONIC__)
180   auto t0 = std::chrono::steady_clock::now();
181   ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_timeout_test", "a",
182                                               200ms));
183   auto t1 = std::chrono::steady_clock::now();
184 
185   ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
186   // Upper bounds on timing are inherently flaky, but let's try...
187   ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
188 #else
189   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
190 #endif
191 }
192 
TEST(properties,WaitForProperty_MaxTimeout)193 TEST(properties, WaitForProperty_MaxTimeout) {
194 #if defined(__BIONIC__)
195   std::atomic<bool> flag{false};
196   std::thread thread([&]() {
197     android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
198     while (!flag) std::this_thread::yield();
199     std::this_thread::sleep_for(500ms);
200     android::base::SetProperty("debug.libbase.WaitForProperty_test", "b");
201   });
202 
203   ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "a", 1s));
204   flag = true;
205   // Test that this does not immediately return false due to overflow issues with the timeout.
206   ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b"));
207   thread.join();
208 #else
209   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
210 #endif
211 }
212 
TEST(properties,WaitForProperty_NegativeTimeout)213 TEST(properties, WaitForProperty_NegativeTimeout) {
214 #if defined(__BIONIC__)
215   std::atomic<bool> flag{false};
216   std::thread thread([&]() {
217     android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
218     while (!flag) std::this_thread::yield();
219     std::this_thread::sleep_for(500ms);
220     android::base::SetProperty("debug.libbase.WaitForProperty_test", "b");
221   });
222 
223   ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "a", 1s));
224   flag = true;
225   // Assert that this immediately returns with a negative timeout
226   ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", -100ms));
227   thread.join();
228 #else
229   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
230 #endif
231 }
232 
TEST(properties,WaitForPropertyCreation)233 TEST(properties, WaitForPropertyCreation) {
234 #if defined(__BIONIC__)
235   std::thread thread([&]() {
236     std::this_thread::sleep_for(100ms);
237     android::base::SetProperty("debug.libbase.WaitForPropertyCreation_test", "a");
238   });
239 
240   ASSERT_TRUE(android::base::WaitForPropertyCreation(
241           "debug.libbase.WaitForPropertyCreation_test", 1s));
242   thread.join();
243 #else
244   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
245 #endif
246 }
247 
TEST(properties,WaitForPropertyCreation_timeout)248 TEST(properties, WaitForPropertyCreation_timeout) {
249 #if defined(__BIONIC__)
250   auto t0 = std::chrono::steady_clock::now();
251   ASSERT_FALSE(android::base::WaitForPropertyCreation(
252           "debug.libbase.WaitForPropertyCreation_timeout_test", 200ms));
253   auto t1 = std::chrono::steady_clock::now();
254 
255   ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
256   // Upper bounds on timing are inherently flaky, but let's try...
257   ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
258 #else
259   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
260 #endif
261 }
262 
TEST(properties,CachedProperty)263 TEST(properties, CachedProperty) {
264 #if defined(__BIONIC__)
265   android::base::CachedProperty cached_property("debug.libbase.CachedProperty_test");
266   bool changed;
267   cached_property.Get(&changed);
268 
269   android::base::SetProperty("debug.libbase.CachedProperty_test", "foo");
270   ASSERT_STREQ("foo", cached_property.Get(&changed));
271   ASSERT_TRUE(changed);
272 
273   ASSERT_STREQ("foo", cached_property.Get(&changed));
274   ASSERT_FALSE(changed);
275 
276   android::base::SetProperty("debug.libbase.CachedProperty_test", "bar");
277   ASSERT_STREQ("bar", cached_property.Get(&changed));
278   ASSERT_TRUE(changed);
279 
280   ASSERT_STREQ("bar", cached_property.Get(&changed));
281   ASSERT_FALSE(changed);
282 
283 #else
284   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
285 #endif
286 }
287 
SetAfter(const std::string & key,const std::string & value,std::chrono::milliseconds delay)288 void SetAfter(const std::string& key, const std::string& value, std::chrono::milliseconds delay) {
289   std::thread([key, value, delay]() {
290     std::this_thread::sleep_for(delay);
291     android::base::SetProperty(key, value);
292   }).detach();
293 }
294 
TEST(properties,CachedProperty_WaitForChange)295 TEST(properties, CachedProperty_WaitForChange) {
296 #if defined(__BIONIC__)
297   unsigned long now =
298       std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
299   std::string key = android::base::StringPrintf("debug.libbase.CachedProperty_test_%lu", now);
300   android::base::CachedProperty cached_property(key);
301 
302   // If the property doesn't exist yet, Get returns the empty string.
303   EXPECT_STREQ("", cached_property.Get());
304 
305   // Property doesn't exist yet, timeout.
306   EXPECT_EQ(nullptr, cached_property.WaitForChange(0ms));
307   EXPECT_EQ(nullptr, cached_property.WaitForChange(100ms));
308 
309   // Property doesn't exist yet, success.
310   SetAfter(key, "foo", 100ms);
311   EXPECT_STREQ("foo", cached_property.WaitForChange());
312 
313   // Property exists, timeout.
314   EXPECT_EQ(nullptr, cached_property.WaitForChange(0ms));
315   EXPECT_EQ(nullptr, cached_property.WaitForChange(100ms));
316 
317   // Property exists, success.
318   SetAfter(key, "bar", 100ms);
319   EXPECT_STREQ("bar", cached_property.WaitForChange());
320 #else
321   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
322 #endif
323 }
324 
TEST(properties,CachedBoolProperty)325 TEST(properties, CachedBoolProperty) {
326 #if defined(__BIONIC__)
327   unsigned long now =
328       std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
329   std::string key = android::base::StringPrintf("debug.libbase.CachedBoolProperty_test_%lu", now);
330   android::base::CachedBoolProperty cached_bool_property(key);
331 
332   // Not set yet.
333   EXPECT_EQ(std::nullopt, cached_bool_property.GetOptional());
334 
335   android::base::SetProperty(key, "foo");
336   EXPECT_EQ(std::nullopt, cached_bool_property.GetOptional());
337 
338   android::base::SetProperty(key, "1");
339   EXPECT_EQ(std::optional(true), cached_bool_property.GetOptional());
340 
341   android::base::SetProperty(key, "0");
342   EXPECT_EQ(std::optional(false), cached_bool_property.GetOptional());
343 #else
344   GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
345 #endif
346 }
347