Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef IDMAP2_TESTS_TESTHELPERS_H_ |
| 18 | #define IDMAP2_TESTS_TESTHELPERS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 22 | #include "gmock/gmock.h" |
| 23 | #include "gtest/gtest.h" |
| 24 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 25 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 26 | |
| 27 | const unsigned char idmap_raw_data[] = { |
| 28 | // IDMAP HEADER |
| 29 | // 0x0: magic |
| 30 | 0x49, 0x44, 0x4d, 0x50, |
| 31 | |
| 32 | // 0x4: version |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 33 | 0x07, 0x00, 0x00, 0x00, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 34 | |
| 35 | // 0x8: target crc |
| 36 | 0x34, 0x12, 0x00, 0x00, |
| 37 | |
| 38 | // 0xc: overlay crc |
| 39 | 0x78, 0x56, 0x00, 0x00, |
| 40 | |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 41 | // 0x10: fulfilled policies |
| 42 | 0x11, 0x00, 0x00, 0x00, |
| 43 | |
| 44 | // 0x14: enforce overlayable |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 45 | 0x01, 0x00, 0x00, 0x00, |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 46 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 47 | // 0x18: target path length |
| 48 | 0x0b, 0x00, 0x00, 0x00, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 49 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 50 | // 0x1c: target path "targetX.apk" |
| 51 | 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 53 | // 0x28: overlay path length |
| 54 | 0x0c, 0x00, 0x00, 0x00, |
| 55 | |
| 56 | // 0x2c: overlay path "overlayX.apk" |
| 57 | 0x6f, 0x76, 0x65, 0x72, 0x6c, 0x61, 0x79, 0x58, 0x2e, 0x61, 0x70, 0x6b, |
| 58 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 59 | // 0x38: overlay name length |
| 60 | 0x0b, 0x00, 0x00, 0x00, |
| 61 | |
| 62 | // 0x3c: overlay name "OverlayName" |
| 63 | 0x4f, 0x76, 0x65, 0x72, 0x6c, 0x61, 0x79, 0x4e, 0x61, 0x6D, 0x65, 0x00, |
| 64 | |
| 65 | // 0x48 -> 4c: debug string |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 66 | // string length, |
| 67 | 0x05, 0x00, 0x00, 0x00, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 68 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 69 | // 0x4c string contents "debug\0\0\0" (padded to word alignment) |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 70 | 0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00, |
| 71 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 72 | // DATA HEADER |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 73 | // 0x54: target_package_id |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 74 | 0x7f, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 75 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 76 | // 0x55: overlay_package_id |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 77 | 0x7f, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 78 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 79 | // 0x56: padding |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 80 | 0x00, 0x00, |
| 81 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 82 | // 0x58: target_entry_count |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 83 | 0x03, 0x00, 0x00, 0x00, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 84 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 85 | // 0x5c: target_inline_entry_count |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 86 | 0x01, 0x00, 0x00, 0x00, |
| 87 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 88 | // 0x60: overlay_entry_count |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 89 | 0x03, 0x00, 0x00, 0x00, |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 90 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 91 | // 0x64: string_pool_offset |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 92 | 0x00, 0x00, 0x00, 0x00, |
| 93 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 94 | // TARGET ENTRIES |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 95 | // 0x68: target id (0x7f020000) |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 96 | 0x00, 0x00, 0x02, 0x7f, |
| 97 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 98 | // 0x6c: overlay_id (0x7f020000) |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 99 | 0x00, 0x00, 0x02, 0x7f, |
| 100 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 101 | // 0x70: target id (0x7f030000) |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 102 | 0x00, 0x00, 0x03, 0x7f, |
| 103 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 104 | // 0x74: overlay_id (0x7f030000) |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 105 | 0x00, 0x00, 0x03, 0x7f, |
| 106 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 107 | // 0x78: target id (0x7f030002) |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 108 | 0x02, 0x00, 0x03, 0x7f, |
| 109 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 110 | // 0x7c: overlay_id (0x7f030001) |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 111 | 0x01, 0x00, 0x03, 0x7f, |
| 112 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 113 | // INLINE TARGET ENTRIES |
| 114 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 115 | // 0x80: target_id |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 116 | 0x00, 0x00, 0x04, 0x7f, |
| 117 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 118 | // 0x84: Res_value::size (value ignored by idmap) |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 119 | 0x08, 0x00, |
| 120 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 121 | // 0x87: Res_value::res0 (value ignored by idmap) |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 122 | 0x00, |
| 123 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 124 | // 0x88: Res_value::dataType (TYPE_INT_HEX) |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 125 | 0x11, |
| 126 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 127 | // 0x8c: Res_value::data |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 128 | 0x78, 0x56, 0x34, 0x12, |
| 129 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 130 | // OVERLAY ENTRIES |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 131 | // 0x90: 0x7f020000 -> 0x7f020000 |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 132 | 0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f, |
| 133 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 134 | // 0x98: 0x7f030000 -> 0x7f030000 |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 135 | 0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f, |
| 136 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 137 | // 0xa0: 0x7f030001 -> 0x7f030002 |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 138 | 0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f, |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 139 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 140 | // 0xa4: string pool |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 141 | // string length, |
| 142 | 0x04, 0x00, 0x00, 0x00, |
| 143 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 144 | // 0xa8 string contents "test" |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 145 | 0x74, 0x65, 0x73, 0x74}; |
| 146 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 147 | const unsigned int kIdmapRawDataLen = 0xac; |
| 148 | const unsigned int kIdmapRawDataOffset = 0x54; |
| 149 | const unsigned int kIdmapRawDataTargetCrc = 0x1234; |
| 150 | const unsigned int kIdmapRawOverlayCrc = 0x5678; |
| 151 | const unsigned int kIdmapRawDataPolicies = 0x11; |
| 152 | inline const std::string kIdmapRawTargetPath = "targetX.apk"; |
| 153 | inline const std::string kIdmapRawOverlayPath = "overlayX.apk"; |
| 154 | inline const std::string kIdmapRawOverlayName = "OverlayName"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 155 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 156 | std::string GetTestDataPath(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 157 | |
| 158 | class Idmap2Tests : public testing::Test { |
| 159 | protected: |
Mårten Kongstad | cb85d9c | 2018-12-21 08:28:40 +0100 | [diff] [blame] | 160 | void SetUp() override { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 161 | #ifdef __ANDROID__ |
| 162 | tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX"; |
| 163 | #else |
| 164 | tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX"; |
| 165 | #endif |
| 166 | EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr) |
| 167 | << "Failed to create temporary directory: " << strerror(errno); |
| 168 | target_apk_path_ = GetTestDataPath() + "/target/target.apk"; |
| 169 | overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk"; |
| 170 | idmap_path_ = tmp_dir_path_ + "/a.idmap"; |
| 171 | } |
| 172 | |
Mårten Kongstad | cb85d9c | 2018-12-21 08:28:40 +0100 | [diff] [blame] | 173 | void TearDown() override { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 174 | EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0) |
| 175 | << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno); |
| 176 | } |
| 177 | |
| 178 | const std::string& GetTempDirPath() { |
| 179 | return tmp_dir_path_; |
| 180 | } |
| 181 | |
| 182 | const std::string& GetTargetApkPath() { |
| 183 | return target_apk_path_; |
| 184 | } |
| 185 | |
| 186 | const std::string& GetOverlayApkPath() { |
| 187 | return overlay_apk_path_; |
| 188 | } |
| 189 | |
| 190 | const std::string& GetIdmapPath() { |
| 191 | return idmap_path_; |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | std::string tmp_dir_path_; |
| 196 | std::string target_apk_path_; |
| 197 | std::string overlay_apk_path_; |
| 198 | std::string idmap_path_; |
| 199 | }; |
| 200 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 201 | } // namespace android::idmap2 |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 202 | |
| 203 | #endif // IDMAP2_TESTS_TESTHELPERS_H_ |