blob: 5c934a33ebb66841b5ab6fd5d2cdecdc44b10dca [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070022#include "gmock/gmock.h"
23#include "gtest/gtest.h"
24
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010025namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020026
27const unsigned char idmap_raw_data[] = {
28 // IDMAP HEADER
29 // 0x0: magic
30 0x49, 0x44, 0x4d, 0x50,
31
32 // 0x4: version
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080033 0x06, 0x00, 0x00, 0x00,
Mårten Kongstad02751232018-04-27 13:16:32 +020034
35 // 0x8: target crc
36 0x34, 0x12, 0x00, 0x00,
37
38 // 0xc: overlay crc
39 0x78, 0x56, 0x00, 0x00,
40
Ryan Mitchella7070132020-05-13 14:17:52 -070041 // 0x10: fulfilled policies
42 0x11, 0x00, 0x00, 0x00,
43
44 // 0x14: enforce overlayable
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070045 0x01, 0x00, 0x00, 0x00,
Ryan Mitchella7070132020-05-13 14:17:52 -070046
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080047 // 0x18: target path length
48 0x0b, 0x00, 0x00, 0x00,
Mårten Kongstad02751232018-04-27 13:16:32 +020049
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080050 // 0x1c: target path "targetX.apk"
51 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00,
Mårten Kongstad02751232018-04-27 13:16:32 +020052
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080053 // 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: debug string
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070060 // string length,
61 0x05, 0x00, 0x00, 0x00,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020062
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080063 // 0x3c string contents "debug\0\0\0" (padded to word alignment)
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020064 0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00,
65
Mårten Kongstad02751232018-04-27 13:16:32 +020066 // DATA HEADER
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080067 // 0x44: target_package_id
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070068 0x7f,
Mårten Kongstad02751232018-04-27 13:16:32 +020069
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080070 // 0x45: overlay_package_id
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070071 0x7f,
Mårten Kongstad02751232018-04-27 13:16:32 +020072
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080073 // 0x46: padding
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070074 0x00, 0x00,
75
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080076 // 0x48: target_entry_count
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070077 0x03, 0x00, 0x00, 0x00,
Mårten Kongstad02751232018-04-27 13:16:32 +020078
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080079 // 0x4c: target_inline_entry_count
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070080 0x01, 0x00, 0x00, 0x00,
81
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080082 // 0x50: overlay_entry_count
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070083 0x03, 0x00, 0x00, 0x00,
Mårten Kongstad02751232018-04-27 13:16:32 +020084
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080085 // 0x54: string_pool_offset
Mårten Kongstad02751232018-04-27 13:16:32 +020086 0x00, 0x00, 0x00, 0x00,
87
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070088 // TARGET ENTRIES
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080089 // 0x58: target id (0x7f020000)
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070090 0x00, 0x00, 0x02, 0x7f,
91
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080092 // 0x5c: overlay_id (0x7f020000)
Ryan Mitchella7070132020-05-13 14:17:52 -070093 0x00, 0x00, 0x02, 0x7f,
94
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080095 // 0x60: target id (0x7f030000)
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070096 0x00, 0x00, 0x03, 0x7f,
97
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080098 // 0x64: overlay_id (0x7f030000)
Ryan Mitchella7070132020-05-13 14:17:52 -070099 0x00, 0x00, 0x03, 0x7f,
100
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800101 // 0x68: target id (0x7f030002)
Ryan Mitchella7070132020-05-13 14:17:52 -0700102 0x02, 0x00, 0x03, 0x7f,
103
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800104 // 0x6c: overlay_id (0x7f030001)
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700105 0x01, 0x00, 0x03, 0x7f,
106
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700107 // INLINE TARGET ENTRIES
108
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800109 // 0x70: target_id
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700110 0x00, 0x00, 0x04, 0x7f,
111
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800112 // 0x74: Res_value::size (value ignored by idmap)
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700113 0x08, 0x00,
114
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800115 // 0x77: Res_value::res0 (value ignored by idmap)
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700116 0x00,
117
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800118 // 0x78: Res_value::dataType (TYPE_INT_HEX)
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700119 0x11,
120
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800121 // 0x7c: Res_value::data
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700122 0x78, 0x56, 0x34, 0x12,
123
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700124 // OVERLAY ENTRIES
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800125 // 0x80: 0x7f020000 -> 0x7f020000
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700126 0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f,
127
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800128 // 0x88: 0x7f030000 -> 0x7f030000
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700129 0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f,
130
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800131 // 0x90: 0x7f030001 -> 0x7f030002
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700132 0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f,
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700133
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800134 // 0x94: string pool
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700135 // string length,
136 0x04, 0x00, 0x00, 0x00,
137
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800138 // 0x98 string contents "test"
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700139 0x74, 0x65, 0x73, 0x74};
140
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800141const unsigned int idmap_raw_data_len = 0x9c;
142const unsigned int idmap_raw_data_offset = 0x44;
143const unsigned int idmap_raw_data_target_crc = 0x1234;
144const unsigned int idmap_raw_data_overlay_crc = 0x5678;
145const unsigned int idmap_raw_data_policies = 0x11;
146inline const std::string idmap_raw_data_target_path = "targetX.apk";
147inline const std::string idmap_raw_data_overlay_path = "overlayX.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200148
Mårten Kongstadb8779022018-11-29 09:53:17 +0100149std::string GetTestDataPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200150
151class Idmap2Tests : public testing::Test {
152 protected:
Mårten Kongstadcb85d9c2018-12-21 08:28:40 +0100153 void SetUp() override {
Mårten Kongstad02751232018-04-27 13:16:32 +0200154#ifdef __ANDROID__
155 tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX";
156#else
157 tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX";
158#endif
159 EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr)
160 << "Failed to create temporary directory: " << strerror(errno);
161 target_apk_path_ = GetTestDataPath() + "/target/target.apk";
162 overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk";
163 idmap_path_ = tmp_dir_path_ + "/a.idmap";
164 }
165
Mårten Kongstadcb85d9c2018-12-21 08:28:40 +0100166 void TearDown() override {
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0)
168 << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno);
169 }
170
171 const std::string& GetTempDirPath() {
172 return tmp_dir_path_;
173 }
174
175 const std::string& GetTargetApkPath() {
176 return target_apk_path_;
177 }
178
179 const std::string& GetOverlayApkPath() {
180 return overlay_apk_path_;
181 }
182
183 const std::string& GetIdmapPath() {
184 return idmap_path_;
185 }
186
187 private:
188 std::string tmp_dir_path_;
189 std::string target_apk_path_;
190 std::string overlay_apk_path_;
191 std::string idmap_path_;
192};
193
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100194} // namespace android::idmap2
Mårten Kongstad02751232018-04-27 13:16:32 +0200195
196#endif // IDMAP2_TESTS_TESTHELPERS_H_