blob: d717ec0fcb03859fef2f87102edcc227899dc81f [file] [log] [blame]
Andreas Gampee1459ae2016-06-29 09:36:30 -07001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
18#define ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_
19
20#include <fstream>
21#include <string>
22#include <vector>
23
24#include <gtest/gtest.h>
25
26#include "common_runtime_test.h"
27#include "compiler_callbacks.h"
28#include "gc/heap.h"
29#include "gc/space/image_space.h"
30#include "oat_file_assistant.h"
31#include "os.h"
32#include "runtime.h"
33#include "utils.h"
34
35namespace art {
36
37// Test class that provides some helpers to set a test up for compilation using dex2oat.
38class Dex2oatEnvironmentTest : public CommonRuntimeTest {
39 public:
40 virtual void SetUp() OVERRIDE {
41 CommonRuntimeTest::SetUp();
42
43 // Create a scratch directory to work from.
44 scratch_dir_ = android_data_ + "/Dex2oatEnvironmentTest";
45 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
46
47 // Create a subdirectory in scratch for odex files.
48 odex_oat_dir_ = scratch_dir_ + "/oat";
49 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
50
51 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
52 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
53
54 // Verify the environment is as we expect
55 uint32_t checksum;
56 std::string error_msg;
57 ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str()))
58 << "Expected pre-compiled boot image to be at: " << GetSystemImageFile();
59 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
60 << "Expected dex file to be at: " << GetDexSrc1();
61 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
62 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
63 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
64 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
65 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
66 << "Expected dex file to be at: " << GetDexSrc2();
67
68 // GetMultiDexSrc2 should have the same primary dex checksum as
69 // GetMultiDexSrc1, but a different secondary dex checksum.
70 static constexpr bool kVerifyChecksum = true;
71 std::vector<std::unique_ptr<const DexFile>> multi1;
72 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc1().c_str(),
73 GetMultiDexSrc1().c_str(), kVerifyChecksum, &error_msg, &multi1)) << error_msg;
74 ASSERT_GT(multi1.size(), 1u);
75
76 std::vector<std::unique_ptr<const DexFile>> multi2;
77 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(),
78 GetMultiDexSrc2().c_str(), kVerifyChecksum, &error_msg, &multi2)) << error_msg;
79 ASSERT_GT(multi2.size(), 1u);
80
81 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
82 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
83 }
84
85 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
86 // options->push_back(std::make_pair("-verbose:oat", nullptr));
87
88 // Set up the image location.
89 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
90 nullptr));
91 // Make sure compilercallbacks are not set so that relocation will be
92 // enabled.
93 callbacks_.reset();
94 }
95
96 virtual void TearDown() OVERRIDE {
97 ClearDirectory(odex_dir_.c_str());
98 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
99
100 ClearDirectory(odex_oat_dir_.c_str());
101 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
102
103 ClearDirectory(scratch_dir_.c_str());
104 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
105
106 CommonRuntimeTest::TearDown();
107 }
108
109 static void Copy(const std::string& src, const std::string& dst) {
110 std::ifstream src_stream(src, std::ios::binary);
111 std::ofstream dst_stream(dst, std::ios::binary);
112
113 dst_stream << src_stream.rdbuf();
114 }
115
116 // Returns the directory where the pre-compiled core.art can be found.
117 // TODO: We should factor out this into common tests somewhere rather than
118 // re-hardcoding it here (This was copied originally from the elf writer
119 // test).
120 std::string GetImageDirectory() const {
121 if (IsHost()) {
122 const char* host_dir = getenv("ANDROID_HOST_OUT");
123 CHECK(host_dir != nullptr);
124 return std::string(host_dir) + "/framework";
125 } else {
126 return std::string("/data/art-test");
127 }
128 }
129
130 std::string GetImageLocation() const {
131 return GetImageDirectory() + "/core.art";
132 }
133
134 std::string GetSystemImageFile() const {
135 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
136 + "/core.art";
137 }
138
139 bool GetCachedImageFile(/*out*/std::string* image, std::string* error_msg) const {
Richard Uhler55b58b62016-08-12 09:05:13 -0700140 std::string cache;
141 bool have_android_data;
142 bool dalvik_cache_exists;
143 bool is_global_cache;
144 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
145 true,
146 &cache,
147 &have_android_data,
148 &dalvik_cache_exists,
149 &is_global_cache);
150 if (!dalvik_cache_exists) {
151 *error_msg = "Failed to create dalvik cache";
152 return false;
153 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700154 return GetDalvikCacheFilename(GetImageLocation().c_str(), cache.c_str(), image, error_msg);
155 }
156
157 std::string GetDexSrc1() const {
158 return GetTestDexFileName("Main");
159 }
160
161 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
162 // file stripped.
163 std::string GetStrippedDexSrc1() const {
164 return GetTestDexFileName("MainStripped");
165 }
166
167 std::string GetMultiDexSrc1() const {
168 return GetTestDexFileName("MultiDex");
169 }
170
171 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
172 // with the contents of the secondary dex file changed.
173 std::string GetMultiDexSrc2() const {
174 return GetTestDexFileName("MultiDexModifiedSecondary");
175 }
176
177 std::string GetDexSrc2() const {
178 return GetTestDexFileName("Nested");
179 }
180
181 // Scratch directory, for dex and odex files (oat files will go in the
182 // dalvik cache).
183 const std::string& GetScratchDir() const {
184 return scratch_dir_;
185 }
186
187 // Odex directory is the subdirectory in the scratch directory where odex
188 // files should be located.
189 const std::string& GetOdexDir() const {
190 return odex_dir_;
191 }
192
193 private:
194 std::string scratch_dir_;
195 std::string odex_oat_dir_;
196 std::string odex_dir_;
197};
198
199} // namespace art
200
201#endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_