• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
18 
19 #include <numeric>
20 
21 #include "gtest/gtest.h"
22 
23 #include "base/mutex.h"
24 #include "base/utils.h"
25 #include "jdwp_provider.h"
26 #include "experimental_flags.h"
27 #include "parsed_options.h"
28 #include "runtime.h"
29 #include "runtime_options.h"
30 
31 #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
32                                         reinterpret_cast<void*>(nullptr));
33 
34 namespace art {
35   bool UsuallyEquals(double expected, double actual);
36 
37   // This has a gtest dependency, which is why it's in the gtest only.
operator ==(const ProfileSaverOptions & lhs,const ProfileSaverOptions & rhs)38   bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) {
39     return lhs.enabled_ == rhs.enabled_ &&
40         lhs.min_save_period_ms_ == rhs.min_save_period_ms_ &&
41         lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ &&
42         lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ &&
43         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_;
47   }
48 
UsuallyEquals(double expected,double actual)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>
UsuallyEquals(const T & expected,const T & actual,typename std::enable_if<detail::SupportsEqualityOperator<T>::value>::type * =0)60   bool UsuallyEquals(const T& expected, const T& actual,
61                      typename std::enable_if<
62                          detail::SupportsEqualityOperator<T>::value>::type* = 0) {
63     return expected == actual;
64   }
65 
66   // Try to use memcmp to compare simple plain-old-data structs.
67   //
68   // This should *not* generate false positives, but it can generate false negatives.
69   // This will mostly work except for fields like float which can have different bit patterns
70   // that are nevertheless equal.
71   // If a test is failing because the structs aren't "equal" when they really are
72   // then it's recommended to implement operator== for it instead.
73   template <typename T, typename ... Ignore>
UsuallyEquals(const T & expected,const T & actual,const Ignore &...more ATTRIBUTE_UNUSED,typename std::enable_if<std::is_pod<T>::value>::type * =0,typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type * =0)74   bool UsuallyEquals(const T& expected, const T& actual,
75                      const Ignore& ... more ATTRIBUTE_UNUSED,
76                      typename std::enable_if<std::is_pod<T>::value>::type* = 0,
77                      typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0
78                      ) {
79     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
80   }
81 
UsuallyEquals(const XGcOption & expected,const XGcOption & actual)82   bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
83     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
84   }
85 
UsuallyEquals(const char * expected,const std::string & actual)86   bool UsuallyEquals(const char* expected, const std::string& actual) {
87     return std::string(expected) == actual;
88   }
89 
90   template <typename TMap, typename TKey, typename T>
IsExpectedKeyValue(const T & expected,const TMap & map,const TKey & key)91   ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
92                                                 const TMap& map,
93                                                 const TKey& key) {
94     auto* actual = map.Get(key);
95     if (actual != nullptr) {
96       if (!UsuallyEquals(expected, *actual)) {
97         return ::testing::AssertionFailure()
98           << "expected " << detail::ToStringAny(expected) << " but got "
99           << detail::ToStringAny(*actual);
100       }
101       return ::testing::AssertionSuccess();
102     }
103 
104     return ::testing::AssertionFailure() << "key was not in the map";
105   }
106 
107   template <typename TMap, typename TKey, typename T>
IsExpectedDefaultKeyValue(const T & expected,const TMap & map,const TKey & key)108   ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected,
109                                                        const TMap& map,
110                                                        const TKey& key) {
111     const T& actual = map.GetOrDefault(key);
112     if (!UsuallyEquals(expected, actual)) {
113       return ::testing::AssertionFailure()
114           << "expected " << detail::ToStringAny(expected) << " but got "
115           << detail::ToStringAny(actual);
116      }
117     return ::testing::AssertionSuccess();
118   }
119 
120 class CmdlineParserTest : public ::testing::Test {
121  public:
122   CmdlineParserTest() = default;
123   ~CmdlineParserTest() = default;
124 
125  protected:
126   using M = RuntimeArgumentMap;
127   using RuntimeParser = ParsedOptions::RuntimeParser;
128 
SetUpTestCase()129   static void SetUpTestCase() {
130     art::Locks::Init();
131     art::InitLogging(nullptr, art::Runtime::Abort);  // argv = null
132   }
133 
SetUp()134   virtual void SetUp() {
135     parser_ = ParsedOptions::MakeParser(false);  // do not ignore unrecognized options
136   }
137 
IsResultSuccessful(const CmdlineResult & result)138   static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) {
139     if (result.IsSuccess()) {
140       return ::testing::AssertionSuccess();
141     } else {
142       return ::testing::AssertionFailure()
143         << result.GetStatus() << " with: " << result.GetMessage();
144     }
145   }
146 
IsResultFailure(const CmdlineResult & result,CmdlineResult::Status failure_status)147   static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result,
148                                                     CmdlineResult::Status failure_status) {
149     if (result.IsSuccess()) {
150       return ::testing::AssertionFailure() << " got success but expected failure: "
151           << failure_status;
152     } else if (result.GetStatus() == failure_status) {
153       return ::testing::AssertionSuccess();
154     }
155 
156     return ::testing::AssertionFailure() << " expected failure " << failure_status
157         << " but got " << result.GetStatus();
158   }
159 
160   std::unique_ptr<RuntimeParser> parser_;
161 };
162 
163 #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
164 #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
165 #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
166 
167 #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)              \
168   do {                                                        \
169     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
170     EXPECT_EQ(0u, parser_->GetArgumentsMap().Size());         \
171 
172 #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)               \
173   _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);                   \
174   } while (false)
175 
176 #define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\
177   _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);                   \
178     RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
179     EXPECT_DEFAULT_KEY_VALUE(args, key, expected);            \
180   } while (false)                                             // NOLINT [readability/namespace] [5]
181 
182 #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key)                \
183   do {                                                        \
184     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
185     RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
186     EXPECT_EQ(1u, args.Size());                               \
187     EXPECT_KEY_EXISTS(args, key);                             \
188 
189 #define EXPECT_SINGLE_PARSE_EXISTS(argv, key)                 \
190     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
191   } while (false)
192 
193 #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key)        \
194     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
195     EXPECT_KEY_VALUE(args, key, expected);                    \
196   } while (false)
197 
198 #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key)    \
199   EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
200 
201 #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status)         \
202     do {                                                       \
203       EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
204       RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
205       EXPECT_EQ(0u, args.Size());                              \
206     } while (false)
207 
TEST_F(CmdlineParserTest,TestSimpleSuccesses)208 TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
209   auto& parser = *parser_;
210 
211   EXPECT_LT(0u, parser.CountDefinedArguments());
212 
213   {
214     // Test case 1: No command line arguments
215     EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
216     RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
217     EXPECT_EQ(0u, args.Size());
218   }
219 
220   EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
221   EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
222   EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
223   EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
224   EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
225   EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
226   EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
227   EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
228   EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
229   EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
230 }  // TEST_F
231 
TEST_F(CmdlineParserTest,TestSimpleFailures)232 TEST_F(CmdlineParserTest, TestSimpleFailures) {
233   // Test argument is unknown to the parser
234   EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
235   // Test value map substitution fails
236   EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
237   // Test value type parsing failures
238   EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure);  // invalid memory value
239   EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure);       // memory value too small
240   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange);  // toosmal
241   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange);  // toolarg
242   EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange);  // too small
243   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // not a valid suboption
244 }  // TEST_F
245 
TEST_F(CmdlineParserTest,TestLogVerbosity)246 TEST_F(CmdlineParserTest, TestLogVerbosity) {
247   {
248     const char* log_args = "-verbose:"
249         "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup,"
250         "third-party-jni,threads,verifier,verifier-debug";
251 
252     LogVerbosity log_verbosity = LogVerbosity();
253     log_verbosity.class_linker = true;
254     log_verbosity.compiler = true;
255     log_verbosity.gc = true;
256     log_verbosity.heap = true;
257     log_verbosity.jdwp = true;
258     log_verbosity.jni = true;
259     log_verbosity.monitor = true;
260     log_verbosity.profiler = true;
261     log_verbosity.signals = true;
262     log_verbosity.simulator = true;
263     log_verbosity.startup = true;
264     log_verbosity.third_party_jni = true;
265     log_verbosity.threads = true;
266     log_verbosity.verifier = true;
267     log_verbosity.verifier_debug = true;
268 
269     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
270   }
271 
272   {
273     const char* log_args = "-verbose:"
274         "class,compiler,gc,heap,jdwp,jni,monitor";
275 
276     LogVerbosity log_verbosity = LogVerbosity();
277     log_verbosity.class_linker = true;
278     log_verbosity.compiler = true;
279     log_verbosity.gc = true;
280     log_verbosity.heap = true;
281     log_verbosity.jdwp = true;
282     log_verbosity.jni = true;
283     log_verbosity.monitor = true;
284 
285     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
286   }
287 
288   EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage);  // invalid verbose opt
289 
290   {
291     const char* log_args = "-verbose:deopt";
292     LogVerbosity log_verbosity = LogVerbosity();
293     log_verbosity.deopt = true;
294     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
295   }
296 
297   {
298     const char* log_args = "-verbose:collector";
299     LogVerbosity log_verbosity = LogVerbosity();
300     log_verbosity.collector = true;
301     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
302   }
303 
304   {
305     const char* log_args = "-verbose:oat";
306     LogVerbosity log_verbosity = LogVerbosity();
307     log_verbosity.oat = true;
308     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
309   }
310 
311   {
312     const char* log_args = "-verbose:dex";
313     LogVerbosity log_verbosity = LogVerbosity();
314     log_verbosity.dex = true;
315     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
316   }
317 }  // TEST_F
318 
319 // TODO: Enable this b/19274810
TEST_F(CmdlineParserTest,DISABLED_TestXGcOption)320 TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
321   /*
322    * Test success
323    */
324   {
325     XGcOption option_all_true{};
326     option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
327     option_all_true.verify_pre_gc_heap_ = true;
328     option_all_true.verify_pre_sweeping_heap_ = true;
329     option_all_true.verify_post_gc_heap_ = true;
330     option_all_true.verify_pre_gc_rosalloc_ = true;
331     option_all_true.verify_pre_sweeping_rosalloc_ = true;
332     option_all_true.verify_post_gc_rosalloc_ = true;
333 
334     const char * xgc_args_all_true = "-Xgc:concurrent,"
335         "preverify,presweepingverify,postverify,"
336         "preverify_rosalloc,presweepingverify_rosalloc,"
337         "postverify_rosalloc,precise,"
338         "verifycardtable";
339 
340     EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
341 
342     XGcOption option_all_false{};
343     option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
344     option_all_false.verify_pre_gc_heap_ = false;
345     option_all_false.verify_pre_sweeping_heap_ = false;
346     option_all_false.verify_post_gc_heap_ = false;
347     option_all_false.verify_pre_gc_rosalloc_ = false;
348     option_all_false.verify_pre_sweeping_rosalloc_ = false;
349     option_all_false.verify_post_gc_rosalloc_ = false;
350 
351     const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
352         "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
353         "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
354 
355     EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
356 
357     XGcOption option_all_default{};
358 
359     const char* xgc_args_blank = "-Xgc:";
360     EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
361   }
362 
363   /*
364    * Test failures
365    */
366   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // invalid Xgc opt
367 }  // TEST_F
368 
369 /*
370  * { "-XjdwpProvider:_" }
371  */
TEST_F(CmdlineParserTest,TestJdwpProviderEmpty)372 TEST_F(CmdlineParserTest, TestJdwpProviderEmpty) {
373   {
374     EXPECT_SINGLE_PARSE_DEFAULT_VALUE(JdwpProvider::kNone, "", M::JdwpProvider);
375   }
376 }  // TEST_F
377 
TEST_F(CmdlineParserTest,TestJdwpProviderDefault)378 TEST_F(CmdlineParserTest, TestJdwpProviderDefault) {
379   const char* opt_args = "-XjdwpProvider:default";
380   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kDefaultJdwpProvider, opt_args, M::JdwpProvider);
381 }  // TEST_F
382 
TEST_F(CmdlineParserTest,TestJdwpProviderInternal)383 TEST_F(CmdlineParserTest, TestJdwpProviderInternal) {
384   const char* opt_args = "-XjdwpProvider:internal";
385   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kInternal, opt_args, M::JdwpProvider);
386 }  // TEST_F
387 
TEST_F(CmdlineParserTest,TestJdwpProviderNone)388 TEST_F(CmdlineParserTest, TestJdwpProviderNone) {
389   const char* opt_args = "-XjdwpProvider:none";
390   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kNone, opt_args, M::JdwpProvider);
391 }  // TEST_F
392 
TEST_F(CmdlineParserTest,TestJdwpProviderAdbconnection)393 TEST_F(CmdlineParserTest, TestJdwpProviderAdbconnection) {
394   const char* opt_args = "-XjdwpProvider:adbconnection";
395   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kAdbConnection, opt_args, M::JdwpProvider);
396 }  // TEST_F
397 
TEST_F(CmdlineParserTest,TestJdwpProviderHelp)398 TEST_F(CmdlineParserTest, TestJdwpProviderHelp) {
399   EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:help", CmdlineResult::kUsage);
400 }  // TEST_F
401 
TEST_F(CmdlineParserTest,TestJdwpProviderFail)402 TEST_F(CmdlineParserTest, TestJdwpProviderFail) {
403   EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:blablabla", CmdlineResult::kFailure);
404 }  // TEST_F
405 
406 /*
407  * -D_ -D_ -D_ ...
408  */
TEST_F(CmdlineParserTest,TestPropertiesList)409 TEST_F(CmdlineParserTest, TestPropertiesList) {
410   /*
411    * Test successes
412    */
413   {
414     std::vector<std::string> opt = {"hello"};
415 
416     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
417   }
418 
419   {
420     std::vector<std::string> opt = {"hello", "world"};
421 
422     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
423   }
424 
425   {
426     std::vector<std::string> opt = {"one", "two", "three"};
427 
428     EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
429   }
430 }  // TEST_F
431 
432 /*
433 * -Xcompiler-option foo -Xcompiler-option bar ...
434 */
TEST_F(CmdlineParserTest,TestCompilerOption)435 TEST_F(CmdlineParserTest, TestCompilerOption) {
436  /*
437   * Test successes
438   */
439   {
440     std::vector<std::string> opt = {"hello"};
441     EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
442   }
443 
444   {
445     std::vector<std::string> opt = {"hello", "world"};
446     EXPECT_SINGLE_PARSE_VALUE(opt,
447                               "-Xcompiler-option hello -Xcompiler-option world",
448                               M::CompilerOptions);
449   }
450 
451   {
452     std::vector<std::string> opt = {"one", "two", "three"};
453     EXPECT_SINGLE_PARSE_VALUE(opt,
454                               "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
455                               M::CompilerOptions);
456   }
457 }  // TEST_F
458 
459 /*
460 * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
461 */
TEST_F(CmdlineParserTest,TestJitOptions)462 TEST_F(CmdlineParserTest, TestJitOptions) {
463  /*
464   * Test successes
465   */
466   {
467     EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
468     EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
469   }
470   {
471     EXPECT_SINGLE_PARSE_VALUE(
472         MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
473     EXPECT_SINGLE_PARSE_VALUE(
474         MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
475   }
476   {
477     EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
478   }
479 }  // TEST_F
480 
481 /*
482 * -Xps-*
483 */
TEST_F(CmdlineParserTest,ProfileSaverOptions)484 TEST_F(CmdlineParserTest, ProfileSaverOptions) {
485   ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true);
486 
487   EXPECT_SINGLE_PARSE_VALUE(opt,
488                             "-Xjitsaveprofilinginfo "
489                             "-Xps-min-save-period-ms:1 "
490                             "-Xps-save-resolved-classes-delay-ms:2 "
491                             "-Xps-hot-startup-method-samples:3 "
492                             "-Xps-min-methods-to-save:4 "
493                             "-Xps-min-classes-to-save:5 "
494                             "-Xps-min-notification-before-wake:6 "
495                             "-Xps-max-notification-before-wake:7 "
496                             "-Xps-profile-path:abc "
497                             "-Xps-profile-boot-class-path",
498                             M::ProfileSaverOpts);
499 }  // TEST_F
500 
501 /* -Xexperimental:_ */
TEST_F(CmdlineParserTest,TestExperimentalFlags)502 TEST_F(CmdlineParserTest, TestExperimentalFlags) {
503   // Default
504   EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
505                                     "",
506                                     M::Experimental);
507 
508   // Disabled explicitly
509   EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
510                             "-Xexperimental:none",
511                             M::Experimental);
512 }
513 
514 // -Xverify:_
TEST_F(CmdlineParserTest,TestVerify)515 TEST_F(CmdlineParserTest, TestVerify) {
516   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone,     "-Xverify:none",     M::Verify);
517   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:remote",   M::Verify);
518   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:all",      M::Verify);
519   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
520 }
521 
TEST_F(CmdlineParserTest,TestIgnoreUnrecognized)522 TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
523   RuntimeParser::Builder parserBuilder;
524 
525   parserBuilder
526       .Define("-help")
527           .IntoKey(M::Help)
528       .IgnoreUnrecognized(true);
529 
530   parser_.reset(new RuntimeParser(parserBuilder.Build()));
531 
532   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
533   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
534 }  //  TEST_F
535 
TEST_F(CmdlineParserTest,TestIgnoredArguments)536 TEST_F(CmdlineParserTest, TestIgnoredArguments) {
537   std::initializer_list<const char*> ignored_args = {
538       "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
539       "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
540       "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
541       "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
542       "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
543       "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
544       "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
545   };
546 
547   // Check they are ignored when parsed one at a time
548   for (auto&& arg : ignored_args) {
549     SCOPED_TRACE(arg);
550     EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
551   }
552 
553   // Check they are ignored when we pass it all together at once
554   std::vector<const char*> argv = ignored_args;
555   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
556 }  //  TEST_F
557 
TEST_F(CmdlineParserTest,MultipleArguments)558 TEST_F(CmdlineParserTest, MultipleArguments) {
559   EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
560       "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
561       "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
562 
563   auto&& map = parser_->ReleaseArgumentsMap();
564   EXPECT_EQ(5u, map.Size());
565   EXPECT_KEY_VALUE(map, M::Help, Unit{});
566   EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
567   EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
568   EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});
569   EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
570 }  //  TEST_F
571 }  // namespace art
572