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