1 /*
2  * Copyright (C) 2013 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 <gtest/gtest.h>
18 
19 #include <errno.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
23 #include <string>
24 #include <thread>
25 
26 #include <android-base/file.h>
27 #include <android-base/silent_death_test.h>
28 #include <android-base/stringprintf.h>
29 
30 #include "utils.h"
31 
32 using namespace std::literals;
33 
34 #if defined(__BIONIC__)
35 
36 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37 #include <stdlib.h>
38 #include <sys/_system_properties.h>
39 #include <sys/mount.h>
40 
41 #include <system_properties/system_properties.h>
42 
43 class SystemPropertiesTest : public SystemProperties {
44  public:
SystemPropertiesTest()45   SystemPropertiesTest() : SystemProperties(false) {
46     appcompat_path = android::base::StringPrintf("%s/appcompat_override", dir_.path);
47     mount_path = android::base::StringPrintf("%s/__properties__", dir_.path);
48     mkdir(appcompat_path.c_str(), S_IRWXU | S_IXGRP | S_IXOTH);
49     valid_ = AreaInit(dir_.path, nullptr, true);
50   }
~SystemPropertiesTest()51   ~SystemPropertiesTest() {
52     if (valid_) {
53       contexts_->FreeAndUnmap();
54     }
55     umount2(dir_.path, MNT_DETACH);
56     umount2(real_sysprop_dir.c_str(), MNT_DETACH);
57   }
58 
valid() const59   bool valid() const {
60     return valid_;
61   }
62 
get_path() const63   const char* get_path() const { return dir_.path; }
64 
get_appcompat_path() const65   const char* get_appcompat_path() const { return appcompat_path.c_str(); }
66 
get_mount_path() const67   const char* get_mount_path() const { return mount_path.c_str(); }
68 
get_real_sysprop_dir() const69   const char* get_real_sysprop_dir() const { return real_sysprop_dir.c_str(); }
70 
71   std::string appcompat_path;
72   std::string mount_path;
73   std::string real_sysprop_dir = "/dev/__properties__";
74 
75  private:
76   TemporaryDir dir_;
77   bool valid_;
78 };
79 
foreach_test_callback(const prop_info * pi,void * cookie)80 static void foreach_test_callback(const prop_info *pi, void* cookie) {
81     size_t *count = static_cast<size_t *>(cookie);
82 
83     ASSERT_TRUE(pi != nullptr);
84     (*count)++;
85 }
86 
hierarchical_test_callback(const prop_info * pi,void * cookie)87 static void hierarchical_test_callback(const prop_info *pi, void *cookie) {
88     bool (*ok)[8][8] = static_cast<bool (*)[8][8]>(cookie);
89 
90     char name[PROP_NAME_MAX];
91     char value[PROP_VALUE_MAX];
92 
93     __system_property_read(pi, name, value);
94 
95     int name_i, name_j, name_k;
96     int value_i, value_j, value_k;
97     ASSERT_EQ(3, sscanf(name, "property_%d.%d.%d", &name_i, &name_j, &name_k));
98     ASSERT_EQ(3, sscanf(value, "value_%d.%d.%d", &value_i, &value_j, &value_k));
99     ASSERT_EQ(name_i, value_i);
100     ASSERT_GE(name_i, 0);
101     ASSERT_LT(name_i, 8);
102     ASSERT_EQ(name_j, value_j);
103     ASSERT_GE(name_j, 0);
104     ASSERT_LT(name_j, 8);
105     ASSERT_EQ(name_k, value_k);
106     ASSERT_GE(name_k, 0);
107     ASSERT_LT(name_k, 8);
108 
109     ok[name_i][name_j][name_k] = true;
110 }
111 
112 #endif // __BIONIC__
113 
TEST(properties,__system_property_add)114 TEST(properties, __system_property_add) {
115 #if defined(__BIONIC__)
116     SystemPropertiesTest system_properties;
117     ASSERT_TRUE(system_properties.valid());
118 
119     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
120     ASSERT_EQ(0, system_properties.Add("other_property", 14, "value2", 6));
121     ASSERT_EQ(0, system_properties.Add("property_other", 14, "value3", 6));
122 
123     // check that there is no limit on property name length
124     char name[PROP_NAME_MAX + 11];
125     name[0] = 'p';
126     for (size_t i = 1; i < sizeof(name); i++) {
127       name[i] = 'x';
128     }
129 
130     name[sizeof(name)-1] = '\0';
131     ASSERT_EQ(0, system_properties.Add(name, strlen(name), "value", 5));
132 
133     char propvalue[PROP_VALUE_MAX];
134     ASSERT_EQ(6, system_properties.Get("property", propvalue));
135     ASSERT_STREQ(propvalue, "value1");
136 
137     ASSERT_EQ(6, system_properties.Get("other_property", propvalue));
138     ASSERT_STREQ(propvalue, "value2");
139 
140     ASSERT_EQ(6, system_properties.Get("property_other", propvalue));
141     ASSERT_STREQ(propvalue, "value3");
142 
143     ASSERT_EQ(5, system_properties.Get(name, propvalue));
144     ASSERT_STREQ(propvalue, "value");
145 #else // __BIONIC__
146     GTEST_SKIP() << "bionic-only test";
147 #endif // __BIONIC__
148 }
149 
TEST(properties,__system_property_add_appcompat)150 TEST(properties, __system_property_add_appcompat) {
151 #if defined(__BIONIC__)
152     if (getuid() != 0) GTEST_SKIP() << "test requires root";
153     SystemPropertiesTest system_properties;
154     ASSERT_TRUE(system_properties.valid());
155 
156     char name[] = "ro.property";
157     char override_name[] = "ro.appcompat_override.ro.property";
158     char name_not_written[] = "ro.property_other";
159     char override_with_no_real[] = "ro.appcompat_override.ro.property_other";
160     ASSERT_EQ(0, system_properties.Add(name, strlen(name), "value1", 6));
161     ASSERT_EQ(0, system_properties.Add(override_name, strlen(override_name), "value2", 6));
162     ASSERT_EQ(0, system_properties.Add(override_with_no_real, strlen(override_with_no_real),
163                                        "value3", 6));
164 
165     char propvalue[PROP_VALUE_MAX];
166     ASSERT_EQ(6, system_properties.Get(name, propvalue));
167     ASSERT_STREQ(propvalue, "value1");
168 
169     ASSERT_EQ(6, system_properties.Get(override_name, propvalue));
170     ASSERT_STREQ(propvalue, "value2");
171 
172     ASSERT_EQ(0, system_properties.Get(name_not_written, propvalue));
173     ASSERT_STREQ(propvalue, "");
174 
175     ASSERT_EQ(6, system_properties.Get(override_with_no_real, propvalue));
176     ASSERT_STREQ(propvalue, "value3");
177 
178     int ret = mount(system_properties.get_appcompat_path(), system_properties.get_path(), nullptr,
179                     MS_BIND | MS_REC, nullptr);
180     if (ret != 0) {
181       ASSERT_ERRNO(0);
182     }
183     system_properties.Reload(true);
184 
185     ASSERT_EQ(6, system_properties.Get(name, propvalue));
186     ASSERT_STREQ(propvalue, "value2");
187 
188     ASSERT_EQ(0, system_properties.Get(override_name, propvalue));
189     ASSERT_STREQ(propvalue, "");
190 
191     ASSERT_EQ(6, system_properties.Get(name_not_written, propvalue));
192     ASSERT_STREQ(propvalue, "value3");
193 
194     ASSERT_EQ(0, system_properties.Get(override_with_no_real, propvalue));
195     ASSERT_STREQ(propvalue, "");
196 
197 #else   // __BIONIC__
198     GTEST_SKIP() << "bionic-only test";
199 #endif  // __BIONIC__
200 }
201 
TEST(properties,__system_property_update)202 TEST(properties, __system_property_update) {
203 #if defined(__BIONIC__)
204     SystemPropertiesTest system_properties;
205     ASSERT_TRUE(system_properties.valid());
206 
207     ASSERT_EQ(0, system_properties.Add("property", 8, "oldvalue1", 9));
208     ASSERT_EQ(0, system_properties.Add("other_property", 14, "value2", 6));
209     ASSERT_EQ(0, system_properties.Add("property_other", 14, "value3", 6));
210 
211     const prop_info* pi = system_properties.Find("property");
212     ASSERT_TRUE(pi != nullptr);
213     system_properties.Update(const_cast<prop_info*>(pi), "value4", 6);
214 
215     pi = system_properties.Find("other_property");
216     ASSERT_TRUE(pi != nullptr);
217     system_properties.Update(const_cast<prop_info*>(pi), "newvalue5", 9);
218 
219     pi = system_properties.Find("property_other");
220     ASSERT_TRUE(pi != nullptr);
221     system_properties.Update(const_cast<prop_info*>(pi), "value6", 6);
222 
223     char propvalue[PROP_VALUE_MAX];
224     ASSERT_EQ(6, system_properties.Get("property", propvalue));
225     ASSERT_STREQ(propvalue, "value4");
226 
227     ASSERT_EQ(9, system_properties.Get("other_property", propvalue));
228     ASSERT_STREQ(propvalue, "newvalue5");
229 
230     ASSERT_EQ(6, system_properties.Get("property_other", propvalue));
231     ASSERT_STREQ(propvalue, "value6");
232 #else // __BIONIC__
233     GTEST_SKIP() << "bionic-only test";
234 #endif // __BIONIC__
235 }
236 
TEST(properties,fill)237 TEST(properties, fill) {
238 #if defined(__BIONIC__)
239     SystemPropertiesTest system_properties;
240     ASSERT_TRUE(system_properties.valid());
241 
242     char prop_name[PROP_NAME_MAX];
243     char prop_value[PROP_VALUE_MAX];
244     char prop_value_ret[PROP_VALUE_MAX];
245     int count = 0;
246     int ret;
247 
248     while (true) {
249         ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d", count);
250         memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
251         ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d", count);
252         memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
253         prop_name[PROP_NAME_MAX - 1] = 0;
254         prop_value[PROP_VALUE_MAX - 1] = 0;
255 
256         ret = system_properties.Add(prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1);
257         if (ret < 0)
258             break;
259 
260         count++;
261     }
262 
263     // For historical reasons at least 247 properties must be supported
264     ASSERT_GE(count, 247);
265 
266     for (int i = 0; i < count; i++) {
267         ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d", i);
268         memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
269         ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d", i);
270         memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
271         prop_name[PROP_NAME_MAX - 1] = 0;
272         prop_value[PROP_VALUE_MAX - 1] = 0;
273         memset(prop_value_ret, '\0', PROP_VALUE_MAX);
274 
275         ASSERT_EQ(PROP_VALUE_MAX - 1, system_properties.Get(prop_name, prop_value_ret));
276         ASSERT_EQ(0, memcmp(prop_value, prop_value_ret, PROP_VALUE_MAX));
277     }
278 #else // __BIONIC__
279     GTEST_SKIP() << "bionic-only test";
280 #endif // __BIONIC__
281 }
282 
TEST(properties,__system_property_foreach)283 TEST(properties, __system_property_foreach) {
284 #if defined(__BIONIC__)
285     SystemPropertiesTest system_properties;
286     ASSERT_TRUE(system_properties.valid());
287 
288     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
289     ASSERT_EQ(0, system_properties.Add("other_property", 14, "value2", 6));
290     ASSERT_EQ(0, system_properties.Add("property_other", 14, "value3", 6));
291 
292     size_t count = 0;
293     ASSERT_EQ(0, system_properties.Foreach(foreach_test_callback, &count));
294     ASSERT_EQ(3U, count);
295 #else // __BIONIC__
296     GTEST_SKIP() << "bionic-only test";
297 #endif // __BIONIC__
298 }
299 
TEST(properties,__system_property_find_nth)300 TEST(properties, __system_property_find_nth) {
301 #if defined(__BIONIC__)
302     SystemPropertiesTest system_properties;
303     ASSERT_TRUE(system_properties.valid());
304 
305     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
306     ASSERT_EQ(0, system_properties.Add("other_property", 14, "value2", 6));
307     ASSERT_EQ(0, system_properties.Add("property_other", 14, "value3", 6));
308 
309     char name[PROP_NAME_MAX];
310     char value[PROP_VALUE_MAX];
311     EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(0), name, value));
312     EXPECT_STREQ("property", name);
313     EXPECT_STREQ("value1", value);
314     EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(1), name, value));
315     EXPECT_STREQ("other_property", name);
316     EXPECT_STREQ("value2", value);
317     EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(2), name, value));
318     EXPECT_STREQ("property_other", name);
319     EXPECT_STREQ("value3", value);
320 
321     for (unsigned i = 3; i < 1024; ++i) {
322       ASSERT_TRUE(system_properties.FindNth(i) == nullptr);
323     }
324 #else // __BIONIC__
325     GTEST_SKIP() << "bionic-only test";
326 #endif // __BIONIC__
327 }
328 
TEST(properties,fill_hierarchical)329 TEST(properties, fill_hierarchical) {
330 #if defined(__BIONIC__)
331     SystemPropertiesTest system_properties;
332     ASSERT_TRUE(system_properties.valid());
333 
334     char prop_name[PROP_NAME_MAX];
335     char prop_value[PROP_VALUE_MAX];
336     char prop_value_ret[PROP_VALUE_MAX];
337     int ret;
338 
339     for (int i = 0; i < 8; i++) {
340         for (int j = 0; j < 8; j++) {
341             for (int k = 0; k < 8; k++) {
342                 ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d.%d.%d", i, j, k);
343                 memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
344                 ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d.%d.%d", i, j, k);
345                 memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
346                 prop_name[PROP_NAME_MAX - 1] = 0;
347                 prop_value[PROP_VALUE_MAX - 1] = 0;
348 
349                 ASSERT_EQ(0, system_properties.Add(
350                     prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1));
351             }
352         }
353     }
354 
355     for (int i = 0; i < 8; i++) {
356         for (int j = 0; j < 8; j++) {
357             for (int k = 0; k < 8; k++) {
358                 ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d.%d.%d", i, j, k);
359                 memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
360                 ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d.%d.%d", i, j, k);
361                 memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
362                 prop_name[PROP_NAME_MAX - 1] = 0;
363                 prop_value[PROP_VALUE_MAX - 1] = 0;
364                 memset(prop_value_ret, '\0', PROP_VALUE_MAX);
365 
366                 ASSERT_EQ(PROP_VALUE_MAX - 1, system_properties.Get(prop_name, prop_value_ret));
367                 ASSERT_EQ(0, memcmp(prop_value, prop_value_ret, PROP_VALUE_MAX));
368             }
369         }
370     }
371 
372     bool ok[8][8][8];
373     memset(ok, 0, sizeof(ok));
374     system_properties.Foreach(hierarchical_test_callback, ok);
375 
376     for (int i = 0; i < 8; i++) {
377         for (int j = 0; j < 8; j++) {
378             for (int k = 0; k < 8; k++) {
379                 ASSERT_TRUE(ok[i][j][k]);
380             }
381         }
382     }
383 #else // __BIONIC__
384     GTEST_SKIP() << "bionic-only test";
385 #endif // __BIONIC__
386 }
387 
TEST(properties,errors)388 TEST(properties, errors) {
389 #if defined(__BIONIC__)
390     SystemPropertiesTest system_properties;
391     ASSERT_TRUE(system_properties.valid());
392 
393     char prop_value[PROP_NAME_MAX];
394 
395     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
396     ASSERT_EQ(0, system_properties.Add("other_property", 14, "value2", 6));
397     ASSERT_EQ(0, system_properties.Add("property_other", 14, "value3", 6));
398 
399     ASSERT_EQ(0, system_properties.Find("property1"));
400     ASSERT_EQ(0, system_properties.Get("property1", prop_value));
401 
402     ASSERT_EQ(-1, system_properties.Add("name", 4, "value", PROP_VALUE_MAX));
403     ASSERT_EQ(-1, system_properties.Update(NULL, "value", PROP_VALUE_MAX));
404 #else // __BIONIC__
405     GTEST_SKIP() << "bionic-only test";
406 #endif // __BIONIC__
407 }
408 
TEST(properties,__system_property_serial)409 TEST(properties, __system_property_serial) {
410 #if defined(__BIONIC__)
411     SystemPropertiesTest system_properties;
412     ASSERT_TRUE(system_properties.valid());
413 
414     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
415     const prop_info* pi = system_properties.Find("property");
416     ASSERT_TRUE(pi != nullptr);
417     unsigned serial = __system_property_serial(pi);
418     ASSERT_EQ(0, system_properties.Update(const_cast<prop_info*>(pi), "value2", 6));
419     ASSERT_NE(serial, __system_property_serial(pi));
420 #else // __BIONIC__
421     GTEST_SKIP() << "bionic-only test";
422 #endif // __BIONIC__
423 }
424 
TEST(properties,__system_property_wait_any)425 TEST(properties, __system_property_wait_any) {
426 #if defined(__BIONIC__)
427     SystemPropertiesTest system_properties;
428     ASSERT_TRUE(system_properties.valid());
429 
430     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
431     unsigned serial = system_properties.WaitAny(0);
432 
433     prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
434     ASSERT_TRUE(pi != nullptr);
435     system_properties.Update(pi, "value2", 6);
436     serial = system_properties.WaitAny(serial);
437 
438     int flag = 0;
439     std::thread thread([&system_properties, &flag]() {
440         prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
441         usleep(100000);
442 
443         flag = 1;
444         system_properties.Update(pi, "value3", 6);
445     });
446     ASSERT_EQ(flag, 0);
447     serial = system_properties.WaitAny(serial);
448     ASSERT_EQ(flag, 1);
449 
450     thread.join();
451 #else // __BIONIC__
452     GTEST_SKIP() << "bionic-only test";
453 #endif // __BIONIC__
454 }
455 
TEST(properties,__system_property_wait)456 TEST(properties, __system_property_wait) {
457 #if defined(__BIONIC__)
458     SystemPropertiesTest system_properties;
459     ASSERT_TRUE(system_properties.valid());
460 
461     ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
462 
463     prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
464     ASSERT_TRUE(pi != nullptr);
465 
466     unsigned serial = __system_property_serial(pi);
467 
468     std::thread thread([&system_properties]() {
469         prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
470         ASSERT_TRUE(pi != nullptr);
471 
472         system_properties.Update(pi, "value2", 6);
473     });
474 
475     uint32_t new_serial;
476     system_properties.Wait(pi, serial, &new_serial, nullptr);
477     ASSERT_GT(new_serial, serial);
478 
479     char value[PROP_VALUE_MAX];
480     ASSERT_EQ(6, system_properties.Get("property", value));
481     ASSERT_STREQ("value2", value);
482 
483     thread.join();
484 #else // __BIONIC__
485     GTEST_SKIP() << "bionic-only test";
486 #endif // __BIONIC__
487 }
488 
489 class KilledByFault {
490     public:
KilledByFault()491         explicit KilledByFault() {};
492         bool operator()(int exit_status) const;
493 };
494 
operator ()(int exit_status) const495 bool KilledByFault::operator()(int exit_status) const {
496     return WIFSIGNALED(exit_status) &&
497         (WTERMSIG(exit_status) == SIGSEGV ||
498          WTERMSIG(exit_status) == SIGBUS ||
499          WTERMSIG(exit_status) == SIGABRT);
500 }
501 
502 using properties_DeathTest = SilentDeathTest;
503 
TEST_F(properties_DeathTest,read_only)504 TEST_F(properties_DeathTest, read_only) {
505 #if defined(__BIONIC__)
506 
507   // This test only makes sense if we're talking to the real system property service.
508   struct stat sb;
509   ASSERT_FALSE(stat(PROP_DIRNAME, &sb) == -1 && errno == ENOENT);
510 
511   ASSERT_EXIT(__system_property_add("property", 8, "value", 5), KilledByFault(), "");
512 #else // __BIONIC__
513   GTEST_SKIP() << "bionic-only test";
514 #endif // __BIONIC__
515 }
516 
TEST(properties,__system_property_extra_long_read_only)517 TEST(properties, __system_property_extra_long_read_only) {
518 #if defined(__BIONIC__)
519   SystemPropertiesTest system_properties;
520   ASSERT_TRUE(system_properties.valid());
521 
522   std::vector<std::pair<std::string, std::string>> short_properties = {
523     { "ro.0char", std::string() },
524     { "ro.50char", std::string(50, 'x') },
525     { "ro.91char", std::string(91, 'x') },
526   };
527 
528   std::vector<std::pair<std::string, std::string>> long_properties = {
529     { "ro.92char", std::string(92, 'x') },
530     { "ro.93char", std::string(93, 'x') },
531     { "ro.1000char", std::string(1000, 'x') },
532   };
533 
534   for (const auto& property : short_properties) {
535     const std::string& name = property.first;
536     const std::string& value = property.second;
537     ASSERT_EQ(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
538   }
539 
540   for (const auto& property : long_properties) {
541     const std::string& name = property.first;
542     const std::string& value = property.second;
543     ASSERT_EQ(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
544   }
545 
546   auto check_with_legacy_read = [&system_properties](const std::string& name,
547                                                      const std::string& expected_value) {
548     char value[PROP_VALUE_MAX];
549     EXPECT_EQ(static_cast<int>(expected_value.size()), system_properties.Get(name.c_str(), value))
550         << name;
551     EXPECT_EQ(expected_value, value) << name;
552   };
553 
554   auto check_with_read_callback = [&system_properties](const std::string& name,
555                                                        const std::string& expected_value) {
556     const prop_info* pi = system_properties.Find(name.c_str());
557     ASSERT_NE(nullptr, pi);
558     std::string value;
559     system_properties.ReadCallback(pi,
560                                    [](void* cookie, const char*, const char* value, uint32_t) {
561                                      auto* out_value = reinterpret_cast<std::string*>(cookie);
562                                      *out_value = value;
563                                    },
564                                    &value);
565     EXPECT_EQ(expected_value, value) << name;
566   };
567 
568   for (const auto& property : short_properties) {
569     const std::string& name = property.first;
570     const std::string& value = property.second;
571     check_with_legacy_read(name, value);
572     check_with_read_callback(name, value);
573   }
574 
575   static constexpr const char* kExtraLongLegacyError =
576       "Must use __system_property_read_callback() to read";
577   for (const auto& property : long_properties) {
578     const std::string& name = property.first;
579     const std::string& value = property.second;
580     check_with_legacy_read(name, kExtraLongLegacyError);
581     check_with_read_callback(name, value);
582   }
583 
584 #else   // __BIONIC__
585   GTEST_SKIP() << "bionic-only test";
586 #endif  // __BIONIC__
587 }
588 
589 // pa_size is 128 * 1024 currently, if a property is longer then we expect it to fail gracefully.
TEST(properties,__system_property_extra_long_read_only_too_long)590 TEST(properties, __system_property_extra_long_read_only_too_long) {
591 #if defined(__BIONIC__)
592   SystemPropertiesTest system_properties;
593   ASSERT_TRUE(system_properties.valid());
594 
595   auto name = "ro.super_long_property"s;
596 
597 #ifdef LARGE_SYSTEM_PROPERTY_NODE
598   auto value = std::string(1024 * 1024 + 1, 'x');
599 #else
600   auto value = std::string(128 * 1024 + 1, 'x');
601 #endif
602 
603   ASSERT_NE(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
604 
605 #else   // __BIONIC__
606   GTEST_SKIP() << "bionic-only test";
607 #endif  // __BIONIC__
608 }
609 
610 // Note that this test affects global state of the system
611 // this tests tries to mitigate this by using utime+pid
612 // prefix for the property name. It is still results in
613 // pollution of property service since properties cannot
614 // be removed.
615 //
616 // Note that there is also possibility to run into "out-of-memory"
617 // if this test if it is executed often enough without reboot.
TEST(properties,__system_property_reload_no_op)618 TEST(properties, __system_property_reload_no_op) {
619 #if defined(__BIONIC__)
620   std::string property_name =
621       android::base::StringPrintf("debug.test.%d.%" PRId64 ".property", getpid(), NanoTime());
622   ASSERT_EQ(0, __system_property_find(property_name.c_str()));
623   ASSERT_EQ(0, __system_property_set(property_name.c_str(), "test value"));
624   ASSERT_EQ(0, __system_properties_zygote_reload());
625   const prop_info* readptr = __system_property_find(property_name.c_str());
626   std::string expected_name = property_name;
627   __system_property_read_callback(
628       readptr,
629       [](void*, const char*, const char* value, unsigned) { ASSERT_STREQ("test value", value); },
630       &expected_name);
631 #else   // __BIONIC__
632   GTEST_SKIP() << "bionic-only test";
633 #endif  // __BIONIC__
634 }
635 
TEST(properties,__system_property_reload_invalid)636 TEST(properties, __system_property_reload_invalid) {
637 #if defined(__BIONIC__)
638   if (getuid() != 0) GTEST_SKIP() << "test requires root";
639   SystemPropertiesTest system_properties;
640 
641   // Create an invalid property_info file, so the system will attempt to initialize a
642   // ContextSerialized
643   std::string property_info_file =
644       android::base::StringPrintf("%s/property_info", system_properties.get_path());
645   fclose(fopen(property_info_file.c_str(), "w"));
646   int ret = mount(system_properties.get_path(), system_properties.get_real_sysprop_dir(), nullptr,
647                   MS_BIND | MS_REC, nullptr);
648   if (ret != 0) {
649     ASSERT_ERRNO(0);
650   }
651 
652   ASSERT_EQ(-1, __system_properties_zygote_reload());
653 #else   // __BIONIC__
654   GTEST_SKIP() << "bionic-only test";
655 #endif  // __BIONIC__
656 }
657 
658 // Note that this test affects global state of the system
659 // this tests tries to mitigate this by using utime+pid
660 // prefix for the property name. It is still results in
661 // pollution of property service since properties cannot
662 // be removed.
663 //
664 // Note that there is also possibility to run into "out-of-memory"
665 // if this test if it is executed often enough without reboot.
TEST(properties,__system_property_reload_valid)666 TEST(properties, __system_property_reload_valid) {
667 #if defined(__BIONIC__)
668   if (getuid() != 0) GTEST_SKIP() << "test requires root";
669   SystemPropertiesTest system_properties;
670 
671   // Copy the system properties files into the temp directory
672   std::string shell_cmd = android::base::StringPrintf(
673       "cp -r %s %s", system_properties.get_real_sysprop_dir(), system_properties.get_path());
674   system(shell_cmd.c_str());
675 
676   // Write a system property to the current set of system properties
677   std::string property_name =
678       android::base::StringPrintf("debug.test.%d.%" PRId64 ".property", getpid(), NanoTime());
679   ASSERT_EQ(0, __system_property_find(property_name.c_str()));
680   ASSERT_EQ(0, __system_property_set(property_name.c_str(), "test value"));
681 
682   // Mount the temp directory (which doesn't have the property we just wrote) in place of the
683   // real one
684   int ret = mount(system_properties.get_mount_path(), system_properties.get_real_sysprop_dir(),
685                   nullptr, MS_BIND | MS_REC, nullptr);
686   if (ret != 0) {
687     ASSERT_ERRNO(0);
688   }
689 
690   // reload system properties in the new dir, and verify the property we wrote after we copied the
691   // files isn't there
692   ASSERT_EQ(0, __system_properties_zygote_reload());
693   ASSERT_EQ(0, __system_property_find(property_name.c_str()));
694 
695 #else   // __BIONIC__
696   GTEST_SKIP() << "bionic-only test";
697 #endif  // __BIONIC__
698 }
699