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" |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 25 | #include "base/unix_file/fd_file.h" |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 26 | #include "common_runtime_test.h" |
| 27 | #include "utils.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 31 | static 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 | |
| 43 | static const char kDexFileLayoutInputProfile[] = |
| 44 | "cHJvADAwMgABAAsAAAABAPUpbf5jbGFzc2VzLmRleAEA"; |
| 45 | |
| 46 | static 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 | |
| 58 | static 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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 76 | class DexLayoutTest : public CommonRuntimeTest { |
| 77 | protected: |
| 78 | virtual void SetUp() { |
| 79 | CommonRuntimeTest::SetUp(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 82 | // Runs FullPlainOutput test. |
| 83 | bool FullPlainOutputExec(std::string* error_msg) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 84 | // TODO: dexdump2 -> dexdump ? |
| 85 | ScratchFile dexdump_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 86 | const std::string& dexdump_filename = dexdump_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 87 | std::string dexdump = GetTestAndroidRoot() + "/bin/dexdump2"; |
| 88 | 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] | 89 | |
| 90 | ScratchFile dexlayout_output; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 91 | const std::string& dexlayout_filename = dexlayout_output.GetFilename(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 92 | std::string dexlayout = GetTestAndroidRoot() + "/bin/dexlayout"; |
| 93 | 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] | 94 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 95 | 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 Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 100 | 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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 115 | // Runs DexFileOutput test. |
| 116 | bool DexFileOutputExec(std::string* error_msg) { |
| 117 | ScratchFile tmp_file; |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 118 | const std::string& tmp_name = tmp_file.GetFilename(); |
| 119 | size_t tmp_last_slash = tmp_name.rfind('/'); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 120 | 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 Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 126 | { dexlayout, "-w", tmp_dir, "-o", tmp_name, dex_file }; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 127 | if (!::art::Exec(dexlayout_exec_argv, error_msg)) { |
| 128 | return false; |
| 129 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 130 | size_t dex_file_last_slash = dex_file.rfind("/"); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 131 | 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 Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 151 | return true; |
| 152 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 153 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 154 | // 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 Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | |
| 194 | TEST_F(DexLayoutTest, FullPlainOutput) { |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 195 | // Disable test on target. |
| 196 | TEST_DISABLED_FOR_TARGET(); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 197 | std::string error_msg; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 198 | ASSERT_TRUE(FullPlainOutputExec(&error_msg)) << error_msg; |
| 199 | } |
| 200 | |
| 201 | TEST_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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 208 | TEST_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 Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 215 | } // namespace art |