blob: 052d9ecac406031f4ee08a44d9b1ea466f585b3e [file] [log] [blame]
Igor Murashkinaaebaa02015-01-26 10:55:53 -08001/*
2 * Copyright (C) 2015 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 "cmdline_parser.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080018
Igor Murashkinaaebaa02015-01-26 10:55:53 -080019#include <numeric>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020
Igor Murashkinaaebaa02015-01-26 10:55:53 -080021#include "gtest/gtest.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/mutex.h"
24#include "base/utils.h"
Alex Light40320712017-12-14 11:52:04 -080025#include "jdwp_provider.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070026#include "experimental_flags.h"
27#include "parsed_options.h"
28#include "runtime.h"
29#include "runtime_options.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080030
31#define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
Mathieu Chartier2cebb242015-04-21 16:50:40 -070032 reinterpret_cast<void*>(nullptr));
Igor Murashkinaaebaa02015-01-26 10:55:53 -080033
34namespace art {
35 bool UsuallyEquals(double expected, double actual);
36
37 // This has a gtest dependency, which is why it's in the gtest only.
Calin Juravle138dbff2016-06-28 19:36:58 +010038 bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080039 return lhs.enabled_ == rhs.enabled_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010040 lhs.min_save_period_ms_ == rhs.min_save_period_ms_ &&
41 lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ &&
Mathieu Chartier7b135c82017-06-05 12:54:01 -070042 lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010043 lhs.min_methods_to_save_ == rhs.min_methods_to_save_ &&
44 lhs.min_classes_to_save_ == rhs.min_classes_to_save_ &&
45 lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ &&
46 lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080047 }
48
49 bool UsuallyEquals(double expected, double actual) {
50 using FloatingPoint = ::testing::internal::FloatingPoint<double>;
51
52 FloatingPoint exp(expected);
53 FloatingPoint act(actual);
54
55 // Compare with ULPs instead of comparing with ==
56 return exp.AlmostEquals(act);
57 }
58
59 template <typename T>
60 bool UsuallyEquals(const T& expected, const T& actual,
61 typename std::enable_if<
Yi Kong4b22b342018-08-02 14:43:21 -070062 detail::SupportsEqualityOperator<T>::value>::type* = nullptr) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080063 return expected == actual;
64 }
65
Vladimir Marko91f10322018-12-07 18:04:10 +000066 template <char Separator>
67 bool UsuallyEquals(const std::vector<std::string>& expected,
68 const ParseStringList<Separator>& actual) {
69 return expected == static_cast<std::vector<std::string>>(actual);
70 }
71
Igor Murashkinaaebaa02015-01-26 10:55:53 -080072 // Try to use memcmp to compare simple plain-old-data structs.
73 //
74 // This should *not* generate false positives, but it can generate false negatives.
75 // This will mostly work except for fields like float which can have different bit patterns
76 // that are nevertheless equal.
77 // If a test is failing because the structs aren't "equal" when they really are
78 // then it's recommended to implement operator== for it instead.
79 template <typename T, typename ... Ignore>
80 bool UsuallyEquals(const T& expected, const T& actual,
81 const Ignore& ... more ATTRIBUTE_UNUSED,
Yi Kong4b22b342018-08-02 14:43:21 -070082 typename std::enable_if<std::is_pod<T>::value>::type* = nullptr,
83 typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = nullptr
Igor Murashkinaaebaa02015-01-26 10:55:53 -080084 ) {
85 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
86 }
87
88 bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
89 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
90 }
91
Andreas Gampeca620d72016-11-08 08:09:33 -080092 bool UsuallyEquals(const char* expected, const std::string& actual) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080093 return std::string(expected) == actual;
94 }
95
96 template <typename TMap, typename TKey, typename T>
97 ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
98 const TMap& map,
99 const TKey& key) {
100 auto* actual = map.Get(key);
101 if (actual != nullptr) {
102 if (!UsuallyEquals(expected, *actual)) {
103 return ::testing::AssertionFailure()
104 << "expected " << detail::ToStringAny(expected) << " but got "
105 << detail::ToStringAny(*actual);
106 }
107 return ::testing::AssertionSuccess();
108 }
109
110 return ::testing::AssertionFailure() << "key was not in the map";
111 }
112
Igor Murashkin158f35c2015-06-10 15:55:30 -0700113 template <typename TMap, typename TKey, typename T>
114 ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected,
115 const TMap& map,
116 const TKey& key) {
117 const T& actual = map.GetOrDefault(key);
118 if (!UsuallyEquals(expected, actual)) {
119 return ::testing::AssertionFailure()
120 << "expected " << detail::ToStringAny(expected) << " but got "
121 << detail::ToStringAny(actual);
122 }
123 return ::testing::AssertionSuccess();
124 }
125
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800126class CmdlineParserTest : public ::testing::Test {
127 public:
128 CmdlineParserTest() = default;
129 ~CmdlineParserTest() = default;
130
131 protected:
132 using M = RuntimeArgumentMap;
133 using RuntimeParser = ParsedOptions::RuntimeParser;
134
135 static void SetUpTestCase() {
David Sehrc431b9d2018-03-02 12:01:51 -0800136 art::Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700137 art::InitLogging(nullptr, art::Runtime::Abort); // argv = null
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800138 }
139
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700140 void SetUp() override {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800141 parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options
142 }
143
Andreas Gampeca620d72016-11-08 08:09:33 -0800144 static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800145 if (result.IsSuccess()) {
146 return ::testing::AssertionSuccess();
147 } else {
148 return ::testing::AssertionFailure()
149 << result.GetStatus() << " with: " << result.GetMessage();
150 }
151 }
152
Andreas Gampeca620d72016-11-08 08:09:33 -0800153 static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result,
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800154 CmdlineResult::Status failure_status) {
155 if (result.IsSuccess()) {
156 return ::testing::AssertionFailure() << " got success but expected failure: "
157 << failure_status;
158 } else if (result.GetStatus() == failure_status) {
159 return ::testing::AssertionSuccess();
160 }
161
162 return ::testing::AssertionFailure() << " expected failure " << failure_status
163 << " but got " << result.GetStatus();
164 }
165
166 std::unique_ptr<RuntimeParser> parser_;
167};
168
169#define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
170#define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
Igor Murashkin158f35c2015-06-10 15:55:30 -0700171#define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800172
Igor Murashkin158f35c2015-06-10 15:55:30 -0700173#define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800174 do { \
175 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
176 EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700177
178#define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
179 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800180 } while (false)
181
Igor Murashkin158f35c2015-06-10 15:55:30 -0700182#define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\
183 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
184 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
185 EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \
186 } while (false) // NOLINT [readability/namespace] [5]
187
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800188#define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
189 do { \
190 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
191 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
192 EXPECT_EQ(1u, args.Size()); \
193 EXPECT_KEY_EXISTS(args, key); \
194
195#define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
196 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
197 } while (false)
198
199#define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \
200 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
201 EXPECT_KEY_VALUE(args, key, expected); \
Igor Murashkin5573c372017-11-16 13:34:30 -0800202 } while (false)
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800203
204#define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \
205 EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
206
207#define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \
208 do { \
209 EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
210 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
211 EXPECT_EQ(0u, args.Size()); \
212 } while (false)
213
214TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
215 auto& parser = *parser_;
216
217 EXPECT_LT(0u, parser.CountDefinedArguments());
218
219 {
220 // Test case 1: No command line arguments
221 EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
222 RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
223 EXPECT_EQ(0u, args.Size());
224 }
225
226 EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
Vladimir Marko91f10322018-12-07 18:04:10 +0000227 EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello/world"}),
228 "-Xbootclasspath:/hello/world",
229 M::BootClassPath);
230 EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello", "/world"}),
231 "-Xbootclasspath:/hello:/world",
232 M::BootClassPath);
233 EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-classpath /hello/world", M::ClassPath);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800234 EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
235 EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
236 EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
237 EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
238 EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
239 EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
Jean Christophe Beyler24e04aa2014-09-12 12:03:25 -0700240 EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800241} // TEST_F
242
243TEST_F(CmdlineParserTest, TestSimpleFailures) {
244 // Test argument is unknown to the parser
245 EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
246 // Test value map substitution fails
247 EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
248 // Test value type parsing failures
249 EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value
250 EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small
251 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal
252 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg
253 EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small
254 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption
255} // TEST_F
256
257TEST_F(CmdlineParserTest, TestLogVerbosity) {
258 {
259 const char* log_args = "-verbose:"
Mathieu Chartier765b2a02019-05-02 11:04:13 -0700260 "class,compiler,gc,heap,interpreter,jdwp,jni,monitor,profiler,signals,simulator,startup,"
Andreas Gampe92d77202017-12-06 20:49:00 -0800261 "third-party-jni,threads,verifier,verifier-debug";
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800262
263 LogVerbosity log_verbosity = LogVerbosity();
264 log_verbosity.class_linker = true;
265 log_verbosity.compiler = true;
266 log_verbosity.gc = true;
267 log_verbosity.heap = true;
Mathieu Chartier765b2a02019-05-02 11:04:13 -0700268 log_verbosity.interpreter = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800269 log_verbosity.jdwp = true;
270 log_verbosity.jni = true;
271 log_verbosity.monitor = true;
272 log_verbosity.profiler = true;
273 log_verbosity.signals = true;
Phil Wang751beff2015-08-28 15:17:15 +0800274 log_verbosity.simulator = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800275 log_verbosity.startup = true;
276 log_verbosity.third_party_jni = true;
277 log_verbosity.threads = true;
278 log_verbosity.verifier = true;
Andreas Gampe92d77202017-12-06 20:49:00 -0800279 log_verbosity.verifier_debug = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800280
281 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
282 }
283
284 {
285 const char* log_args = "-verbose:"
286 "class,compiler,gc,heap,jdwp,jni,monitor";
287
288 LogVerbosity log_verbosity = LogVerbosity();
289 log_verbosity.class_linker = true;
290 log_verbosity.compiler = true;
291 log_verbosity.gc = true;
292 log_verbosity.heap = true;
293 log_verbosity.jdwp = true;
294 log_verbosity.jni = true;
295 log_verbosity.monitor = true;
296
297 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
298 }
299
300 EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt
Richard Uhler66d874d2015-01-15 09:37:19 -0800301
302 {
Sebastien Hertzbba348e2015-06-01 08:28:18 +0200303 const char* log_args = "-verbose:deopt";
304 LogVerbosity log_verbosity = LogVerbosity();
305 log_verbosity.deopt = true;
306 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
307 }
308
309 {
Mathieu Chartier66a55392016-02-19 10:25:39 -0800310 const char* log_args = "-verbose:collector";
311 LogVerbosity log_verbosity = LogVerbosity();
312 log_verbosity.collector = true;
313 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
314 }
315
316 {
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 const char* log_args = "-verbose:oat";
318 LogVerbosity log_verbosity = LogVerbosity();
319 log_verbosity.oat = true;
320 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
321 }
Andreas Gampebec07a02017-04-11 13:48:37 -0700322
323 {
324 const char* log_args = "-verbose:dex";
325 LogVerbosity log_verbosity = LogVerbosity();
326 log_verbosity.dex = true;
327 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
328 }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800329} // TEST_F
330
Nicolas Geoffray8f4ee5c2015-02-05 10:14:10 +0000331// TODO: Enable this b/19274810
332TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800333 /*
334 * Test success
335 */
336 {
Igor Murashkin5573c372017-11-16 13:34:30 -0800337 XGcOption option_all_true{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800338 option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
339 option_all_true.verify_pre_gc_heap_ = true;
340 option_all_true.verify_pre_sweeping_heap_ = true;
341 option_all_true.verify_post_gc_heap_ = true;
342 option_all_true.verify_pre_gc_rosalloc_ = true;
343 option_all_true.verify_pre_sweeping_rosalloc_ = true;
344 option_all_true.verify_post_gc_rosalloc_ = true;
345
346 const char * xgc_args_all_true = "-Xgc:concurrent,"
347 "preverify,presweepingverify,postverify,"
348 "preverify_rosalloc,presweepingverify_rosalloc,"
349 "postverify_rosalloc,precise,"
350 "verifycardtable";
351
352 EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
353
Igor Murashkin5573c372017-11-16 13:34:30 -0800354 XGcOption option_all_false{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800355 option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
356 option_all_false.verify_pre_gc_heap_ = false;
357 option_all_false.verify_pre_sweeping_heap_ = false;
358 option_all_false.verify_post_gc_heap_ = false;
359 option_all_false.verify_pre_gc_rosalloc_ = false;
360 option_all_false.verify_pre_sweeping_rosalloc_ = false;
361 option_all_false.verify_post_gc_rosalloc_ = false;
362
363 const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
364 "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
365 "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
366
367 EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
368
Igor Murashkin5573c372017-11-16 13:34:30 -0800369 XGcOption option_all_default{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800370
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800371 const char* xgc_args_blank = "-Xgc:";
372 EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
373 }
374
375 /*
376 * Test failures
377 */
378 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt
379} // TEST_F
380
381/*
Alex Light40320712017-12-14 11:52:04 -0800382 * { "-XjdwpProvider:_" }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800383 */
Alex Light40320712017-12-14 11:52:04 -0800384TEST_F(CmdlineParserTest, TestJdwpProviderEmpty) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800385 {
Alex Lightbaac7e42018-06-08 15:30:11 +0000386 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(JdwpProvider::kUnset, "", M::JdwpProvider);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800387 }
Alex Light40320712017-12-14 11:52:04 -0800388} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800389
Alex Light40320712017-12-14 11:52:04 -0800390TEST_F(CmdlineParserTest, TestJdwpProviderDefault) {
391 const char* opt_args = "-XjdwpProvider:default";
Alex Light2183d4d2018-01-26 14:24:54 -0800392 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kDefaultJdwpProvider, opt_args, M::JdwpProvider);
Alex Light40320712017-12-14 11:52:04 -0800393} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800394
Alex Light40320712017-12-14 11:52:04 -0800395TEST_F(CmdlineParserTest, TestJdwpProviderInternal) {
396 const char* opt_args = "-XjdwpProvider:internal";
397 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kInternal, opt_args, M::JdwpProvider);
398} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800399
Alex Light40320712017-12-14 11:52:04 -0800400TEST_F(CmdlineParserTest, TestJdwpProviderNone) {
401 const char* opt_args = "-XjdwpProvider:none";
402 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kNone, opt_args, M::JdwpProvider);
403} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800404
Alex Lightfbf96702017-12-14 13:27:13 -0800405TEST_F(CmdlineParserTest, TestJdwpProviderAdbconnection) {
406 const char* opt_args = "-XjdwpProvider:adbconnection";
407 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kAdbConnection, opt_args, M::JdwpProvider);
408} // TEST_F
409
Alex Light40320712017-12-14 11:52:04 -0800410TEST_F(CmdlineParserTest, TestJdwpProviderHelp) {
411 EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:help", CmdlineResult::kUsage);
412} // TEST_F
413
414TEST_F(CmdlineParserTest, TestJdwpProviderFail) {
415 EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:blablabla", CmdlineResult::kFailure);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800416} // TEST_F
417
418/*
419 * -D_ -D_ -D_ ...
420 */
421TEST_F(CmdlineParserTest, TestPropertiesList) {
422 /*
423 * Test successes
424 */
425 {
426 std::vector<std::string> opt = {"hello"};
427
428 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
429 }
430
431 {
432 std::vector<std::string> opt = {"hello", "world"};
433
434 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
435 }
436
437 {
438 std::vector<std::string> opt = {"one", "two", "three"};
439
440 EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
441 }
442} // TEST_F
443
444/*
445* -Xcompiler-option foo -Xcompiler-option bar ...
446*/
447TEST_F(CmdlineParserTest, TestCompilerOption) {
448 /*
449 * Test successes
450 */
451 {
452 std::vector<std::string> opt = {"hello"};
453 EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
454 }
455
456 {
457 std::vector<std::string> opt = {"hello", "world"};
458 EXPECT_SINGLE_PARSE_VALUE(opt,
459 "-Xcompiler-option hello -Xcompiler-option world",
460 M::CompilerOptions);
461 }
462
463 {
464 std::vector<std::string> opt = {"one", "two", "three"};
465 EXPECT_SINGLE_PARSE_VALUE(opt,
466 "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
467 M::CompilerOptions);
468 }
469} // TEST_F
470
471/*
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800472* -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
473*/
474TEST_F(CmdlineParserTest, TestJitOptions) {
475 /*
476 * Test successes
477 */
478 {
Calin Juravleffc87072016-04-20 14:22:09 +0100479 EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
480 EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800481 }
482 {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000483 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000484 MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000485 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000486 MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800487 }
488 {
489 EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
490 }
491} // TEST_F
492
493/*
Calin Juravle138dbff2016-06-28 19:36:58 +0100494* -Xps-*
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800495*/
Calin Juravle138dbff2016-06-28 19:36:58 +0100496TEST_F(CmdlineParserTest, ProfileSaverOptions) {
Mathieu Chartier885a7132017-06-10 14:35:11 -0700497 ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800498
Calin Juravle138dbff2016-06-28 19:36:58 +0100499 EXPECT_SINGLE_PARSE_VALUE(opt,
500 "-Xjitsaveprofilinginfo "
501 "-Xps-min-save-period-ms:1 "
502 "-Xps-save-resolved-classes-delay-ms:2 "
Mathieu Chartier7b135c82017-06-05 12:54:01 -0700503 "-Xps-hot-startup-method-samples:3 "
Calin Juravle138dbff2016-06-28 19:36:58 +0100504 "-Xps-min-methods-to-save:4 "
505 "-Xps-min-classes-to-save:5 "
506 "-Xps-min-notification-before-wake:6 "
Calin Juravle9545f6d2017-03-16 19:05:09 -0700507 "-Xps-max-notification-before-wake:7 "
Mathieu Chartier885a7132017-06-10 14:35:11 -0700508 "-Xps-profile-path:abc "
509 "-Xps-profile-boot-class-path",
Calin Juravle138dbff2016-06-28 19:36:58 +0100510 M::ProfileSaverOpts);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800511} // TEST_F
512
Alex Lighteb7c1442015-08-31 13:17:42 -0700513/* -Xexperimental:_ */
514TEST_F(CmdlineParserTest, TestExperimentalFlags) {
Neil Fuller9724c632016-01-07 15:42:47 +0000515 // Default
Alex Lighteb7c1442015-08-31 13:17:42 -0700516 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700517 "",
Alex Lighteb7c1442015-08-31 13:17:42 -0700518 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700519
520 // Disabled explicitly
Alex Lighteb7c1442015-08-31 13:17:42 -0700521 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
522 "-Xexperimental:none",
523 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700524}
525
Igor Murashkin7617abd2015-07-10 18:27:47 -0700526// -Xverify:_
527TEST_F(CmdlineParserTest, TestVerify) {
528 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify);
529 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify);
530 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify);
531 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
532}
533
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800534TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
535 RuntimeParser::Builder parserBuilder;
536
537 parserBuilder
538 .Define("-help")
539 .IntoKey(M::Help)
540 .IgnoreUnrecognized(true);
541
542 parser_.reset(new RuntimeParser(parserBuilder.Build()));
543
544 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
545 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
546} // TEST_F
547
548TEST_F(CmdlineParserTest, TestIgnoredArguments) {
549 std::initializer_list<const char*> ignored_args = {
550 "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
551 "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
552 "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
553 "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800554 "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
555 "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800556 "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
557 };
558
559 // Check they are ignored when parsed one at a time
560 for (auto&& arg : ignored_args) {
561 SCOPED_TRACE(arg);
562 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
563 }
564
565 // Check they are ignored when we pass it all together at once
566 std::vector<const char*> argv = ignored_args;
567 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
568} // TEST_F
569
570TEST_F(CmdlineParserTest, MultipleArguments) {
571 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
572 "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000573 "-Xmethod-trace -XX:LargeObjectSpace=map")));
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800574
575 auto&& map = parser_->ReleaseArgumentsMap();
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000576 EXPECT_EQ(4u, map.Size());
Igor Murashkin5573c372017-11-16 13:34:30 -0800577 EXPECT_KEY_VALUE(map, M::Help, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800578 EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
Igor Murashkin5573c372017-11-16 13:34:30 -0800579 EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800580 EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
581} // TEST_F
582} // namespace art