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