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