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