1 /*
2  * Copyright (C) 2017 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 <errno.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20 
21 #include <chrono>
22 #include <sstream>
23 #include <string>
24 
25 #include <gtest/gtest.h>
26 
27 #include "utils.h"
28 
29 #if defined(__BIONIC__)
30 #include <sys/system_properties.h>
31 #endif
32 
33 // Note that this test affects global state of the system
34 // this tests tries to mitigate this by using utime+pid
35 // prefix for the property name. It is still results in
36 // pollution of property service since properties cannot
37 // be removed.
38 //
39 // Note that there is also possibility to run into "out-of-memory"
40 // if this test if it is executed often enough without reboot.
TEST(properties,smoke)41 TEST(properties, smoke) {
42 #if defined(__BIONIC__)
43     char propvalue[PROP_VALUE_MAX];
44 
45     std::stringstream ss;
46     ss << "debug.test." << getpid() << "." << NanoTime() << ".";
47     const std::string property_prefix = ss.str();
48     const std::string property_name = property_prefix + "property1";
49 
50     // Set brand new property
51     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1"));
52     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
53     ASSERT_STREQ("value1", propvalue);
54 
55     std::string long_value = "property-";
56     for (size_t i = 0; i < PROP_VALUE_MAX; i++) {
57       long_value += "y";
58     }
59 
60     // Make sure that attempts to set invalid property value fails and preserves
61     // previous value.
62     propvalue[0] = '\0';
63     ASSERT_EQ(-1, __system_property_set(property_name.c_str(), long_value.c_str()));
64     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
65     ASSERT_STREQ("value1", propvalue);
66 
67     // Update property
68     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1-1"));
69     ASSERT_EQ(8, __system_property_get(property_name.c_str(), propvalue));
70     ASSERT_STREQ("value1-1", propvalue);
71 
72 
73     // check that there is no limit on property name length
74     char suffix[1024];
75     for (size_t i = 0; i < sizeof(suffix); i++) {
76       suffix[i] = 'x';
77     }
78 
79     suffix[sizeof(suffix)-1] = '\0';
80     const std::string long_property_name = property_prefix + suffix;
81 
82     ASSERT_EQ(0, __system_property_set(long_property_name.c_str(), "value2"));
83     ASSERT_EQ(6, __system_property_get(long_property_name.c_str(), propvalue));
84     ASSERT_STREQ("value2", propvalue);
85 
86     // test find and read_callback
87     const prop_info* pi = __system_property_find(property_name.c_str());
88     ASSERT_TRUE(pi != nullptr);
89 
90     std::string expected_name = property_name;
91     __system_property_read_callback(pi,
92       [](void* cookie, const char* name, const char* value, unsigned /*serial*/) {
93         const std::string* expected_name = static_cast<const std::string*>(cookie);
94         ASSERT_EQ(*expected_name, name);
95         ASSERT_STREQ("value1-1", value);
96     }, &expected_name);
97 
98     pi = __system_property_find(long_property_name.c_str());
99     ASSERT_TRUE(pi != nullptr);
100 
101     expected_name = long_property_name;
102     __system_property_read_callback(pi,
103       [](void* cookie, const char* name, const char* value, unsigned /*serial*/) {
104         const std::string* expected_name = static_cast<const std::string*>(cookie);
105         ASSERT_EQ(*expected_name, name);
106         ASSERT_STREQ("value2", value);
107     }, &expected_name);
108 
109     // Check that read() for long names still works but returns truncated version of the name
110     pi = __system_property_find(property_name.c_str());
111     ASSERT_TRUE(pi != nullptr);
112     char legacy_name[PROP_NAME_MAX];
113     expected_name = std::string(property_name.c_str(), PROP_NAME_MAX-1);
114     ASSERT_EQ(8, __system_property_read(pi, &legacy_name[0], propvalue));
115     ASSERT_EQ(expected_name, legacy_name);
116     ASSERT_STREQ("value1-1", propvalue);
117 
118     const prop_info* pi_long = __system_property_find(long_property_name.c_str());
119     ASSERT_TRUE(pi != nullptr);
120     expected_name = std::string(long_property_name.c_str(), PROP_NAME_MAX-1);
121     ASSERT_EQ(6, __system_property_read(pi_long, &legacy_name[0], propvalue));
122     ASSERT_EQ(expected_name, legacy_name);
123     ASSERT_STREQ("value2", propvalue);
124 #else // __BIONIC__
125     GTEST_SKIP() << "bionic-only test";
126 #endif // __BIONIC__
127 }
128 
TEST(properties,no_fd_leaks)129 TEST(properties, no_fd_leaks) {
130 #if defined(__BIONIC__)
131   FdLeakChecker leak_checker;
132   std::stringstream ss;
133   ss << "debug.test." << getpid() << "." << NanoTime() << ".";
134   const std::string property_prefix = ss.str();
135   const std::string property_name = property_prefix + "property1";
136 
137   for (size_t i = 0; i < 100; ++i) {
138     char propvalue[PROP_VALUE_MAX];
139     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value1"));
140     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
141     ASSERT_STREQ("value1", propvalue);
142 
143     ASSERT_EQ(0, __system_property_set(property_name.c_str(), "value2"));
144     ASSERT_EQ(6, __system_property_get(property_name.c_str(), propvalue));
145     ASSERT_STREQ("value2", propvalue);
146   }
147 #else   // __BIONIC__
148   GTEST_SKIP() << "bionic-only test";
149 #endif  // __BIONIC__
150 }
151 
TEST(properties,empty_value)152 TEST(properties, empty_value) {
153 #if defined(__BIONIC__)
154     char propvalue[PROP_VALUE_MAX];
155 
156     std::stringstream ss;
157     ss << "debug.test." << getpid() << "." << NanoTime() << "." << "property_empty";
158     const std::string property_name = ss.str();
159 
160     for (size_t i = 0; i < 1000; ++i) {
161       ASSERT_EQ(0, __system_property_set(property_name.c_str(), ""));
162       ASSERT_EQ(0, __system_property_get(property_name.c_str(), propvalue));
163       ASSERT_STREQ("", propvalue);
164     }
165 
166 #else  // __BIONIC__
167   GTEST_SKIP() << "bionic-only test";
168 #endif // __BIONIC__
169 }
170