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 22 #include "gmock/gmock.h" 23 #include "gtest/gtest.h" 24 25 namespace android::idmap2 { 26 27 const unsigned char kIdmapRawData[] = { 28 // IDMAP HEADER 29 // 0x0: magic 30 0x49, 0x44, 0x4d, 0x50, 31 32 // 0x4: version 33 0x08, 0x00, 0x00, 0x00, 34 35 // 0x8: target crc 36 0x34, 0x12, 0x00, 0x00, 37 38 // 0xc: overlay crc 39 0x78, 0x56, 0x00, 0x00, 40 41 // 0x10: fulfilled policies 42 0x11, 0x00, 0x00, 0x00, 43 44 // 0x14: enforce overlayable 45 0x01, 0x00, 0x00, 0x00, 46 47 // 0x18: target path length 48 0x0b, 0x00, 0x00, 0x00, 49 50 // 0x1c: target path "targetX.apk" 51 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00, 52 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 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 66 // string length, 67 0x05, 0x00, 0x00, 0x00, 68 69 // 0x4c string contents "debug\0\0\0" (padded to word alignment) 70 0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00, 71 72 // DATA HEADER 73 // 0x54: target_entry_count 74 0x03, 0x00, 0x00, 0x00, 75 76 // 0x58: target_inline_entry_count 77 0x01, 0x00, 0x00, 0x00, 78 79 // 0x5c: overlay_entry_count 80 0x03, 0x00, 0x00, 0x00, 81 82 // 0x60: string_pool_offset 83 0x00, 0x00, 0x00, 0x00, 84 85 // TARGET ENTRIES 86 // 0x64: target id (0x7f020000) 87 0x00, 0x00, 0x02, 0x7f, 88 89 // 0x68: overlay_id (0x7f020000) 90 0x00, 0x00, 0x02, 0x7f, 91 92 // 0x6c: target id (0x7f030000) 93 0x00, 0x00, 0x03, 0x7f, 94 95 // 0x70: overlay_id (0x7f030000) 96 0x00, 0x00, 0x03, 0x7f, 97 98 // 0x74: target id (0x7f030002) 99 0x02, 0x00, 0x03, 0x7f, 100 101 // 0x78: overlay_id (0x7f030001) 102 0x01, 0x00, 0x03, 0x7f, 103 104 // INLINE TARGET ENTRIES 105 106 // 0x7c: target_id 107 0x00, 0x00, 0x04, 0x7f, 108 109 // 0x80: Res_value::size (value ignored by idmap) 110 0x08, 0x00, 111 112 // 0x82: Res_value::res0 (value ignored by idmap) 113 0x00, 114 115 // 0x83: Res_value::dataType (TYPE_INT_HEX) 116 0x11, 117 118 // 0x84: Res_value::data 119 0x78, 0x56, 0x34, 0x12, 120 121 // OVERLAY ENTRIES 122 // 0x88: 0x7f020000 -> 0x7f020000 123 0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f, 124 125 // 0x90: 0x7f030000 -> 0x7f030000 126 0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f, 127 128 // 0x98: 0x7f030001 -> 0x7f030002 129 0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f, 130 131 // 0xa0: string pool 132 // string length, 133 0x04, 0x00, 0x00, 0x00, 134 135 // 0xa4 string contents "test" 136 0x74, 0x65, 0x73, 0x74}; 137 138 const unsigned int kIdmapRawDataLen = 0xa8; 139 const unsigned int kIdmapRawDataOffset = 0x54; 140 const unsigned int kIdmapRawDataTargetCrc = 0x1234; 141 const unsigned int kIdmapRawOverlayCrc = 0x5678; 142 const unsigned int kIdmapRawDataPolicies = 0x11; 143 inline const std::string kIdmapRawTargetPath = "targetX.apk"; 144 inline const std::string kIdmapRawOverlayPath = "overlayX.apk"; 145 inline const std::string kIdmapRawOverlayName = "OverlayName"; 146 147 std::string GetTestDataPath(); 148 149 class Idmap2Tests : public testing::Test { 150 protected: SetUp()151 void SetUp() override { 152 #ifdef __ANDROID__ 153 tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX"; 154 #else 155 tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX"; 156 #endif 157 EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr) 158 << "Failed to create temporary directory: " << strerror(errno); 159 target_apk_path_ = GetTestDataPath() + "/target/target.apk"; 160 overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk"; 161 idmap_path_ = tmp_dir_path_ + "/a.idmap"; 162 } 163 TearDown()164 void TearDown() override { 165 EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0) 166 << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno); 167 } 168 GetTempDirPath()169 const std::string& GetTempDirPath() { 170 return tmp_dir_path_; 171 } 172 GetTargetApkPath()173 const std::string& GetTargetApkPath() { 174 return target_apk_path_; 175 } 176 GetOverlayApkPath()177 const std::string& GetOverlayApkPath() { 178 return overlay_apk_path_; 179 } 180 GetIdmapPath()181 const std::string& GetIdmapPath() { 182 return idmap_path_; 183 } 184 185 private: 186 std::string tmp_dir_path_; 187 std::string target_apk_path_; 188 std::string overlay_apk_path_; 189 std::string idmap_path_; 190 }; 191 192 } // namespace android::idmap2 193 194 #endif // IDMAP2_TESTS_TESTHELPERS_H_ 195