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:
FlagsTests()101 FlagsTests() {
102 this->use_boot_image_ = true; // Make the Runtime creation cheaper.
103 }
104
105 // We need to initialize the flag after the ScratchDir is created
106 // but before we configure the runtime options (so that we can get
107 // the right name for the config).
108 //
109 // So we do it in SetUpRuntimeOptions.
SetUpRuntimeOptions(RuntimeOptions * options)110 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
111 test_flag_.reset(new TestFlag(new ScratchFile(), FlagType::kDeviceConfig));
112 CommonRuntimeTest::SetUpRuntimeOptions(options);
113 }
114
TearDown()115 virtual void TearDown() {
116 test_flag_ = nullptr;
117 CommonRuntimeTest::TearDown();
118 }
119
120 std::unique_ptr<TestFlag> test_flag_;
121 };
122
123 class FlagsTestsWithCmdLineBase : public FlagsTests {
124 public:
FlagsTestsWithCmdLineBase(FlagType type)125 explicit FlagsTestsWithCmdLineBase(FlagType type) : flag_type_(type) {
126 }
127
128 protected:
TearDown()129 virtual void TearDown() {
130 android::base::SetProperty(test_flag_->SystemProperty(), "");
131 android::base::SetProperty(test_flag_->ServerSetting(), "");
132 FlagsTests::TearDown();
133 }
134
SetUpRuntimeOptions(RuntimeOptions * options)135 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
136 test_flag_.reset(new TestFlag(new ScratchFile(), flag_type_));
137 std::string option = "-X" + test_flag_->CmdLineName() + ":1";
138 options->emplace_back(option.c_str(), nullptr);
139 }
140
141 FlagType flag_type_;
142 };
143
144 class FlagsTestsWithCmdLine : public FlagsTestsWithCmdLineBase {
145 public:
FlagsTestsWithCmdLine()146 FlagsTestsWithCmdLine() : FlagsTestsWithCmdLineBase(FlagType::kDeviceConfig) {
147 }
148 };
149
150 class FlagsTestsCmdLineOnly : public FlagsTestsWithCmdLineBase {
151 public:
FlagsTestsCmdLineOnly()152 FlagsTestsCmdLineOnly() : FlagsTestsWithCmdLineBase(FlagType::kCmdlineOnly) {
153 }
154 };
155
156 // Validate that when no flag is set, the default is taken and none of the other
157 // locations are populated
TEST_F(FlagsTests,ValidateDefaultValue)158 TEST_F(FlagsTests, ValidateDefaultValue) {
159 FlagBase::ReloadAllFlags("test");
160
161 test_flag_->AssertCmdlineValue(false, 1);
162 test_flag_->AssertSysPropValue(false, 2);
163 test_flag_->AssertServerSettingValue(false, 3);
164 test_flag_->AssertDefaultValue(42);
165
166 ASSERT_EQ(test_flag_->Value(), 42);
167 }
168
169 // Validate that the server side config is picked when it is set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueServerSetting)170 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueServerSetting) {
171 // On older releases (e.g. nougat) the system properties have very strict
172 // limitations (e.g. for length) and setting the properties will fail.
173 // On modern platforms this should not be the case, so condition the test
174 // based on the success of setting the properties.
175 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
176 LOG(ERROR) << "Release does not support property setting, skipping test: "
177 << test_flag_->SystemProperty();
178 return;
179 }
180
181 if (!android::base::SetProperty(test_flag_->ServerSetting(), "3")) {
182 LOG(ERROR) << "Release does not support property setting, skipping test: "
183 << test_flag_->ServerSetting();
184 return;
185 }
186
187 FlagBase::ReloadAllFlags("test");
188
189 test_flag_->AssertCmdlineValue(true, 1);
190 test_flag_->AssertSysPropValue(true, 2);
191 test_flag_->AssertServerSettingValue(true, 3);
192 test_flag_->AssertDefaultValue(42);
193
194 ASSERT_EQ(test_flag_->Value(), 3);
195 }
196
197 // Validate that the system property value is picked when the server one is not set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueSysProperty)198 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueSysProperty) {
199 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
200 LOG(ERROR) << "Release does not support property setting, skipping test: "
201 << test_flag_->SystemProperty();
202 return;
203 }
204
205 FlagBase::ReloadAllFlags("test");
206
207 test_flag_->AssertCmdlineValue(true, 1);
208 test_flag_->AssertSysPropValue(true, 2);
209 test_flag_->AssertServerSettingValue(false, 3);
210 test_flag_->AssertDefaultValue(42);
211
212 ASSERT_EQ(test_flag_->Value(), 2);
213 }
214
215 // Validate that the cmdline value is picked when no properties are set.
TEST_F(FlagsTestsWithCmdLine,FlagsTestsGetValueCmdline)216 TEST_F(FlagsTestsWithCmdLine, FlagsTestsGetValueCmdline) {
217 FlagBase::ReloadAllFlags("test");
218
219 test_flag_->AssertCmdlineValue(true, 1);
220 test_flag_->AssertSysPropValue(false, 2);
221 test_flag_->AssertServerSettingValue(false, 3);
222 test_flag_->AssertDefaultValue(42);
223
224 ASSERT_EQ(test_flag_->Value(), 1);
225 }
226
227 // Validate that cmdline only flags don't read system properties.
TEST_F(FlagsTestsCmdLineOnly,CmdlineOnlyFlags)228 TEST_F(FlagsTestsCmdLineOnly, CmdlineOnlyFlags) {
229 if (!android::base::SetProperty(test_flag_->SystemProperty(), "2")) {
230 LOG(ERROR) << "Release does not support property setting, skipping test: "
231 << test_flag_->SystemProperty();
232 return;
233 }
234
235 if (!android::base::SetProperty(test_flag_->ServerSetting(), "3")) {
236 LOG(ERROR) << "Release does not support property setting, skipping test: "
237 << test_flag_->ServerSetting();
238 return;
239 }
240
241 FlagBase::ReloadAllFlags("test");
242
243 test_flag_->AssertCmdlineValue(true, 1);
244 test_flag_->AssertSysPropValue(false, 2);
245 test_flag_->AssertServerSettingValue(false, 3);
246 test_flag_->AssertDefaultValue(42);
247
248 ASSERT_EQ(test_flag_->Value(), 1);
249 }
250
251 } // namespace art
252