• 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 <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     0x09, 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: target_inline_entry_value_count
80     0x01, 0x00, 0x00, 0x00,
81 
82     // 0x60: config_count
83     0x01, 0x00, 0x00, 0x00,
84 
85     // 0x64: overlay_entry_count
86     0x03, 0x00, 0x00, 0x00,
87 
88     // 0x68: string_pool_offset
89     0x00, 0x00, 0x00, 0x00,
90 
91     // TARGET ENTRIES
92     // 0x6c: target id (0x7f020000)
93     0x00, 0x00, 0x02, 0x7f,
94 
95     // 0x70: overlay_id (0x7f020000)
96     0x00, 0x00, 0x02, 0x7f,
97 
98     // 0x74: target id (0x7f030000)
99     0x00, 0x00, 0x03, 0x7f,
100 
101     // 0x78: overlay_id (0x7f030000)
102     0x00, 0x00, 0x03, 0x7f,
103 
104     // 0x7c: target id (0x7f030002)
105     0x02, 0x00, 0x03, 0x7f,
106 
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 
178     // OVERLAY ENTRIES
179     // 0xe4: 0x7f020000 -> 0x7f020000
180     0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f,
181 
182     // 0xec: 0x7f030000 -> 0x7f030000
183     0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f,
184 
185     // 0xf4: 0x7f030001 -> 0x7f030002
186     0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f,
187 
188     // 0xfc: string pool
189     // string length,
190     0x04, 0x00, 0x00, 0x00,
191 
192     // 0x100 string contents "test"
193     0x74, 0x65, 0x73, 0x74};
194 
195 constexpr unsigned int kIdmapRawDataLen = std::size(kIdmapRawData);
196 const unsigned int kIdmapRawDataOffset = 0x54;
197 const unsigned int kIdmapRawDataTargetCrc = 0x1234;
198 const unsigned int kIdmapRawOverlayCrc = 0x5678;
199 const unsigned int kIdmapRawDataPolicies = 0x11;
200 inline const std::string kIdmapRawTargetPath = "targetX.apk";
201 inline const std::string kIdmapRawOverlayPath = "overlayX.apk";
202 inline const std::string kIdmapRawOverlayName = "OverlayName";
203 
204 std::string GetTestDataPath();
205 
206 class Idmap2Tests : public testing::Test {
207  protected:
SetUp()208   void SetUp() override {
209 #ifdef __ANDROID__
210     tmp_dir_path_ = "/data/local/tmp/idmap2-tests-XXXXXX";
211 #else
212     tmp_dir_path_ = "/tmp/idmap2-tests-XXXXXX";
213 #endif
214     EXPECT_NE(mkdtemp(const_cast<char*>(tmp_dir_path_.c_str())), nullptr)
215         << "Failed to create temporary directory: " << strerror(errno);
216     target_apk_path_ = GetTestDataPath() + "/target/target.apk";
217     overlay_apk_path_ = GetTestDataPath() + "/overlay/overlay.apk";
218     idmap_path_ = tmp_dir_path_ + "/a.idmap";
219   }
220 
TearDown()221   void TearDown() override {
222     EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0)
223         << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno);
224   }
225 
GetTempDirPath()226   const std::string& GetTempDirPath() {
227     return tmp_dir_path_;
228   }
229 
GetTargetApkPath()230   const std::string& GetTargetApkPath() {
231     return target_apk_path_;
232   }
233 
GetOverlayApkPath()234   const std::string& GetOverlayApkPath() {
235     return overlay_apk_path_;
236   }
237 
GetIdmapPath()238   const std::string& GetIdmapPath() {
239     return idmap_path_;
240   }
241 
242  private:
243   std::string tmp_dir_path_;
244   std::string target_apk_path_;
245   std::string overlay_apk_path_;
246   std::string idmap_path_;
247 };
248 
249 }  // namespace android::idmap2
250 
251 #endif  // IDMAP2_TESTS_TESTHELPERS_H_
252