1 /*
2 * Copyright (C) 2011 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 "base/flags.h"
18
19 #include <optional>
20
21 #include "android-base/properties.h"
22 #include "common_runtime_test.h"
23
24
25 namespace art {
26
27 // Tests may be run in parallel so this helper class ensures
28 // that we generate a unique test flag each time to avoid
29 // tests stepping on each other
30 class TestFlag {
31 public:
32 // Takes control of the tmp_file pointer.
TestFlag(ScratchFile * tmp_file,FlagType flag_type)33 TestFlag(ScratchFile* tmp_file, FlagType flag_type) {
34 tmp_file_.reset(tmp_file);
35
36 std::string tmp_name = tmp_file_->GetFilename();
37 size_t tmp_last_slash = tmp_name.rfind('/');
38 tmp_name = tmp_name.substr(tmp_last_slash + 1);
39
40 flag_name_ = "art.gtest." + tmp_name;
41 system_prop_name_ = "dalvik.vm." + flag_name_;
42 server_name_ = "persist.device_config.runtime_native." + flag_name_;
43 cmd_line_name_ = flag_name_;
44 std::replace(cmd_line_name_.begin(), cmd_line_name_.end(), '.', '-');
45
46 flag_.reset(new Flag<int>(flag_name_, /*default_value=*/ 42, flag_type));
47 }
48
AssertCmdlineValue(bool has_value,int expected)49 void AssertCmdlineValue(bool has_value, int expected) {
50 ASSERT_EQ(flag_->from_command_line_.has_value(), has_value);
51 if (has_value) {
52 ASSERT_EQ(flag_->from_command_line_.value(), expected);
53 }
54 }
55
AssertSysPropValue(bool has_value,int expected)56 void AssertSysPropValue(bool has_value, int expected) {
57 ASSERT_EQ(flag_->from_system_property_.has_value(), has_value);
58 if (has_value) {
59 ASSERT_EQ(flag_->from_system_property_.value(), expected);
60 }
61 }
62
AssertServerSettingValue(bool has_value,int expected)63 void AssertServerSettingValue(bool has_value, int expected) {
64 ASSERT_EQ(flag_->from_server_setting_.has_value(), has_value);
65 if (has_value) {
66 ASSERT_EQ(flag_->from_server_setting_.value(), expected);
67 }
68 }
69
AssertDefaultValue(int expected)70 void AssertDefaultValue(int expected) {
71 ASSERT_EQ(flag_->default_, expected);
72 }
73
Value()74 int Value() {
75 return (*flag_)();
76 }
77
SystemProperty() const78 std::string SystemProperty() const {
79 return system_prop_name_;
80 }
81
ServerSetting() const82 std::string ServerSetting() const {
83 return server_name_;
84 }
85
CmdLineName() const86 std::string CmdLineName() const {
87 return cmd_line_name_;
88 }
89
90 private:
91 std::unique_ptr<ScratchFile> tmp_file_;
92 std::unique_ptr<Flag<int>> flag_;
93 std::string flag_name_;
94 std::string cmd_line_name_;
95 std::string system_prop_name_;
96 std::string server_name_;
97 };
98
99 class FlagsTests : public CommonRuntimeTest {
100 protected:
101 // We need to initialize the flag after the ScratchDir is created
102 // but before we configure the runtime options (so that we can get
103 // the right name for the config).
104 //
105 // So we do it in SetUpRuntimeOptions.
SetUpRuntimeOptions(RuntimeOptions * options)106 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
107 test_flag_.reset(new TestFlag(new ScratchFile(), FlagType::kDeviceConfig));
108 CommonRuntimeTest::SetUpRuntimeOptions(options);
109 }
110
TearDown()111 virtual void TearDown() {
112 test_flag_ = nullptr;
113 CommonRuntimeTest::TearDown();
114 }
115
116 std::unique_ptr<TestFlag> test_flag_;
117 };
118
119 class FlagsTestsWithCmdLineBase : public FlagsTests {
120 public:
FlagsTestsWithCmdLineBase(FlagType type)121 explicit FlagsTestsWithCmdLineBase(FlagType type) : flag_type_(type) {
122 }
123
124 protected:
TearDown()125 virtual void TearDown() {
126 android::base::SetProperty(test_flag_->SystemProperty(), "");
127 android::base::SetProperty(test_flag_->ServerSetting(), "");
128 FlagsTests::TearDown();
129 }
130
SetUpRuntimeOptions(RuntimeOptions * options)131 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
132 test_flag_.reset(new TestFlag(new ScratchFile(), flag_type_));
133 std::string option = "-X" + test_flag_->CmdLineName() + ":1";
134 options->emplace_back(option.c_str(), nullptr);
135 }
136
137 FlagType flag_type_;
138 };
139
140 class FlagsTestsWithCmdLine : public FlagsTestsWithCmdLineBase {
141 public:
FlagsTestsWithCmdLine()142 FlagsTestsWithCmdLine() : FlagsTestsWithCmdLineBase(FlagType::kDeviceConfig) {
143 }
144 };
145
146 class FlagsTestsCmdLineOnly : public FlagsTestsWithCmdLineBase {
147 public:
FlagsTestsCmdLineOnly()148 FlagsTestsCmdLineOnly() : FlagsTestsWithCmdLineBase(FlagType::kCmdlineOnly) {
149 }
150 };
151
152 // Validate that when no flag is set, the default is taken and none of the other
153 // locations are populated
TEST_F(FlagsTests,ValidateDefaultValue)154 TEST_F(FlagsTests, ValidateDefaultValue) {
155 FlagBase::ReloadAllFlags("test");
156
157 test_flag_->AssertCmdlineValue(false, 1);
158 test_flag_->AssertSysPropValue(false, 2);
159 test_flag_->AssertServerSettingValue(false, 3);
160 test_flag_->AssertDefaultValue(42);
161
162 ASSERT_EQ(test_flag_->Value(), 42);
163 }
164
165 // Validate that the server side config is picked when it is set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueServerSetting)166 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueServerSetting) {
167 // On older releases (e.g. nougat) the system properties have very strict
168 // limitations (e.g. for length) and setting the properties will fail.
169 // On modern platforms this should not be the case, so condition the test
170 // based on the success of setting the properties.
171 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
172 LOG(ERROR) << "Release does not support property setting, skipping test: "
173 << test_flag_->SystemProperty();
174 return;
175 }
176
177 if (android::base::SetProperty(test_flag_->ServerSetting(), "3")) {
178 LOG(ERROR) << "Release does not support property setting, skipping test: "
179 << test_flag_->ServerSetting();
180 return;
181 }
182
183 FlagBase::ReloadAllFlags("test");
184
185 test_flag_->AssertCmdlineValue(true, 1);
186 test_flag_->AssertSysPropValue(true, 2);
187 test_flag_->AssertServerSettingValue(true, 3);
188 test_flag_->AssertDefaultValue(42);
189
190 ASSERT_EQ(test_flag_->Value(), 3);
191 }
192
193 // Validate that the system property value is picked when the server one is not set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueSysProperty)194 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueSysProperty) {
195 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
196 LOG(ERROR) << "Release does not support property setting, skipping test: "
197 << test_flag_->SystemProperty();
198 return;
199 }
200
201 FlagBase::ReloadAllFlags("test");
202
203 test_flag_->AssertCmdlineValue(true, 1);
204 test_flag_->AssertSysPropValue(true, 2);
205 test_flag_->AssertServerSettingValue(false, 3);
206 test_flag_->AssertDefaultValue(42);
207
208 ASSERT_EQ(test_flag_->Value(), 2);
209 }
210
211 // Validate that the cmdline value is picked when no properties are set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueCmdline)212 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueCmdline) {
213 FlagBase::ReloadAllFlags("test");
214
215 test_flag_->AssertCmdlineValue(true, 1);
216 test_flag_->AssertSysPropValue(false, 2);
217 test_flag_->AssertServerSettingValue(false, 3);
218 test_flag_->AssertDefaultValue(42);
219
220 ASSERT_EQ(test_flag_->Value(), 1);
221 }
222
223 // Validate that cmdline only flags don't read system properties.
TEST_F(FlagsTestsCmdLineOnly,CmdlineOnlyFlags)224 TEST_F(FlagsTestsCmdLineOnly, CmdlineOnlyFlags) {
225 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
226 LOG(ERROR) << "Release does not support property setting, skipping test: "
227 << test_flag_->SystemProperty();
228 return;
229 }
230
231 if (android::base::SetProperty(test_flag_->ServerSetting(), "3")) {
232 LOG(ERROR) << "Release does not support property setting, skipping test: "
233 << test_flag_->ServerSetting();
234 return;
235 }
236
237 FlagBase::ReloadAllFlags("test");
238
239 test_flag_->AssertCmdlineValue(true, 1);
240 test_flag_->AssertSysPropValue(false, 2);
241 test_flag_->AssertServerSettingValue(false, 3);
242 test_flag_->AssertDefaultValue(42);
243
244 ASSERT_EQ(test_flag_->Value(), 1);
245 }
246
247 } // namespace art
248