blob: 89544d7ef4467a7e6d20bccf3668f660da4d4e50 [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"
25#include "common_runtime_test.h"
26#include "utils.h"
27
28namespace art {
29
30class DexLayoutTest : public CommonRuntimeTest {
31 protected:
32 virtual void SetUp() {
33 CommonRuntimeTest::SetUp();
Jeff Haoc3acfc52016-08-29 14:18:26 -070034 }
35
Jeff Haoa8621002016-10-04 18:13:44 +000036 // Runs FullPlainOutput test.
37 bool FullPlainOutputExec(std::string* error_msg) {
Jeff Haoc3acfc52016-08-29 14:18:26 -070038 // TODO: dexdump2 -> dexdump ?
39 ScratchFile dexdump_output;
40 std::string dexdump_filename = dexdump_output.GetFilename();
41 std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
42 EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070043
44 ScratchFile dexlayout_output;
45 std::string dexlayout_filename = dexlayout_output.GetFilename();
46 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
47 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
Jeff Haoc3acfc52016-08-29 14:18:26 -070048
Jeff Haoa8621002016-10-04 18:13:44 +000049 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
50 std::vector<std::string> dexdump_exec_argv =
51 { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file };
52 std::vector<std::string> dexlayout_exec_argv =
53 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file };
54
55 if (!::art::Exec(dexdump_exec_argv, error_msg)) {
56 return false;
57 }
58 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
59 return false;
60 }
61 std::vector<std::string> diff_exec_argv =
62 { "/usr/bin/diff", dexdump_filename, dexlayout_filename };
63 if (!::art::Exec(diff_exec_argv, error_msg)) {
64 return false;
65 }
Jeff Haoc3acfc52016-08-29 14:18:26 -070066 }
67 return true;
68 }
69
Jeff Haoa8621002016-10-04 18:13:44 +000070 // Runs DexFileOutput test.
71 bool DexFileOutputExec(std::string* error_msg) {
72 ScratchFile tmp_file;
73 std::string tmp_name = tmp_file.GetFilename();
74 size_t tmp_last_slash = tmp_name.rfind("/");
75 std::string tmp_dir = tmp_name.substr(0, tmp_last_slash + 1);
76 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
77 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
78
79 for (const std::string &dex_file : GetLibCoreDexFileNames()) {
80 std::vector<std::string> dexlayout_exec_argv =
81 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-w", tmp_dir, "-o", tmp_name, dex_file };
82
83 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
84 return false;
85 }
86
87 size_t dex_file_last_slash = dex_file.rfind("/");
88 std::string dex_file_name = dex_file.substr(dex_file_last_slash + 1);
89 std::vector<std::string> unzip_exec_argv =
90 { "/usr/bin/unzip", dex_file, "classes.dex", "-d", tmp_dir};
91 if (!::art::Exec(unzip_exec_argv, error_msg)) {
92 return false;
93 }
94 std::vector<std::string> diff_exec_argv =
95 { "/usr/bin/diff", tmp_dir + "classes.dex" , tmp_dir + dex_file_name };
96 if (!::art::Exec(diff_exec_argv, error_msg)) {
97 return false;
98 }
99 std::vector<std::string> rm_zip_exec_argv = { "/bin/rm", tmp_dir + "classes.dex" };
100 if (!::art::Exec(rm_zip_exec_argv, error_msg)) {
101 return false;
102 }
103 std::vector<std::string> rm_out_exec_argv = { "/bin/rm", tmp_dir + dex_file_name };
104 if (!::art::Exec(rm_out_exec_argv, error_msg)) {
105 return false;
106 }
107 }
108
109 return true;
110 }
Jeff Haoc3acfc52016-08-29 14:18:26 -0700111};
112
113
114TEST_F(DexLayoutTest, FullPlainOutput) {
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700115 // Disable test on target.
116 TEST_DISABLED_FOR_TARGET();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700117 std::string error_msg;
Jeff Haoa8621002016-10-04 18:13:44 +0000118 ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
119}
120
121TEST_F(DexLayoutTest, DexFileOutput) {
122 // Disable test on target.
123 TEST_DISABLED_FOR_TARGET();
124 std::string error_msg;
125 ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
Jeff Haoc3acfc52016-08-29 14:18:26 -0700126}
127
128} // namespace art