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 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 37 | #include "R.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 38 | #include "TestHelpers.h" |
| 39 | #include "androidfw/PosixUtils.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 40 | #include "gmock/gmock.h" |
| 41 | #include "gtest/gtest.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 42 | #include "idmap2/FileUtils.h" |
| 43 | #include "idmap2/Idmap.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 44 | #include "private/android_filesystem_config.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 71 | #ifdef __ANDROID__ |
| 72 | #define SKIP_TEST_IF_CANT_EXEC_IDMAP2 \ |
| 73 | do { \ |
| 74 | const uid_t uid = getuid(); \ |
| 75 | if (uid != AID_ROOT && uid != AID_SYSTEM) { \ |
| 76 | GTEST_SKIP(); \ |
| 77 | } \ |
| 78 | } while (0) |
| 79 | #else |
| 80 | #define SKIP_TEST_IF_CANT_EXEC_IDMAP2 |
| 81 | #endif |
| 82 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 83 | } // namespace |
| 84 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 85 | TEST_F(Idmap2BinaryTests, Create) { |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 86 | SKIP_TEST_IF_CANT_EXEC_IDMAP2; |
| 87 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 88 | // clang-format off |
| 89 | auto result = ExecuteBinary({"idmap2", |
| 90 | "create", |
| 91 | "--target-apk-path", GetTargetApkPath(), |
| 92 | "--overlay-apk-path", GetOverlayApkPath(), |
| 93 | "--idmap-path", GetIdmapPath()}); |
| 94 | // clang-format on |
| 95 | ASSERT_THAT(result, NotNull()); |
| 96 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 97 | |
| 98 | struct stat st; |
| 99 | ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0); |
| 100 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 101 | std::ifstream fin(GetIdmapPath()); |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 102 | const auto idmap = Idmap::FromBinaryStream(fin); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 103 | fin.close(); |
| 104 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 105 | ASSERT_TRUE(idmap); |
| 106 | ASSERT_IDMAP(**idmap, GetTargetApkPath(), GetOverlayApkPath()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 107 | |
| 108 | unlink(GetIdmapPath().c_str()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(Idmap2BinaryTests, Dump) { |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 112 | SKIP_TEST_IF_CANT_EXEC_IDMAP2; |
| 113 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 114 | // clang-format off |
| 115 | auto result = ExecuteBinary({"idmap2", |
| 116 | "create", |
| 117 | "--target-apk-path", GetTargetApkPath(), |
| 118 | "--overlay-apk-path", GetOverlayApkPath(), |
| 119 | "--idmap-path", GetIdmapPath()}); |
| 120 | // clang-format on |
| 121 | ASSERT_THAT(result, NotNull()); |
| 122 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 123 | |
| 124 | // clang-format off |
| 125 | result = ExecuteBinary({"idmap2", |
| 126 | "dump", |
| 127 | "--idmap-path", GetIdmapPath()}); |
| 128 | // clang-format on |
| 129 | ASSERT_THAT(result, NotNull()); |
| 130 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 131 | ASSERT_NE(result->stdout.find(R::target::integer::literal::int1 + " -> 0x7f010000"), |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 132 | std::string::npos); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 133 | ASSERT_NE(result->stdout.find(R::target::string::literal::str1 + " -> 0x7f020000"), |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 134 | std::string::npos); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 135 | ASSERT_NE(result->stdout.find(R::target::string::literal::str3 + " -> 0x7f020001"), |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 136 | std::string::npos); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 137 | ASSERT_NE(result->stdout.find(R::target::string::literal::str4 + " -> 0x7f020002"), |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 138 | std::string::npos); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 139 | |
| 140 | // clang-format off |
| 141 | result = ExecuteBinary({"idmap2", |
| 142 | "dump", |
| 143 | "--verbose", |
| 144 | "--idmap-path", GetIdmapPath()}); |
| 145 | // clang-format on |
| 146 | ASSERT_THAT(result, NotNull()); |
| 147 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 148 | ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 149 | |
| 150 | // clang-format off |
| 151 | result = ExecuteBinary({"idmap2", |
| 152 | "dump", |
| 153 | "--verbose", |
| 154 | "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"}); |
| 155 | // clang-format on |
| 156 | ASSERT_THAT(result, NotNull()); |
| 157 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 158 | |
| 159 | unlink(GetIdmapPath().c_str()); |
| 160 | } |
| 161 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 162 | TEST_F(Idmap2BinaryTests, Lookup) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 163 | SKIP_TEST_IF_CANT_EXEC_IDMAP2; |
| 164 | |
| 165 | // clang-format off |
| 166 | auto result = ExecuteBinary({"idmap2", |
| 167 | "create", |
| 168 | "--target-apk-path", GetTargetApkPath(), |
| 169 | "--overlay-apk-path", GetOverlayApkPath(), |
| 170 | "--idmap-path", GetIdmapPath()}); |
| 171 | // clang-format on |
| 172 | ASSERT_THAT(result, NotNull()); |
| 173 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 174 | |
| 175 | // clang-format off |
| 176 | result = ExecuteBinary({"idmap2", |
| 177 | "lookup", |
| 178 | "--idmap-path", GetIdmapPath(), |
| 179 | "--config", "", |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 180 | "--resid", R::target::string::literal::str1}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 181 | // clang-format on |
| 182 | ASSERT_THAT(result, NotNull()); |
| 183 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 184 | ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos); |
| 185 | ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 186 | |
| 187 | // clang-format off |
| 188 | result = ExecuteBinary({"idmap2", |
| 189 | "lookup", |
| 190 | "--idmap-path", GetIdmapPath(), |
| 191 | "--config", "", |
| 192 | "--resid", "test.target:string/str1"}); |
| 193 | // clang-format on |
| 194 | ASSERT_THAT(result, NotNull()); |
| 195 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 196 | ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos); |
| 197 | ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 198 | |
| 199 | // clang-format off |
| 200 | result = ExecuteBinary({"idmap2", |
| 201 | "lookup", |
| 202 | "--idmap-path", GetIdmapPath(), |
| 203 | "--config", "sv", |
| 204 | "--resid", "test.target:string/str1"}); |
| 205 | // clang-format on |
| 206 | ASSERT_THAT(result, NotNull()); |
| 207 | ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr; |
| 208 | ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos); |
| 209 | |
| 210 | unlink(GetIdmapPath().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) { |
Mårten Kongstad | 1da49dc | 2019-01-14 10:03:53 +0100 | [diff] [blame] | 214 | SKIP_TEST_IF_CANT_EXEC_IDMAP2; |
| 215 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 216 | const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST"; |
| 217 | |
| 218 | // missing mandatory options |
| 219 | // clang-format off |
| 220 | auto result = ExecuteBinary({"idmap2", |
| 221 | "create"}); |
| 222 | // clang-format on |
| 223 | ASSERT_THAT(result, NotNull()); |
| 224 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 225 | |
| 226 | // missing argument to option |
| 227 | // clang-format off |
| 228 | result = ExecuteBinary({"idmap2", |
| 229 | "create", |
| 230 | "--target-apk-path", GetTargetApkPath(), |
| 231 | "--overlay-apk-path", GetOverlayApkPath(), |
| 232 | "--idmap-path"}); |
| 233 | // clang-format on |
| 234 | ASSERT_THAT(result, NotNull()); |
| 235 | ASSERT_NE(result->status, EXIT_SUCCESS); |
| 236 | |
| 237 | // invalid target apk path |
| 238 | // clang-format off |
| 239 | result = ExecuteBinary({"idmap2", |
| 240 | "create", |
| 241 | "--target-apk-path", invalid_target_apk_path, |
| 242 | "--overlay-apk-path", GetOverlayApkPath(), |
| 243 | "--idmap-path", GetIdmapPath()}); |
| 244 | // clang-format on |
| 245 | ASSERT_THAT(result, NotNull()); |
| 246 | ASSERT_NE(result->status, EXIT_SUCCESS); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 247 | |
| 248 | // unknown policy |
| 249 | // clang-format off |
| 250 | result = ExecuteBinary({"idmap2", |
| 251 | "create", |
| 252 | "--target-apk-path", GetTargetApkPath(), |
| 253 | "--overlay-apk-path", GetOverlayApkPath(), |
| 254 | "--idmap-path", GetIdmapPath(), |
| 255 | "--policy", "this-does-not-exist"}); |
| 256 | // clang-format on |
| 257 | ASSERT_THAT(result, NotNull()); |
| 258 | ASSERT_NE(result->status, EXIT_SUCCESS); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 261 | } // namespace android::idmap2 |