xref: /aosp_15_r20/bionic/tests/system_properties_test2.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  *
4*8d67ca89SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker  *
8*8d67ca89SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker  *
10*8d67ca89SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker  * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker  */
16*8d67ca89SAndroid Build Coastguard Worker 
17*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <sys/wait.h>
19*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
20*8d67ca89SAndroid Build Coastguard Worker 
21*8d67ca89SAndroid Build Coastguard Worker #include <chrono>
22*8d67ca89SAndroid Build Coastguard Worker #include <sstream>
23*8d67ca89SAndroid Build Coastguard Worker #include <string>
24*8d67ca89SAndroid Build Coastguard Worker 
25*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
26*8d67ca89SAndroid Build Coastguard Worker 
27*8d67ca89SAndroid Build Coastguard Worker #include "utils.h"
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
30*8d67ca89SAndroid Build Coastguard Worker #include <sys/system_properties.h>
31*8d67ca89SAndroid Build Coastguard Worker #endif
32*8d67ca89SAndroid Build Coastguard Worker 
33*8d67ca89SAndroid Build Coastguard Worker // Note that this test affects global state of the system
34*8d67ca89SAndroid Build Coastguard Worker // this tests tries to mitigate this by using utime+pid
35*8d67ca89SAndroid Build Coastguard Worker // prefix for the property name. It is still results in
36*8d67ca89SAndroid Build Coastguard Worker // pollution of property service since properties cannot
37*8d67ca89SAndroid Build Coastguard Worker // be removed.
38*8d67ca89SAndroid Build Coastguard Worker //
39*8d67ca89SAndroid Build Coastguard Worker // Note that there is also possibility to run into "out-of-memory"
40*8d67ca89SAndroid Build Coastguard Worker // if this test if it is executed often enough without reboot.
TEST(properties,smoke)41*8d67ca89SAndroid Build Coastguard Worker TEST(properties, smoke) {
42*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
43*8d67ca89SAndroid Build Coastguard Worker     char propvalue[PROP_VALUE_MAX];
44*8d67ca89SAndroid Build Coastguard Worker 
45*8d67ca89SAndroid Build Coastguard Worker     std::stringstream ss;
46*8d67ca89SAndroid Build Coastguard Worker     ss << "debug.test." << getpid() << "." << NanoTime() << ".";
47*8d67ca89SAndroid Build Coastguard Worker     const std::string property_prefix = ss.str();
48*8d67ca89SAndroid Build Coastguard Worker     const std::string property_name = property_prefix + "property1";
49*8d67ca89SAndroid Build Coastguard Worker 
50*8d67ca89SAndroid Build Coastguard Worker     // Set brand new property
51*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1"));
52*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
53*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value1", propvalue);
54*8d67ca89SAndroid Build Coastguard Worker 
55*8d67ca89SAndroid Build Coastguard Worker     std::string long_value = "property-";
56*8d67ca89SAndroid Build Coastguard Worker     for (size_t i = 0; i < PROP_VALUE_MAX; i++) {
57*8d67ca89SAndroid Build Coastguard Worker       long_value += "y";
58*8d67ca89SAndroid Build Coastguard Worker     }
59*8d67ca89SAndroid Build Coastguard Worker 
60*8d67ca89SAndroid Build Coastguard Worker     // Make sure that attempts to set invalid property value fails and preserves
61*8d67ca89SAndroid Build Coastguard Worker     // previous value.
62*8d67ca89SAndroid Build Coastguard Worker     propvalue[0] = '\0';
63*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(-1, __system_property_set(property_name.c_str(), long_value.c_str()));
64*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
65*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value1", propvalue);
66*8d67ca89SAndroid Build Coastguard Worker 
67*8d67ca89SAndroid Build Coastguard Worker     // Update property
68*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1-1"));
69*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(8, __system_property_get(property_name.c_str(), propvalue));
70*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value1-1", propvalue);
71*8d67ca89SAndroid Build Coastguard Worker 
72*8d67ca89SAndroid Build Coastguard Worker 
73*8d67ca89SAndroid Build Coastguard Worker     // check that there is no limit on property name length
74*8d67ca89SAndroid Build Coastguard Worker     char suffix[1024];
75*8d67ca89SAndroid Build Coastguard Worker     for (size_t i = 0; i < sizeof(suffix); i++) {
76*8d67ca89SAndroid Build Coastguard Worker       suffix[i] = 'x';
77*8d67ca89SAndroid Build Coastguard Worker     }
78*8d67ca89SAndroid Build Coastguard Worker 
79*8d67ca89SAndroid Build Coastguard Worker     suffix[sizeof(suffix)-1] = '\0';
80*8d67ca89SAndroid Build Coastguard Worker     const std::string long_property_name = property_prefix + suffix;
81*8d67ca89SAndroid Build Coastguard Worker 
82*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, __system_property_set(long_property_name.c_str(), "value2"));
83*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_get(long_property_name.c_str(), propvalue));
84*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value2", propvalue);
85*8d67ca89SAndroid Build Coastguard Worker 
86*8d67ca89SAndroid Build Coastguard Worker     // test find and read_callback
87*8d67ca89SAndroid Build Coastguard Worker     const prop_info* pi = __system_property_find(property_name.c_str());
88*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(pi != nullptr);
89*8d67ca89SAndroid Build Coastguard Worker 
90*8d67ca89SAndroid Build Coastguard Worker     std::string expected_name = property_name;
91*8d67ca89SAndroid Build Coastguard Worker     __system_property_read_callback(pi,
92*8d67ca89SAndroid Build Coastguard Worker       [](void* cookie, const char* name, const char* value, unsigned /*serial*/) {
93*8d67ca89SAndroid Build Coastguard Worker         const std::string* expected_name = static_cast<const std::string*>(cookie);
94*8d67ca89SAndroid Build Coastguard Worker         ASSERT_EQ(*expected_name, name);
95*8d67ca89SAndroid Build Coastguard Worker         ASSERT_STREQ("value1-1", value);
96*8d67ca89SAndroid Build Coastguard Worker     }, &expected_name);
97*8d67ca89SAndroid Build Coastguard Worker 
98*8d67ca89SAndroid Build Coastguard Worker     pi = __system_property_find(long_property_name.c_str());
99*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(pi != nullptr);
100*8d67ca89SAndroid Build Coastguard Worker 
101*8d67ca89SAndroid Build Coastguard Worker     expected_name = long_property_name;
102*8d67ca89SAndroid Build Coastguard Worker     __system_property_read_callback(pi,
103*8d67ca89SAndroid Build Coastguard Worker       [](void* cookie, const char* name, const char* value, unsigned /*serial*/) {
104*8d67ca89SAndroid Build Coastguard Worker         const std::string* expected_name = static_cast<const std::string*>(cookie);
105*8d67ca89SAndroid Build Coastguard Worker         ASSERT_EQ(*expected_name, name);
106*8d67ca89SAndroid Build Coastguard Worker         ASSERT_STREQ("value2", value);
107*8d67ca89SAndroid Build Coastguard Worker     }, &expected_name);
108*8d67ca89SAndroid Build Coastguard Worker 
109*8d67ca89SAndroid Build Coastguard Worker     // Check that read() for long names still works but returns truncated version of the name
110*8d67ca89SAndroid Build Coastguard Worker     pi = __system_property_find(property_name.c_str());
111*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(pi != nullptr);
112*8d67ca89SAndroid Build Coastguard Worker     char legacy_name[PROP_NAME_MAX];
113*8d67ca89SAndroid Build Coastguard Worker     expected_name = std::string(property_name.c_str(), PROP_NAME_MAX-1);
114*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(8, __system_property_read(pi, &legacy_name[0], propvalue));
115*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(expected_name, legacy_name);
116*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value1-1", propvalue);
117*8d67ca89SAndroid Build Coastguard Worker 
118*8d67ca89SAndroid Build Coastguard Worker     const prop_info* pi_long = __system_property_find(long_property_name.c_str());
119*8d67ca89SAndroid Build Coastguard Worker     ASSERT_TRUE(pi != nullptr);
120*8d67ca89SAndroid Build Coastguard Worker     expected_name = std::string(long_property_name.c_str(), PROP_NAME_MAX-1);
121*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_read(pi_long, &legacy_name[0], propvalue));
122*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(expected_name, legacy_name);
123*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value2", propvalue);
124*8d67ca89SAndroid Build Coastguard Worker #else // __BIONIC__
125*8d67ca89SAndroid Build Coastguard Worker     GTEST_SKIP() << "bionic-only test";
126*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
127*8d67ca89SAndroid Build Coastguard Worker }
128*8d67ca89SAndroid Build Coastguard Worker 
TEST(properties,no_fd_leaks)129*8d67ca89SAndroid Build Coastguard Worker TEST(properties, no_fd_leaks) {
130*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
131*8d67ca89SAndroid Build Coastguard Worker   FdLeakChecker leak_checker;
132*8d67ca89SAndroid Build Coastguard Worker   std::stringstream ss;
133*8d67ca89SAndroid Build Coastguard Worker   ss << "debug.test." << getpid() << "." << NanoTime() << ".";
134*8d67ca89SAndroid Build Coastguard Worker   const std::string property_prefix = ss.str();
135*8d67ca89SAndroid Build Coastguard Worker   const std::string property_name = property_prefix + "property1";
136*8d67ca89SAndroid Build Coastguard Worker 
137*8d67ca89SAndroid Build Coastguard Worker   for (size_t i = 0; i < 100; ++i) {
138*8d67ca89SAndroid Build Coastguard Worker     char propvalue[PROP_VALUE_MAX];
139*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1"));
140*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
141*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value1", propvalue);
142*8d67ca89SAndroid Build Coastguard Worker 
143*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value2"));
144*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
145*8d67ca89SAndroid Build Coastguard Worker     ASSERT_STREQ("value2", propvalue);
146*8d67ca89SAndroid Build Coastguard Worker   }
147*8d67ca89SAndroid Build Coastguard Worker #else   // __BIONIC__
148*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
149*8d67ca89SAndroid Build Coastguard Worker #endif  // __BIONIC__
150*8d67ca89SAndroid Build Coastguard Worker }
151*8d67ca89SAndroid Build Coastguard Worker 
TEST(properties,empty_value)152*8d67ca89SAndroid Build Coastguard Worker TEST(properties, empty_value) {
153*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
154*8d67ca89SAndroid Build Coastguard Worker     char propvalue[PROP_VALUE_MAX];
155*8d67ca89SAndroid Build Coastguard Worker 
156*8d67ca89SAndroid Build Coastguard Worker     std::stringstream ss;
157*8d67ca89SAndroid Build Coastguard Worker     ss << "debug.test." << getpid() << "." << NanoTime() << "." << "property_empty";
158*8d67ca89SAndroid Build Coastguard Worker     const std::string property_name = ss.str();
159*8d67ca89SAndroid Build Coastguard Worker 
160*8d67ca89SAndroid Build Coastguard Worker     for (size_t i = 0; i < 1000; ++i) {
161*8d67ca89SAndroid Build Coastguard Worker       ASSERT_EQ(0, __system_property_set(property_name.c_str(), ""));
162*8d67ca89SAndroid Build Coastguard Worker       ASSERT_EQ(0, __system_property_get(property_name.c_str(), propvalue));
163*8d67ca89SAndroid Build Coastguard Worker       ASSERT_STREQ("", propvalue);
164*8d67ca89SAndroid Build Coastguard Worker     }
165*8d67ca89SAndroid Build Coastguard Worker 
166*8d67ca89SAndroid Build Coastguard Worker #else  // __BIONIC__
167*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "bionic-only test";
168*8d67ca89SAndroid Build Coastguard Worker #endif // __BIONIC__
169*8d67ca89SAndroid Build Coastguard Worker }
170