Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace art { |
| 29 | |
| 30 | class DexLayoutTest : public CommonRuntimeTest { |
| 31 | protected: |
| 32 | virtual void SetUp() { |
| 33 | CommonRuntimeTest::SetUp(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 36 | // Runs FullPlainOutput test. |
| 37 | bool FullPlainOutputExec(std::string* error_msg) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 38 | // TODO: dexdump2 -> dexdump ? |
| 39 | ScratchFile dexdump_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 40 | const std::string& dexdump_filename = dexdump_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 41 | std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2"; |
| 42 | EXPECT_TRUE(OS::FileExists(dexdump.c_str())) << dexdump << " should be a valid file path"; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 43 | |
| 44 | ScratchFile dexlayout_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 45 | const std::string& dexlayout_filename = dexlayout_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 46 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 47 | EXPECT_TRUE(OS::FileExists(dexlayout.c_str())) << dexlayout << " should be a valid file path"; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 48 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 49 | 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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 70 | // Runs DexFileOutput test. |
| 71 | bool DexFileOutputExec(std::string* error_msg) { |
| 72 | ScratchFile tmp_file; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 73 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 74 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 87 | size_t dex_file_last_slash = dex_file.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
| 114 | TEST_F(DexLayoutTest, FullPlainOutput) { |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 115 | // Disable test on target. |
| 116 | TEST_DISABLED_FOR_TARGET(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 117 | std::string error_msg; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 118 | ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg; |
| 119 | } |
| 120 | |
| 121 | TEST_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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace art |