blob: 46a1c435484cb309be6154b470bf02fd02294ef9 [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
Jeff Hao042e8982016-10-19 11:17:11 -070024#include "base/unix_file/fd_file.h"
Jeff Haoc3acfc52016-08-29 14:18:26 -070025#include "common_runtime_test.h"
26#include "utils.h"
27
28namespace art {
29
Jeff Hao042e8982016-10-19 11:17:11 -070030static const char kDexFileLayoutInputDex[] =
31 "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH"
32 "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB"
33 "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA"
34 "AAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAEAAAAAAAAAdQEAAAAAAAABAAAA"
35 "AAAAAAIAAAAAAAAAAgAAAAAAAAB/AQAAAAAAAAEAAQABAAAAaQEAAAQAAABwEAIAAAAOAAEAAQAB"
36 "AAAAbwEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph"
37 "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQAAgIAEgAIAAAEAAYCABJgCAAAACwAA"
38 "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA"
39 "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC"
40 "AAAAdQEAAAAQAAABAAAAjAEAAA==";
41
42static const char kDexFileLayoutInputProfile[] =
43 "cHJvADAwMgABAAsAAAABAPUpbf5jbGFzc2VzLmRleAEA";
44
45static const char kDexFileLayoutExpectedOutputDex[] =
46 "ZGV4CjAzNQD1KW3+B8NAB0f2A/ZVIBJ0aHrGIqcpVTAUAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAH"
47 "AAAAcAAAAAQAAACMAAAAAQAAAJwAAAAAAAAAAAAAAAMAAACoAAAAAgAAAMAAAAAUAQAAAAEAADAB"
48 "AAA4AQAAQAEAAEgBAABNAQAAUgEAAGYBAAADAAAABAAAAAUAAAAGAAAABgAAAAMAAAAAAAAAAAAA"
49 "AAAAAAABAAAAAAAAAAIAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAIAAAAAAAAAdQEAAAAAAAAAAAAA"
50 "AAAAAAIAAAAAAAAAAQAAAAAAAAB/AQAAAAAAAAEAAQABAAAAbwEAAAQAAABwEAIAAAAOAAEAAQAB"
51 "AAAAaQEAAAQAAABwEAIAAAAOAAY8aW5pdD4ABkEuamF2YQAGQi5qYXZhAANMQTsAA0xCOwASTGph"
52 "dmEvbGFuZy9PYmplY3Q7AAFWAAQABw48AAQABw48AAAAAQABgIAEgAIAAAEAAICABJgCAAAACwAA"
53 "AAAAAAABAAAAAAAAAAEAAAAHAAAAcAAAAAIAAAAEAAAAjAAAAAMAAAABAAAAnAAAAAUAAAADAAAA"
54 "qAAAAAYAAAACAAAAwAAAAAEgAAACAAAAAAEAAAIgAAAHAAAAMAEAAAMgAAACAAAAaQEAAAAgAAAC"
55 "AAAAdQEAAAAQAAABAAAAjAEAAA==";
56
57static void WriteFileBase64(const char* base64, const char* location) {
58 // Decode base64.
59 CHECK(base64 != nullptr);
60 size_t length;
61 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
62 CHECK(bytes.get() != nullptr);
63
64 // Write to provided file.
65 std::unique_ptr<File> file(OS::CreateEmptyFile(location));
66 CHECK(file.get() != nullptr);
67 if (!file->WriteFully(bytes.get(), length)) {
68 PLOG(FATAL) << "Failed to write base64 as file";
69 }
70 if (file->FlushCloseOrErase() != 0) {
71 PLOG(FATAL) << "Could not flush and close test file.";
72 }
73}
74
Jeff Haoc3acfc52016-08-29 14:18:26 -070075class DexLayoutTest : public CommonRuntimeTest {
76 protected:
77 virtual void SetUp() {
78 CommonRuntimeTest::SetUp();
Jeff Haoc3acfc52016-08-29 14:18:26 -070079 }
80
Jeff Haoa8621002016-10-04 18:13:44 +000081 // Runs FullPlainOutput test.
82 bool FullPlainOutputExec(std::string* error_msg) {
Jeff Haoc3acfc52016-08-29 14:18:26 -070083 // TODO: dexdump2 -> dexdump ?
84 ScratchFile dexdump_output;
Andreas Gampeca620d72016-11-08 08:09:33 -080085 const std::string& dexdump_filename = dexdump_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -070086 std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
87 EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070088
89 ScratchFile dexlayout_output;
Andreas Gampeca620d72016-11-08 08:09:33 -080090 const std::string& dexlayout_filename = dexlayout_output.GetFilename();
Jeff Haoc3acfc52016-08-29 14:18:26 -070091 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
92 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070093
Jeff Haoa8621002016-10-04 18:13:44 +000094 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
95 std::vector<std::string> dexdump_exec_argv =
96 { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file };
97 std::vector<std::string> dexlayout_exec_argv =
98 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file };
Jeff Haoa8621002016-10-04 18:13:44 +000099 if (!::art::Exec(dexdump_exec_argv, error_msg)) {
100 return false;
101 }
102 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
103 return false;
104 }
105 std::vector<std::string> diff_exec_argv =
106 { "/usr/bin/diff", dexdump_filename, dexlayout_filename };
107 if (!::art::Exec(diff_exec_argv, error_msg)) {
108 return false;
109 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700110 }
111 return true;
112 }
113
Jeff Haoa8621002016-10-04 18:13:44 +0000114 // Runs DexFileOutput test.
115 bool DexFileOutputExec(std::string* error_msg) {
116 ScratchFile tmp_file;
Andreas Gampeca620d72016-11-08 08:09:33 -0800117 const std::string& tmp_name = tmp_file.GetFilename();
118 size_t tmp_last_slash = tmp_name.rfind('/');
Jeff Haoa8621002016-10-04 18:13:44 +0000119 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
120 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
121 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
122
123 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
124 std::vector<std::string> dexlayout_exec_argv =
Jeff Hao042e8982016-10-19 11:17:11 -0700125 { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file };
Jeff Haoa8621002016-10-04 18:13:44 +0000126 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
127 return false;
128 }
Jeff Hao042e8982016-10-19 11:17:11 -0700129 size_t dex_file_last_slash = dex_file.rfind("/");
Jeff Haoa8621002016-10-04 18:13:44 +0000130 std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1);
131 std::vector<std::string> unzip_exec_argv =
132 { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir};
133 if (!::art::Exec(unzip_exec_argv, error_msg)) {
134 return false;
135 }
136 std::vector<std::string> diff_exec_argv =
137 { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name };
138 if (!::art::Exec(diff_exec_argv, error_msg)) {
139 return false;
140 }
141 std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" };
142 if (!::art::Exec(rm_zip_exec_argv, error_msg)) {
143 return false;
144 }
145 std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name };
146 if (!::art::Exec(rm_out_exec_argv, error_msg)) {
147 return false;
148 }
149 }
Jeff Hao042e8982016-10-19 11:17:11 -0700150 return true;
151 }
Jeff Haoa8621002016-10-04 18:13:44 +0000152
Jeff Hao042e8982016-10-19 11:17:11 -0700153 // Runs DexFileOutput test.
154 bool DexFileLayoutExec(std::string* error_msg) {
155 ScratchFile tmp_file;
156 std::string tmp_name = tmp_file.GetFilename();
157 size_t tmp_last_slash = tmp_name.rfind("/");
158 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
159
160 // Write inputs and expected outputs.
161 std::string dex_file = tmp_dir + "classes.dex";
162 WriteFileBase64(kDexFileLayoutInputDex, dex_file.c_str());
163 std::string profile_file = tmp_dir + "primary.prof";
164 WriteFileBase64(kDexFileLayoutInputProfile, profile_file.c_str());
165 std::string expected_output = tmp_dir + "expected.dex";
166 WriteFileBase64(kDexFileLayoutExpectedOutputDex, expected_output.c_str());
167 std::string output_dex = tmp_dir + "classes.dex.new";
168
169 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
170 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
171
172 std::vector<std::string> dexlayout_exec_argv =
173 { dexlayout, "-w", tmp_dir, "-o", tmp_name, "-p", profile_file, dex_file };
174 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
175 return false;
176 }
177 std::vector<std::string> diff_exec_argv =
178 { "/usr/bin/diff", expected_output, output_dex };
179 if (!::art::Exec(diff_exec_argv, error_msg)) {
180 return false;
181 }
182
183 std::vector<std::string> rm_exec_argv =
184 { "/bin/rm", dex_file, profile_file, expected_output, output_dex };
185 if (!::art::Exec(rm_exec_argv, error_msg)) {
186 return false;
187 }
Jeff Haoa8621002016-10-04 18:13:44 +0000188 return true;
189 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700190};
191
192
193TEST_F(DexLayoutTest, FullPlainOutput) {
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700194 // Disable test on target.
195 TEST_DISABLED_FOR_TARGET();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700196 std::string error_msg;
Jeff Haoa8621002016-10-04 18:13:44 +0000197 ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
198}
199
200TEST_F(DexLayoutTest, DexFileOutput) {
201 // Disable test on target.
202 TEST_DISABLED_FOR_TARGET();
203 std::string error_msg;
204 ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
Jeff Haoc3acfc52016-08-29 14:18:26 -0700205}
206
Jeff Hao042e8982016-10-19 11:17:11 -0700207TEST_F(DexLayoutTest, DexFileLayout) {
208 // Disable test on target.
209 TEST_DISABLED_FOR_TARGET();
210 std::string error_msg;
211 ASSERT_TRUE(DexFileLayoutExec(&error_msg)) << error_msg;
212}
213
Jeff Haoc3acfc52016-08-29 14:18:26 -0700214} // namespace art