Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_file.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "base/stl_util.h" |
| 24 | #include "base/unix_file/fd_file.h" |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 25 | #include "code_item_accessors-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 26 | #include "common_runtime_test.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 28 | #include "dex_file_loader.h" |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 29 | #include "mem_map.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 | #include "os.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 32 | #include "thread-current-inl.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 33 | #include "utils.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 34 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 37 | class DexFileTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 38 | |
| 39 | TEST_F(DexFileTest, Open) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 40 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 41 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested")); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 42 | ASSERT_TRUE(dex.get() != nullptr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 43 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 44 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 45 | static inline std::vector<uint8_t> DecodeBase64Vec(const char* src) { |
| 46 | std::vector<uint8_t> res; |
| 47 | size_t size; |
| 48 | std::unique_ptr<uint8_t[]> data(DecodeBase64(src, &size)); |
| 49 | res.resize(size); |
| 50 | memcpy(res.data(), data.get(), size); |
| 51 | return res; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 54 | // Although this is the same content logically as the Nested test dex, |
| 55 | // the DexFileHeader test is sensitive to subtle changes in the |
| 56 | // contents due to the checksum etc, so we embed the exact input here. |
| 57 | // |
| 58 | // class Nested { |
| 59 | // class Inner { |
| 60 | // } |
| 61 | // } |
| 62 | static const char kRawDex[] = |
| 63 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 64 | "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" |
| 65 | "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" |
| 66 | "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" |
| 67 | "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" |
| 68 | "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" |
| 69 | "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" |
| 70 | "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" |
| 71 | "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" |
| 72 | "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" |
| 73 | "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" |
| 74 | "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" |
| 75 | "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" |
| 76 | "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" |
| 77 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 78 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 79 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 80 | // kRawDex{38,39,40,41} are dex'ed versions of the following Java source : |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 81 | // |
| 82 | // public class Main { |
| 83 | // public static void main(String[] foo) { |
| 84 | // } |
| 85 | // } |
| 86 | // |
| 87 | // The dex file was manually edited to change its dex version code to 38 |
| 88 | // or 39, respectively. |
| 89 | static const char kRawDex38[] = |
| 90 | "ZGV4CjAzOAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 91 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 92 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 93 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 94 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 95 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 96 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 97 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 98 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 99 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 100 | |
| 101 | static const char kRawDex39[] = |
| 102 | "ZGV4CjAzOQC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 103 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 104 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 105 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 106 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 107 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 108 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 109 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 110 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 111 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 112 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 113 | static const char kRawDex40[] = |
| 114 | "ZGV4CjA0MAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 115 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 116 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 117 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 118 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 119 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 120 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 121 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 122 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 123 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 124 | |
| 125 | static const char kRawDex41[] = |
| 126 | "ZGV4CjA0MQC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI" |
| 127 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB" |
| 128 | "AAAqAQAAMgEAAEYBAABRAQAAVAEAAFgBAABtAQAAAQAAAAIAAAAEAAAABgAAAAQAAAACAAAAAAAA" |
| 129 | "AAUAAAACAAAAHAEAAAAAAAAAAAAAAAABAAcAAAABAAAAAAAAAAAAAAABAAAAAQAAAAAAAAADAAAA" |
| 130 | "AAAAAH4BAAAAAAAAAQABAAEAAABzAQAABAAAAHAQAgAAAA4AAQABAAAAAAB4AQAAAQAAAA4AAAAB" |
| 131 | "AAAAAwAGPGluaXQ+AAZMTWFpbjsAEkxqYXZhL2xhbmcvT2JqZWN0OwAJTWFpbi5qYXZhAAFWAAJW" |
| 132 | "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA" |
| 133 | "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA" |
| 134 | "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC" |
| 135 | "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA=="; |
| 136 | |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 137 | static const char kRawDexZeroLength[] = |
| 138 | "UEsDBAoAAAAAAOhxAkkAAAAAAAAAAAAAAAALABwAY2xhc3Nlcy5kZXhVVAkAA2QNoVdnDaFXdXgL" |
| 139 | "AAEE5AMBAASIEwAAUEsBAh4DCgAAAAAA6HECSQAAAAAAAAAAAAAAAAsAGAAAAAAAAAAAAKCBAAAA" |
| 140 | "AGNsYXNzZXMuZGV4VVQFAANkDaFXdXgLAAEE5AMBAASIEwAAUEsFBgAAAAABAAEAUQAAAEUAAAAA" |
| 141 | "AA=="; |
| 142 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 143 | static const char kRawZipClassesDexPresent[] = |
| 144 | "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VXdXgL" |
| 145 | "AAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMDQ0WY" |
| 146 | "iRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEIEEcA" |
| 147 | "cS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu4IOa" |
| 148 | "wczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhHIykL" |
| 149 | "LinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkVIGpA" |
| 150 | "Yc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEBAAAg" |
| 151 | "AgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEABIgTAABQ" |
| 152 | "SwUGAAAAAAEAAQBRAAAAdgEAAAAA"; |
| 153 | |
| 154 | static const char kRawZipClassesDexAbsent[] = |
| 155 | "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAAOABwAbm90Y2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VX" |
| 156 | "dXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMD" |
| 157 | "Q0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEI" |
| 158 | "EEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu" |
| 159 | "4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhH" |
| 160 | "IykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkV" |
| 161 | "IGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEB" |
| 162 | "AAAgAgAADgAYAAAAAAAAAAAAoIEAAAAAbm90Y2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEA" |
| 163 | "BIgTAABQSwUGAAAAAAEAAQBUAAAAeQEAAAAA"; |
| 164 | |
| 165 | static const char kRawZipThreeDexFiles[] = |
| 166 | "UEsDBBQAAAAIAP1WN0ms99lIMQEAACACAAAMABwAY2xhc3NlczIuZGV4VVQJAAOtbOVXrWzlV3V4" |
| 167 | "CwABBOQDAQAEiBMAAEtJreAyMLZg2GHVfXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NF" |
| 168 | "mIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBAAGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBH" |
| 169 | "AHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiDHWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCD" |
| 170 | "msHMwGaTmZdZYsfA5uObmJlnzSDkk5VYlqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMp" |
| 171 | "Cy4pysxLt2ZgyQUqAzmYj4EZTIL909jA8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBq" |
| 172 | "QGHOKAARB4UbkwLETFA8MEPVgMKCQQGiBhxOUPWgeAYAUEsDBBQAAAAIAABXN0ms99lIMQEAACAC" |
| 173 | "AAAMABwAY2xhc3NlczMuZGV4VVQJAAOvbOVXr2zlV3V4CwABBOQDAQAEiBMAAEtJreAyMLZg2GHV" |
| 174 | "fXLK3JLPN5acWTdrw/H/9adW3ll972m0AhMDQwEDA0NFmIkQAxT0MDIwcDBAxFmAeAIQA5UxLGBA" |
| 175 | "AGYg3gHEQKUMF4DYAMj4AKSVgLQWEBsBsRsQBwJxCBBHAHEuI0Q9E9RcNijNBDWTFcqWYUSyCKiD" |
| 176 | "HWoPQgTBZ4bSdYwwOUawXDEjxOwCAZCJfFBRoD+hbuCDmsHMwGaTmZdZYsfA5uObmJlnzSDkk5VY" |
| 177 | "lqifk5iXru+flJWaXGLNwAmS0gOJMzCGMTCF+TAIRyMpCy4pysxLt2ZgyQUqAzmYj4EZTIL909jA" |
| 178 | "8oGRkbODiQfJ/TAaFs6wMJkA9RMsvFmRwpoNKbwZFSBqQGHOKAARB4UbkwLETFA8MEPVgMKCQQGi" |
| 179 | "BhxOUPWgeAYAUEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj" |
| 180 | "5VetbOVXdXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQC" |
| 181 | "EwNDAQMDQ0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGx" |
| 182 | "GxAHAnEIEEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8" |
| 183 | "UFGgP6Fu4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYx" |
| 184 | "MIX5MAhHIykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHC" |
| 185 | "mg0pvBkVIGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACAD9VjdJ" |
| 186 | "rPfZSDEBAAAgAgAADAAYAAAAAAAAAAAAoIEAAAAAY2xhc3NlczIuZGV4VVQFAAOtbOVXdXgLAAEE" |
| 187 | "5AMBAASIEwAAUEsBAh4DFAAAAAgAAFc3Saz32UgxAQAAIAIAAAwAGAAAAAAAAAAAAKCBdwEAAGNs" |
| 188 | "YXNzZXMzLmRleFVUBQADr2zlV3V4CwABBOQDAQAEiBMAAFBLAQIeAxQAAAAIANVRN0ms99lIMQEA" |
| 189 | "ACACAAALABgAAAAAAAAAAACgge4CAABjbGFzc2VzLmRleFVUBQADAWPlV3V4CwABBOQDAQAEiBMA" |
| 190 | "AFBLBQYAAAAAAwADAPUAAABkBAAAAAA="; |
| 191 | |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 192 | static const char kRawDexBadMapOffset[] = |
| 193 | "ZGV4CjAzNQAZKGSz85r+tXJ1I24FYi+FpQtWbXtelAmoAQAAcAAAAHhWNBIAAAAAAAAAAEAwIBAF" |
| 194 | "AAAAcAAAAAMAAACEAAAAAQAAAJAAAAAAAAAAAAAAAAIAAACcAAAAAQAAAKwAAADcAAAAzAAAAOQA" |
| 195 | "AADsAAAA9AAAAPkAAAANAQAAAgAAAAMAAAAEAAAABAAAAAIAAAAAAAAAAAAAAAAAAAABAAAAAAAA" |
| 196 | "AAAAAAABAAAAAQAAAAAAAAABAAAAAAAAABUBAAAAAAAAAQABAAEAAAAQAQAABAAAAHAQAQAAAA4A" |
| 197 | "Bjxpbml0PgAGQS5qYXZhAANMQTsAEkxqYXZhL2xhbmcvT2JqZWN0OwABVgABAAcOAAAAAQAAgYAE" |
| 198 | "zAEACwAAAAAAAAABAAAAAAAAAAEAAAAFAAAAcAAAAAIAAAADAAAAhAAAAAMAAAABAAAAkAAAAAUA" |
| 199 | "AAACAAAAnAAAAAYAAAABAAAArAAAAAEgAAABAAAAzAAAAAIgAAAFAAAA5AAAAAMgAAABAAAAEAEA" |
| 200 | "AAAgAAABAAAAFQEAAAAQAAABAAAAIAEAAA=="; |
| 201 | |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 202 | static const char kRawDexDebugInfoLocalNullType[] = |
| 203 | "ZGV4CjAzNQA+Kwj2g6OZMH88OvK9Ey6ycdIsFCt18ED8AQAAcAAAAHhWNBIAAAAAAAAAAHQBAAAI" |
| 204 | "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAMAQAA8AAAABwB" |
| 205 | "AAAkAQAALAEAAC8BAAA0AQAASAEAAEsBAABOAQAAAgAAAAMAAAAEAAAABQAAAAIAAAAAAAAAAAAA" |
| 206 | "AAUAAAADAAAAAAAAAAEAAQAAAAAAAQAAAAYAAAACAAEAAAAAAAEAAAABAAAAAgAAAAAAAAABAAAA" |
| 207 | "AAAAAGMBAAAAAAAAAQABAAEAAABUAQAABAAAAHAQAgAAAA4AAgABAAAAAABZAQAAAgAAABIQDwAG" |
| 208 | "PGluaXQ+AAZBLmphdmEAAUkAA0xBOwASTGphdmEvbGFuZy9PYmplY3Q7AAFWAAFhAAR0aGlzAAEA" |
| 209 | "Bw4AAwAHDh4DAAcAAAAAAQEAgYAE8AEBAIgCAAAACwAAAAAAAAABAAAAAAAAAAEAAAAIAAAAcAAA" |
| 210 | "AAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAAuAAAAAYAAAABAAAA0AAAAAEgAAACAAAA" |
| 211 | "8AAAAAIgAAAIAAAAHAEAAAMgAAACAAAAVAEAAAAgAAABAAAAYwEAAAAQAAABAAAAdAEAAA=="; |
| 212 | |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 213 | static void DecodeAndWriteDexFile(const char* base64, const char* location) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 214 | // decode base64 |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 215 | CHECK(base64 != nullptr); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 216 | std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 217 | CHECK_NE(dex_bytes.size(), 0u); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 218 | |
| 219 | // write to provided file |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 220 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 221 | CHECK(file.get() != nullptr); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 222 | if (!file->WriteFully(dex_bytes.data(), dex_bytes.size())) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 223 | PLOG(FATAL) << "Failed to write base64 as dex file"; |
| 224 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 225 | if (file->FlushCloseOrErase() != 0) { |
| 226 | PLOG(FATAL) << "Could not flush and close test file."; |
| 227 | } |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 228 | } |
| 229 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 230 | static bool OpenDexFilesBase64(const char* base64, |
| 231 | const char* location, |
| 232 | std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 233 | std::string* error_msg) { |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 234 | DecodeAndWriteDexFile(base64, location); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 235 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 236 | // read dex file(s) |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 237 | ScopedObjectAccess soa(Thread::Current()); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 238 | static constexpr bool kVerifyChecksum = true; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 239 | std::vector<std::unique_ptr<const DexFile>> tmp; |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 240 | bool success = DexFileLoader::Open( |
| 241 | location, location, /* verify */ true, kVerifyChecksum, error_msg, &tmp); |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 242 | if (success) { |
| 243 | for (std::unique_ptr<const DexFile>& dex_file : tmp) { |
| 244 | EXPECT_EQ(PROT_READ, dex_file->GetPermissions()); |
| 245 | EXPECT_TRUE(dex_file->IsReadOnly()); |
| 246 | } |
| 247 | *dex_files = std::move(tmp); |
| 248 | } |
| 249 | return success; |
| 250 | } |
| 251 | |
| 252 | static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64, |
| 253 | const char* location) { |
| 254 | // read dex files. |
| 255 | std::string error_msg; |
| 256 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 257 | bool success = OpenDexFilesBase64(base64, location, &dex_files, &error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 258 | CHECK(success) << error_msg; |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 259 | EXPECT_EQ(1U, dex_files.size()); |
| 260 | return std::move(dex_files[0]); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 263 | static std::unique_ptr<const DexFile> OpenDexFileInMemoryBase64(const char* base64, |
| 264 | const char* location, |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 265 | uint32_t location_checksum, |
| 266 | bool expect_success) { |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 267 | CHECK(base64 != nullptr); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 268 | std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 269 | CHECK_NE(dex_bytes.size(), 0u); |
| 270 | |
| 271 | std::string error_message; |
| 272 | std::unique_ptr<MemMap> region(MemMap::MapAnonymous("test-region", |
| 273 | nullptr, |
| 274 | dex_bytes.size(), |
| 275 | PROT_READ | PROT_WRITE, |
| 276 | /* low_4gb */ false, |
| 277 | /* reuse */ false, |
| 278 | &error_message)); |
| 279 | memcpy(region->Begin(), dex_bytes.data(), dex_bytes.size()); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 280 | std::unique_ptr<const DexFile> dex_file(DexFileLoader::Open(location, |
| 281 | location_checksum, |
| 282 | std::move(region), |
| 283 | /* verify */ true, |
| 284 | /* verify_checksum */ true, |
| 285 | &error_message)); |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 286 | if (expect_success) { |
| 287 | CHECK(dex_file != nullptr) << error_message; |
| 288 | } else { |
| 289 | CHECK(dex_file == nullptr) << "Expected dex file open to fail."; |
| 290 | } |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 291 | return dex_file; |
| 292 | } |
| 293 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 294 | static void ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file) { |
| 295 | static const uint8_t kExpectedDexFileMagic[8] = { |
| 296 | /* d */ 0x64, /* e */ 0x64, /* x */ 0x78, /* \n */ 0x0d, |
| 297 | /* 0 */ 0x30, /* 3 */ 0x33, /* 5 */ 0x35, /* \0 */ 0x00 |
| 298 | }; |
| 299 | static const uint8_t kExpectedSha1[DexFile::kSha1DigestSize] = { |
| 300 | 0x7b, 0xb8, 0x0c, 0xd4, 0x1f, 0xd6, 0x1e, 0xc5, |
| 301 | 0x89, 0xe8, 0xbe, 0xe5, 0x18, 0x02, 0x12, 0x18, |
| 302 | 0x2e, 0xf2, 0x8c, 0x3d, |
| 303 | }; |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 304 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 305 | const DexFile::Header& header = dex_file->GetHeader(); |
| 306 | EXPECT_EQ(*kExpectedDexFileMagic, *header.magic_); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 307 | EXPECT_EQ(0x00d87910U, header.checksum_); |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 308 | EXPECT_EQ(*kExpectedSha1, *header.signature_); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 309 | EXPECT_EQ(904U, header.file_size_); |
| 310 | EXPECT_EQ(112U, header.header_size_); |
| 311 | EXPECT_EQ(0U, header.link_size_); |
| 312 | EXPECT_EQ(0U, header.link_off_); |
| 313 | EXPECT_EQ(15U, header.string_ids_size_); |
| 314 | EXPECT_EQ(112U, header.string_ids_off_); |
| 315 | EXPECT_EQ(7U, header.type_ids_size_); |
| 316 | EXPECT_EQ(172U, header.type_ids_off_); |
| 317 | EXPECT_EQ(2U, header.proto_ids_size_); |
| 318 | EXPECT_EQ(200U, header.proto_ids_off_); |
| 319 | EXPECT_EQ(1U, header.field_ids_size_); |
| 320 | EXPECT_EQ(224U, header.field_ids_off_); |
| 321 | EXPECT_EQ(3U, header.method_ids_size_); |
| 322 | EXPECT_EQ(232U, header.method_ids_off_); |
| 323 | EXPECT_EQ(2U, header.class_defs_size_); |
| 324 | EXPECT_EQ(256U, header.class_defs_off_); |
| 325 | EXPECT_EQ(584U, header.data_size_); |
| 326 | EXPECT_EQ(320U, header.data_off_); |
| 327 | |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 328 | EXPECT_EQ(header.checksum_, dex_file->GetLocationChecksum()); |
| 329 | } |
| 330 | |
| 331 | TEST_F(DexFileTest, Header) { |
| 332 | ScratchFile tmp; |
| 333 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); |
| 334 | ValidateDexFileHeader(std::move(raw)); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | TEST_F(DexFileTest, HeaderInMemory) { |
| 338 | ScratchFile tmp; |
| 339 | std::unique_ptr<const DexFile> raw = |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 340 | OpenDexFileInMemoryBase64(kRawDex, tmp.GetFilename().c_str(), 0x00d87910U, true); |
Orion Hodson | cffc529 | 2016-09-05 13:43:11 +0100 | [diff] [blame] | 341 | ValidateDexFileHeader(std::move(raw)); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 344 | TEST_F(DexFileTest, Version38Accepted) { |
| 345 | ScratchFile tmp; |
| 346 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, tmp.GetFilename().c_str())); |
| 347 | ASSERT_TRUE(raw.get() != nullptr); |
| 348 | |
| 349 | const DexFile::Header& header = raw->GetHeader(); |
| 350 | EXPECT_EQ(38u, header.GetVersion()); |
| 351 | } |
| 352 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 353 | TEST_F(DexFileTest, Version39Accepted) { |
| 354 | ScratchFile tmp; |
| 355 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex39, tmp.GetFilename().c_str())); |
| 356 | ASSERT_TRUE(raw.get() != nullptr); |
| 357 | |
| 358 | const DexFile::Header& header = raw->GetHeader(); |
| 359 | EXPECT_EQ(39u, header.GetVersion()); |
| 360 | } |
| 361 | |
| 362 | TEST_F(DexFileTest, Version40Rejected) { |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 363 | ScratchFile tmp; |
| 364 | const char* location = tmp.GetFilename().c_str(); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 365 | DecodeAndWriteDexFile(kRawDex40, location); |
| 366 | |
| 367 | ScopedObjectAccess soa(Thread::Current()); |
| 368 | static constexpr bool kVerifyChecksum = true; |
| 369 | std::string error_msg; |
| 370 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 371 | ASSERT_FALSE(DexFileLoader::Open( |
| 372 | location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | TEST_F(DexFileTest, Version41Rejected) { |
| 376 | ScratchFile tmp; |
| 377 | const char* location = tmp.GetFilename().c_str(); |
| 378 | DecodeAndWriteDexFile(kRawDex41, location); |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 379 | |
| 380 | ScopedObjectAccess soa(Thread::Current()); |
| 381 | static constexpr bool kVerifyChecksum = true; |
| 382 | std::string error_msg; |
| 383 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 384 | ASSERT_FALSE(DexFileLoader::Open( |
| 385 | location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 386 | } |
| 387 | |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 388 | TEST_F(DexFileTest, ZeroLengthDexRejected) { |
| 389 | ScratchFile tmp; |
| 390 | const char* location = tmp.GetFilename().c_str(); |
| 391 | DecodeAndWriteDexFile(kRawDexZeroLength, location); |
| 392 | |
| 393 | ScopedObjectAccess soa(Thread::Current()); |
| 394 | static constexpr bool kVerifyChecksum = true; |
| 395 | std::string error_msg; |
| 396 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 397 | ASSERT_FALSE(DexFileLoader::Open( |
| 398 | location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)); |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 401 | TEST_F(DexFileTest, GetLocationChecksum) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 402 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 403 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main")); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 404 | EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); |
| 405 | } |
| 406 | |
| 407 | TEST_F(DexFileTest, GetChecksum) { |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 408 | std::vector<uint32_t> checksums; |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 409 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 410 | std::string error_msg; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 411 | EXPECT_TRUE(DexFileLoader::GetMultiDexChecksums(GetLibCoreDexFileNames()[0].c_str(), |
| 412 | &checksums, |
| 413 | &error_msg)) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 414 | << error_msg; |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 415 | ASSERT_EQ(1U, checksums.size()); |
| 416 | EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksums[0]); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 419 | TEST_F(DexFileTest, GetMultiDexChecksums) { |
| 420 | std::string error_msg; |
| 421 | std::vector<uint32_t> checksums; |
| 422 | std::string multidex_file = GetTestDexFileName("MultiDex"); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 423 | EXPECT_TRUE(DexFileLoader::GetMultiDexChecksums(multidex_file.c_str(), |
| 424 | &checksums, |
| 425 | &error_msg)) << error_msg; |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 426 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 427 | std::vector<std::unique_ptr<const DexFile>> dexes = OpenTestDexFiles("MultiDex"); |
| 428 | ASSERT_EQ(2U, dexes.size()); |
| 429 | ASSERT_EQ(2U, checksums.size()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 430 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 431 | EXPECT_EQ(dexes[0]->GetLocation(), DexFileLoader::GetMultiDexLocation(0, multidex_file.c_str())); |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 432 | EXPECT_EQ(dexes[0]->GetLocationChecksum(), checksums[0]); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 433 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 434 | EXPECT_EQ(dexes[1]->GetLocation(), DexFileLoader::GetMultiDexLocation(1, multidex_file.c_str())); |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 435 | EXPECT_EQ(dexes[1]->GetLocationChecksum(), checksums[1]); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 438 | TEST_F(DexFileTest, ClassDefs) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 439 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 440 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested")); |
| 441 | ASSERT_TRUE(raw.get() != nullptr); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 442 | EXPECT_EQ(3U, raw->NumClassDefs()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 443 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 444 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 445 | EXPECT_STREQ("LNested$1;", raw->GetClassDescriptor(c0)); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 446 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 447 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Andreas Gampe | ae78c26 | 2017-02-01 20:40:44 -0800 | [diff] [blame] | 448 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c1)); |
| 449 | |
| 450 | const DexFile::ClassDef& c2 = raw->GetClassDef(2); |
| 451 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c2)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 454 | TEST_F(DexFileTest, GetMethodSignature) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 455 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 456 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); |
| 457 | ASSERT_TRUE(raw.get() != nullptr); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 458 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 459 | |
| 460 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 461 | ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 462 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 463 | const uint8_t* class_data = raw->GetClassData(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 464 | ASSERT_TRUE(class_data != nullptr); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 465 | ClassDataItemIterator it(*raw, class_data); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 466 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 467 | EXPECT_EQ(1u, it.NumDirectMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 468 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 469 | // Check the signature for the static initializer. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 470 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 471 | ASSERT_EQ(1U, it.NumDirectMethods()); |
| 472 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 473 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 474 | ASSERT_STREQ("<init>", name); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 475 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 476 | ASSERT_EQ("()V", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 479 | // Check all virtual methods. |
| 480 | struct Result { |
| 481 | const char* name; |
| 482 | const char* signature; |
| 483 | const char* pretty_method; |
| 484 | }; |
| 485 | static const Result results[] = { |
| 486 | { |
| 487 | "m1", |
| 488 | "(IDJLjava/lang/Object;)Ljava/lang/Float;", |
| 489 | "java.lang.Float GetMethodSignature.m1(int, double, long, java.lang.Object)" |
| 490 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 491 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 492 | "m2", |
| 493 | "(ZSC)LGetMethodSignature;", |
| 494 | "GetMethodSignature GetMethodSignature.m2(boolean, short, char)" |
| 495 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 496 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 497 | "m3", |
| 498 | "()V", |
| 499 | "void GetMethodSignature.m3()" |
| 500 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 501 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 502 | "m4", |
| 503 | "(I)V", |
| 504 | "void GetMethodSignature.m4(int)" |
| 505 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 506 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 507 | "m5", |
| 508 | "(II)V", |
| 509 | "void GetMethodSignature.m5(int, int)" |
| 510 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 511 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 512 | "m6", |
| 513 | "(II[[I)V", |
| 514 | "void GetMethodSignature.m6(int, int, int[][])" |
| 515 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 516 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 517 | "m7", |
| 518 | "(II[[ILjava/lang/Object;)V", |
| 519 | "void GetMethodSignature.m7(int, int, int[][], java.lang.Object)" |
| 520 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 521 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 522 | "m8", |
| 523 | "(II[[ILjava/lang/Object;[[Ljava/lang/Object;)V", |
| 524 | "void GetMethodSignature.m8(int, int, int[][], java.lang.Object, java.lang.Object[][])" |
| 525 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 526 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 527 | "m9", |
| 528 | "()I", |
| 529 | "int GetMethodSignature.m9()" |
| 530 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 531 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 532 | "mA", |
| 533 | "()[[I", |
| 534 | "int[][] GetMethodSignature.mA()" |
| 535 | }, |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 536 | { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 537 | "mB", |
| 538 | "()[[Ljava/lang/Object;", |
| 539 | "java.lang.Object[][] GetMethodSignature.mB()" |
| 540 | }, |
| 541 | }; |
| 542 | ASSERT_EQ(arraysize(results), it.NumVirtualMethods()); |
| 543 | for (const Result& r : results) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 544 | it.Next(); |
| 545 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 546 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 547 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 548 | ASSERT_STREQ(r.name, name); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 549 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 550 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 551 | ASSERT_EQ(r.signature, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 552 | |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 553 | std::string plain_method = std::string("GetMethodSignature.") + r.name; |
| 554 | ASSERT_EQ(plain_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ false)); |
| 555 | ASSERT_EQ(r.pretty_method, raw->PrettyMethod(it.GetMemberIndex(), /* with_signature */ true)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | TEST_F(DexFileTest, FindStringId) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 560 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 561 | std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature")); |
| 562 | ASSERT_TRUE(raw.get() != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 563 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 564 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 565 | const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 566 | "D", "I", "J", nullptr }; |
| 567 | for (size_t i = 0; strings[i] != nullptr; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 568 | const char* str = strings[i]; |
| 569 | const DexFile::StringId* str_id = raw->FindStringId(str); |
| 570 | const char* dex_str = raw->GetStringData(*str_id); |
| 571 | EXPECT_STREQ(dex_str, str); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | TEST_F(DexFileTest, FindTypeId) { |
| 576 | for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 577 | const char* type_str = java_lang_dex_file_->StringByTypeIdx(dex::TypeIndex(i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 578 | const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 579 | ASSERT_TRUE(type_str_id != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 580 | dex::StringIndex type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 581 | const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 582 | ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 583 | ASSERT_TRUE(type_id != nullptr); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 584 | EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id).index_, i); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | TEST_F(DexFileTest, FindProtoId) { |
| 589 | for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { |
| 590 | const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); |
| 591 | const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 592 | std::vector<dex::TypeIndex> to_find_types; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 593 | if (to_find_tl != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 594 | for (size_t j = 0; j < to_find_tl->Size(); j++) { |
| 595 | to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); |
| 596 | } |
| 597 | } |
| 598 | const DexFile::ProtoId* found = |
| 599 | java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 600 | ASSERT_TRUE(found != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 601 | EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | TEST_F(DexFileTest, FindMethodId) { |
| 606 | for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { |
| 607 | const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); |
| 608 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 609 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 610 | const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); |
| 611 | const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 612 | ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": " |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 613 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 614 | << java_lang_dex_file_->GetStringData(name) |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 615 | << java_lang_dex_file_->GetMethodSignature(to_find); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 616 | EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 617 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 620 | TEST_F(DexFileTest, FindFieldId) { |
| 621 | for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { |
| 622 | const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); |
| 623 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 624 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 625 | const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); |
| 626 | const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 627 | ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": " |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 628 | << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " |
| 629 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 630 | << java_lang_dex_file_->GetStringData(name); |
| 631 | EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); |
| 632 | } |
| 633 | } |
| 634 | |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 635 | TEST_F(DexFileTest, GetMultiDexClassesDexName) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 636 | ASSERT_EQ("classes.dex", DexFileLoader::GetMultiDexClassesDexName(0)); |
| 637 | ASSERT_EQ("classes2.dex", DexFileLoader::GetMultiDexClassesDexName(1)); |
| 638 | ASSERT_EQ("classes3.dex", DexFileLoader::GetMultiDexClassesDexName(2)); |
| 639 | ASSERT_EQ("classes100.dex", DexFileLoader::GetMultiDexClassesDexName(99)); |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | TEST_F(DexFileTest, GetMultiDexLocation) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 643 | std::string dex_location_str = "/system/app/framework.jar"; |
| 644 | const char* dex_location = dex_location_str.c_str(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 645 | ASSERT_EQ("/system/app/framework.jar", DexFileLoader::GetMultiDexLocation(0, dex_location)); |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 646 | ASSERT_EQ("/system/app/framework.jar!classes2.dex", |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 647 | DexFileLoader::GetMultiDexLocation(1, dex_location)); |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 648 | ASSERT_EQ("/system/app/framework.jar!classes101.dex", |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 649 | DexFileLoader::GetMultiDexLocation(100, dex_location)); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | TEST_F(DexFileTest, GetDexCanonicalLocation) { |
| 653 | ScratchFile file; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 654 | UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr)); |
| 655 | std::string dex_location(dex_location_real.get()); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 656 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 657 | ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location.c_str())); |
| 658 | std::string multidex_location = DexFileLoader::GetMultiDexLocation(1, dex_location.c_str()); |
| 659 | ASSERT_EQ(multidex_location, DexFileLoader::GetDexCanonicalLocation(multidex_location.c_str())); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 660 | |
| 661 | std::string dex_location_sym = dex_location + "symlink"; |
| 662 | ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str())); |
| 663 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 664 | ASSERT_EQ(dex_location, DexFileLoader::GetDexCanonicalLocation(dex_location_sym.c_str())); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 665 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 666 | std::string multidex_location_sym = DexFileLoader::GetMultiDexLocation( |
| 667 | 1, dex_location_sym.c_str()); |
| 668 | ASSERT_EQ(multidex_location, |
| 669 | DexFileLoader::GetDexCanonicalLocation(multidex_location_sym.c_str())); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 670 | |
| 671 | ASSERT_EQ(0, unlink(dex_location_sym.c_str())); |
| 672 | } |
| 673 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 674 | TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 675 | EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar")); |
| 676 | EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar!classes2.dex")); |
| 677 | EXPECT_EQ("/foo/bar/baz.jar", DexFileLoader::GetBaseLocation("/foo/bar/baz.jar!classes8.dex")); |
| 678 | EXPECT_EQ("", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar")); |
| 679 | EXPECT_EQ("!classes2.dex", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar!classes2.dex")); |
| 680 | EXPECT_EQ("!classes8.dex", DexFileLoader::GetMultiDexSuffix("/foo/bar/baz.jar!classes8.dex")); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 681 | } |
| 682 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 683 | TEST_F(DexFileTest, ZipOpenClassesPresent) { |
| 684 | ScratchFile tmp; |
| 685 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 686 | std::string error_msg; |
| 687 | ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent, tmp.GetFilename().c_str(), &dex_files, |
| 688 | &error_msg)); |
| 689 | EXPECT_EQ(dex_files.size(), 1u); |
| 690 | } |
| 691 | |
| 692 | TEST_F(DexFileTest, ZipOpenClassesAbsent) { |
| 693 | ScratchFile tmp; |
| 694 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 695 | std::string error_msg; |
| 696 | ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent, tmp.GetFilename().c_str(), &dex_files, |
| 697 | &error_msg)); |
| 698 | EXPECT_EQ(dex_files.size(), 0u); |
| 699 | } |
| 700 | |
| 701 | TEST_F(DexFileTest, ZipOpenThreeDexFiles) { |
| 702 | ScratchFile tmp; |
| 703 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 704 | std::string error_msg; |
| 705 | ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles, tmp.GetFilename().c_str(), &dex_files, |
| 706 | &error_msg)); |
| 707 | EXPECT_EQ(dex_files.size(), 3u); |
| 708 | } |
| 709 | |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 710 | TEST_F(DexFileTest, OpenDexBadMapOffset) { |
| 711 | ScratchFile tmp; |
| 712 | std::unique_ptr<const DexFile> raw = |
| 713 | OpenDexFileInMemoryBase64(kRawDexBadMapOffset, tmp.GetFilename().c_str(), 0xb3642819U, false); |
| 714 | EXPECT_EQ(raw, nullptr); |
| 715 | } |
| 716 | |
Jeff Hao | 120504f | 2017-04-14 14:33:52 -0700 | [diff] [blame] | 717 | TEST_F(DexFileTest, GetStringWithNoIndex) { |
| 718 | ScratchFile tmp; |
| 719 | std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); |
| 720 | dex::TypeIndex idx; |
| 721 | EXPECT_EQ(raw->StringByTypeIdx(idx), nullptr); |
| 722 | } |
| 723 | |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 724 | static void Callback(void* context ATTRIBUTE_UNUSED, |
| 725 | const DexFile::LocalInfo& entry ATTRIBUTE_UNUSED) { |
| 726 | } |
| 727 | |
| 728 | TEST_F(DexFileTest, OpenDexDebugInfoLocalNullType) { |
| 729 | ScratchFile tmp; |
| 730 | std::unique_ptr<const DexFile> raw = OpenDexFileInMemoryBase64( |
| 731 | kRawDexDebugInfoLocalNullType, tmp.GetFilename().c_str(), 0xf25f2b38U, true); |
| 732 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
| 733 | const DexFile::CodeItem* code_item = raw->GetCodeItem(raw->FindCodeItemOffset(class_def, 1)); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame^] | 734 | CodeItemDebugInfoAccessor accessor(raw.get(), code_item); |
| 735 | ASSERT_TRUE(accessor.DecodeDebugLocalInfo(true, 1, Callback, nullptr)); |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 738 | } // namespace art |