• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     0x0b, 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     // CONSTRAINTS
77     // 0x54: constraints_count
78     0x02, 0x00, 0x00, 0x00,
79 
80     // 0x58: constraint_type
81     0x00, 0x00, 0x00, 0x00,
82 
83     // 0x5c: constraint_value
84     0x01, 0x00, 0x00, 0x00,
85 
86     // 0x60: constraint_type
87     0x01, 0x00, 0x00, 0x00,
88 
89     // 0x64: constraint_value
90     0x02, 0x00, 0x00, 0x00,
91 
92     // DATA HEADER
93     // 0x68: target_entry_count
94     0x03, 0x00, 0x00, 0x00,
95 
96     // 0x6c: target_inline_entry_count
97     0x01, 0x00, 0x00, 0x00,
98 
99     // 0x70: target_inline_entry_value_count
100     0x01, 0x00, 0x00, 0x00,
101 
102     // 0x60: config_count
103     0x01, 0x00, 0x00, 0x00,
104 
105     // 0x74: overlay_entry_count
106     0x03, 0x00, 0x00, 0x00,
107 
108     // 0x78: string_pool_offset
109     0x00, 0x00, 0x00, 0x00,
110 
111     // TARGET ENTRIES
112     // 0x7c: target id (0x7f020000)
113     0x00, 0x00, 0x02, 0x7f,
114     // 0x80: target id (0x7f030000)
115     0x00, 0x00, 0x03, 0x7f,
116     // 0x84: target id (0x7f030002)
117     0x02, 0x00, 0x03, 0x7f,
118 
119     // 0x88: overlay_id (0x7f020000)
120     0x00, 0x00, 0x02, 0x7f,
121     // 0x8c: overlay_id (0x7f030000)
122     0x00, 0x00, 0x03, 0x7f,
123     // 0x90: overlay_id (0x7f030001)
124     0x01, 0x00, 0x03, 0x7f,
125 
126     // INLINE TARGET ENTRIES
127 
128     // 0x94: target_id
129     0x00, 0x00, 0x04, 0x7f,
130 
131     // 0x98: start value index
132     0x00, 0x00, 0x00, 0x00,
133 
134     // 0x9c: value count
135     0x01, 0x00, 0x00, 0x00,
136 
137     // INLINE TARGET ENTRY VALUES
138 
139     // 0xa0: config index
140     0x00, 0x00, 0x00, 0x00,
141 
142     // 0xa4: Res_value::size (value ignored by idmap)
143     0x08, 0x00,
144 
145     // 0xa8: Res_value::res0 (value ignored by idmap)
146     0x00,
147 
148     // 0xac: Res_value::dataType (TYPE_INT_HEX)
149     0x11,
150 
151     // 0xb0: Res_value::data
152     0x78, 0x56, 0x34, 0x12,
153 
154     // CONFIGURATIONS
155 
156     // 0xb4: ConfigDescription
157     // size
158     0x40, 0x00, 0x00, 0x00,
159     // 0xb8: imsi
160     0x00, 0x00, 0x00, 0x00,
161     // 0xbc: locale
162     0x00, 0x00, 0x00, 0x00,
163     // 0xc0: screenType
164     0x02, 0x00, 0xe0, 0x01,
165     // 0xc4: input
166     0x00, 0x00, 0x00, 0x00,
167     // 0xc8: screenSize
168     0x00, 0x00, 0x00, 0x00,
169     // 0xcc: version
170     0x07, 0x00, 0x00, 0x00,
171     // 0xd0: screenConfig
172     0x00, 0x00, 0x00, 0x00,
173     // 0xd4: screenSizeDp
174     0x00, 0x00, 0x00, 0x00,
175     // 0xd8: localeScript
176     0x00, 0x00, 0x00, 0x00,
177     // 0xdc: localVariant(1)
178     0x00, 0x00, 0x00, 0x00,
179     // 0xe0: localVariant(2)
180     0x00, 0x00, 0x00, 0x00,
181     // 0xe4: screenConfig2
182     0x00, 0x00, 0x00, 0x00,
183     // 0xe8: localeScriptWasComputed
184     0x00,
185     // 0xe9: localeNumberingSystem(1)
186     0x00, 0x00, 0x00, 0x00,
187     // 0xed: localeNumberingSystem(2)
188     0x00, 0x00, 0x00, 0x00,
189 
190     // 0xf1: padding
191     0x00, 0x00, 0x00,
192 
193     // OVERLAY ENTRIES
194     // 0xf4: 0x7f020000 -> ...
195     0x00, 0x00, 0x02, 0x7f,
196     // 0xf8: 0x7f030000 -> ...
197     0x00, 0x00, 0x03, 0x7f,
198     // 0xfc: 0x7f030001 -> ...
199     0x01, 0x00, 0x03, 0x7f,
200 
201     // 0x100: ... -> 0x7f020000
202     0x00, 0x00, 0x02, 0x7f,
203     // 0x104: ... -> 0x7f030000
204     0x00, 0x00, 0x03, 0x7f,
205     // 0x108: ... -> 0x7f030002
206     0x02, 0x00, 0x03, 0x7f,
207 
208     // 0x10c: string pool
209     // string length,
210     0x04, 0x00, 0x00, 0x00,
211 
212     // 0x110 string contents "test"
213     0x74, 0x65, 0x73, 0x74};
214 
215 constexpr unsigned int kIdmapRawDataLen = std::size(kIdmapRawData);
216 const unsigned int kIdmapRawDataOffset = 0x68;
217 const unsigned int kIdmapRawDataTargetCrc = 0x1234;
218 const unsigned int kIdmapRawOverlayCrc = 0x5678;
219 const unsigned int kIdmapRawDataPolicies = 0x11;
220 inline const std::string_view kIdmapRawTargetPath = "targetX.apk";
221 inline const std::string_view kIdmapRawOverlayPath = "overlayX.apk";
222 inline const std::string_view kIdmapRawOverlayName = "OverlayName";
223 
224 std::string GetTestDataPath();
225 
226 class TempFrroFile : public TemporaryFile {
227 public:
TempFrroFile()228   TempFrroFile() {
229     std::string new_path = path;
230     new_path += ".frro";
231     ::rename(path, new_path.c_str());
232     const auto new_len = new_path.copy(path, sizeof(path) - 1);
233     path[new_len] = '\0';
234   }
235 };
236 
237 class Idmap2Tests : public testing::Test {
238  protected:
SetUp()239   void SetUp() override {
240 #ifdef __ANDROID__
241     tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX";
242 #else
243     tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX";
244 #endif
245     EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr)
246         << "Failed to create temporary directory: " << strerror(errno);
247     target_apk_path_ = GetTestDataPath() + "/target/target.apk";
248     overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk";
249     idmap_path_ = tmp_dir_path_ + "/a.idmap";
250   }
251 
TearDown()252   void TearDown() override {
253     EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0)
254         << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno);
255   }
256 
GetTempDirPath()257   const std::string& GetTempDirPath() {
258     return tmp_dir_path_;
259   }
260 
GetTargetApkPath()261   const std::string& GetTargetApkPath() {
262     return target_apk_path_;
263   }
264 
GetOverlayApkPath()265   const std::string& GetOverlayApkPath() {
266     return overlay_apk_path_;
267   }
268 
GetIdmapPath()269   const std::string& GetIdmapPath() {
270     return idmap_path_;
271   }
272 
273  private:
274   std::string tmp_dir_path_;
275   std::string target_apk_path_;
276   std::string overlay_apk_path_;
277   std::string idmap_path_;
278 };
279 
280 }  // namespace android::idmap2
281 
282 #endif  // IDMAP2_TESTS_TESTHELPERS_H_
283