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 idmap_raw_data[] = {
28     // IDMAP HEADER
29     // 0x0: magic
30     0x49, 0x44, 0x4d, 0x50,
31 
32     // 0x4: version
33     0x04, 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,
46 
47     // 0x15: target path "targetX.apk"
48     0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x00,
49     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 
65     // 0x115: overlay path "overlayX.apk"
66     0x6f, 0x76, 0x65, 0x72, 0x6c, 0x61, 0x79, 0x58, 0x2e, 0x61, 0x70, 0x6b, 0x00, 0x00, 0x00, 0x00,
67     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 
83     // 0x215: debug string
84     // string length, including terminating null
85     0x08, 0x00, 0x00, 0x00,
86 
87     // string contents "debug\0\0\0" (padded to word alignment)
88     0x64, 0x65, 0x62, 0x75, 0x67, 0x00, 0x00, 0x00,
89 
90     // DATA HEADER
91     // 0x221: target_package_id
92     0x7f,
93 
94     // 0x222: overlay_package_id
95     0x7f,
96 
97     // 0x223: target_entry_count
98     0x03, 0x00, 0x00, 0x00,
99 
100     // 0x227: overlay_entry_count
101     0x03, 0x00, 0x00, 0x00,
102 
103     // 0x22b: string_pool_offset
104     0x00, 0x00, 0x00, 0x00,
105 
106     // 0x22f: string_pool_byte_length
107     0x00, 0x00, 0x00, 0x00,
108 
109     // TARGET ENTRIES
110     // 0x233: 0x7f020000
111     0x00, 0x00, 0x02, 0x7f,
112 
113     // 0x237: TYPE_REFERENCE
114     0x01,
115 
116     // 0x238: 0x7f020000
117     0x00, 0x00, 0x02, 0x7f,
118 
119     // 0x23c: 0x7f030000
120     0x00, 0x00, 0x03, 0x7f,
121 
122     // 0x240: TYPE_REFERENCE
123     0x01,
124 
125     // 0x241: 0x7f030000
126     0x00, 0x00, 0x03, 0x7f,
127 
128     // 0x245: 0x7f030002
129     0x02, 0x00, 0x03, 0x7f,
130 
131     // 0x249: TYPE_REFERENCE
132     0x01,
133 
134     // 0x24a: 0x7f030001
135     0x01, 0x00, 0x03, 0x7f,
136 
137     // OVERLAY ENTRIES
138     // 0x24e: 0x7f020000 -> 0x7f020000
139     0x00, 0x00, 0x02, 0x7f, 0x00, 0x00, 0x02, 0x7f,
140 
141     // 0x256: 0x7f030000 -> 0x7f030000
142     0x00, 0x00, 0x03, 0x7f, 0x00, 0x00, 0x03, 0x7f,
143 
144     // 0x25e: 0x7f030001 -> 0x7f030002
145     0x01, 0x00, 0x03, 0x7f, 0x02, 0x00, 0x03, 0x7f};
146 
147 const unsigned int idmap_raw_data_len = 0x266;
148 
149 std::string GetTestDataPath();
150 
151 class Idmap2Tests : public testing::Test {
152  protected:
SetUp()153   void SetUp() override {
154 #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 
TearDown()166   void TearDown() override {
167     EXPECT_EQ(rmdir(tmp_dir_path_.c_str()), 0)
168         << "Failed to remove temporary directory " << tmp_dir_path_ << ": " << strerror(errno);
169   }
170 
GetTempDirPath()171   const std::string& GetTempDirPath() {
172     return tmp_dir_path_;
173   }
174 
GetTargetApkPath()175   const std::string& GetTargetApkPath() {
176     return target_apk_path_;
177   }
178 
GetOverlayApkPath()179   const std::string& GetOverlayApkPath() {
180     return overlay_apk_path_;
181   }
182 
GetIdmapPath()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 
194 }  // namespace android::idmap2
195 
196 #endif  // IDMAP2_TESTS_TESTHELPERS_H_
197