blob: 665baa6c7319d9c1923751bf849ae7ff23d52f84 [file] [log] [blame]
Jeff Haoc3acfc52016-08-29 14:18:26 -07001/*
2 * Copyright (C) 2016 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 <string>
18#include <vector>
19#include <sstream>
20
21#include <sys/types.h>
22#include <unistd.h>
23
24#include "base/stringprintf.h"
Jeff Hao042e8982016-10-19 11:17:11 -070025#include "base/unix_file/fd_file.h"
Jeff Haoc3acfc52016-08-29 14:18:26 -070026#include "common_runtime_test.h"
27#include "utils.h"
28
29namespace art {
30
Jeff Hao042e8982016-10-19 11:17:11 -070031static const char kDexFileLayoutInputDex[] =
32 "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH"
33 "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB"
34 "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA"
35 "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAEAAAAAAAAAdQEAAAAAAAABAAAA"
36 "AAAAAAIAAAAAAAAAAgAAAAAAAAB/AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB"
37 "AAAAbwEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph"
38 "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQAAgIAEgAIAAAEAAYCABJgCAAAACwAA"
39 "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA"
40 "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC"
41 "AAAAdQEAAAAQAAABAAAAjAEAAA==";
42
43static const char kDexFileLayoutInputProfile[] =
44 "cHJvADAwMgABAAsAAAABAPUpbf5jbGFzc2VzLmRleAEA";
45
46static const char kDexFileLayoutExpectedOutputDex[] =
47 "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH"
48 "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB"
49 "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA"
50 "AAAAAAABAAAAAAAAAAIAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAIAAAAAAAAAdQEAAAAAAAAAAAAA"
51 "AAAAAAIAAAAAAAAAAQAAAAAAAAB/AQAAAAAAAAEAAQABAAAAbwEAAAQAAABwEAIAAAAOAAEAAQAB"
52 "AAAAaQEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph"
53 "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQABgIAEgAIAAAEAAICABJgCAAAACwAA"
54 "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA"
55 "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC"
56 "AAAAdQEAAAAQAAABAAAAjAEAAA==";
57
58static void WriteFileBase64(const char* base64, const char* location) {
59 // Decode base64.
60 CHECK(base64 != nullptr);
61 size_t length;
62 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
63 CHECK(bytes.get() != nullptr);
64
65 // Write to provided file.
66 std::unique_ptr<File> file(OS::CreateEmptyFile(location));
67 CHECK(file.get() != nullptr);
68 if (!file->WriteFully(bytes.get(), length)) {
69 PLOG(FATAL) << "Failed to write base64 as file";
70 }
71 if (file->FlushCloseOrErase() != 0) {
72 PLOG(FATAL) << "Could not flush and close test file.";
73 }
74}
75
Jeff Haoc3acfc52016-08-29 14:18:26 -070076class DexLayoutTest : public CommonRuntimeTest {
77 protected:
78 virtual void SetUp() {
79 CommonRuntimeTest::SetUp();
Jeff Haoc3acfc52016-08-29 14:18:26 -070080 }
81
Jeff Haoa8621002016-10-04 18:13:44 +000082 // Runs FullPlainOutput test.
83 bool FullPlainOutputExec(std::string* error_msg) {
Jeff Haoc3acfc52016-08-29 14:18:26 -070084 // TODO: dexdump2 -> dexdump ?
85 ScratchFile dexdump_output;
Andreas Gampeca620d72016-11-08 08:09:33 -080086 const std::string& dexdump_filename = dexdump_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -070087 std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
88 EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070089
90 ScratchFile dexlayout_output;
Andreas Gampeca620d72016-11-08 08:09:33 -080091 const std::string& dexlayout_filename = dexlayout_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -070092 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
93 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070094
Jeff Haoa8621002016-10-04 18:13:44 +000095 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
96 std::vector<std::string> dexdump_exec_argv =
97 { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file };
98 std::vector<std::string> dexlayout_exec_argv =
99 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file };
Jeff Haoa8621002016-10-04 18:13:44 +0000100 if (!::art::Exec(dexdump_exec_argv, error_msg)) {
101 return false;
102 }
103 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
104 return false;
105 }
106 std::vector<std::string> diff_exec_argv =
107 { "/usr/bin/diff", dexdump_filename, dexlayout_filename };
108 if (!::art::Exec(diff_exec_argv, error_msg)) {
109 return false;
110 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700111 }
112 return true;
113 }
114
Jeff Haoa8621002016-10-04 18:13:44 +0000115 // Runs DexFileOutput test.
116 bool DexFileOutputExec(std::string* error_msg) {
117 ScratchFile tmp_file;
Andreas Gampeca620d72016-11-08 08:09:33 -0800118 const std::string& tmp_name = tmp_file.GetFilename();
119 size_t tmp_last_slash = tmp_name.rfind('/');
Jeff Haoa8621002016-10-04 18:13:44 +0000120 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
121 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
122 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
123
124 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
125 std::vector<std::string> dexlayout_exec_argv =
Jeff Hao042e8982016-10-19 11:17:11 -0700126 { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file };
Jeff Haoa8621002016-10-04 18:13:44 +0000127 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
128 return false;
129 }
Jeff Hao042e8982016-10-19 11:17:11 -0700130 size_t dex_file_last_slash = dex_file.rfind("/");
Jeff Haoa8621002016-10-04 18:13:44 +0000131 std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1);
132 std::vector<std::string> unzip_exec_argv =
133 { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir};
134 if (!::art::Exec(unzip_exec_argv, error_msg)) {
135 return false;
136 }
137 std::vector<std::string> diff_exec_argv =
138 { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name };
139 if (!::art::Exec(diff_exec_argv, error_msg)) {
140 return false;
141 }
142 std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" };
143 if (!::art::Exec(rm_zip_exec_argv, error_msg)) {
144 return false;
145 }
146 std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name };
147 if (!::art::Exec(rm_out_exec_argv, error_msg)) {
148 return false;
149 }
150 }
Jeff Hao042e8982016-10-19 11:17:11 -0700151 return true;
152 }
Jeff Haoa8621002016-10-04 18:13:44 +0000153
Jeff Hao042e8982016-10-19 11:17:11 -0700154 // Runs DexFileOutput test.
155 bool DexFileLayoutExec(std::string* error_msg) {
156 ScratchFile tmp_file;
157 std::string tmp_name = tmp_file.GetFilename();
158 size_t tmp_last_slash = tmp_name.rfind("/");
159 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
160
161 // Write inputs and expected outputs.
162 std::string dex_file = tmp_dir + "classes.dex";
163 WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str());
164 std::string profile_file = tmp_dir + "primary.prof";
165 WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str());
166 std::string expected_output = tmp_dir + "expected.dex";
167 WriteFileBase64(kDexFileLayoutExpectedOutputDex, expected_output.c_str());
168 std::string output_dex = tmp_dir + "classes.dex.new";
169
170 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
171 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
172
173 std::vector<std::string> dexlayout_exec_argv =
174 { dexlayout, "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file };
175 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
176 return false;
177 }
178 std::vector<std::string> diff_exec_argv =
179 { "/usr/bin/diff", expected_output, output_dex };
180 if (!::art::Exec(diff_exec_argv, error_msg)) {
181 return false;
182 }
183
184 std::vector<std::string> rm_exec_argv =
185 { "/bin/rm", dex_file, profile_file, expected_output, output_dex };
186 if (!::art::Exec(rm_exec_argv, error_msg)) {
187 return false;
188 }
Jeff Haoa8621002016-10-04 18:13:44 +0000189 return true;
190 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700191};
192
193
194TEST_F(DexLayoutTest, FullPlainOutput) {
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700195 // Disable test on target.
196 TEST_DISABLED_FOR_TARGET();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700197 std::string error_msg;
Jeff Haoa8621002016-10-04 18:13:44 +0000198 ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
199}
200
201TEST_F(DexLayoutTest, DexFileOutput) {
202 // Disable test on target.
203 TEST_DISABLED_FOR_TARGET();
204 std::string error_msg;
205 ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
Jeff Haoc3acfc52016-08-29 14:18:26 -0700206}
207
Jeff Hao042e8982016-10-19 11:17:11 -0700208TEST_F(DexLayoutTest, DexFileLayout) {
209 // Disable test on target.
210 TEST_DISABLED_FOR_TARGET();
211 std::string error_msg;
212 ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg;
213}
214
Jeff Haoc3acfc52016-08-29 14:18:26 -0700215} // namespace art