blob: 63dc9728f1d96262b3c8005aa802eab64e4afbae [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();
34 // TODO: Test with other dex files for improved coverage.
35 // Dogfood our own lib core dex file.
36 dex_file_ = GetLibCoreDexFileNames()[0];
37 }
38
Jeff Hao69b58cf2016-09-22 18:02:49 -070039 // Runs FullPlainOutput test.
40 bool FullPlainOutputExec(std::string* error_msg) {
Jeff Haoc3acfc52016-08-29 14:18:26 -070041 // TODO: dexdump2 -> dexdump ?
42 ScratchFile dexdump_output;
43 std::string dexdump_filename = dexdump_output.GetFilename();
44 std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2";
45 EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path";
46 std::vector<std::string> dexdump_exec_argv =
47 { dexdump, "-d", "-f", "-h", "-l", "plain", "-o", dexdump_filename, dex_file_ };
48
49 ScratchFile dexlayout_output;
50 std::string dexlayout_filename = dexlayout_output.GetFilename();
51 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
52 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
53 std::vector<std::string> dexlayout_exec_argv =
54 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-o", dexlayout_filename, dex_file_ };
55
56 if (!::art::Exec(dexdump_exec_argv, error_msg)) {
57 return false;
58 }
59 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
60 return false;
61 }
62 std::vector<std::string> diff_exec_argv =
63 { "/usr/bin/diff", dexdump_filename, dexlayout_filename };
64 if (!::art::Exec(diff_exec_argv, error_msg)) {
65 return false;
66 }
67 return true;
68 }
69
Jeff Hao69b58cf2016-09-22 18:02:49 -070070 // Runs FullPlainOutput test.
71 bool DexFileOutputExec(std::string* error_msg) {
72 ScratchFile dexlayout_output;
73 std::string dexlayout_filename = dexlayout_output.GetFilename();
74 std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout";
75 EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path";
76 std::vector<std::string> dexlayout_exec_argv =
77 { dexlayout, "-d", "-f", "-h", "-l", "plain", "-w", "-o", dexlayout_filename, dex_file_ };
78
79 if (!::art::Exec(dexlayout_exec_argv, error_msg)) {
80 return false;
81 }
82
83 size_t last_slash = dexlayout_filename.rfind("/");
84 std::string scratch_directory = dexlayout_filename.substr(0, last_slash + 1);
85 std::vector<std::string> unzip_exec_argv =
86 { "/usr/bin/unzip", dex_file_, "classes.dex", "-d", scratch_directory};
87 if (!::art::Exec(unzip_exec_argv, error_msg)) {
88 return false;
89 }
90
91 std::vector<std::string> diff_exec_argv =
92 { "/usr/bin/diff", scratch_directory + "classes.dex" , dex_file_ + ".out" };
93 if (!::art::Exec(diff_exec_argv, error_msg)) {
94 return false;
95 }
96
97 std::vector<std::string> rm_exec_argv = { "/bin/rm", scratch_directory + "classes.dex" };
98 if (!::art::Exec(rm_exec_argv, error_msg)) {
99 return false;
100 }
101
102 return true;
103 }
104
Jeff Haoc3acfc52016-08-29 14:18:26 -0700105 std::string dex_file_;
106};
107
108
109TEST_F(DexLayoutTest, FullPlainOutput) {
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700110 // Disable test on target.
111 TEST_DISABLED_FOR_TARGET();
Jeff Haoc3acfc52016-08-29 14:18:26 -0700112 std::string error_msg;
Jeff Hao69b58cf2016-09-22 18:02:49 -0700113 ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg;
114}
115
116TEST_F(DexLayoutTest, DexFileOutput) {
117 // Disable test on target.
118 TEST_DISABLED_FOR_TARGET();
119 std::string error_msg;
120 ASSERT_TRUE(DexFileOutputExec(&error_msg)) << error_msg;
Jeff Haoc3acfc52016-08-29 14:18:26 -0700121}
122
123} // namespace art