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 <stdio.h> 21 #include <string> 22 #include <string_view> 23 24 #include "gmock/gmock.h" 25 #include "gtest/gtest.h" 26 27 #include "android-base/file.h" 28 29 namespace android::idmap2 { 30 31 const unsigned char kIdmapRawData[] = { 32 // IDMAP HEADER 33 // 0x0: magic 34 0x49, 0x44, 0x4d, 0x50, 35 36 // 0x4: version 37 0x09, 0x00, 0x00, 0x00, 38 39 // 0x8: target crc 40 0x34, 0x12, 0x00, 0x00, 41 42 // 0xc: overlay crc 43 0x78, 0x56, 0x00, 0x00, 44 45 // 0x10: fulfilled policies 46 0x11, 0x00, 0x00, 0x00, 47 48 // 0x14: enforce overlayable 49 0x01, 0x00, 0x00, 0x00, 50 51 // 0x18: target path length 52 0x0b, 0x00, 0x00, 0x00, 53 54 // 0x1c: target path "targetX.apk" 55 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00, 56 57 // 0x28: overlay path length 58 0x0c, 0x00, 0x00, 0x00, 59 60 // 0x2c: overlay path "overlayX.apk" 61 0x6f, 0x76, 0x65, 0x72, 0x6c, 0x61, 0x79, 0x58, 0x2e, 0x61, 0x70, 0x6b, 62 63 // 0x38: overlay name length 64 0x0b, 0x00, 0x00, 0x00, 65 66 // 0x3c: overlay name "OverlayName" 67 0x4f, 0x76, 0x65, 0x72, 0x6c, 0x61, 0x79, 0x4e, 0x61, 0x6D, 0x65, 0x00, 68 69 // 0x48 -> 4c: debug string 70 // string length, 71 0x05, 0x00, 0x00, 0x00, 72 73 // 0x4c string contents "debug\0\0\0" (padded to word alignment) 74 0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00, 75 76 // DATA HEADER 77 // 0x54: target_entry_count 78 0x03, 0x00, 0x00, 0x00, 79 80 // 0x58: target_inline_entry_count 81 0x01, 0x00, 0x00, 0x00, 82 83 // 0x5c: target_inline_entry_value_count 84 0x01, 0x00, 0x00, 0x00, 85 86 // 0x60: config_count 87 0x01, 0x00, 0x00, 0x00, 88 89 // 0x64: overlay_entry_count 90 0x03, 0x00, 0x00, 0x00, 91 92 // 0x68: string_pool_offset 93 0x00, 0x00, 0x00, 0x00, 94 95 // TARGET ENTRIES 96 // 0x6c: target id (0x7f020000) 97 0x00, 0x00, 0x02, 0x7f, 98 99 // 0x70: overlay_id (0x7f020000) 100 0x00, 0x00, 0x02, 0x7f, 101 102 // 0x74: target id (0x7f030000) 103 0x00, 0x00, 0x03, 0x7f, 104 105 // 0x78: overlay_id (0x7f030000) 106 0x00, 0x00, 0x03, 0x7f, 107 108 // 0x7c: target id (0x7f030002) 109 0x02, 0x00, 0x03, 0x7f, 110 111 // 0x80: overlay_id (0x7f030001) 112 0x01, 0x00, 0x03, 0x7f, 113 114 // INLINE TARGET ENTRIES 115 116 // 0x84: target_id 117 0x00, 0x00, 0x04, 0x7f, 118 119 // 0x88: start value index 120 0x00, 0x00, 0x00, 0x00, 121 122 // 0x8c: value count 123 0x01, 0x00, 0x00, 0x00, 124 125 // INLINE TARGET ENTRY VALUES 126 127 // 0x90: config index 128 0x00, 0x00, 0x00, 0x00, 129 130 // 0x94: Res_value::size (value ignored by idmap) 131 0x08, 0x00, 132 133 // 0x98: Res_value::res0 (value ignored by idmap) 134 0x00, 135 136 // 0x9c: Res_value::dataType (TYPE_INT_HEX) 137 0x11, 138 139 // 0xa0: Res_value::data 140 0x78, 0x56, 0x34, 0x12, 141 142 // CONFIGURATIONS 143 144 // 0xa4: ConfigDescription 145 // size 146 0x40, 0x00, 0x00, 0x00, 147 // 0xa8: imsi 148 0x00, 0x00, 0x00, 0x00, 149 // 0xac: locale 150 0x00, 0x00, 0x00, 0x00, 151 // 0xb0: screenType 152 0x02, 0x00, 0xe0, 0x01, 153 // 0xb4: input 154 0x00, 0x00, 0x00, 0x00, 155 // 0xb8: screenSize 156 0x00, 0x00, 0x00, 0x00, 157 // 0xbc: version 158 0x07, 0x00, 0x00, 0x00, 159 // 0xc0: screenConfig 160 0x00, 0x00, 0x00, 0x00, 161 // 0xc4: screenSizeDp 162 0x00, 0x00, 0x00, 0x00, 163 // 0xc8: localeScript 164 0x00, 0x00, 0x00, 0x00, 165 // 0xcc: localVariant(1) 166 0x00, 0x00, 0x00, 0x00, 167 // 0xd0: localVariant(2) 168 0x00, 0x00, 0x00, 0x00, 169 // 0xd4: screenConfig2 170 0x00, 0x00, 0x00, 0x00, 171 // 0xd8: localeScriptWasComputed 172 0x00, 173 // 0xd9: localeNumberingSystem(1) 174 0x00, 0x00, 0x00, 0x00, 175 // 0xdd: localeNumberingSystem(2) 176 0x00, 0x00, 0x00, 0x00, 177 178 // 0xe1: padding 179 0x00, 0x00, 0x00, 180 181 182 // OVERLAY ENTRIES 183 // 0xe4: 0x7f020000 -> 0x7f020000 184 0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f, 185 186 // 0xec: 0x7f030000 -> 0x7f030000 187 0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f, 188 189 // 0xf4: 0x7f030001 -> 0x7f030002 190 0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f, 191 192 // 0xfc: string pool 193 // string length, 194 0x04, 0x00, 0x00, 0x00, 195 196 // 0x100 string contents "test" 197 0x74, 0x65, 0x73, 0x74}; 198 199 constexpr unsigned int kIdmapRawDataLen = std::size(kIdmapRawData); 200 const unsigned int kIdmapRawDataOffset = 0x54; 201 const unsigned int kIdmapRawDataTargetCrc = 0x1234; 202 const unsigned int kIdmapRawOverlayCrc = 0x5678; 203 const unsigned int kIdmapRawDataPolicies = 0x11; 204 inline const std::string_view kIdmapRawTargetPath = "targetX.apk"; 205 inline const std::string_view kIdmapRawOverlayPath = "overlayX.apk"; 206 inline const std::string_view kIdmapRawOverlayName = "OverlayName"; 207 208 std::string GetTestDataPath(); 209 210 class TempFrroFile : public TemporaryFile { 211 public: TempFrroFile()212 TempFrroFile() { 213 std::string new_path = path; 214 new_path += ".frro"; 215 ::rename(path, new_path.c_str()); 216 const auto new_len = new_path.copy(path, sizeof(path) - 1); 217 path[new_len] = '\0'; 218 } 219 }; 220 221 class Idmap2Tests : public testing::Test { 222 protected: SetUp()223 void SetUp() override { 224 #ifdef __ANDROID__ 225 tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX"; 226 #else 227 tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX"; 228 #endif 229 EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr) 230 << "Failed to create temporary directory: " << strerror(errno); 231 target_apk_path_ = GetTestDataPath() + "/target/target.apk"; 232 overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk"; 233 idmap_path_ = tmp_dir_path_ + "/a.idmap"; 234 } 235 TearDown()236 void TearDown() override { 237 EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0) 238 << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno); 239 } 240 GetTempDirPath()241 const std::string& GetTempDirPath() { 242 return tmp_dir_path_; 243 } 244 GetTargetApkPath()245 const std::string& GetTargetApkPath() { 246 return target_apk_path_; 247 } 248 GetOverlayApkPath()249 const std::string& GetOverlayApkPath() { 250 return overlay_apk_path_; 251 } 252 GetIdmapPath()253 const std::string& GetIdmapPath() { 254 return idmap_path_; 255 } 256 257 private: 258 std::string tmp_dir_path_; 259 std::string target_apk_path_; 260 std::string overlay_apk_path_; 261 std::string idmap_path_; 262 }; 263 264 } // namespace android::idmap2 265 266 #endif // IDMAP2_TESTS_TESTHELPERS_H_ 267