xref: /aosp_15_r20/frameworks/base/cmds/idmap2/tests/TestHelpers.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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     0x0a, 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     // 0x70: target id (0x7f030000)
99     0x00, 0x00, 0x03, 0x7f,
100     // 0x74: target id (0x7f030002)
101     0x02, 0x00, 0x03, 0x7f,
102 
103     // 0x78: overlay_id (0x7f020000)
104     0x00, 0x00, 0x02, 0x7f,
105     // 0x7c: overlay_id (0x7f030000)
106     0x00, 0x00, 0x03, 0x7f,
107     // 0x80: overlay_id (0x7f030001)
108     0x01, 0x00, 0x03, 0x7f,
109 
110     // INLINE TARGET ENTRIES
111 
112     // 0x84: target_id
113     0x00, 0x00, 0x04, 0x7f,
114 
115     // 0x88: start value index
116     0x00, 0x00, 0x00, 0x00,
117 
118     // 0x8c: value count
119     0x01, 0x00, 0x00, 0x00,
120 
121     // INLINE TARGET ENTRY VALUES
122 
123     // 0x90: config index
124     0x00, 0x00, 0x00, 0x00,
125 
126     // 0x94: Res_value::size (value ignored by idmap)
127     0x08, 0x00,
128 
129     // 0x98: Res_value::res0 (value ignored by idmap)
130     0x00,
131 
132     // 0x9c: Res_value::dataType (TYPE_INT_HEX)
133     0x11,
134 
135     // 0xa0: Res_value::data
136     0x78, 0x56, 0x34, 0x12,
137 
138     // CONFIGURATIONS
139 
140     // 0xa4: ConfigDescription
141     // size
142     0x40, 0x00, 0x00, 0x00,
143     // 0xa8: imsi
144     0x00, 0x00, 0x00, 0x00,
145     // 0xac: locale
146     0x00, 0x00, 0x00, 0x00,
147     // 0xb0: screenType
148     0x02, 0x00, 0xe0, 0x01,
149     // 0xb4: input
150     0x00, 0x00, 0x00, 0x00,
151     // 0xb8: screenSize
152     0x00, 0x00, 0x00, 0x00,
153     // 0xbc: version
154     0x07, 0x00, 0x00, 0x00,
155     // 0xc0: screenConfig
156     0x00, 0x00, 0x00, 0x00,
157     // 0xc4: screenSizeDp
158     0x00, 0x00, 0x00, 0x00,
159     // 0xc8: localeScript
160     0x00, 0x00, 0x00, 0x00,
161     // 0xcc: localVariant(1)
162     0x00, 0x00, 0x00, 0x00,
163     // 0xd0: localVariant(2)
164     0x00, 0x00, 0x00, 0x00,
165     // 0xd4: screenConfig2
166     0x00, 0x00, 0x00, 0x00,
167     // 0xd8: localeScriptWasComputed
168     0x00,
169     // 0xd9: localeNumberingSystem(1)
170     0x00, 0x00, 0x00, 0x00,
171     // 0xdd: localeNumberingSystem(2)
172     0x00, 0x00, 0x00, 0x00,
173 
174     // 0xe1: padding
175     0x00, 0x00, 0x00,
176 
177     // OVERLAY ENTRIES
178     // 0xe4: 0x7f020000 -> ...
179     0x00, 0x00, 0x02, 0x7f,
180     // 0xe8: 0x7f030000 -> ...
181     0x00, 0x00, 0x03, 0x7f,
182     // 0xec: 0x7f030001 -> ...
183     0x01, 0x00, 0x03, 0x7f,
184 
185     // 0xf0: ... -> 0x7f020000
186     0x00, 0x00, 0x02, 0x7f,
187     // 0xf4: ... -> 0x7f030000
188     0x00, 0x00, 0x03, 0x7f,
189     // 0xf8: ... -> 0x7f030002
190     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