blob: 5b5e00955274cd278bcb1094448cd2cc979d8c24 [file] [log] [blame]
Colin Crossb27e2002013-01-28 17:19:43 -08001/*
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>
Yabin Cui9df70402014-11-05 18:01:01 -080018
Elliott Hughese4375192013-10-21 17:09:52 -070019#include <errno.h>
Yabin Cui9df70402014-11-05 18:01:01 -080020#include <sys/wait.h>
Greg Hackmanncb215a72013-02-13 14:41:48 -080021#include <unistd.h>
Elliott Hughesa0d374d2017-02-10 18:13:46 -080022
Greg Hackmanncb215a72013-02-13 14:41:48 -080023#include <string>
Elliott Hughesa0d374d2017-02-10 18:13:46 -080024#include <thread>
Colin Crossb27e2002013-01-28 17:19:43 -080025
Mark Salyzynba1a7232018-11-14 15:19:53 -080026#include <android-base/file.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070027#include <android-base/silent_death_test.h>
Nate Myrenb8c87b12023-08-28 16:46:39 -070028#include <android-base/stringprintf.h>
29
30#include "utils.h"
Tom Cherrye275d6d2017-12-11 23:31:33 -080031
Tom Cherry8a311632017-10-09 13:49:17 -070032using namespace std::literals;
33
Christopher Ferrisf04935c2013-12-20 18:43:21 -080034#if defined(__BIONIC__)
Colin Crossb27e2002013-01-28 17:19:43 -080035
Nate Myren0ab06152023-10-19 16:50:59 -070036#include <stdlib.h>
Nate Myrenb8c87b12023-08-28 16:46:39 -070037#include <sys/mount.h>
Elliott Hughes620eec12024-08-07 17:59:53 +000038#include <sys/system_properties.h>
Colin Crossb27e2002013-01-28 17:19:43 -080039
Tom Cherrye275d6d2017-12-11 23:31:33 -080040#include <system_properties/system_properties.h>
Greg Hackmanncb215a72013-02-13 14:41:48 -080041
Tom Cherrye275d6d2017-12-11 23:31:33 -080042class SystemPropertiesTest : public SystemProperties {
43 public:
44 SystemPropertiesTest() : SystemProperties(false) {
Nate Myrenb8c87b12023-08-28 16:46:39 -070045 appcompat_path = android::base::StringPrintf("%s/appcompat_override", dir_.path);
Nate Myren0ab06152023-10-19 16:50:59 -070046 mount_path = android::base::StringPrintf("%s/__properties__", dir_.path);
Nate Myrenb8c87b12023-08-28 16:46:39 -070047 mkdir(appcompat_path.c_str(), S_IRWXU | S_IXGRP | S_IXOTH);
48 valid_ = AreaInit(dir_.path, nullptr, true);
Tom Cherrye275d6d2017-12-11 23:31:33 -080049 }
50 ~SystemPropertiesTest() {
51 if (valid_) {
Tom Cherryee8e3dd2018-02-21 15:01:22 -080052 contexts_->FreeAndUnmap();
Colin Crossb27e2002013-01-28 17:19:43 -080053 }
Nate Myren0ab06152023-10-19 16:50:59 -070054 umount2(dir_.path, MNT_DETACH);
55 umount2(real_sysprop_dir.c_str(), MNT_DETACH);
Tom Cherrye275d6d2017-12-11 23:31:33 -080056 }
Colin Crossb27e2002013-01-28 17:19:43 -080057
Tom Cherrye275d6d2017-12-11 23:31:33 -080058 bool valid() const {
59 return valid_;
60 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080061
Nate Myrenb8c87b12023-08-28 16:46:39 -070062 const char* get_path() const { return dir_.path; }
63
64 const char* get_appcompat_path() const { return appcompat_path.c_str(); }
65
Nate Myren0ab06152023-10-19 16:50:59 -070066 const char* get_mount_path() const { return mount_path.c_str(); }
67
68 const char* get_real_sysprop_dir() const { return real_sysprop_dir.c_str(); }
69
Nate Myrenb8c87b12023-08-28 16:46:39 -070070 std::string appcompat_path;
Nate Myren0ab06152023-10-19 16:50:59 -070071 std::string mount_path;
72 std::string real_sysprop_dir = "/dev/__properties__";
Nate Myrenb8c87b12023-08-28 16:46:39 -070073
Tom Cherrye275d6d2017-12-11 23:31:33 -080074 private:
75 TemporaryDir dir_;
76 bool valid_;
Colin Crossb27e2002013-01-28 17:19:43 -080077};
78
Christopher Ferrisf04935c2013-12-20 18:43:21 -080079static void foreach_test_callback(const prop_info *pi, void* cookie) {
80 size_t *count = static_cast<size_t *>(cookie);
81
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +000082 ASSERT_TRUE(pi != nullptr);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080083 (*count)++;
84}
85
86static 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
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800111#endif // __BIONIC__
112
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800113TEST(properties, __system_property_add) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800114#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800115 SystemPropertiesTest system_properties;
116 ASSERT_TRUE(system_properties.valid());
Colin Crossb27e2002013-01-28 17:19:43 -0800117
Tom Cherrye275d6d2017-12-11 23:31:33 -0800118 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));
Colin Crossb27e2002013-01-28 17:19:43 -0800121
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000122 // 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';
Tom Cherrye275d6d2017-12-11 23:31:33 -0800130 ASSERT_EQ(0, system_properties.Add(name, strlen(name), "value", 5));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000131
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800132 char propvalue[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800133 ASSERT_EQ(6, system_properties.Get("property", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800134 ASSERT_STREQ(propvalue, "value1");
135
Tom Cherrye275d6d2017-12-11 23:31:33 -0800136 ASSERT_EQ(6, system_properties.Get("other_property", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800137 ASSERT_STREQ(propvalue, "value2");
138
Tom Cherrye275d6d2017-12-11 23:31:33 -0800139 ASSERT_EQ(6, system_properties.Get("property_other", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800140 ASSERT_STREQ(propvalue, "value3");
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000141
Tom Cherrye275d6d2017-12-11 23:31:33 -0800142 ASSERT_EQ(5, system_properties.Get(name, propvalue));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000143 ASSERT_STREQ(propvalue, "value");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800144#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800145 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800146#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800147}
148
Nate Myrenb8c87b12023-08-28 16:46:39 -0700149TEST(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
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800201TEST(properties, __system_property_update) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800202#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800203 SystemPropertiesTest system_properties;
204 ASSERT_TRUE(system_properties.valid());
Colin Crossb27e2002013-01-28 17:19:43 -0800205
Tom Cherrye275d6d2017-12-11 23:31:33 -0800206 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));
Colin Crossb27e2002013-01-28 17:19:43 -0800209
Tom Cherrye275d6d2017-12-11 23:31:33 -0800210 const prop_info* pi = system_properties.Find("property");
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800211 ASSERT_TRUE(pi != nullptr);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800212 system_properties.Update(const_cast<prop_info*>(pi), "value4", 6);
Colin Crossb27e2002013-01-28 17:19:43 -0800213
Tom Cherrye275d6d2017-12-11 23:31:33 -0800214 pi = system_properties.Find("other_property");
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800215 ASSERT_TRUE(pi != nullptr);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800216 system_properties.Update(const_cast<prop_info*>(pi), "newvalue5", 9);
Colin Crossb27e2002013-01-28 17:19:43 -0800217
Tom Cherrye275d6d2017-12-11 23:31:33 -0800218 pi = system_properties.Find("property_other");
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800219 ASSERT_TRUE(pi != nullptr);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800220 system_properties.Update(const_cast<prop_info*>(pi), "value6", 6);
Colin Crossb27e2002013-01-28 17:19:43 -0800221
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800222 char propvalue[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800223 ASSERT_EQ(6, system_properties.Get("property", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800224 ASSERT_STREQ(propvalue, "value4");
225
Tom Cherrye275d6d2017-12-11 23:31:33 -0800226 ASSERT_EQ(9, system_properties.Get("other_property", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800227 ASSERT_STREQ(propvalue, "newvalue5");
228
Tom Cherrye275d6d2017-12-11 23:31:33 -0800229 ASSERT_EQ(6, system_properties.Get("property_other", propvalue));
Colin Crossb27e2002013-01-28 17:19:43 -0800230 ASSERT_STREQ(propvalue, "value6");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800231#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800232 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800233#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800234}
235
Greg Hackmanncb215a72013-02-13 14:41:48 -0800236TEST(properties, fill) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800237#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800238 SystemPropertiesTest system_properties;
239 ASSERT_TRUE(system_properties.valid());
240
Colin Crossb27e2002013-01-28 17:19:43 -0800241 char prop_name[PROP_NAME_MAX];
242 char prop_value[PROP_VALUE_MAX];
243 char prop_value_ret[PROP_VALUE_MAX];
Greg Hackmanncb215a72013-02-13 14:41:48 -0800244 int count = 0;
Colin Crossb27e2002013-01-28 17:19:43 -0800245 int ret;
246
Greg Hackmanncb215a72013-02-13 14:41:48 -0800247 while (true) {
248 ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d", count);
Colin Crossb27e2002013-01-28 17:19:43 -0800249 memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800250 ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d", count);
Colin Crossb27e2002013-01-28 17:19:43 -0800251 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
Tom Cherrye275d6d2017-12-11 23:31:33 -0800255 ret = system_properties.Add(prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1);
Greg Hackmanncb215a72013-02-13 14:41:48 -0800256 if (ret < 0)
257 break;
258
259 count++;
Colin Crossb27e2002013-01-28 17:19:43 -0800260 }
261
Greg Hackmanncb215a72013-02-13 14:41:48 -0800262 // For historical reasons at least 247 properties must be supported
263 ASSERT_GE(count, 247);
264
265 for (int i = 0; i < count; i++) {
Colin Crossb27e2002013-01-28 17:19:43 -0800266 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
Tom Cherrye275d6d2017-12-11 23:31:33 -0800274 ASSERT_EQ(PROP_VALUE_MAX - 1, system_properties.Get(prop_name, prop_value_ret));
Colin Crossb27e2002013-01-28 17:19:43 -0800275 ASSERT_EQ(0, memcmp(prop_value, prop_value_ret, PROP_VALUE_MAX));
276 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800277#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800278 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800279#endif // __BIONIC__
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800280}
281
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800282TEST(properties, __system_property_foreach) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800283#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800284 SystemPropertiesTest system_properties;
285 ASSERT_TRUE(system_properties.valid());
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800286
Tom Cherrye275d6d2017-12-11 23:31:33 -0800287 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));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800290
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800291 size_t count = 0;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800292 ASSERT_EQ(0, system_properties.Foreach(foreach_test_callback, &count));
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800293 ASSERT_EQ(3U, count);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800294#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800295 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800296#endif // __BIONIC__
Greg Hackmannc6ff8442013-02-12 16:39:31 -0800297}
298
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800299TEST(properties, __system_property_find_nth) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800300#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800301 SystemPropertiesTest system_properties;
302 ASSERT_TRUE(system_properties.valid());
Colin Crossb27e2002013-01-28 17:19:43 -0800303
Tom Cherrye275d6d2017-12-11 23:31:33 -0800304 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));
Colin Crossb27e2002013-01-28 17:19:43 -0800307
Elliott Hughes438e0192017-04-17 14:53:07 -0700308 char name[PROP_NAME_MAX];
309 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800310 EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(0), name, value));
Elliott Hughes438e0192017-04-17 14:53:07 -0700311 EXPECT_STREQ("property", name);
312 EXPECT_STREQ("value1", value);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800313 EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(1), name, value));
Elliott Hughes438e0192017-04-17 14:53:07 -0700314 EXPECT_STREQ("other_property", name);
315 EXPECT_STREQ("value2", value);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800316 EXPECT_EQ(6, system_properties.Read(system_properties.FindNth(2), name, value));
Elliott Hughes438e0192017-04-17 14:53:07 -0700317 EXPECT_STREQ("property_other", name);
318 EXPECT_STREQ("value3", value);
319
320 for (unsigned i = 3; i < 1024; ++i) {
Tom Cherrye275d6d2017-12-11 23:31:33 -0800321 ASSERT_TRUE(system_properties.FindNth(i) == nullptr);
Elliott Hughes438e0192017-04-17 14:53:07 -0700322 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800323#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800324 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800325#endif // __BIONIC__
Greg Hackmannd5276422013-06-17 12:37:09 -0700326}
327
328TEST(properties, fill_hierarchical) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800329#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800330 SystemPropertiesTest system_properties;
331 ASSERT_TRUE(system_properties.valid());
332
Greg Hackmannd5276422013-06-17 12:37:09 -0700333 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
Tom Cherrye275d6d2017-12-11 23:31:33 -0800348 ASSERT_EQ(0, system_properties.Add(
349 prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1));
Greg Hackmannd5276422013-06-17 12:37:09 -0700350 }
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
Tom Cherrye275d6d2017-12-11 23:31:33 -0800365 ASSERT_EQ(PROP_VALUE_MAX - 1, system_properties.Get(prop_name, prop_value_ret));
Greg Hackmannd5276422013-06-17 12:37:09 -0700366 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));
Tom Cherrye275d6d2017-12-11 23:31:33 -0800373 system_properties.Foreach(hierarchical_test_callback, ok);
Greg Hackmannd5276422013-06-17 12:37:09 -0700374
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 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800382#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800383 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800384#endif // __BIONIC__
Greg Hackmannd5276422013-06-17 12:37:09 -0700385}
386
Colin Crossb27e2002013-01-28 17:19:43 -0800387TEST(properties, errors) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800388#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800389 SystemPropertiesTest system_properties;
390 ASSERT_TRUE(system_properties.valid());
391
Colin Crossb27e2002013-01-28 17:19:43 -0800392 char prop_value[PROP_NAME_MAX];
393
Tom Cherrye275d6d2017-12-11 23:31:33 -0800394 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));
Colin Crossb27e2002013-01-28 17:19:43 -0800397
Tom Cherrye275d6d2017-12-11 23:31:33 -0800398 ASSERT_EQ(0, system_properties.Find("property1"));
399 ASSERT_EQ(0, system_properties.Get("property1", prop_value));
Colin Crossb27e2002013-01-28 17:19:43 -0800400
Tom Cherrye275d6d2017-12-11 23:31:33 -0800401 ASSERT_EQ(-1, system_properties.Add("name", 4, "value", PROP_VALUE_MAX));
402 ASSERT_EQ(-1, system_properties.Update(NULL, "value", PROP_VALUE_MAX));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800403#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800404 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800405#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800406}
407
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800408TEST(properties, __system_property_serial) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800409#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800410 SystemPropertiesTest system_properties;
411 ASSERT_TRUE(system_properties.valid());
Colin Crossb27e2002013-01-28 17:19:43 -0800412
Tom Cherrye275d6d2017-12-11 23:31:33 -0800413 ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
414 const prop_info* pi = system_properties.Find("property");
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800415 ASSERT_TRUE(pi != nullptr);
Raman Tennetib481a2e2019-11-12 20:41:55 +0000416 unsigned serial = __system_property_serial(pi);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800417 ASSERT_EQ(0, system_properties.Update(const_cast<prop_info*>(pi), "value2", 6));
Raman Tennetib481a2e2019-11-12 20:41:55 +0000418 ASSERT_NE(serial, __system_property_serial(pi));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800419#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800420 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800421#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800422}
423
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800424TEST(properties, __system_property_wait_any) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800425#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800426 SystemPropertiesTest system_properties;
427 ASSERT_TRUE(system_properties.valid());
Colin Crossb27e2002013-01-28 17:19:43 -0800428
Tom Cherrye275d6d2017-12-11 23:31:33 -0800429 ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
430 unsigned serial = system_properties.WaitAny(0);
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000431
Tom Cherrye275d6d2017-12-11 23:31:33 -0800432 prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
Dimitry Ivanov16b2a4d2017-01-24 20:43:29 +0000433 ASSERT_TRUE(pi != nullptr);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800434 system_properties.Update(pi, "value2", 6);
435 serial = system_properties.WaitAny(serial);
Colin Crossb27e2002013-01-28 17:19:43 -0800436
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800437 int flag = 0;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800438 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 });
Colin Crossb27e2002013-01-28 17:19:43 -0800445 ASSERT_EQ(flag, 0);
Tom Cherrye275d6d2017-12-11 23:31:33 -0800446 serial = system_properties.WaitAny(serial);
Colin Crossb27e2002013-01-28 17:19:43 -0800447 ASSERT_EQ(flag, 1);
448
Tom Cherrye275d6d2017-12-11 23:31:33 -0800449 thread.join();
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800450#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800451 GTEST_SKIP() << "bionic-only test";
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800452#endif // __BIONIC__
453}
454
455TEST(properties, __system_property_wait) {
456#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800457 SystemPropertiesTest system_properties;
458 ASSERT_TRUE(system_properties.valid());
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800459
Tom Cherrye275d6d2017-12-11 23:31:33 -0800460 ASSERT_EQ(0, system_properties.Add("property", 8, "value1", 6));
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800461
Tom Cherrye275d6d2017-12-11 23:31:33 -0800462 prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800463 ASSERT_TRUE(pi != nullptr);
464
Raman Tennetib481a2e2019-11-12 20:41:55 +0000465 unsigned serial = __system_property_serial(pi);
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800466
Tom Cherrye275d6d2017-12-11 23:31:33 -0800467 std::thread thread([&system_properties]() {
468 prop_info* pi = const_cast<prop_info*>(system_properties.Find("property"));
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800469 ASSERT_TRUE(pi != nullptr);
470
Tom Cherrye275d6d2017-12-11 23:31:33 -0800471 system_properties.Update(pi, "value2", 6);
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800472 });
473
Elliott Hughes40c885a2017-02-16 17:13:04 -0800474 uint32_t new_serial;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800475 system_properties.Wait(pi, serial, &new_serial, nullptr);
Elliott Hughes40c885a2017-02-16 17:13:04 -0800476 ASSERT_GT(new_serial, serial);
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800477
478 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800479 ASSERT_EQ(6, system_properties.Get("property", value));
Elliott Hughesa0d374d2017-02-10 18:13:46 -0800480 ASSERT_STREQ("value2", value);
481
482 thread.join();
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800483#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800484 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800485#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800486}
487
488class KilledByFault {
489 public:
490 explicit KilledByFault() {};
491 bool operator()(int exit_status) const;
492};
493
494bool 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
Elliott Hughes141b9172021-04-09 17:13:09 -0700501using properties_DeathTest = SilentDeathTest;
Yabin Cui9df70402014-11-05 18:01:01 -0800502
503TEST_F(properties_DeathTest, read_only) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800504#if defined(__BIONIC__)
Elliott Hughese4375192013-10-21 17:09:52 -0700505
506 // This test only makes sense if we're talking to the real system property service.
507 struct stat sb;
Nate Myrenb8c87b12023-08-28 16:46:39 -0700508 ASSERT_FALSE(stat(PROP_DIRNAME, &sb) == -1 && errno == ENOENT);
Elliott Hughese4375192013-10-21 17:09:52 -0700509
510 ASSERT_EXIT(__system_property_add("property", 8, "value", 5), KilledByFault(), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800511#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800512 GTEST_SKIP() << "bionic-only test";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800513#endif // __BIONIC__
Colin Crossb27e2002013-01-28 17:19:43 -0800514}
Tom Cherry8a311632017-10-09 13:49:17 -0700515
516TEST(properties, __system_property_extra_long_read_only) {
517#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800518 SystemPropertiesTest system_properties;
519 ASSERT_TRUE(system_properties.valid());
Tom Cherry8a311632017-10-09 13:49:17 -0700520
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;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800536 ASSERT_EQ(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
Tom Cherry8a311632017-10-09 13:49:17 -0700537 }
538
539 for (const auto& property : long_properties) {
540 const std::string& name = property.first;
541 const std::string& value = property.second;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800542 ASSERT_EQ(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
Tom Cherry8a311632017-10-09 13:49:17 -0700543 }
544
Tom Cherrye275d6d2017-12-11 23:31:33 -0800545 auto check_with_legacy_read = [&system_properties](const std::string& name,
546 const std::string& expected_value) {
Tom Cherry8a311632017-10-09 13:49:17 -0700547 char value[PROP_VALUE_MAX];
Tom Cherrye275d6d2017-12-11 23:31:33 -0800548 EXPECT_EQ(static_cast<int>(expected_value.size()), system_properties.Get(name.c_str(), value))
Tom Cherry8a311632017-10-09 13:49:17 -0700549 << name;
550 EXPECT_EQ(expected_value, value) << name;
551 };
552
Tom Cherrye275d6d2017-12-11 23:31:33 -0800553 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());
Tom Cherry8a311632017-10-09 13:49:17 -0700556 ASSERT_NE(nullptr, pi);
557 std::string value;
Tom Cherrye275d6d2017-12-11 23:31:33 -0800558 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);
Tom Cherry8a311632017-10-09 13:49:17 -0700564 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
Elliott Hughes30088842023-09-14 18:35:11 +0000574 static constexpr const char* kExtraLongLegacyError =
Tom Cherry8a311632017-10-09 13:49:17 -0700575 "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__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800584 GTEST_SKIP() << "bionic-only test";
Tom Cherry8a311632017-10-09 13:49:17 -0700585#endif // __BIONIC__
586}
587
588// pa_size is 128 * 1024 currently, if a property is longer then we expect it to fail gracefully.
589TEST(properties, __system_property_extra_long_read_only_too_long) {
590#if defined(__BIONIC__)
Tom Cherrye275d6d2017-12-11 23:31:33 -0800591 SystemPropertiesTest system_properties;
592 ASSERT_TRUE(system_properties.valid());
Tom Cherry8a311632017-10-09 13:49:17 -0700593
594 auto name = "ro.super_long_property"s;
Dennis Shen5951b412023-12-21 19:57:13 +0000595
596#ifdef LARGE_SYSTEM_PROPERTY_NODE
597 auto value = std::string(1024 * 1024 + 1, 'x');
598#else
Dennis Shen3d1ce8c2024-01-04 14:18:38 +0000599 auto value = std::string(128 * 1024 + 1, 'x');
Dennis Shen5951b412023-12-21 19:57:13 +0000600#endif
601
Tom Cherrye275d6d2017-12-11 23:31:33 -0800602 ASSERT_NE(0, system_properties.Add(name.c_str(), name.size(), value.c_str(), value.size()));
Tom Cherry8a311632017-10-09 13:49:17 -0700603
604#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800605 GTEST_SKIP() << "bionic-only test";
Tom Cherry8a311632017-10-09 13:49:17 -0700606#endif // __BIONIC__
607}
Nate Myren0ab06152023-10-19 16:50:59 -0700608
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.
617TEST(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
635TEST(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.
665TEST(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}