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 "parsed_options.h"
18
19#include <memory>
20
21#include "arch/instruction_set.h"
22#include "base/stringprintf.h"
23#include "common_runtime_test.h"
24
25namespace art {
26
27class ParsedOptionsTest : public ::testing::Test {
28 public:
29  static void SetUpTestCase() {
30    CommonRuntimeTest::SetUpAndroidRoot();
31  }
32};
33
34TEST_F(ParsedOptionsTest, ParsedOptions) {
35  void* test_vfprintf = reinterpret_cast<void*>(0xa);
36  void* test_abort = reinterpret_cast<void*>(0xb);
37  void* test_exit = reinterpret_cast<void*>(0xc);
38
39  std::string boot_class_path;
40  std::string class_path;
41  boot_class_path += "-Xbootclasspath:";
42
43  bool first_dex_file = true;
44  for (const std::string &dex_file_name :
45           CommonRuntimeTest::GetLibCoreDexFileNames()) {
46    if (!first_dex_file) {
47      class_path += ":";
48    } else {
49      first_dex_file = false;
50    }
51    class_path += dex_file_name;
52  }
53  boot_class_path += class_path;
54
55  RuntimeOptions options;
56  options.push_back(std::make_pair(boot_class_path.c_str(), nullptr));
57  options.push_back(std::make_pair("-classpath", nullptr));
58  options.push_back(std::make_pair(class_path.c_str(), nullptr));
59  options.push_back(std::make_pair("-cp", nullptr));
60  options.push_back(std::make_pair(class_path.c_str(), nullptr));
61  options.push_back(std::make_pair("-Ximage:boot_image", nullptr));
62  options.push_back(std::make_pair("-Xcheck:jni", nullptr));
63  options.push_back(std::make_pair("-Xms2048", nullptr));
64  options.push_back(std::make_pair("-Xmx4k", nullptr));
65  options.push_back(std::make_pair("-Xss1m", nullptr));
66  options.push_back(std::make_pair("-XX:HeapTargetUtilization=0.75", nullptr));
67  options.push_back(std::make_pair("-Dfoo=bar", nullptr));
68  options.push_back(std::make_pair("-Dbaz=qux", nullptr));
69  options.push_back(std::make_pair("-verbose:gc,class,jni", nullptr));
70  options.push_back(std::make_pair("vfprintf", test_vfprintf));
71  options.push_back(std::make_pair("abort", test_abort));
72  options.push_back(std::make_pair("exit", test_exit));
73
74  RuntimeArgumentMap map;
75  bool parsed = ParsedOptions::Parse(options, false, &map);
76  ASSERT_TRUE(parsed);
77  ASSERT_NE(0u, map.Size());
78
79  using Opt = RuntimeArgumentMap;
80
81#define EXPECT_PARSED_EQ(expected, actual_key) EXPECT_EQ(expected, map.GetOrDefault(actual_key))
82#define EXPECT_PARSED_EXISTS(actual_key) EXPECT_TRUE(map.Exists(actual_key))
83
84  EXPECT_PARSED_EQ(class_path, Opt::BootClassPath);
85  EXPECT_PARSED_EQ(class_path, Opt::ClassPath);
86  EXPECT_PARSED_EQ(std::string("boot_image"), Opt::Image);
87  EXPECT_PARSED_EXISTS(Opt::CheckJni);
88  EXPECT_PARSED_EQ(2048U, Opt::MemoryInitialSize);
89  EXPECT_PARSED_EQ(4 * KB, Opt::MemoryMaximumSize);
90  EXPECT_PARSED_EQ(1 * MB, Opt::StackSize);
91  EXPECT_DOUBLE_EQ(0.75, map.GetOrDefault(Opt::HeapTargetUtilization));
92  EXPECT_TRUE(test_vfprintf == map.GetOrDefault(Opt::HookVfprintf));
93  EXPECT_TRUE(test_exit == map.GetOrDefault(Opt::HookExit));
94  EXPECT_TRUE(test_abort == map.GetOrDefault(Opt::HookAbort));
95  EXPECT_TRUE(VLOG_IS_ON(class_linker));
96  EXPECT_FALSE(VLOG_IS_ON(compiler));
97  EXPECT_FALSE(VLOG_IS_ON(heap));
98  EXPECT_TRUE(VLOG_IS_ON(gc));
99  EXPECT_FALSE(VLOG_IS_ON(jdwp));
100  EXPECT_TRUE(VLOG_IS_ON(jni));
101  EXPECT_FALSE(VLOG_IS_ON(monitor));
102  EXPECT_FALSE(VLOG_IS_ON(signals));
103  EXPECT_FALSE(VLOG_IS_ON(simulator));
104  EXPECT_FALSE(VLOG_IS_ON(startup));
105  EXPECT_FALSE(VLOG_IS_ON(third_party_jni));
106  EXPECT_FALSE(VLOG_IS_ON(threads));
107
108  auto&& properties_list = map.GetOrDefault(Opt::PropertiesList);
109  ASSERT_EQ(2U, properties_list.size());
110  EXPECT_EQ("foo=bar", properties_list[0]);
111  EXPECT_EQ("baz=qux", properties_list[1]);
112}
113
114TEST_F(ParsedOptionsTest, ParsedOptionsGc) {
115  RuntimeOptions options;
116  options.push_back(std::make_pair("-Xgc:MC", nullptr));
117
118  RuntimeArgumentMap map;
119  bool parsed = ParsedOptions::Parse(options, false, &map);
120  ASSERT_TRUE(parsed);
121  ASSERT_NE(0u, map.Size());
122
123  using Opt = RuntimeArgumentMap;
124
125  EXPECT_TRUE(map.Exists(Opt::GcOption));
126
127  XGcOption xgc = map.GetOrDefault(Opt::GcOption);
128  EXPECT_EQ(gc::kCollectorTypeMC, xgc.collector_type_);
129}
130
131TEST_F(ParsedOptionsTest, ParsedOptionsInstructionSet) {
132  using Opt = RuntimeArgumentMap;
133
134  {
135    // Nothing set, should be kRuntimeISA.
136    RuntimeOptions options;
137    RuntimeArgumentMap map;
138    bool parsed = ParsedOptions::Parse(options, false, &map);
139    ASSERT_TRUE(parsed);
140    InstructionSet isa = map.GetOrDefault(Opt::ImageInstructionSet);
141    EXPECT_EQ(kRuntimeISA, isa);
142  }
143
144  const char* isa_strings[] = { "arm", "arm64", "x86", "x86_64", "mips", "mips64" };
145  InstructionSet ISAs[] = { InstructionSet::kArm,
146                            InstructionSet::kArm64,
147                            InstructionSet::kX86,
148                            InstructionSet::kX86_64,
149                            InstructionSet::kMips,
150                            InstructionSet::kMips64 };
151  static_assert(arraysize(isa_strings) == arraysize(ISAs), "Need same amount.");
152
153  for (size_t i = 0; i < arraysize(isa_strings); ++i) {
154    RuntimeOptions options;
155    options.push_back(std::make_pair("imageinstructionset", isa_strings[i]));
156    RuntimeArgumentMap map;
157    bool parsed = ParsedOptions::Parse(options, false, &map);
158    ASSERT_TRUE(parsed);
159    InstructionSet isa = map.GetOrDefault(Opt::ImageInstructionSet);
160    EXPECT_EQ(ISAs[i], isa);
161  }
162}
163
164}  // namespace art
165