blob: b0c4597d731b09774799a4c64fbda3ab0fec3eeb [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
Richard Uhler03bc6592016-11-22 09:42:04 +0000139 bool GetCachedImageFile(const std::string& image_location,
140 /*out*/std::string* image,
141 /*out*/std::string* error_msg) const {
Richard Uhler55b58b62016-08-12 09:05:13 -0700142 std::string cache;
143 bool have_android_data;
144 bool dalvik_cache_exists;
145 bool is_global_cache;
146 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
147 true,
148 &cache,
149 &have_android_data,
150 &dalvik_cache_exists,
151 &is_global_cache);
152 if (!dalvik_cache_exists) {
153 *error_msg = "Failed to create dalvik cache";
154 return false;
155 }
Richard Uhler03bc6592016-11-22 09:42:04 +0000156 return GetDalvikCacheFilename(image_location.c_str(), cache.c_str(), image, error_msg);
157 }
158
159 // Returns the path to an image location whose contents differ from the
160 // image at GetImageLocation(). This is used for testing mismatched
161 // image checksums in the oat_file_assistant_tests.
162 std::string GetImageLocation2() const {
163 return GetImageDirectory() + "/core-npic.art";
Andreas Gampee1459ae2016-06-29 09:36:30 -0700164 }
165
166 std::string GetDexSrc1() const {
167 return GetTestDexFileName("Main");
168 }
169
170 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
171 // file stripped.
172 std::string GetStrippedDexSrc1() const {
173 return GetTestDexFileName("MainStripped");
174 }
175
176 std::string GetMultiDexSrc1() const {
177 return GetTestDexFileName("MultiDex");
178 }
179
180 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
181 // with the contents of the secondary dex file changed.
182 std::string GetMultiDexSrc2() const {
183 return GetTestDexFileName("MultiDexModifiedSecondary");
184 }
185
186 std::string GetDexSrc2() const {
187 return GetTestDexFileName("Nested");
188 }
189
190 // Scratch directory, for dex and odex files (oat files will go in the
191 // dalvik cache).
192 const std::string& GetScratchDir() const {
193 return scratch_dir_;
194 }
195
196 // Odex directory is the subdirectory in the scratch directory where odex
197 // files should be located.
198 const std::string& GetOdexDir() const {
199 return odex_dir_;
200 }
201
202 private:
203 std::string scratch_dir_;
204 std::string odex_oat_dir_;
205 std::string odex_dir_;
206};
207
208} // namespace art
209
210#endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_