Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | /* |
| 18 | * The tests in this file operate on a higher level than the tests in the other |
| 19 | * files. Here, all tests execute the idmap2 binary and only depend on |
| 20 | * libidmap2 to verify the output of idmap2. |
| 21 | */ |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <cerrno> |
| 29 | #include <cstdlib> |
| 30 | #include <cstring> // strerror |
| 31 | #include <fstream> |
| 32 | #include <memory> |
| 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include "gmock/gmock.h" |
| 38 | #include "gtest/gtest.h" |
| 39 | |
| 40 | #include "androidfw/PosixUtils.h" |
| 41 | #include "idmap2/FileUtils.h" |
| 42 | #include "idmap2/Idmap.h" |
| 43 | |
| 44 | #include "TestHelpers.h" |
| 45 | |
| 46 | using ::android::util::ExecuteBinary; |
| 47 | using ::testing::NotNull; |
| 48 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 49 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 50 | |
| 51 | class Idmap2BinaryTests : public Idmap2Tests {}; |
| 52 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path, |
| 56 | const std::string& overlay_apk_path) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 57 | // check that the idmap file looks reasonable (IdmapTests is responsible for |
| 58 | // more in-depth verification) |
| 59 | ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic); |
| 60 | ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion); |
| 61 | ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path); |
| 62 | ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 63 | ASSERT_EQ(idmap.GetData().size(), 1U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \ |
| 67 | do { \ |
| 68 | ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \ |
| 69 | } while (0) |
| 70 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 73 | TEST_F(Idmap2BinaryTests, Create) { |
| 74 | // clang-format off |
| 75 | auto result = ExecuteBinary({"idmap2", |
| 76 | "create", |
| 77 | "--target-apk-path", GetTargetApkPath(), |
| 78 | "--overlay-apk-path", GetOverlayApkPath(), |
| 79 | "--idmap-path", GetIdmapPath()}); |
| 80 | // clang-format on |
| 81 | ASSERT_THAT(result, NotNull()); |
| 82 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 83 | |
| 84 | struct stat st; |
| 85 | ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0); |
| 86 | |
| 87 | std::stringstream error; |
| 88 | std::ifstream fin(GetIdmapPath()); |
| 89 | std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, error); |
| 90 | fin.close(); |
| 91 | |
| 92 | ASSERT_THAT(idmap, NotNull()); |
| 93 | ASSERT_IDMAP(*idmap, GetTargetApkPath(), GetOverlayApkPath()); |
| 94 | |
| 95 | unlink(GetIdmapPath().c_str()); |
| 96 | } |
| 97 | |
| 98 | TEST_F(Idmap2BinaryTests, Dump) { |
| 99 | // clang-format off |
| 100 | auto result = ExecuteBinary({"idmap2", |
| 101 | "create", |
| 102 | "--target-apk-path", GetTargetApkPath(), |
| 103 | "--overlay-apk-path", GetOverlayApkPath(), |
| 104 | "--idmap-path", GetIdmapPath()}); |
| 105 | // clang-format on |
| 106 | ASSERT_THAT(result, NotNull()); |
| 107 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 108 | |
| 109 | // clang-format off |
| 110 | result = ExecuteBinary({"idmap2", |
| 111 | "dump", |
| 112 | "--idmap-path", GetIdmapPath()}); |
| 113 | // clang-format on |
| 114 | ASSERT_THAT(result, NotNull()); |
| 115 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 116 | ASSERT_NE(result->stdout.find("0x7f010000 -> 0x7f010000 integer/int1"), std::string::npos); |
| 117 | ASSERT_NE(result->stdout.find("0x7f020003 -> 0x7f020000 string/str1"), std::string::npos); |
| 118 | ASSERT_NE(result->stdout.find("0x7f020005 -> 0x7f020001 string/str3"), std::string::npos); |
| 119 | ASSERT_EQ(result->stdout.find("00000210: 007f target package id"), std::string::npos); |
| 120 | |
| 121 | // clang-format off |
| 122 | result = ExecuteBinary({"idmap2", |
| 123 | "dump", |
| 124 | "--verbose", |
| 125 | "--idmap-path", GetIdmapPath()}); |
| 126 | // clang-format on |
| 127 | ASSERT_THAT(result, NotNull()); |
| 128 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 129 | ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos); |
| 130 | ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos); |
| 131 | |
| 132 | // clang-format off |
| 133 | result = ExecuteBinary({"idmap2", |
| 134 | "dump", |
| 135 | "--verbose", |
| 136 | "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"}); |
| 137 | // clang-format on |
| 138 | ASSERT_THAT(result, NotNull()); |
| 139 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 140 | |
| 141 | unlink(GetIdmapPath().c_str()); |
| 142 | } |
| 143 | |
| 144 | TEST_F(Idmap2BinaryTests, Scan) { |
| 145 | const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk"; |
| 146 | const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk"; |
| 147 | const std::string idmap_static_1_path = |
| 148 | Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path); |
| 149 | const std::string idmap_static_2_path = |
| 150 | Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path); |
| 151 | |
| 152 | // single input directory, recursive |
| 153 | // clang-format off |
| 154 | auto result = ExecuteBinary({"idmap2", |
| 155 | "scan", |
| 156 | "--input-directory", GetTestDataPath(), |
| 157 | "--recursive", |
| 158 | "--target-package-name", "test.target", |
| 159 | "--target-apk-path", GetTargetApkPath(), |
| 160 | "--output-directory", GetTempDirPath()}); |
| 161 | // clang-format on |
| 162 | ASSERT_THAT(result, NotNull()); |
| 163 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 164 | std::stringstream expected; |
| 165 | expected << idmap_static_1_path << std::endl; |
| 166 | expected << idmap_static_2_path << std::endl; |
| 167 | ASSERT_EQ(result->stdout, expected.str()); |
| 168 | |
| 169 | std::stringstream error; |
| 170 | auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path); |
| 171 | auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string); |
| 172 | auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error); |
| 173 | ASSERT_THAT(idmap_static_1, NotNull()); |
| 174 | ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path); |
| 175 | |
| 176 | auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path); |
| 177 | auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string); |
| 178 | auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error); |
| 179 | ASSERT_THAT(idmap_static_2, NotNull()); |
| 180 | ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path); |
| 181 | |
| 182 | unlink(idmap_static_2_path.c_str()); |
| 183 | unlink(idmap_static_1_path.c_str()); |
| 184 | |
| 185 | // multiple input directories, non-recursive |
| 186 | // clang-format off |
| 187 | result = ExecuteBinary({"idmap2", |
| 188 | "scan", |
| 189 | "--input-directory", GetTestDataPath() + "/target", |
| 190 | "--input-directory", GetTestDataPath() + "/overlay", |
| 191 | "--target-package-name", "test.target", |
| 192 | "--target-apk-path", GetTargetApkPath(), |
| 193 | "--output-directory", GetTempDirPath()}); |
| 194 | // clang-format on |
| 195 | ASSERT_THAT(result, NotNull()); |
| 196 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 197 | ASSERT_EQ(result->stdout, expected.str()); |
| 198 | unlink(idmap_static_2_path.c_str()); |
| 199 | unlink(idmap_static_1_path.c_str()); |
| 200 | |
| 201 | // the same input directory given twice, but no duplicate entries |
| 202 | // clang-format off |
| 203 | result = ExecuteBinary({"idmap2", |
| 204 | "scan", |
| 205 | "--input-directory", GetTestDataPath(), |
| 206 | "--input-directory", GetTestDataPath(), |
| 207 | "--recursive", |
| 208 | "--target-package-name", "test.target", |
| 209 | "--target-apk-path", GetTargetApkPath(), |
| 210 | "--output-directory", GetTempDirPath()}); |
| 211 | // clang-format on |
| 212 | ASSERT_THAT(result, NotNull()); |
| 213 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 214 | ASSERT_EQ(result->stdout, expected.str()); |
| 215 | unlink(idmap_static_2_path.c_str()); |
| 216 | unlink(idmap_static_1_path.c_str()); |
| 217 | |
| 218 | // no APKs in input-directory: ok, but no output |
| 219 | // clang-format off |
| 220 | result = ExecuteBinary({"idmap2", |
| 221 | "scan", |
| 222 | "--input-directory", GetTempDirPath(), |
| 223 | "--target-package-name", "test.target", |
| 224 | "--target-apk-path", GetTargetApkPath(), |
| 225 | "--output-directory", GetTempDirPath()}); |
| 226 | // clang-format on |
| 227 | ASSERT_THAT(result, NotNull()); |
| 228 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 229 | ASSERT_EQ(result->stdout, ""); |
| 230 | } |
| 231 | |
| 232 | TEST_F(Idmap2BinaryTests, Lookup) { |
| 233 | // clang-format off |
| 234 | auto result = ExecuteBinary({"idmap2", |
| 235 | "create", |
| 236 | "--target-apk-path", GetTargetApkPath(), |
| 237 | "--overlay-apk-path", GetOverlayApkPath(), |
| 238 | "--idmap-path", GetIdmapPath()}); |
| 239 | // clang-format on |
| 240 | ASSERT_THAT(result, NotNull()); |
| 241 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 242 | |
| 243 | // clang-format off |
| 244 | result = ExecuteBinary({"idmap2", |
| 245 | "lookup", |
| 246 | "--idmap-path", GetIdmapPath(), |
| 247 | "--config", "", |
| 248 | "--resid", "0x7f020003"}); // string/str1 |
| 249 | // clang-format on |
| 250 | ASSERT_THAT(result, NotNull()); |
| 251 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 252 | ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos); |
| 253 | ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 254 | |
| 255 | // clang-format off |
| 256 | result = ExecuteBinary({"idmap2", |
| 257 | "lookup", |
| 258 | "--idmap-path", GetIdmapPath(), |
| 259 | "--config", "", |
| 260 | "--resid", "test.target:string/str1"}); |
| 261 | // clang-format on |
| 262 | ASSERT_THAT(result, NotNull()); |
| 263 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 264 | ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos); |
| 265 | ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 266 | |
| 267 | // clang-format off |
| 268 | result = ExecuteBinary({"idmap2", |
| 269 | "lookup", |
| 270 | "--idmap-path", GetIdmapPath(), |
| 271 | "--config", "sv", |
| 272 | "--resid", "test.target:string/str1"}); |
| 273 | // clang-format on |
| 274 | ASSERT_THAT(result, NotNull()); |
| 275 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 276 | ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 277 | |
| 278 | unlink(GetIdmapPath().c_str()); |
| 279 | } |
| 280 | |
| 281 | TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) { |
| 282 | const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST"; |
| 283 | |
| 284 | // missing mandatory options |
| 285 | // clang-format off |
| 286 | auto result = ExecuteBinary({"idmap2", |
| 287 | "create"}); |
| 288 | // clang-format on |
| 289 | ASSERT_THAT(result, NotNull()); |
| 290 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 291 | |
| 292 | // missing argument to option |
| 293 | // clang-format off |
| 294 | result = ExecuteBinary({"idmap2", |
| 295 | "create", |
| 296 | "--target-apk-path", GetTargetApkPath(), |
| 297 | "--overlay-apk-path", GetOverlayApkPath(), |
| 298 | "--idmap-path"}); |
| 299 | // clang-format on |
| 300 | ASSERT_THAT(result, NotNull()); |
| 301 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 302 | |
| 303 | // invalid target apk path |
| 304 | // clang-format off |
| 305 | result = ExecuteBinary({"idmap2", |
| 306 | "create", |
| 307 | "--target-apk-path", invalid_target_apk_path, |
| 308 | "--overlay-apk-path", GetOverlayApkPath(), |
| 309 | "--idmap-path", GetIdmapPath()}); |
| 310 | // clang-format on |
| 311 | ASSERT_THAT(result, NotNull()); |
| 312 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 313 | } |
| 314 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 315 | } // namespace android::idmap2 |