Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [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 | |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 17 | #include <stdio.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 18 | #include <algorithm> |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 19 | #include <filesystem> |
Andreas Gampe | a158e3d | 2018-12-10 13:46:30 -0800 | [diff] [blame] | 20 | #include <fstream> |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 21 | #include <functional> |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 22 | #include <memory> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 23 | #include <string> |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 24 | #include <unordered_set> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | #include <grp.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
Andreas Gampe | 5ec47c5 | 2018-11-14 14:33:53 -0800 | [diff] [blame] | 33 | #include <android-base/macros.h> |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 34 | #include <android-base/properties.h> |
Andreas Gampe | c766c0a | 2018-12-06 15:51:21 -0800 | [diff] [blame] | 35 | #include <android-base/scopeguard.h> |
Andreas Gampe | 5ec47c5 | 2018-11-14 14:33:53 -0800 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 38 | #include <android/os/IVold.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 39 | #include <binder/IServiceManager.h> |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 40 | #include <gmock/gmock.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 41 | #include <gtest/gtest.h> |
Andreas Gampe | da5f506 | 2019-03-26 12:19:13 -0700 | [diff] [blame] | 42 | #include <libdm/dm.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 43 | #include <selinux/selinux.h> |
| 44 | |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 45 | #include <android/apex/ApexInfo.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 46 | #include <android/apex/IApexService.h> |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 47 | |
Andreas Gampe | f35e179 | 2019-04-01 15:58:03 -0700 | [diff] [blame] | 48 | #include "apex_constants.h" |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 49 | #include "apex_file.h" |
| 50 | #include "apex_manifest.h" |
Andreas Gampe | 4510d49 | 2018-12-12 15:56:05 -0800 | [diff] [blame] | 51 | #include "apexd_private.h" |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 52 | #include "apexd_session.h" |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 53 | #include "apexd_test_utils.h" |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 54 | #include "apexd_utils.h" |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 55 | #include "status_or.h" |
| 56 | |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 57 | #include "session_state.pb.h" |
| 58 | |
| 59 | using apex::proto::SessionState; |
| 60 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 61 | namespace android { |
| 62 | namespace apex { |
| 63 | |
| 64 | using android::sp; |
| 65 | using android::String16; |
Nikita Ioffe | fa7b5da | 2019-04-10 12:15:53 +0100 | [diff] [blame] | 66 | using android::apex::testing::ApexInfoEq; |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 67 | using android::apex::testing::CreateSessionInfo; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 68 | using android::apex::testing::IsOk; |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 69 | using android::apex::testing::SessionInfoEq; |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 70 | using android::base::Join; |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 71 | using android::base::StringPrintf; |
Nikita Ioffe | fa7b5da | 2019-04-10 12:15:53 +0100 | [diff] [blame] | 72 | using ::testing::Contains; |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 73 | using ::testing::EndsWith; |
| 74 | using ::testing::HasSubstr; |
| 75 | using ::testing::Not; |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 76 | using ::testing::UnorderedElementsAre; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 77 | using ::testing::UnorderedElementsAreArray; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 78 | |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 79 | namespace fs = std::filesystem; |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 80 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 81 | class ApexServiceTest : public ::testing::Test { |
| 82 | public: |
| 83 | ApexServiceTest() { |
| 84 | using android::IBinder; |
| 85 | using android::IServiceManager; |
| 86 | |
| 87 | sp<IServiceManager> sm = android::defaultServiceManager(); |
| 88 | sp<IBinder> binder = sm->getService(String16("apexservice")); |
| 89 | if (binder != nullptr) { |
| 90 | service_ = android::interface_cast<IApexService>(binder); |
| 91 | } |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 92 | binder = sm->getService(String16("vold")); |
| 93 | if (binder != nullptr) { |
| 94 | vold_service_ = android::interface_cast<android::os::IVold>(binder); |
| 95 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 98 | protected: |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 99 | void SetUp() override { |
| 100 | ASSERT_NE(nullptr, service_.get()); |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 101 | ASSERT_NE(nullptr, vold_service_.get()); |
| 102 | android::binder::Status status = |
| 103 | vold_service_->supportsCheckpoint(&supports_fs_checkpointing_); |
| 104 | ASSERT_TRUE(IsOk(status)); |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 105 | CleanUp(); |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 108 | void TearDown() override { CleanUp(); } |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 109 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 110 | static std::string GetTestDataDir() { |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 111 | return android::base::GetExecutableDirectory(); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 112 | } |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 113 | static std::string GetTestFile(const std::string& name) { |
| 114 | return GetTestDataDir() + "/" + name; |
| 115 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 116 | |
| 117 | static bool HaveSelinux() { return 1 == is_selinux_enabled(); } |
| 118 | |
| 119 | static bool IsSelinuxEnforced() { return 0 != security_getenforce(); } |
| 120 | |
| 121 | StatusOr<bool> IsActive(const std::string& name, int64_t version) { |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 122 | std::vector<ApexInfo> list; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 123 | android::binder::Status status = service_->getActivePackages(&list); |
| 124 | if (status.isOk()) { |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 125 | for (const ApexInfo& p : list) { |
| 126 | if (p.packageName == name && p.versionCode == version) { |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 127 | return StatusOr<bool>(true); |
| 128 | } |
| 129 | } |
| 130 | return StatusOr<bool>(false); |
| 131 | } |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 132 | return StatusOr<bool>::MakeError(status.exceptionMessage().c_str()); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 135 | StatusOr<std::vector<ApexInfo>> GetAllPackages() { |
| 136 | std::vector<ApexInfo> list; |
| 137 | android::binder::Status status = service_->getAllPackages(&list); |
| 138 | if (status.isOk()) { |
| 139 | return StatusOr<std::vector<ApexInfo>>(list); |
| 140 | } |
| 141 | |
| 142 | return StatusOr<std::vector<ApexInfo>>::MakeError( |
| 143 | status.toString8().c_str()); |
| 144 | } |
| 145 | |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 146 | StatusOr<std::vector<ApexInfo>> GetActivePackages() { |
| 147 | std::vector<ApexInfo> list; |
| 148 | android::binder::Status status = service_->getActivePackages(&list); |
| 149 | if (status.isOk()) { |
| 150 | return StatusOr<std::vector<ApexInfo>>(list); |
| 151 | } |
| 152 | |
| 153 | return StatusOr<std::vector<ApexInfo>>::MakeError( |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 154 | status.exceptionMessage().c_str()); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 157 | StatusOr<std::vector<ApexInfo>> GetInactivePackages() { |
| 158 | std::vector<ApexInfo> list; |
| 159 | android::binder::Status status = service_->getAllPackages(&list); |
| 160 | list.erase(std::remove_if( |
| 161 | list.begin(), list.end(), |
| 162 | [](const ApexInfo& apexInfo) { return apexInfo.isActive; }), |
| 163 | list.end()); |
| 164 | if (status.isOk()) { |
| 165 | return StatusOr<std::vector<ApexInfo>>(std::move(list)); |
| 166 | } |
| 167 | |
| 168 | return StatusOr<std::vector<ApexInfo>>::MakeError( |
| 169 | status.toString8().c_str()); |
| 170 | } |
| 171 | |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 172 | StatusOr<ApexInfo> GetActivePackage(const std::string& name) { |
| 173 | ApexInfo package; |
| 174 | android::binder::Status status = service_->getActivePackage(name, &package); |
| 175 | if (status.isOk()) { |
| 176 | return StatusOr<ApexInfo>(package); |
| 177 | } |
| 178 | |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 179 | return StatusOr<ApexInfo>::MakeError(status.exceptionMessage().c_str()); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 182 | std::string GetPackageString(const ApexInfo& p) { |
| 183 | return p.packageName + "@" + std::to_string(p.versionCode) + |
| 184 | " [path=" + p.packagePath + "]"; |
| 185 | } |
| 186 | |
| 187 | std::vector<std::string> GetPackagesStrings( |
| 188 | const std::vector<ApexInfo>& list) { |
| 189 | std::vector<std::string> ret; |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 190 | ret.reserve(list.size()); |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 191 | for (const ApexInfo& p : list) { |
| 192 | ret.push_back(GetPackageString(p)); |
| 193 | } |
| 194 | return ret; |
| 195 | } |
| 196 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 197 | std::vector<std::string> GetActivePackagesStrings() { |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 198 | std::vector<ApexInfo> list; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 199 | android::binder::Status status = service_->getActivePackages(&list); |
| 200 | if (status.isOk()) { |
Dario Freni | 6425511 | 2019-02-18 22:13:38 +0000 | [diff] [blame] | 201 | std::vector<std::string> ret(list.size()); |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 202 | for (const ApexInfo& p : list) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 203 | ret.push_back(GetPackageString(p)); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 204 | } |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | std::vector<std::string> error; |
| 209 | error.push_back("ERROR"); |
| 210 | return error; |
| 211 | } |
| 212 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 213 | StatusOr<std::vector<ApexInfo>> GetFactoryPackages() { |
| 214 | std::vector<ApexInfo> list; |
| 215 | android::binder::Status status = service_->getAllPackages(&list); |
| 216 | list.erase( |
| 217 | std::remove_if(list.begin(), list.end(), |
| 218 | [](ApexInfo& apexInfo) { return !apexInfo.isFactory; }), |
| 219 | list.end()); |
| 220 | if (status.isOk()) { |
| 221 | return StatusOr<std::vector<ApexInfo>>(std::move(list)); |
| 222 | } |
| 223 | |
| 224 | return StatusOr<std::vector<ApexInfo>>::MakeError( |
| 225 | status.toString8().c_str()); |
| 226 | } |
| 227 | |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 228 | static std::vector<std::string> ListDir(const std::string& path) { |
| 229 | std::vector<std::string> ret; |
Mohammad Samiul Islam | 2d56f37 | 2019-03-27 17:07:57 +0000 | [diff] [blame] | 230 | std::error_code ec; |
| 231 | if (!fs::is_directory(path, ec)) { |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 232 | return ret; |
| 233 | } |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 234 | WalkDir(path, [&](const fs::directory_entry& entry) { |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 235 | std::string tmp; |
Mohammad Samiul Islam | 2d56f37 | 2019-03-27 17:07:57 +0000 | [diff] [blame] | 236 | switch (entry.symlink_status(ec).type()) { |
| 237 | case fs::file_type::directory: |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 238 | tmp = "[dir]"; |
| 239 | break; |
Mohammad Samiul Islam | 2d56f37 | 2019-03-27 17:07:57 +0000 | [diff] [blame] | 240 | case fs::file_type::symlink: |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 241 | tmp = "[lnk]"; |
| 242 | break; |
Mohammad Samiul Islam | 2d56f37 | 2019-03-27 17:07:57 +0000 | [diff] [blame] | 243 | case fs::file_type::regular: |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 244 | tmp = "[reg]"; |
| 245 | break; |
| 246 | default: |
| 247 | tmp = "[other]"; |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 248 | } |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 249 | ret.push_back(tmp.append(entry.path().filename())); |
| 250 | }); |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 251 | std::sort(ret.begin(), ret.end()); |
| 252 | return ret; |
| 253 | } |
| 254 | |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 255 | static std::string GetLogcat() { |
| 256 | // For simplicity, log to file and read it. |
| 257 | std::string file = GetTestFile("logcat.tmp.txt"); |
| 258 | std::vector<std::string> args{ |
| 259 | "/system/bin/logcat", |
| 260 | "-d", |
| 261 | "-f", |
| 262 | file, |
| 263 | }; |
| 264 | std::string error_msg; |
| 265 | int res = ForkAndRun(args, &error_msg); |
| 266 | CHECK_EQ(0, res) << error_msg; |
| 267 | |
| 268 | std::string data; |
| 269 | CHECK(android::base::ReadFileToString(file, &data)); |
| 270 | |
| 271 | unlink(file.c_str()); |
| 272 | |
| 273 | return data; |
| 274 | } |
| 275 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 276 | struct PrepareTestApexForInstall { |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 277 | static constexpr const char* kTestDir = "/data/app-staging/apexservice_tmp"; |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 278 | |
| 279 | // This is given to the constructor. |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 280 | std::string test_input; // Original test file. |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 281 | std::string selinux_label_input; // SELinux label to apply. |
| 282 | std::string test_dir_input; |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 283 | |
| 284 | // This is derived from the input. |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 285 | std::string test_file; // Prepared path. Under test_dir_input. |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 286 | std::string test_installed_file; // Where apexd will store it. |
| 287 | |
| 288 | std::string package; // APEX package name. |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 289 | uint64_t version; // APEX version |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 290 | |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 291 | explicit PrepareTestApexForInstall( |
| 292 | const std::string& test, |
| 293 | const std::string& test_dir = std::string(kTestDir), |
Nikita Ioffe | 936a997 | 2019-02-13 02:11:21 +0000 | [diff] [blame] | 294 | const std::string& selinux_label = "staging_data_file") { |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 295 | test_input = test; |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 296 | selinux_label_input = selinux_label; |
| 297 | test_dir_input = test_dir; |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 298 | |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 299 | test_file = test_dir_input + "/" + android::base::Basename(test); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 300 | |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 301 | package = ""; // Explicitly mark as not initialized. |
| 302 | |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 303 | StatusOr<ApexFile> apex_file = ApexFile::Open(test); |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 304 | if (!apex_file.Ok()) { |
| 305 | return; |
| 306 | } |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 307 | |
| 308 | const ApexManifest& manifest = apex_file->GetManifest(); |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 309 | package = manifest.name(); |
| 310 | version = manifest.version(); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 311 | |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 312 | test_installed_file = std::string(kActiveApexPackagesDataDir) + "/" + |
| 313 | package + "@" + std::to_string(version) + ".apex"; |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 314 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 315 | |
| 316 | bool Prepare() { |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 317 | if (package.empty()) { |
| 318 | // Failure in constructor. Redo work to get error message. |
| 319 | auto fail_fn = [&]() { |
| 320 | StatusOr<ApexFile> apex_file = ApexFile::Open(test_input); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 321 | ASSERT_FALSE(IsOk(apex_file)); |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 322 | ASSERT_TRUE(apex_file.Ok()) |
| 323 | << test_input << " failed to load: " << apex_file.ErrorMessage(); |
| 324 | }; |
| 325 | fail_fn(); |
| 326 | return false; |
| 327 | } |
| 328 | |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 329 | auto prepare = [](const std::string& src, const std::string& trg, |
| 330 | const std::string& selinux_label) { |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 331 | ASSERT_EQ(0, access(src.c_str(), F_OK)) |
| 332 | << src << ": " << strerror(errno); |
| 333 | const std::string trg_dir = android::base::Dirname(trg); |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 334 | if (0 != mkdir(trg_dir.c_str(), 0777)) { |
| 335 | int saved_errno = errno; |
| 336 | ASSERT_EQ(saved_errno, EEXIST) << trg << ":" << strerror(saved_errno); |
| 337 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 338 | |
Andreas Gampe | a158e3d | 2018-12-10 13:46:30 -0800 | [diff] [blame] | 339 | // Do not use a hardlink, even though it's the simplest solution. |
| 340 | // b/119569101. |
| 341 | { |
| 342 | std::ifstream src_stream(src, std::ios::binary); |
| 343 | ASSERT_TRUE(src_stream.good()); |
| 344 | std::ofstream trg_stream(trg, std::ios::binary); |
| 345 | ASSERT_TRUE(trg_stream.good()); |
Andreas Gampe | 2f097cf | 2018-11-14 16:59:33 -0800 | [diff] [blame] | 346 | |
Andreas Gampe | a158e3d | 2018-12-10 13:46:30 -0800 | [diff] [blame] | 347 | trg_stream << src_stream.rdbuf(); |
Andreas Gampe | 2f097cf | 2018-11-14 16:59:33 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 350 | ASSERT_EQ(0, chmod(trg.c_str(), 0666)) << strerror(errno); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 351 | struct group* g = getgrnam("system"); |
| 352 | ASSERT_NE(nullptr, g); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 353 | ASSERT_EQ(0, chown(trg.c_str(), /* root uid */ 0, g->gr_gid)) |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 354 | << strerror(errno); |
Andreas Gampe | 2f097cf | 2018-11-14 16:59:33 -0800 | [diff] [blame] | 355 | |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 356 | int rc = setfilecon( |
| 357 | trg_dir.c_str(), |
| 358 | std::string("u:object_r:" + selinux_label + ":s0").c_str()); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 359 | ASSERT_TRUE(0 == rc || !HaveSelinux()) << strerror(errno); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 360 | rc = setfilecon( |
| 361 | trg.c_str(), |
| 362 | std::string("u:object_r:" + selinux_label + ":s0").c_str()); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 363 | ASSERT_TRUE(0 == rc || !HaveSelinux()) << strerror(errno); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 364 | }; |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 365 | prepare(test_input, test_file, selinux_label_input); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 366 | return !HasFatalFailure(); |
| 367 | } |
| 368 | |
| 369 | ~PrepareTestApexForInstall() { |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 370 | if (unlink(test_file.c_str()) != 0) { |
| 371 | PLOG(ERROR) << "Unable to unlink " << test_file; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 372 | } |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 373 | if (rmdir(test_dir_input.c_str()) != 0) { |
| 374 | PLOG(ERROR) << "Unable to rmdir " << test_dir_input; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 375 | } |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 376 | |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 377 | if (!package.empty()) { |
| 378 | // For cleanliness, also attempt to delete apexd's file. |
| 379 | // TODO: to the unstaging using APIs |
| 380 | if (unlink(test_installed_file.c_str()) != 0) { |
| 381 | PLOG(ERROR) << "Unable to unlink " << test_installed_file; |
| 382 | } |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 383 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 384 | } |
| 385 | }; |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 386 | |
| 387 | std::string GetDebugStr(PrepareTestApexForInstall* installer) { |
| 388 | StringLog log; |
| 389 | |
| 390 | if (installer != nullptr) { |
| 391 | log << "test_input=" << installer->test_input << " "; |
| 392 | log << "test_file=" << installer->test_file << " "; |
| 393 | log << "test_installed_file=" << installer->test_installed_file << " "; |
| 394 | log << "package=" << installer->package << " "; |
| 395 | log << "version=" << installer->version << " "; |
| 396 | } |
| 397 | |
| 398 | log << "active=[" << Join(GetActivePackagesStrings(), ',') << "] "; |
Nikita Ioffe | a8453da | 2019-01-30 21:29:13 +0000 | [diff] [blame] | 399 | log << kActiveApexPackagesDataDir << "=[" |
| 400 | << Join(ListDir(kActiveApexPackagesDataDir), ',') << "] "; |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 401 | log << kApexRoot << "=[" << Join(ListDir(kApexRoot), ',') << "]"; |
| 402 | |
| 403 | return log; |
| 404 | } |
| 405 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 406 | sp<IApexService> service_; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 407 | sp<android::os::IVold> vold_service_; |
| 408 | bool supports_fs_checkpointing_; |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 409 | |
| 410 | private: |
| 411 | void CleanUp() { |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 412 | auto status = WalkDir(kApexDataDir, [](const fs::directory_entry& p) { |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 413 | std::error_code ec; |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 414 | fs::file_status status = p.status(ec); |
| 415 | ASSERT_FALSE(ec) << "Failed to stat " << p.path() << " : " |
| 416 | << ec.message(); |
| 417 | if (fs::is_directory(status)) { |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 418 | fs::remove_all(p.path(), ec); |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 419 | } else { |
| 420 | fs::remove(p.path(), ec); |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 421 | } |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 422 | ASSERT_FALSE(ec) << "Failed to delete " << p.path() << " : " |
| 423 | << ec.message(); |
| 424 | }); |
| 425 | ASSERT_TRUE(IsOk(status)); |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 426 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 427 | }; |
| 428 | |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 429 | namespace { |
| 430 | |
| 431 | bool RegularFileExists(const std::string& path) { |
| 432 | struct stat buf; |
| 433 | if (0 != stat(path.c_str(), &buf)) { |
| 434 | return false; |
| 435 | } |
| 436 | return S_ISREG(buf.st_mode); |
| 437 | } |
| 438 | |
| 439 | } // namespace |
| 440 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 441 | TEST_F(ApexServiceTest, HaveSelinux) { |
| 442 | // We want to test under selinux. |
| 443 | EXPECT_TRUE(HaveSelinux()); |
| 444 | } |
| 445 | |
Andreas Gampe | 1b6fa37 | 2018-11-05 09:47:28 -0800 | [diff] [blame] | 446 | // Skip for b/119032200. |
| 447 | TEST_F(ApexServiceTest, DISABLED_EnforceSelinux) { |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 448 | // Crude cutout for virtual devices. |
| 449 | #if !defined(__i386__) && !defined(__x86_64__) |
| 450 | constexpr bool kIsX86 = false; |
| 451 | #else |
| 452 | constexpr bool kIsX86 = true; |
| 453 | #endif |
| 454 | EXPECT_TRUE(IsSelinuxEnforced() || kIsX86); |
| 455 | } |
| 456 | |
| 457 | TEST_F(ApexServiceTest, StageFailAccess) { |
| 458 | if (!IsSelinuxEnforced()) { |
| 459 | LOG(WARNING) << "Skipping InstallFailAccess because of selinux"; |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | // Use an extra copy, so that even if this test fails (incorrectly installs), |
| 464 | // we have the testdata file still around. |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 465 | std::string orig_test_file = GetTestFile("apex.apexd_test.apex"); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 466 | std::string test_file = orig_test_file + ".2"; |
| 467 | ASSERT_EQ(0, link(orig_test_file.c_str(), test_file.c_str())) |
| 468 | << strerror(errno); |
| 469 | struct Deleter { |
| 470 | std::string to_delete; |
Chih-Hung Hsieh | ab73a44 | 2019-01-02 11:28:05 -0800 | [diff] [blame] | 471 | explicit Deleter(const std::string& t) : to_delete(t) {} |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 472 | ~Deleter() { |
| 473 | if (unlink(to_delete.c_str()) != 0) { |
| 474 | PLOG(ERROR) << "Could not unlink " << to_delete; |
| 475 | } |
| 476 | } |
| 477 | }; |
| 478 | Deleter del(test_file); |
| 479 | |
| 480 | bool success; |
| 481 | android::binder::Status st = service_->stagePackage(test_file, &success); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 482 | ASSERT_FALSE(IsOk(st)); |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 483 | std::string error = st.exceptionMessage().c_str(); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 484 | EXPECT_NE(std::string::npos, error.find("Failed to open package")) << error; |
| 485 | EXPECT_NE(std::string::npos, error.find("I/O error")) << error; |
| 486 | } |
| 487 | |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 488 | // TODO(jiyong): re-enable this test. This test is disabled because the build |
| 489 | // system now always bundles the public key that was used to sign the APEX. |
| 490 | // In debuggable build, the bundled public key is used as the last fallback. |
| 491 | // As a result, the verification is always successful (and thus test fails). |
| 492 | // In order to re-enable this test, we have to manually create an APEX |
| 493 | // where public key is not bundled. |
| 494 | #if 0 |
Andreas Gampe | cfb1ffe | 2018-12-26 15:23:53 -0800 | [diff] [blame] | 495 | TEST_F(ApexServiceTest, StageFailKey) { |
| 496 | PrepareTestApexForInstall installer( |
| 497 | GetTestFile("apex.apexd_test_no_inst_key.apex")); |
| 498 | if (!installer.Prepare()) { |
| 499 | return; |
| 500 | } |
| 501 | ASSERT_EQ(std::string("com.android.apex.test_package.no_inst_key"), |
| 502 | installer.package); |
| 503 | |
| 504 | bool success; |
| 505 | android::binder::Status st = |
| 506 | service_->stagePackage(installer.test_file, &success); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 507 | ASSERT_FALSE(IsOk(st)); |
Jiyong Park | 997fcbf | 2018-12-27 16:07:49 +0900 | [diff] [blame] | 508 | |
| 509 | // May contain one of two errors. |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 510 | std::string error = st.exceptionMessage().c_str(); |
Jiyong Park | 997fcbf | 2018-12-27 16:07:49 +0900 | [diff] [blame] | 511 | |
Andreas Gampe | cfb1ffe | 2018-12-26 15:23:53 -0800 | [diff] [blame] | 512 | constexpr const char* kExpectedError1 = "Failed to get realpath of "; |
Jiyong Park | 997fcbf | 2018-12-27 16:07:49 +0900 | [diff] [blame] | 513 | const size_t pos1 = error.find(kExpectedError1); |
Andreas Gampe | cfb1ffe | 2018-12-26 15:23:53 -0800 | [diff] [blame] | 514 | constexpr const char* kExpectedError2 = |
| 515 | "/etc/security/apex/com.android.apex.test_package.no_inst_key"; |
Jiyong Park | 997fcbf | 2018-12-27 16:07:49 +0900 | [diff] [blame] | 516 | const size_t pos2 = error.find(kExpectedError2); |
| 517 | |
| 518 | constexpr const char* kExpectedError3 = |
| 519 | "Error verifying " |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 520 | "/data/app-staging/apexservice_tmp/apex.apexd_test_no_inst_key.apex: " |
Jiyong Park | 997fcbf | 2018-12-27 16:07:49 +0900 | [diff] [blame] | 521 | "couldn't verify public key: Failed to compare the bundled public key " |
| 522 | "with key"; |
| 523 | const size_t pos3 = error.find(kExpectedError3); |
| 524 | |
| 525 | const size_t npos = std::string::npos; |
| 526 | EXPECT_TRUE((pos1 != npos && pos2 != npos) || pos3 != npos) << error; |
Andreas Gampe | cfb1ffe | 2018-12-26 15:23:53 -0800 | [diff] [blame] | 527 | } |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 528 | #endif |
Andreas Gampe | cfb1ffe | 2018-12-26 15:23:53 -0800 | [diff] [blame] | 529 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 530 | TEST_F(ApexServiceTest, StageSuccess) { |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 531 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex")); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 532 | if (!installer.Prepare()) { |
| 533 | return; |
| 534 | } |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 535 | ASSERT_EQ(std::string("com.android.apex.test_package"), installer.package); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 536 | |
| 537 | bool success; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 538 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &success))); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 539 | ASSERT_TRUE(success); |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 540 | EXPECT_TRUE(RegularFileExists(installer.test_installed_file)); |
| 541 | } |
| 542 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 543 | TEST_F(ApexServiceTest, |
| 544 | SubmitStagegSessionSuccessDoesNotLeakTempVerityDevices) { |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 545 | using android::dm::DeviceMapper; |
| 546 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 547 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
| 548 | "/data/app-staging/session_1543", |
| 549 | "staging_data_file"); |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 550 | if (!installer.Prepare()) { |
| 551 | return; |
| 552 | } |
| 553 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 554 | ApexInfoList list; |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 555 | bool success; |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 556 | ASSERT_TRUE(IsOk(service_->submitStagedSession(1543, {}, &list, &success))); |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 557 | ASSERT_TRUE(success); |
| 558 | |
| 559 | std::vector<DeviceMapper::DmBlockDevice> devices; |
| 560 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 561 | ASSERT_TRUE(dm.GetAvailableDevices(&devices)); |
| 562 | |
| 563 | for (const auto& device : devices) { |
| 564 | ASSERT_THAT(device.name(), Not(EndsWith(".tmp"))); |
| 565 | } |
| 566 | } |
| 567 | |
Gavin Corkery | a41373a | 2019-09-26 12:53:45 +0100 | [diff] [blame] | 568 | TEST_F(ApexServiceTest, SubmitStagedSessionStoresBuildFingerprint) { |
| 569 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
| 570 | "/data/app-staging/session_1547", |
| 571 | "staging_data_file"); |
| 572 | if (!installer.Prepare()) { |
| 573 | return; |
| 574 | } |
| 575 | ApexInfoList list; |
| 576 | bool success; |
| 577 | ASSERT_TRUE(IsOk(service_->submitStagedSession(1547, {}, &list, &success))); |
| 578 | |
| 579 | auto session = ApexSession::GetSession(1547); |
| 580 | ASSERT_FALSE(session->GetBuildFingerprint().empty()); |
| 581 | } |
| 582 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 583 | TEST_F(ApexServiceTest, SubmitStagedSessionFailDoesNotLeakTempVerityDevices) { |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 584 | using android::dm::DeviceMapper; |
| 585 | |
| 586 | PrepareTestApexForInstall installer( |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 587 | GetTestFile("apex.apexd_test_manifest_mismatch.apex"), |
| 588 | "/data/app-staging/session_239", "staging_data_file"); |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 589 | if (!installer.Prepare()) { |
| 590 | return; |
| 591 | } |
| 592 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 593 | ApexInfoList list; |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 594 | bool success; |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 595 | ASSERT_TRUE(IsOk(service_->submitStagedSession(239, {}, &list, &success))); |
| 596 | ASSERT_FALSE(success); |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 597 | |
| 598 | std::vector<DeviceMapper::DmBlockDevice> devices; |
| 599 | DeviceMapper& dm = DeviceMapper::Instance(); |
| 600 | ASSERT_TRUE(dm.GetAvailableDevices(&devices)); |
| 601 | |
| 602 | for (const auto& device : devices) { |
| 603 | ASSERT_THAT(device.name(), Not(EndsWith(".tmp"))); |
| 604 | } |
| 605 | } |
| 606 | |
Nikita Ioffe | 6a280af | 2019-02-04 15:28:57 +0000 | [diff] [blame] | 607 | TEST_F(ApexServiceTest, StageSuccess_ClearsPreviouslyActivePackage) { |
| 608 | PrepareTestApexForInstall installer1(GetTestFile("apex.apexd_test_v2.apex")); |
| 609 | PrepareTestApexForInstall installer2( |
| 610 | GetTestFile("apex.apexd_test_different_app.apex")); |
| 611 | PrepareTestApexForInstall installer3(GetTestFile("apex.apexd_test.apex")); |
| 612 | auto install_fn = [&](PrepareTestApexForInstall& installer) { |
| 613 | if (!installer.Prepare()) { |
| 614 | return; |
| 615 | } |
| 616 | bool success; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 617 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &success))); |
Nikita Ioffe | 6a280af | 2019-02-04 15:28:57 +0000 | [diff] [blame] | 618 | ASSERT_TRUE(success); |
| 619 | EXPECT_TRUE(RegularFileExists(installer.test_installed_file)); |
| 620 | }; |
| 621 | install_fn(installer1); |
| 622 | install_fn(installer2); |
| 623 | // Simulating a rollback. After this call test_v2_apex_path should be removed. |
| 624 | install_fn(installer3); |
| 625 | |
| 626 | EXPECT_FALSE(RegularFileExists(installer1.test_installed_file)); |
| 627 | EXPECT_TRUE(RegularFileExists(installer2.test_installed_file)); |
| 628 | EXPECT_TRUE(RegularFileExists(installer3.test_installed_file)); |
| 629 | } |
| 630 | |
Nikita Ioffe | a836576 | 2019-03-18 23:59:03 +0000 | [diff] [blame] | 631 | TEST_F(ApexServiceTest, StageAlreadyStagedPackageSuccess) { |
Nikita Ioffe | 8be420a | 2019-02-13 20:51:05 +0000 | [diff] [blame] | 632 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex")); |
| 633 | if (!installer.Prepare()) { |
| 634 | return; |
| 635 | } |
| 636 | ASSERT_EQ(std::string("com.android.apex.test_package"), installer.package); |
| 637 | |
| 638 | bool success = false; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 639 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &success))); |
Nikita Ioffe | 8be420a | 2019-02-13 20:51:05 +0000 | [diff] [blame] | 640 | ASSERT_TRUE(success); |
| 641 | ASSERT_TRUE(RegularFileExists(installer.test_installed_file)); |
| 642 | |
| 643 | success = false; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 644 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &success))); |
Nikita Ioffe | 8be420a | 2019-02-13 20:51:05 +0000 | [diff] [blame] | 645 | ASSERT_TRUE(success); |
| 646 | ASSERT_TRUE(RegularFileExists(installer.test_installed_file)); |
| 647 | } |
| 648 | |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 649 | TEST_F(ApexServiceTest, MultiStageSuccess) { |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 650 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex")); |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 651 | if (!installer.Prepare()) { |
| 652 | return; |
| 653 | } |
| 654 | ASSERT_EQ(std::string("com.android.apex.test_package"), installer.package); |
| 655 | |
| 656 | // TODO: Add second test. Right now, just use a separate version. |
Andreas Gampe | 0562bc5 | 2018-12-12 15:03:58 -0800 | [diff] [blame] | 657 | PrepareTestApexForInstall installer2(GetTestFile("apex.apexd_test_v2.apex")); |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 658 | if (!installer2.Prepare()) { |
| 659 | return; |
| 660 | } |
| 661 | ASSERT_EQ(std::string("com.android.apex.test_package"), installer2.package); |
| 662 | |
| 663 | std::vector<std::string> packages; |
| 664 | packages.push_back(installer.test_file); |
| 665 | packages.push_back(installer2.test_file); |
| 666 | |
| 667 | bool success; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 668 | ASSERT_TRUE(IsOk(service_->stagePackages(packages, &success))); |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 669 | ASSERT_TRUE(success); |
| 670 | EXPECT_TRUE(RegularFileExists(installer.test_installed_file)); |
| 671 | EXPECT_TRUE(RegularFileExists(installer2.test_installed_file)); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 674 | template <typename NameProvider> |
| 675 | class ApexServiceActivationTest : public ApexServiceTest { |
| 676 | public: |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 677 | ApexServiceActivationTest() : stage_package(true) {} |
| 678 | |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 679 | explicit ApexServiceActivationTest(bool stage_package) |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 680 | : stage_package(stage_package) {} |
| 681 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 682 | void SetUp() override { |
| 683 | ApexServiceTest::SetUp(); |
| 684 | ASSERT_NE(nullptr, service_.get()); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 685 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 686 | installer_ = std::make_unique<PrepareTestApexForInstall>( |
| 687 | GetTestFile(NameProvider::GetTestName())); |
| 688 | if (!installer_->Prepare()) { |
| 689 | return; |
| 690 | } |
| 691 | ASSERT_EQ(NameProvider::GetPackageName(), installer_->package); |
| 692 | |
| 693 | { |
| 694 | // Check package is not active. |
| 695 | StatusOr<bool> active = |
| 696 | IsActive(installer_->package, installer_->version); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 697 | ASSERT_TRUE(IsOk(active)); |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 698 | ASSERT_FALSE(*active); |
| 699 | } |
| 700 | |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 701 | if (stage_package) { |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 702 | bool success; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 703 | ASSERT_TRUE( |
| 704 | IsOk(service_->stagePackage(installer_->test_file, &success))); |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 705 | ASSERT_TRUE(success); |
| 706 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 709 | void TearDown() override { |
| 710 | // Attempt to deactivate. |
| 711 | if (installer_ != nullptr) { |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 712 | if (stage_package) { |
| 713 | service_->deactivatePackage(installer_->test_installed_file); |
| 714 | } else { |
| 715 | service_->deactivatePackage(installer_->test_file); |
| 716 | } |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | installer_.reset(); |
Nikita Ioffe | 816a02b | 2019-02-16 16:49:45 +0000 | [diff] [blame] | 720 | // ApexServiceTest::TearDown will wipe out everything under /data/apex. |
| 721 | // Since some of that information is required for deactivePackage binder |
| 722 | // call, it's required to be called after deactivating package. |
| 723 | ApexServiceTest::TearDown(); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 726 | std::unique_ptr<PrepareTestApexForInstall> installer_; |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 727 | |
| 728 | private: |
| 729 | bool stage_package; |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | struct SuccessNameProvider { |
| 733 | static std::string GetTestName() { return "apex.apexd_test.apex"; } |
| 734 | static std::string GetPackageName() { |
| 735 | return "com.android.apex.test_package"; |
| 736 | } |
| 737 | }; |
| 738 | |
Martijn Coenen | 329f112 | 2019-02-28 16:10:08 +0100 | [diff] [blame] | 739 | struct ManifestMismatchNameProvider { |
| 740 | static std::string GetTestName() { |
| 741 | return "apex.apexd_test_manifest_mismatch.apex"; |
| 742 | } |
| 743 | static std::string GetPackageName() { |
| 744 | return "com.android.apex.test_package"; |
| 745 | } |
| 746 | }; |
| 747 | |
| 748 | class ApexServiceActivationManifestMismatchFailure |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 749 | : public ApexServiceActivationTest<ManifestMismatchNameProvider> { |
| 750 | public: |
| 751 | ApexServiceActivationManifestMismatchFailure() |
| 752 | : ApexServiceActivationTest(false) {} |
| 753 | }; |
Martijn Coenen | 329f112 | 2019-02-28 16:10:08 +0100 | [diff] [blame] | 754 | |
| 755 | TEST_F(ApexServiceActivationManifestMismatchFailure, |
| 756 | ActivateFailsWithManifestMismatch) { |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 757 | android::binder::Status st = service_->activatePackage(installer_->test_file); |
Martijn Coenen | 329f112 | 2019-02-28 16:10:08 +0100 | [diff] [blame] | 758 | ASSERT_FALSE(IsOk(st)); |
| 759 | |
Nikita Ioffe | c553626 | 2019-03-29 17:01:58 +0000 | [diff] [blame] | 760 | std::string error = st.exceptionMessage().c_str(); |
Nikita Ioffe | 891723c | 2019-03-25 14:35:39 +0000 | [diff] [blame] | 761 | ASSERT_THAT( |
| 762 | error, |
| 763 | HasSubstr( |
| 764 | "Manifest inside filesystem does not match manifest outside it")); |
Martijn Coenen | 329f112 | 2019-02-28 16:10:08 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 767 | class ApexServiceActivationSuccessTest |
| 768 | : public ApexServiceActivationTest<SuccessNameProvider> {}; |
| 769 | |
| 770 | TEST_F(ApexServiceActivationSuccessTest, Activate) { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 771 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 772 | << GetDebugStr(installer_.get()); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 773 | |
| 774 | { |
| 775 | // Check package is active. |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 776 | StatusOr<bool> active = IsActive(installer_->package, installer_->version); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 777 | ASSERT_TRUE(IsOk(active)); |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 778 | ASSERT_TRUE(*active) << Join(GetActivePackagesStrings(), ','); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | { |
| 782 | // Check that the "latest" view exists. |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 783 | std::string latest_path = |
| 784 | std::string(kApexRoot) + "/" + installer_->package; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 785 | struct stat buf; |
| 786 | ASSERT_EQ(0, stat(latest_path.c_str(), &buf)) << strerror(errno); |
| 787 | // Check that it is a folder. |
| 788 | EXPECT_TRUE(S_ISDIR(buf.st_mode)); |
| 789 | |
| 790 | // Collect direct entries of a folder. |
| 791 | auto collect_entries_fn = [](const std::string& path) { |
| 792 | std::vector<std::string> ret; |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 793 | WalkDir(path, [&](const fs::directory_entry& entry) { |
Mohammad Samiul Islam | 2d56f37 | 2019-03-27 17:07:57 +0000 | [diff] [blame] | 794 | if (!entry.is_directory()) { |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 795 | return; |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 796 | } |
Jooyung Han | ac9fea6 | 2019-05-08 12:16:24 +0900 | [diff] [blame] | 797 | ret.emplace_back(entry.path().filename()); |
| 798 | }); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 799 | std::sort(ret.begin(), ret.end()); |
| 800 | return ret; |
| 801 | }; |
| 802 | |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 803 | std::string versioned_path = std::string(kApexRoot) + "/" + |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 804 | installer_->package + "@" + |
| 805 | std::to_string(installer_->version); |
Andreas Gampe | c45bf32 | 2018-12-06 13:57:45 -0800 | [diff] [blame] | 806 | std::vector<std::string> versioned_folder_entries = |
| 807 | collect_entries_fn(versioned_path); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 808 | std::vector<std::string> latest_folder_entries = |
| 809 | collect_entries_fn(latest_path); |
| 810 | |
| 811 | EXPECT_TRUE(versioned_folder_entries == latest_folder_entries) |
Andreas Gampe | 7f4ff35 | 2018-12-11 08:58:03 -0800 | [diff] [blame] | 812 | << "Versioned: " << Join(versioned_folder_entries, ',') |
| 813 | << " Latest: " << Join(latest_folder_entries, ','); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 814 | } |
Andreas Gampe | a2bbbbe | 2018-12-21 15:05:48 -0800 | [diff] [blame] | 815 | } |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 816 | |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 817 | TEST_F(ApexServiceActivationSuccessTest, GetActivePackages) { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 818 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 819 | << GetDebugStr(installer_.get()); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 820 | |
| 821 | StatusOr<std::vector<ApexInfo>> active = GetActivePackages(); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 822 | ASSERT_TRUE(IsOk(active)); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 823 | ApexInfo match; |
| 824 | |
Dario Freni | 6425511 | 2019-02-18 22:13:38 +0000 | [diff] [blame] | 825 | for (const ApexInfo& info : *active) { |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 826 | if (info.packageName == installer_->package) { |
| 827 | match = info; |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | ASSERT_EQ(installer_->package, match.packageName); |
| 833 | ASSERT_EQ(installer_->version, static_cast<uint64_t>(match.versionCode)); |
| 834 | ASSERT_EQ(installer_->test_installed_file, match.packagePath); |
| 835 | } |
| 836 | |
| 837 | TEST_F(ApexServiceActivationSuccessTest, GetActivePackage) { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 838 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 839 | << GetDebugStr(installer_.get()); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 840 | |
| 841 | StatusOr<ApexInfo> active = GetActivePackage(installer_->package); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 842 | ASSERT_TRUE(IsOk(active)); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 843 | |
| 844 | ASSERT_EQ(installer_->package, active->packageName); |
| 845 | ASSERT_EQ(installer_->version, static_cast<uint64_t>(active->versionCode)); |
| 846 | ASSERT_EQ(installer_->test_installed_file, active->packagePath); |
| 847 | } |
| 848 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 849 | TEST_F(ApexServiceTest, GetFactoryPackages) { |
| 850 | using ::android::base::StartsWith; |
| 851 | StatusOr<std::vector<ApexInfo>> factoryPackages = GetFactoryPackages(); |
| 852 | ASSERT_TRUE(IsOk(factoryPackages)); |
| 853 | ASSERT_TRUE(factoryPackages->size() > 0); |
| 854 | |
| 855 | for (const ApexInfo& package : *factoryPackages) { |
Nikita Ioffe | e44c58a | 2019-04-29 14:07:11 +0100 | [diff] [blame] | 856 | ASSERT_TRUE(isPathForBuiltinApexes(package.packagePath)); |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | TEST_F(ApexServiceTest, NoPackagesAreBothActiveAndInactive) { |
| 861 | StatusOr<std::vector<ApexInfo>> activePackages = GetActivePackages(); |
| 862 | ASSERT_TRUE(IsOk(activePackages)); |
| 863 | ASSERT_TRUE(activePackages->size() > 0); |
| 864 | StatusOr<std::vector<ApexInfo>> inactivePackages = GetInactivePackages(); |
| 865 | ASSERT_TRUE(IsOk(inactivePackages)); |
| 866 | std::vector<std::string> activePackagesStrings = |
| 867 | GetPackagesStrings(*activePackages); |
| 868 | std::vector<std::string> inactivePackagesStrings = |
| 869 | GetPackagesStrings(*inactivePackages); |
| 870 | std::sort(activePackagesStrings.begin(), activePackagesStrings.end()); |
| 871 | std::sort(inactivePackagesStrings.begin(), inactivePackagesStrings.end()); |
| 872 | std::vector<std::string> intersection; |
| 873 | std::set_intersection( |
| 874 | activePackagesStrings.begin(), activePackagesStrings.end(), |
| 875 | inactivePackagesStrings.begin(), inactivePackagesStrings.end(), |
| 876 | std::back_inserter(intersection)); |
| 877 | ASSERT_EQ(intersection.size(), 0UL); |
| 878 | } |
| 879 | |
| 880 | TEST_F(ApexServiceTest, GetAllPackages) { |
| 881 | StatusOr<std::vector<ApexInfo>> allPackages = GetAllPackages(); |
| 882 | ASSERT_TRUE(IsOk(allPackages)); |
| 883 | ASSERT_TRUE(allPackages->size() > 0); |
| 884 | StatusOr<std::vector<ApexInfo>> activePackages = GetActivePackages(); |
| 885 | std::vector<std::string> activeStrings = GetPackagesStrings(*activePackages); |
| 886 | StatusOr<std::vector<ApexInfo>> factoryPackages = GetFactoryPackages(); |
| 887 | std::vector<std::string> factoryStrings = |
| 888 | GetPackagesStrings(*factoryPackages); |
| 889 | for (ApexInfo& apexInfo : *allPackages) { |
| 890 | std::string packageString = GetPackageString(apexInfo); |
| 891 | bool shouldBeActive = std::find(activeStrings.begin(), activeStrings.end(), |
| 892 | packageString) != activeStrings.end(); |
| 893 | bool shouldBeFactory = |
| 894 | std::find(factoryStrings.begin(), factoryStrings.end(), |
| 895 | packageString) != factoryStrings.end(); |
| 896 | ASSERT_EQ(shouldBeActive, apexInfo.isActive); |
| 897 | ASSERT_EQ(shouldBeFactory, apexInfo.isFactory); |
| 898 | } |
| 899 | } |
| 900 | |
Nikita Ioffe | a836576 | 2019-03-18 23:59:03 +0000 | [diff] [blame] | 901 | TEST_F(ApexServiceActivationSuccessTest, StageAlreadyActivePackageSameVersion) { |
| 902 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 903 | << GetDebugStr(installer_.get()); |
| 904 | |
| 905 | bool success = false; |
| 906 | ASSERT_TRUE(IsOk(service_->stagePackage(installer_->test_file, &success))); |
| 907 | ASSERT_TRUE(success); |
| 908 | } |
| 909 | |
Andreas Gampe | da5f506 | 2019-03-26 12:19:13 -0700 | [diff] [blame] | 910 | class ApexServiceDeactivationTest : public ApexServiceActivationSuccessTest { |
| 911 | public: |
| 912 | void SetUp() override { |
| 913 | ApexServiceActivationSuccessTest::SetUp(); |
| 914 | |
| 915 | ASSERT_TRUE(installer_ != nullptr); |
| 916 | } |
| 917 | |
| 918 | void TearDown() override { |
| 919 | installer_.reset(); |
| 920 | ApexServiceActivationSuccessTest::TearDown(); |
| 921 | } |
| 922 | |
| 923 | std::unique_ptr<PrepareTestApexForInstall> installer_; |
| 924 | }; |
| 925 | |
Nikita Ioffe | bfebf65 | 2019-04-05 22:00:00 +0100 | [diff] [blame] | 926 | TEST_F(ApexServiceActivationSuccessTest, DmDeviceTearDown) { |
Andreas Gampe | da5f506 | 2019-03-26 12:19:13 -0700 | [diff] [blame] | 927 | std::string package_id = |
| 928 | installer_->package + "@" + std::to_string(installer_->version); |
| 929 | |
| 930 | auto find_fn = [](const std::string& name) { |
| 931 | auto& dm = dm::DeviceMapper::Instance(); |
| 932 | std::vector<dm::DeviceMapper::DmBlockDevice> devices; |
| 933 | if (!dm.GetAvailableDevices(&devices)) { |
| 934 | return StatusOr<bool>::Fail("GetAvailableDevices failed"); |
| 935 | } |
| 936 | for (const auto& device : devices) { |
| 937 | if (device.name() == name) { |
| 938 | return StatusOr<bool>(true); |
| 939 | } |
| 940 | } |
| 941 | return StatusOr<bool>(false); |
| 942 | }; |
| 943 | |
| 944 | #define ASSERT_FIND(type) \ |
| 945 | { \ |
| 946 | StatusOr<bool> res = find_fn(package_id); \ |
| 947 | ASSERT_TRUE(res.Ok()); \ |
| 948 | ASSERT_##type(*res); \ |
| 949 | } |
| 950 | |
| 951 | ASSERT_FIND(FALSE); |
| 952 | |
| 953 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 954 | << GetDebugStr(installer_.get()); |
| 955 | |
| 956 | ASSERT_FIND(TRUE); |
| 957 | |
| 958 | ASSERT_TRUE( |
| 959 | IsOk(service_->deactivatePackage(installer_->test_installed_file))); |
| 960 | |
| 961 | ASSERT_FIND(FALSE); |
| 962 | |
| 963 | installer_.reset(); // Skip TearDown deactivatePackage. |
| 964 | } |
| 965 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 966 | class ApexServicePrePostInstallTest : public ApexServiceTest { |
| 967 | public: |
| 968 | template <typename Fn> |
| 969 | void RunPrePost(Fn fn, const std::vector<std::string>& apex_names, |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 970 | const char* test_message, bool expect_success = true) { |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 971 | // Using unique_ptr is just the easiest here. |
| 972 | using InstallerUPtr = std::unique_ptr<PrepareTestApexForInstall>; |
| 973 | std::vector<InstallerUPtr> installers; |
| 974 | std::vector<std::string> pkgs; |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 975 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 976 | for (const std::string& apex_name : apex_names) { |
| 977 | InstallerUPtr installer( |
| 978 | new PrepareTestApexForInstall(GetTestFile(apex_name))); |
| 979 | if (!installer->Prepare()) { |
| 980 | return; |
| 981 | } |
| 982 | pkgs.push_back(installer->test_file); |
| 983 | installers.emplace_back(std::move(installer)); |
| 984 | } |
| 985 | android::binder::Status st = (service_.get()->*fn)(pkgs); |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 986 | if (expect_success) { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 987 | ASSERT_TRUE(IsOk(st)); |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 988 | } else { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 989 | ASSERT_FALSE(IsOk(st)); |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 990 | } |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 991 | |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 992 | if (test_message != nullptr) { |
| 993 | std::string logcat = GetLogcat(); |
| 994 | EXPECT_NE(std::string::npos, logcat.find(test_message)) << logcat; |
| 995 | } |
Andreas Gampe | e44b579 | 2018-12-13 15:48:45 -0800 | [diff] [blame] | 996 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 997 | // Ensure that the package is neither active nor mounted. |
| 998 | for (const InstallerUPtr& installer : installers) { |
| 999 | StatusOr<bool> active = IsActive(installer->package, installer->version); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1000 | ASSERT_TRUE(IsOk(active)); |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1001 | EXPECT_FALSE(*active); |
| 1002 | } |
| 1003 | for (const InstallerUPtr& installer : installers) { |
| 1004 | StatusOr<ApexFile> apex = ApexFile::Open(installer->test_input); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1005 | ASSERT_TRUE(IsOk(apex)); |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1006 | std::string path = |
| 1007 | apexd_private::GetPackageMountPoint(apex->GetManifest()); |
| 1008 | std::string entry = std::string("[dir]").append(path); |
| 1009 | std::vector<std::string> slash_apex = ListDir(kApexRoot); |
| 1010 | auto it = std::find(slash_apex.begin(), slash_apex.end(), entry); |
| 1011 | EXPECT_TRUE(it == slash_apex.end()) << Join(slash_apex, ','); |
| 1012 | } |
Andreas Gampe | e44b579 | 2018-12-13 15:48:45 -0800 | [diff] [blame] | 1013 | } |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1014 | }; |
| 1015 | |
| 1016 | TEST_F(ApexServicePrePostInstallTest, Preinstall) { |
| 1017 | RunPrePost(&IApexService::preinstallPackages, |
| 1018 | {"apex.apexd_test_preinstall.apex"}, "sh : PreInstall Test"); |
Andreas Gampe | e44b579 | 2018-12-13 15:48:45 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1021 | TEST_F(ApexServicePrePostInstallTest, MultiPreinstall) { |
| 1022 | constexpr const char* kLogcatText = |
Andreas Gampe | e44b579 | 2018-12-13 15:48:45 -0800 | [diff] [blame] | 1023 | "sh : /apex/com.android.apex.test_package/etc/sample_prebuilt_file"; |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1024 | RunPrePost(&IApexService::preinstallPackages, |
| 1025 | {"apex.apexd_test_preinstall.apex", "apex.apexd_test.apex"}, |
| 1026 | kLogcatText); |
| 1027 | } |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 1028 | |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 1029 | TEST_F(ApexServicePrePostInstallTest, PreinstallFail) { |
| 1030 | RunPrePost(&IApexService::preinstallPackages, |
| 1031 | {"apex.apexd_test_prepostinstall.fail.apex"}, |
| 1032 | /* test_message= */ nullptr, /* expect_success= */ false); |
| 1033 | } |
| 1034 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 1035 | TEST_F(ApexServicePrePostInstallTest, Postinstall) { |
| 1036 | RunPrePost(&IApexService::postinstallPackages, |
| 1037 | {"apex.apexd_test_postinstall.apex"}, |
| 1038 | "sh : PostInstall Test"); |
| 1039 | } |
| 1040 | |
| 1041 | TEST_F(ApexServicePrePostInstallTest, MultiPostinstall) { |
| 1042 | constexpr const char* kLogcatText = |
| 1043 | "sh : /apex/com.android.apex.test_package/etc/sample_prebuilt_file"; |
| 1044 | RunPrePost(&IApexService::postinstallPackages, |
| 1045 | {"apex.apexd_test_postinstall.apex", "apex.apexd_test.apex"}, |
| 1046 | kLogcatText); |
Andreas Gampe | e1a4039 | 2018-11-30 09:47:17 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Andreas Gampe | 1144b18 | 2019-02-04 15:28:12 -0800 | [diff] [blame] | 1049 | TEST_F(ApexServicePrePostInstallTest, PostinstallFail) { |
| 1050 | RunPrePost(&IApexService::postinstallPackages, |
| 1051 | {"apex.apexd_test_prepostinstall.fail.apex"}, |
| 1052 | /* test_message= */ nullptr, /* expect_success= */ false); |
| 1053 | } |
| 1054 | |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 1055 | TEST_F(ApexServiceTest, SubmitSingleSessionTestSuccess) { |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1056 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1057 | "/data/app-staging/session_123", |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1058 | "staging_data_file"); |
| 1059 | if (!installer.Prepare()) { |
| 1060 | FAIL() << GetDebugStr(&installer); |
| 1061 | } |
| 1062 | |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 1063 | ApexInfoList list; |
| 1064 | bool ret_value; |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 1065 | std::vector<int> empty_child_session_ids; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1066 | ASSERT_TRUE(IsOk(service_->submitStagedSession(123, empty_child_session_ids, |
| 1067 | &list, &ret_value))) |
| 1068 | << GetDebugStr(&installer); |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 1069 | EXPECT_TRUE(ret_value); |
| 1070 | EXPECT_EQ(1u, list.apexInfos.size()); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1071 | ApexInfo match; |
Dario Freni | 6425511 | 2019-02-18 22:13:38 +0000 | [diff] [blame] | 1072 | for (const ApexInfo& info : list.apexInfos) { |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1073 | if (info.packageName == installer.package) { |
| 1074 | match = info; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | ASSERT_EQ(installer.package, match.packageName); |
| 1080 | ASSERT_EQ(installer.version, static_cast<uint64_t>(match.versionCode)); |
| 1081 | ASSERT_EQ(installer.test_file, match.packagePath); |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 1082 | |
| 1083 | ApexSessionInfo session; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1084 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(123, &session))) |
| 1085 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1086 | ApexSessionInfo expected = CreateSessionInfo(123); |
| 1087 | expected.isVerified = true; |
| 1088 | EXPECT_THAT(session, SessionInfoEq(expected)); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1089 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1090 | ASSERT_TRUE(IsOk(service_->markStagedSessionReady(123, &ret_value))) |
| 1091 | << GetDebugStr(&installer); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1092 | ASSERT_TRUE(ret_value); |
| 1093 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1094 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(123, &session))) |
| 1095 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1096 | expected.isVerified = false; |
| 1097 | expected.isStaged = true; |
| 1098 | EXPECT_THAT(session, SessionInfoEq(expected)); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Call markStagedSessionReady again. Should be a no-op. |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1101 | ASSERT_TRUE(IsOk(service_->markStagedSessionReady(123, &ret_value))) |
| 1102 | << GetDebugStr(&installer); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1103 | ASSERT_TRUE(ret_value); |
| 1104 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1105 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(123, &session))) |
| 1106 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1107 | EXPECT_THAT(session, SessionInfoEq(expected)); |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 1108 | |
| 1109 | // See if the session is reported with getSessions() as well |
| 1110 | std::vector<ApexSessionInfo> sessions; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1111 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))) |
| 1112 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1113 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(expected))); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Nikita Ioffe | 9bd28b9 | 2019-03-04 22:18:07 +0000 | [diff] [blame] | 1116 | TEST_F(ApexServiceTest, SubmitSingleStagedSessionDeletesPreviousSessions) { |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1117 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1118 | "/data/app-staging/session_239", |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1119 | "staging_data_file"); |
| 1120 | if (!installer.Prepare()) { |
| 1121 | FAIL() << GetDebugStr(&installer); |
| 1122 | } |
| 1123 | |
| 1124 | // First simulate existence of a bunch of sessions. |
| 1125 | auto session1 = ApexSession::CreateSession(37); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1126 | ASSERT_TRUE(IsOk(session1)); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1127 | auto session2 = ApexSession::CreateSession(57); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1128 | ASSERT_TRUE(IsOk(session2)); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1129 | auto session3 = ApexSession::CreateSession(73); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1130 | ASSERT_TRUE(IsOk(session3)); |
| 1131 | ASSERT_TRUE(IsOk(session1->UpdateStateAndCommit(SessionState::VERIFIED))); |
| 1132 | ASSERT_TRUE(IsOk(session2->UpdateStateAndCommit(SessionState::STAGED))); |
Nikita Ioffe | 463d4e8 | 2019-02-10 18:46:20 +0000 | [diff] [blame] | 1133 | ASSERT_TRUE(IsOk(session3->UpdateStateAndCommit(SessionState::SUCCESS))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1134 | |
| 1135 | std::vector<ApexSessionInfo> sessions; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1136 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1137 | |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1138 | ApexSessionInfo expected_session1 = CreateSessionInfo(37); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1139 | expected_session1.isVerified = true; |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1140 | ApexSessionInfo expected_session2 = CreateSessionInfo(57); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1141 | expected_session2.isStaged = true; |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1142 | ApexSessionInfo expected_session3 = CreateSessionInfo(73); |
Nikita Ioffe | 463d4e8 | 2019-02-10 18:46:20 +0000 | [diff] [blame] | 1143 | expected_session3.isSuccess = true; |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1144 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(expected_session1), |
| 1145 | SessionInfoEq(expected_session2), |
| 1146 | SessionInfoEq(expected_session3))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1147 | |
| 1148 | ApexInfoList list; |
| 1149 | bool ret_value; |
| 1150 | std::vector<int> empty_child_session_ids; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1151 | ASSERT_TRUE(IsOk(service_->submitStagedSession(239, empty_child_session_ids, |
| 1152 | &list, &ret_value))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1153 | EXPECT_TRUE(ret_value); |
| 1154 | |
| 1155 | sessions.clear(); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1156 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1157 | |
Nikita Ioffe | 9bd28b9 | 2019-03-04 22:18:07 +0000 | [diff] [blame] | 1158 | ApexSessionInfo new_session = CreateSessionInfo(239); |
| 1159 | new_session.isVerified = true; |
| 1160 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(new_session))); |
Nikita Ioffe | 53c3dcd | 2019-02-08 17:39:00 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 1163 | // TODO(jiyong): re-enable this test. This test is disabled because the build |
| 1164 | // system now always bundles the public key that was used to sign the APEX. |
| 1165 | // In debuggable build, the bundled public key is used as the last fallback. |
| 1166 | // As a result, the verification is always successful (and thus test fails). |
| 1167 | // In order to re-enable this test, we have to manually create an APEX |
| 1168 | // where public key is not bundled. |
| 1169 | #if 0 |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 1170 | TEST_F(ApexServiceTest, SubmitSingleSessionTestFail) { |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1171 | PrepareTestApexForInstall installer( |
| 1172 | GetTestFile("apex.apexd_test_no_inst_key.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1173 | "/data/app-staging/session_456", "staging_data_file"); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1174 | if (!installer.Prepare()) { |
| 1175 | FAIL() << GetDebugStr(&installer); |
| 1176 | } |
| 1177 | |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 1178 | ApexInfoList list; |
| 1179 | bool ret_value; |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 1180 | std::vector<int> empty_child_session_ids; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1181 | ASSERT_TRUE(IsOk(service_->submitStagedSession(456, empty_child_session_ids, |
| 1182 | &list, &ret_value))) |
| 1183 | << GetDebugStr(&installer); |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 1184 | EXPECT_FALSE(ret_value); |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 1185 | |
| 1186 | ApexSessionInfo session; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1187 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(456, &session))) |
| 1188 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1189 | ApexSessionInfo expected = CreateSessionInfo(-1); |
| 1190 | expected.isUnknown = true; |
| 1191 | EXPECT_THAT(session, SessionInfoEq(expected)); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1192 | } |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 1193 | #endif |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 1194 | |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1195 | TEST_F(ApexServiceTest, SubmitMultiSessionTestSuccess) { |
| 1196 | // Parent session id: 10 |
| 1197 | // Children session ids: 20 30 |
| 1198 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1199 | "/data/app-staging/session_20", |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1200 | "staging_data_file"); |
| 1201 | PrepareTestApexForInstall installer2( |
| 1202 | GetTestFile("apex.apexd_test_different_app.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1203 | "/data/app-staging/session_30", "staging_data_file"); |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1204 | if (!installer.Prepare() || !installer2.Prepare()) { |
| 1205 | FAIL() << GetDebugStr(&installer) << GetDebugStr(&installer2); |
| 1206 | } |
| 1207 | |
| 1208 | ApexInfoList list; |
| 1209 | bool ret_value; |
| 1210 | std::vector<int> child_session_ids = {20, 30}; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1211 | ASSERT_TRUE(IsOk( |
| 1212 | service_->submitStagedSession(10, child_session_ids, &list, &ret_value))) |
| 1213 | << GetDebugStr(&installer); |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1214 | ASSERT_TRUE(ret_value); |
| 1215 | EXPECT_EQ(2u, list.apexInfos.size()); |
| 1216 | ApexInfo match; |
| 1217 | bool package1_found = false; |
| 1218 | bool package2_found = false; |
Dario Freni | 6425511 | 2019-02-18 22:13:38 +0000 | [diff] [blame] | 1219 | for (const ApexInfo& info : list.apexInfos) { |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1220 | if (info.packageName == installer.package) { |
| 1221 | ASSERT_EQ(installer.package, info.packageName); |
| 1222 | ASSERT_EQ(installer.version, static_cast<uint64_t>(info.versionCode)); |
| 1223 | ASSERT_EQ(installer.test_file, info.packagePath); |
| 1224 | package1_found = true; |
| 1225 | } else if (info.packageName == installer2.package) { |
| 1226 | ASSERT_EQ(installer2.package, info.packageName); |
| 1227 | ASSERT_EQ(installer2.version, static_cast<uint64_t>(info.versionCode)); |
| 1228 | ASSERT_EQ(installer2.test_file, info.packagePath); |
| 1229 | package2_found = true; |
| 1230 | } else { |
| 1231 | FAIL() << "Unexpected package found " << info.packageName |
| 1232 | << GetDebugStr(&installer) << GetDebugStr(&installer2); |
| 1233 | } |
| 1234 | } |
| 1235 | ASSERT_TRUE(package1_found); |
| 1236 | ASSERT_TRUE(package2_found); |
| 1237 | |
| 1238 | ApexSessionInfo session; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1239 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(10, &session))) |
| 1240 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1241 | ApexSessionInfo expected = CreateSessionInfo(10); |
| 1242 | expected.isVerified = true; |
| 1243 | ASSERT_THAT(session, SessionInfoEq(expected)); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1244 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1245 | ASSERT_TRUE(IsOk(service_->markStagedSessionReady(10, &ret_value))) |
| 1246 | << GetDebugStr(&installer); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1247 | ASSERT_TRUE(ret_value); |
| 1248 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1249 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(10, &session))) |
| 1250 | << GetDebugStr(&installer); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1251 | expected.isVerified = false; |
| 1252 | expected.isStaged = true; |
| 1253 | ASSERT_THAT(session, SessionInfoEq(expected)); |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 1256 | // TODO(jiyong): re-enable this test. This test is disabled because the build |
| 1257 | // system now always bundles the public key that was used to sign the APEX. |
| 1258 | // In debuggable build, the bundled public key is used as the last fallback. |
| 1259 | // As a result, the verification is always successful (and thus test fails). |
| 1260 | // In order to re-enable this test, we have to manually create an APEX |
| 1261 | // where public key is not bundled. |
| 1262 | #if 0 |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1263 | TEST_F(ApexServiceTest, SubmitMultiSessionTestFail) { |
| 1264 | // Parent session id: 11 |
| 1265 | // Children session ids: 21 31 |
| 1266 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1267 | "/data/app-staging/session_21", |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1268 | "staging_data_file"); |
| 1269 | PrepareTestApexForInstall installer2( |
| 1270 | GetTestFile("apex.apexd_test_no_inst_key.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1271 | "/data/app-staging/session_31", "staging_data_file"); |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1272 | if (!installer.Prepare() || !installer2.Prepare()) { |
| 1273 | FAIL() << GetDebugStr(&installer) << GetDebugStr(&installer2); |
| 1274 | } |
| 1275 | ApexInfoList list; |
| 1276 | bool ret_value; |
| 1277 | std::vector<int> child_session_ids = {21, 31}; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1278 | ASSERT_TRUE(IsOk( |
| 1279 | service_->submitStagedSession(11, child_session_ids, &list, &ret_value))) |
| 1280 | << GetDebugStr(&installer); |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1281 | ASSERT_FALSE(ret_value); |
| 1282 | } |
Jiyong Park | 5e81023 | 2019-04-01 15:24:26 +0900 | [diff] [blame] | 1283 | #endif |
Dario Freni | 6dd4dd6 | 2019-01-18 12:45:44 +0000 | [diff] [blame] | 1284 | |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1285 | TEST_F(ApexServiceTest, MarkStagedSessionReadyFail) { |
| 1286 | // We should fail if we ask information about a session we don't know. |
| 1287 | bool ret_value; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1288 | ASSERT_TRUE(IsOk(service_->markStagedSessionReady(666, &ret_value))); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1289 | ASSERT_FALSE(ret_value); |
| 1290 | |
| 1291 | ApexSessionInfo session; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1292 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(666, &session))); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1293 | ApexSessionInfo expected = CreateSessionInfo(-1); |
| 1294 | expected.isUnknown = true; |
| 1295 | ASSERT_THAT(session, SessionInfoEq(expected)); |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | TEST_F(ApexServiceTest, MarkStagedSessionSuccessfulFailsNoSession) { |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1299 | ASSERT_FALSE(IsOk(service_->markStagedSessionSuccessful(37))); |
| 1300 | |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1301 | ApexSessionInfo session_info; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1302 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(37, &session_info))); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1303 | ApexSessionInfo expected = CreateSessionInfo(-1); |
| 1304 | expected.isUnknown = true; |
| 1305 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | TEST_F(ApexServiceTest, MarkStagedSessionSuccessfulFailsSessionInWrongState) { |
| 1309 | auto session = ApexSession::CreateSession(73); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1310 | ASSERT_TRUE(IsOk(session)); |
| 1311 | ASSERT_TRUE( |
| 1312 | IsOk(session->UpdateStateAndCommit(::apex::proto::SessionState::STAGED))); |
| 1313 | |
| 1314 | ASSERT_FALSE(IsOk(service_->markStagedSessionSuccessful(73))); |
| 1315 | |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1316 | ApexSessionInfo session_info; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1317 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(73, &session_info))); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1318 | ApexSessionInfo expected = CreateSessionInfo(73); |
| 1319 | expected.isStaged = true; |
| 1320 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | TEST_F(ApexServiceTest, MarkStagedSessionSuccessfulActivatedSession) { |
| 1324 | auto session = ApexSession::CreateSession(239); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1325 | ASSERT_TRUE(IsOk(session)); |
| 1326 | ASSERT_TRUE(IsOk( |
| 1327 | session->UpdateStateAndCommit(::apex::proto::SessionState::ACTIVATED))); |
| 1328 | |
| 1329 | ASSERT_TRUE(IsOk(service_->markStagedSessionSuccessful(239))); |
| 1330 | |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1331 | ApexSessionInfo session_info; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1332 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(239, &session_info))); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1333 | ApexSessionInfo expected = CreateSessionInfo(239); |
| 1334 | expected.isSuccess = true; |
| 1335 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | TEST_F(ApexServiceTest, MarkStagedSessionSuccessfulNoOp) { |
| 1339 | auto session = ApexSession::CreateSession(1543); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1340 | ASSERT_TRUE(IsOk(session)); |
| 1341 | ASSERT_TRUE(IsOk( |
| 1342 | session->UpdateStateAndCommit(::apex::proto::SessionState::SUCCESS))); |
| 1343 | |
| 1344 | ASSERT_TRUE(IsOk(service_->markStagedSessionSuccessful(1543))); |
| 1345 | |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 1346 | ApexSessionInfo session_info; |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 1347 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(1543, &session_info))); |
Nikita Ioffe | 904e2d3 | 2019-02-19 01:57:13 +0000 | [diff] [blame] | 1348 | ApexSessionInfo expected = CreateSessionInfo(1543); |
| 1349 | expected.isSuccess = true; |
| 1350 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Nikita Ioffe | 463d4e8 | 2019-02-10 18:46:20 +0000 | [diff] [blame] | 1353 | TEST_F(ApexServiceTest, AbortActiveSessionNoSessions) { |
| 1354 | // First ensure there are no sessions. |
| 1355 | std::vector<ApexSessionInfo> sessions; |
| 1356 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1357 | ASSERT_EQ(0u, sessions.size()); |
| 1358 | ASSERT_TRUE(IsOk(service_->abortActiveSession())); |
| 1359 | } |
| 1360 | |
| 1361 | TEST_F(ApexServiceTest, AbortActiveSession) { |
| 1362 | auto session = ApexSession::CreateSession(239); |
| 1363 | session->UpdateStateAndCommit(SessionState::VERIFIED); |
| 1364 | |
| 1365 | std::vector<ApexSessionInfo> sessions; |
| 1366 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1367 | ASSERT_EQ(1u, sessions.size()); |
| 1368 | |
| 1369 | ASSERT_TRUE(IsOk(service_->abortActiveSession())); |
| 1370 | |
| 1371 | sessions.clear(); |
| 1372 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1373 | ASSERT_EQ(0u, sessions.size()); |
| 1374 | } |
| 1375 | |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1376 | TEST_F(ApexServiceTest, BackupActivePackages) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1377 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1378 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1379 | } |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1380 | PrepareTestApexForInstall installer1(GetTestFile("apex.apexd_test.apex")); |
| 1381 | PrepareTestApexForInstall installer2( |
| 1382 | GetTestFile("apex.apexd_test_different_app.apex")); |
| 1383 | PrepareTestApexForInstall installer3(GetTestFile("apex.apexd_test_v2.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1384 | "/data/app-staging/session_23", |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1385 | "staging_data_file"); |
| 1386 | |
| 1387 | if (!installer1.Prepare() || !installer2.Prepare() || !installer3.Prepare()) { |
| 1388 | return; |
| 1389 | } |
| 1390 | |
| 1391 | // Activate some packages, in order to backup them later. |
| 1392 | bool ret = false; |
| 1393 | std::vector<std::string> pkgs = {installer1.test_file, installer2.test_file}; |
| 1394 | ASSERT_TRUE(IsOk(service_->stagePackages(pkgs, &ret))); |
| 1395 | ASSERT_TRUE(ret); |
| 1396 | |
| 1397 | // Make sure that /data/apex/active has activated packages. |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1398 | auto active_pkgs = |
| 1399 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1400 | ASSERT_TRUE(IsOk(active_pkgs)); |
| 1401 | ASSERT_THAT(*active_pkgs, |
| 1402 | UnorderedElementsAre(installer1.test_installed_file, |
| 1403 | installer2.test_installed_file)); |
| 1404 | |
| 1405 | ApexInfoList list; |
| 1406 | std::vector<int> empty_child_session_ids; |
| 1407 | ASSERT_TRUE(IsOk( |
| 1408 | service_->submitStagedSession(23, empty_child_session_ids, &list, &ret))); |
| 1409 | ASSERT_TRUE(ret); |
| 1410 | |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1411 | auto backups = ReadDir(kApexBackupDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1412 | ASSERT_TRUE(IsOk(backups)); |
| 1413 | auto backup1 = |
| 1414 | StringPrintf("%s/com.android.apex.test_package@1.apex", kApexBackupDir); |
| 1415 | auto backup2 = |
| 1416 | StringPrintf("%s/com.android.apex.test_package_2@1.apex", kApexBackupDir); |
| 1417 | ASSERT_THAT(*backups, UnorderedElementsAre(backup1, backup2)); |
| 1418 | } |
| 1419 | |
| 1420 | TEST_F(ApexServiceTest, BackupActivePackagesClearsPreviousBackup) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1421 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1422 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1423 | } |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1424 | PrepareTestApexForInstall installer1(GetTestFile("apex.apexd_test.apex")); |
| 1425 | PrepareTestApexForInstall installer2( |
| 1426 | GetTestFile("apex.apexd_test_different_app.apex")); |
| 1427 | PrepareTestApexForInstall installer3(GetTestFile("apex.apexd_test_v2.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1428 | "/data/app-staging/session_43", |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1429 | "staging_data_file"); |
| 1430 | |
| 1431 | if (!installer1.Prepare() || !installer2.Prepare() || !installer3.Prepare()) { |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | // Make sure /data/apex/backups exists. |
| 1436 | ASSERT_TRUE(IsOk(createDirIfNeeded(std::string(kApexBackupDir), 0700))); |
| 1437 | // Create some bogus files in /data/apex/backups. |
| 1438 | std::ofstream old_backup(StringPrintf("%s/file1", kApexBackupDir)); |
| 1439 | ASSERT_TRUE(old_backup.good()); |
| 1440 | old_backup.close(); |
| 1441 | |
| 1442 | bool ret = false; |
| 1443 | std::vector<std::string> pkgs = {installer1.test_file, installer2.test_file}; |
| 1444 | ASSERT_TRUE(IsOk(service_->stagePackages(pkgs, &ret))); |
| 1445 | ASSERT_TRUE(ret); |
| 1446 | |
| 1447 | // Make sure that /data/apex/active has activated packages. |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1448 | auto active_pkgs = |
| 1449 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1450 | ASSERT_TRUE(IsOk(active_pkgs)); |
| 1451 | ASSERT_THAT(*active_pkgs, |
| 1452 | UnorderedElementsAre(installer1.test_installed_file, |
| 1453 | installer2.test_installed_file)); |
| 1454 | |
| 1455 | ApexInfoList list; |
| 1456 | std::vector<int> empty_child_session_ids; |
| 1457 | ASSERT_TRUE(IsOk( |
| 1458 | service_->submitStagedSession(43, empty_child_session_ids, &list, &ret))); |
| 1459 | ASSERT_TRUE(ret); |
| 1460 | |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1461 | auto backups = ReadDir(kApexBackupDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1462 | ASSERT_TRUE(IsOk(backups)); |
| 1463 | auto backup1 = |
| 1464 | StringPrintf("%s/com.android.apex.test_package@1.apex", kApexBackupDir); |
| 1465 | auto backup2 = |
| 1466 | StringPrintf("%s/com.android.apex.test_package_2@1.apex", kApexBackupDir); |
| 1467 | ASSERT_THAT(*backups, UnorderedElementsAre(backup1, backup2)); |
| 1468 | } |
| 1469 | |
| 1470 | TEST_F(ApexServiceTest, BackupActivePackagesZeroActivePackages) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1471 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1472 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1473 | } |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1474 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1475 | "/data/app-staging/session_41", |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1476 | "staging_data_file"); |
| 1477 | |
| 1478 | if (!installer.Prepare()) { |
| 1479 | return; |
| 1480 | } |
| 1481 | |
| 1482 | // Make sure that /data/apex/active exists and is empty |
| 1483 | ASSERT_TRUE( |
| 1484 | IsOk(createDirIfNeeded(std::string(kActiveApexPackagesDataDir), 0750))); |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1485 | auto active_pkgs = |
| 1486 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1487 | ASSERT_TRUE(IsOk(active_pkgs)); |
| 1488 | ASSERT_EQ(0u, active_pkgs->size()); |
| 1489 | |
| 1490 | ApexInfoList list; |
| 1491 | std::vector<int> empty_child_session_ids; |
| 1492 | bool ret = false; |
| 1493 | ASSERT_TRUE(IsOk( |
| 1494 | service_->submitStagedSession(41, empty_child_session_ids, &list, &ret))); |
| 1495 | ASSERT_TRUE(ret); |
| 1496 | |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1497 | auto backups = ReadDir(kApexBackupDir, [](auto _) { return true; }); |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1498 | ASSERT_TRUE(IsOk(backups)); |
| 1499 | ASSERT_EQ(0u, backups->size()); |
| 1500 | } |
| 1501 | |
| 1502 | TEST_F(ApexServiceTest, ActivePackagesFolderDoesNotExist) { |
| 1503 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex"), |
Gavin Corkery | d12ea22 | 2019-02-27 11:26:16 +0000 | [diff] [blame] | 1504 | "/data/app-staging/session_41", |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1505 | "staging_data_file"); |
| 1506 | |
| 1507 | if (!installer.Prepare()) { |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | // Make sure that /data/apex/active does not exist |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1512 | std::error_code ec; |
| 1513 | fs::remove_all(fs::path(kActiveApexPackagesDataDir), ec); |
| 1514 | ASSERT_FALSE(ec) << "Failed to delete " << kActiveApexPackagesDataDir; |
| 1515 | |
| 1516 | ApexInfoList list; |
| 1517 | std::vector<int> empty_child_session_ids; |
| 1518 | bool ret = false; |
| 1519 | ASSERT_TRUE(IsOk( |
| 1520 | service_->submitStagedSession(41, empty_child_session_ids, &list, &ret))); |
| 1521 | ASSERT_TRUE(ret); |
| 1522 | |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1523 | if (!supports_fs_checkpointing_) { |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1524 | auto backups = ReadDir(kApexBackupDir, [](auto _) { return true; }); |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1525 | ASSERT_TRUE(IsOk(backups)); |
| 1526 | ASSERT_EQ(0u, backups->size()); |
| 1527 | } |
Nikita Ioffe | a82b0a8 | 2019-02-15 18:59:47 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Nikita Ioffe | 496a4a4 | 2019-03-05 16:32:51 +0000 | [diff] [blame] | 1530 | TEST_F(ApexServiceTest, UnstagePackagesSuccess) { |
| 1531 | PrepareTestApexForInstall installer1(GetTestFile("apex.apexd_test.apex")); |
| 1532 | PrepareTestApexForInstall installer2( |
| 1533 | GetTestFile("apex.apexd_test_different_app.apex")); |
| 1534 | |
| 1535 | if (!installer1.Prepare() || !installer2.Prepare()) { |
| 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | bool ret = false; |
| 1540 | std::vector<std::string> pkgs = {installer1.test_file, installer2.test_file}; |
| 1541 | ASSERT_TRUE(IsOk(service_->stagePackages(pkgs, &ret))); |
| 1542 | ASSERT_TRUE(ret); |
| 1543 | |
| 1544 | pkgs = {installer2.test_installed_file}; |
| 1545 | ASSERT_TRUE(IsOk(service_->unstagePackages(pkgs))); |
| 1546 | |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1547 | auto active_packages = |
| 1548 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | 496a4a4 | 2019-03-05 16:32:51 +0000 | [diff] [blame] | 1549 | ASSERT_TRUE(IsOk(active_packages)); |
| 1550 | ASSERT_THAT(*active_packages, |
| 1551 | UnorderedElementsAre(installer1.test_installed_file)); |
| 1552 | } |
| 1553 | |
| 1554 | TEST_F(ApexServiceTest, UnstagePackagesFail) { |
| 1555 | PrepareTestApexForInstall installer1(GetTestFile("apex.apexd_test.apex")); |
| 1556 | PrepareTestApexForInstall installer2( |
| 1557 | GetTestFile("apex.apexd_test_different_app.apex")); |
| 1558 | |
| 1559 | if (!installer1.Prepare() || !installer2.Prepare()) { |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | bool ret = false; |
| 1564 | std::vector<std::string> pkgs = {installer1.test_file}; |
| 1565 | ASSERT_TRUE(IsOk(service_->stagePackages(pkgs, &ret))); |
| 1566 | ASSERT_TRUE(ret); |
| 1567 | |
| 1568 | pkgs = {installer1.test_installed_file, installer2.test_installed_file}; |
| 1569 | ASSERT_FALSE(IsOk(service_->unstagePackages(pkgs))); |
| 1570 | |
| 1571 | // Check that first package wasn't unstaged. |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1572 | auto active_packages = |
| 1573 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | 496a4a4 | 2019-03-05 16:32:51 +0000 | [diff] [blame] | 1574 | ASSERT_TRUE(IsOk(active_packages)); |
| 1575 | ASSERT_THAT(*active_packages, |
| 1576 | UnorderedElementsAre(installer1.test_installed_file)); |
| 1577 | } |
| 1578 | |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1579 | class ApexServiceRollbackTest : public ApexServiceTest { |
| 1580 | protected: |
| 1581 | void SetUp() override { ApexServiceTest::SetUp(); } |
| 1582 | |
Nikita Ioffe | f71e02f | 2019-03-25 16:19:18 +0000 | [diff] [blame] | 1583 | void PrepareBackup(const std::vector<std::string>& pkgs) { |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1584 | ASSERT_TRUE(IsOk(createDirIfNeeded(std::string(kApexBackupDir), 0700))); |
| 1585 | for (const auto& pkg : pkgs) { |
Nikita Ioffe | a4dc3e8 | 2019-02-23 17:37:04 +0000 | [diff] [blame] | 1586 | PrepareTestApexForInstall installer(pkg); |
| 1587 | ASSERT_TRUE(installer.Prepare()) << " failed to prepare " << pkg; |
| 1588 | const std::string& from = installer.test_file; |
| 1589 | std::string to = std::string(kApexBackupDir) + "/" + installer.package + |
| 1590 | "@" + std::to_string(installer.version) + ".apex"; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1591 | std::error_code ec; |
Nikita Ioffe | a4dc3e8 | 2019-02-23 17:37:04 +0000 | [diff] [blame] | 1592 | fs::copy(fs::path(from), fs::path(to), |
| 1593 | fs::copy_options::create_hard_links, ec); |
| 1594 | ASSERT_FALSE(ec) << "Failed to copy " << from << " to " << to << " : " |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1595 | << ec; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | void CheckRollbackWasPerformed( |
| 1600 | const std::vector<std::string>& expected_pkgs) { |
| 1601 | // First check that /data/apex/active exists and has correct permissions. |
| 1602 | struct stat sd; |
| 1603 | ASSERT_EQ(0, stat(kActiveApexPackagesDataDir, &sd)); |
| 1604 | ASSERT_EQ(0750u, sd.st_mode & ALLPERMS); |
| 1605 | |
| 1606 | // Now read content and check it contains expected values. |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1607 | auto active_pkgs = |
| 1608 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1609 | ASSERT_TRUE(IsOk(active_pkgs)); |
| 1610 | ASSERT_THAT(*active_pkgs, UnorderedElementsAreArray(expected_pkgs)); |
| 1611 | } |
| 1612 | }; |
| 1613 | |
| 1614 | TEST_F(ApexServiceRollbackTest, AbortActiveSessionSuccessfulRollback) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1615 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1616 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1617 | } |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1618 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex")); |
| 1619 | if (!installer.Prepare()) { |
| 1620 | return; |
| 1621 | } |
| 1622 | |
| 1623 | auto session = ApexSession::CreateSession(239); |
| 1624 | ASSERT_TRUE(IsOk(session)); |
| 1625 | ASSERT_TRUE(IsOk(session->UpdateStateAndCommit(SessionState::ACTIVATED))); |
| 1626 | |
| 1627 | // Make sure /data/apex/active is non-empty. |
| 1628 | bool ret; |
| 1629 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &ret))); |
| 1630 | ASSERT_TRUE(ret); |
| 1631 | |
| 1632 | PrepareBackup({GetTestFile("apex.apexd_test.apex"), |
| 1633 | GetTestFile("apex.apexd_test_different_app.apex")}); |
| 1634 | |
| 1635 | ASSERT_TRUE(IsOk(service_->abortActiveSession())); |
| 1636 | |
| 1637 | auto pkg1 = StringPrintf("%s/com.android.apex.test_package@1.apex", |
| 1638 | kActiveApexPackagesDataDir); |
| 1639 | auto pkg2 = StringPrintf("%s/com.android.apex.test_package_2@1.apex", |
| 1640 | kActiveApexPackagesDataDir); |
| 1641 | SCOPED_TRACE(""); |
| 1642 | CheckRollbackWasPerformed({pkg1, pkg2}); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1643 | std::vector<ApexSessionInfo> sessions; |
| 1644 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1645 | ApexSessionInfo expected = CreateSessionInfo(239); |
| 1646 | expected.isRolledBack = true; |
| 1647 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(expected))); |
| 1648 | } |
| 1649 | |
| 1650 | TEST_F(ApexServiceRollbackTest, RollbackLastSessionCalledSuccessfulRollback) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1651 | if (supports_fs_checkpointing_) { |
| 1652 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
| 1653 | } |
| 1654 | |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1655 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex")); |
| 1656 | if (!installer.Prepare()) { |
| 1657 | return; |
| 1658 | } |
| 1659 | |
| 1660 | auto session = ApexSession::CreateSession(1543); |
| 1661 | ASSERT_TRUE(IsOk(session)); |
| 1662 | ASSERT_TRUE(IsOk(session->UpdateStateAndCommit(SessionState::ACTIVATED))); |
| 1663 | |
| 1664 | // Make sure /data/apex/active is non-empty. |
| 1665 | bool ret; |
| 1666 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &ret))); |
| 1667 | ASSERT_TRUE(ret); |
| 1668 | |
| 1669 | PrepareBackup({GetTestFile("apex.apexd_test.apex")}); |
| 1670 | |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1671 | ASSERT_TRUE(IsOk(service_->rollbackActiveSession())); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1672 | |
| 1673 | auto pkg = StringPrintf("%s/com.android.apex.test_package@1.apex", |
| 1674 | kActiveApexPackagesDataDir); |
| 1675 | SCOPED_TRACE(""); |
| 1676 | CheckRollbackWasPerformed({pkg}); |
| 1677 | } |
| 1678 | |
| 1679 | TEST_F(ApexServiceRollbackTest, RollbackLastSessionCalledNoActiveSession) { |
| 1680 | // This test simulates a situation that should never happen on user builds: |
| 1681 | // abortLastSession was called, but there are no active sessions. |
| 1682 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex")); |
| 1683 | if (!installer.Prepare()) { |
| 1684 | return; |
| 1685 | } |
| 1686 | |
| 1687 | // Make sure /data/apex/active is non-empty. |
| 1688 | bool ret; |
| 1689 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &ret))); |
| 1690 | ASSERT_TRUE(ret); |
| 1691 | |
| 1692 | PrepareBackup({GetTestFile("apex.apexd_test.apex")}); |
| 1693 | |
| 1694 | // Even though backup is there, no sessions are active, hence rollback request |
| 1695 | // should fail. |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1696 | ASSERT_FALSE(IsOk(service_->rollbackActiveSession())); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | TEST_F(ApexServiceRollbackTest, RollbackFailsNoBackupFolder) { |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1700 | ASSERT_FALSE(IsOk(service_->rollbackActiveSession())); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | TEST_F(ApexServiceRollbackTest, RollbackFailsNoActivePackagesFolder) { |
| 1704 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test.apex")); |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1705 | ASSERT_FALSE(IsOk(service_->rollbackActiveSession())); |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Nikita Ioffe | a4dc3e8 | 2019-02-23 17:37:04 +0000 | [diff] [blame] | 1708 | TEST_F(ApexServiceRollbackTest, MarkStagedSessionSuccessfulCleanupBackup) { |
| 1709 | PrepareBackup({GetTestFile("apex.apexd_test.apex"), |
| 1710 | GetTestFile("apex.apexd_test_different_app.apex")}); |
| 1711 | |
| 1712 | auto session = ApexSession::CreateSession(101); |
| 1713 | ASSERT_TRUE(IsOk(session)); |
| 1714 | ASSERT_TRUE(IsOk(session->UpdateStateAndCommit(SessionState::ACTIVATED))); |
| 1715 | |
| 1716 | ASSERT_TRUE(IsOk(service_->markStagedSessionSuccessful(101))); |
| 1717 | |
| 1718 | ASSERT_TRUE(fs::is_empty(fs::path(kApexBackupDir))); |
| 1719 | } |
| 1720 | |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1721 | TEST_F(ApexServiceRollbackTest, ResumesRollback) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1722 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1723 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1724 | } |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1725 | PrepareBackup({GetTestFile("apex.apexd_test.apex"), |
| 1726 | GetTestFile("apex.apexd_test_different_app.apex")}); |
| 1727 | |
| 1728 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex")); |
| 1729 | if (!installer.Prepare()) { |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | // Make sure /data/apex/active is non-empty. |
| 1734 | bool ret; |
| 1735 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &ret))); |
| 1736 | ASSERT_TRUE(ret); |
| 1737 | |
| 1738 | auto session = ApexSession::CreateSession(17239); |
| 1739 | ASSERT_TRUE(IsOk(session)); |
| 1740 | ASSERT_TRUE( |
| 1741 | IsOk(session->UpdateStateAndCommit(SessionState::ROLLBACK_IN_PROGRESS))); |
| 1742 | |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1743 | ASSERT_TRUE(IsOk(service_->resumeRollbackIfNeeded())); |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1744 | |
| 1745 | auto pkg1 = StringPrintf("%s/com.android.apex.test_package@1.apex", |
| 1746 | kActiveApexPackagesDataDir); |
| 1747 | auto pkg2 = StringPrintf("%s/com.android.apex.test_package_2@1.apex", |
| 1748 | kActiveApexPackagesDataDir); |
| 1749 | SCOPED_TRACE(""); |
| 1750 | CheckRollbackWasPerformed({pkg1, pkg2}); |
| 1751 | |
| 1752 | std::vector<ApexSessionInfo> sessions; |
| 1753 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1754 | ApexSessionInfo expected = CreateSessionInfo(17239); |
| 1755 | expected.isRolledBack = true; |
| 1756 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(expected))); |
| 1757 | } |
| 1758 | |
| 1759 | TEST_F(ApexServiceRollbackTest, DoesNotResumeRollback) { |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1760 | if (supports_fs_checkpointing_) { |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1761 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
Martijn Coenen | 44de00c | 2019-03-22 09:13:17 +0100 | [diff] [blame] | 1762 | } |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1763 | PrepareTestApexForInstall installer(GetTestFile("apex.apexd_test_v2.apex")); |
| 1764 | if (!installer.Prepare()) { |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | // Make sure /data/apex/active is non-empty. |
| 1769 | bool ret; |
| 1770 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &ret))); |
| 1771 | ASSERT_TRUE(ret); |
| 1772 | |
| 1773 | auto session = ApexSession::CreateSession(53); |
| 1774 | ASSERT_TRUE(IsOk(session)); |
| 1775 | ASSERT_TRUE(IsOk(session->UpdateStateAndCommit(SessionState::SUCCESS))); |
| 1776 | |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 1777 | ASSERT_TRUE(IsOk(service_->resumeRollbackIfNeeded())); |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Check that rollback wasn't resumed. |
Mohammad Samiul Islam | d428a18 | 2019-03-19 14:36:24 +0000 | [diff] [blame] | 1780 | auto active_pkgs = |
| 1781 | ReadDir(kActiveApexPackagesDataDir, [](auto _) { return true; }); |
Nikita Ioffe | 2f6936c | 2019-02-26 20:03:32 +0000 | [diff] [blame] | 1782 | ASSERT_TRUE(IsOk(active_pkgs)); |
| 1783 | ASSERT_THAT(*active_pkgs, |
| 1784 | UnorderedElementsAre(installer.test_installed_file)); |
| 1785 | |
| 1786 | std::vector<ApexSessionInfo> sessions; |
| 1787 | ASSERT_TRUE(IsOk(service_->getSessions(&sessions))); |
| 1788 | ApexSessionInfo expected = CreateSessionInfo(53); |
| 1789 | expected.isSuccess = true; |
| 1790 | ASSERT_THAT(sessions, UnorderedElementsAre(SessionInfoEq(expected))); |
| 1791 | } |
| 1792 | |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 1793 | TEST_F(ApexServiceRollbackTest, FailsRollback) { |
| 1794 | if (supports_fs_checkpointing_) { |
| 1795 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
| 1796 | } |
| 1797 | |
| 1798 | auto session = ApexSession::CreateSession(53); |
| 1799 | ASSERT_TRUE(IsOk(session)); |
| 1800 | ASSERT_TRUE(IsOk(session->UpdateStateAndCommit(SessionState::ACTIVATED))); |
| 1801 | |
| 1802 | ASSERT_FALSE(IsOk(service_->rollbackActiveSession())); |
| 1803 | ApexSessionInfo session_info; |
| 1804 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(53, &session_info))); |
| 1805 | ApexSessionInfo expected = CreateSessionInfo(53); |
| 1806 | expected.isRollbackFailed = true; |
| 1807 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
| 1808 | } |
| 1809 | |
| 1810 | TEST_F(ApexServiceRollbackTest, RollbackFailedStateRollbackAttemptFails) { |
| 1811 | if (supports_fs_checkpointing_) { |
| 1812 | GTEST_SKIP() << "Can't run if filesystem checkpointing is enabled"; |
| 1813 | } |
| 1814 | |
| 1815 | auto session = ApexSession::CreateSession(17239); |
| 1816 | ASSERT_TRUE(IsOk(session)); |
| 1817 | ASSERT_TRUE( |
| 1818 | IsOk(session->UpdateStateAndCommit(SessionState::ROLLBACK_FAILED))); |
| 1819 | |
| 1820 | ASSERT_FALSE(IsOk(service_->rollbackActiveSession())); |
| 1821 | ApexSessionInfo session_info; |
| 1822 | ASSERT_TRUE(IsOk(service_->getStagedSessionInfo(17239, &session_info))); |
| 1823 | ApexSessionInfo expected = CreateSessionInfo(17239); |
| 1824 | expected.isRollbackFailed = true; |
| 1825 | ASSERT_THAT(session_info, SessionInfoEq(expected)); |
| 1826 | } |
| 1827 | |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 1828 | static pid_t GetPidOf(const std::string& name) { |
| 1829 | char buf[1024]; |
| 1830 | const std::string cmd = std::string("pidof -s ") + name; |
| 1831 | FILE* cmd_pipe = popen(cmd.c_str(), "r"); |
| 1832 | if (cmd_pipe == nullptr) { |
| 1833 | PLOG(ERROR) << "Cannot open pipe for " << cmd; |
| 1834 | return 0; |
| 1835 | } |
| 1836 | if (fgets(buf, 1024, cmd_pipe) == nullptr) { |
| 1837 | PLOG(ERROR) << "Cannot read pipe for " << cmd; |
| 1838 | pclose(cmd_pipe); |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | pclose(cmd_pipe); |
| 1843 | return strtoul(buf, nullptr, 10); |
| 1844 | } |
| 1845 | |
Nikita Ioffe | f71e02f | 2019-03-25 16:19:18 +0000 | [diff] [blame] | 1846 | static void ExecInMountNamespaceOf(pid_t pid, |
| 1847 | const std::function<void(pid_t)>& func) { |
Jiyong Park | 715e23d | 2019-02-22 22:14:37 +0900 | [diff] [blame] | 1848 | const std::string my_path = "/proc/self/ns/mnt"; |
| 1849 | android::base::unique_fd my_fd(open(my_path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 1850 | ASSERT_TRUE(my_fd.get() >= 0); |
| 1851 | |
| 1852 | const std::string target_path = |
| 1853 | std::string("/proc/") + std::to_string(pid) + "/ns/mnt"; |
| 1854 | android::base::unique_fd target_fd( |
| 1855 | open(target_path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 1856 | ASSERT_TRUE(target_fd.get() >= 0); |
| 1857 | |
| 1858 | int res = setns(target_fd.get(), CLONE_NEWNS); |
| 1859 | ASSERT_NE(-1, res); |
| 1860 | |
| 1861 | func(pid); |
| 1862 | |
| 1863 | res = setns(my_fd.get(), CLONE_NEWNS); |
| 1864 | ASSERT_NE(-1, res); |
| 1865 | } |
| 1866 | |
| 1867 | TEST(ApexdTest, ApexdIsInSameMountNamespaceAsInit) { |
| 1868 | std::string ns_apexd; |
| 1869 | std::string ns_init; |
| 1870 | |
| 1871 | ExecInMountNamespaceOf(GetPidOf("apexd"), [&](pid_t pid) { |
| 1872 | bool res = android::base::Readlink("/proc/self/ns/mnt", &ns_apexd); |
| 1873 | ASSERT_TRUE(res); |
| 1874 | }); |
| 1875 | |
| 1876 | ExecInMountNamespaceOf(1, [&](pid_t pid) { |
| 1877 | bool res = android::base::Readlink("/proc/self/ns/mnt", &ns_init); |
| 1878 | ASSERT_TRUE(res); |
| 1879 | }); |
| 1880 | |
| 1881 | ASSERT_EQ(ns_apexd, ns_init); |
| 1882 | } |
| 1883 | |
| 1884 | // These are NOT exhaustive list of early processes be should be enough |
| 1885 | static const std::vector<const std::string> kEarlyProcesses = { |
| 1886 | "servicemanager", |
| 1887 | "hwservicemanager", |
| 1888 | "vold", |
| 1889 | "logd", |
| 1890 | }; |
| 1891 | |
| 1892 | TEST(ApexdTest, EarlyProcessesAreInDifferentMountNamespace) { |
| 1893 | if (!android::base::GetBoolProperty("ro.apex.updatable", false)) { |
| 1894 | return; |
| 1895 | } |
| 1896 | |
| 1897 | std::string ns_apexd; |
| 1898 | |
| 1899 | ExecInMountNamespaceOf(GetPidOf("apexd"), [&](pid_t _) { |
| 1900 | bool res = android::base::Readlink("/proc/self/ns/mnt", &ns_apexd); |
| 1901 | ASSERT_TRUE(res); |
| 1902 | }); |
| 1903 | |
| 1904 | for (const auto& name : kEarlyProcesses) { |
| 1905 | std::string ns_early_process; |
| 1906 | ExecInMountNamespaceOf(GetPidOf(name), [&](pid_t _) { |
| 1907 | bool res = |
| 1908 | android::base::Readlink("/proc/self/ns/mnt", &ns_early_process); |
| 1909 | ASSERT_TRUE(res); |
| 1910 | }); |
| 1911 | ASSERT_NE(ns_apexd, ns_early_process); |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | TEST(ApexdTest, ApexIsAPrivateMountPoint) { |
| 1916 | std::string mountinfo; |
| 1917 | ASSERT_TRUE( |
| 1918 | android::base::ReadFileToString("/proc/self/mountinfo", &mountinfo)); |
| 1919 | bool found_apex_mountpoint = false; |
| 1920 | for (const auto& line : android::base::Split(mountinfo, "\n")) { |
| 1921 | std::vector<std::string> tokens = android::base::Split(line, " "); |
| 1922 | // line format: |
| 1923 | // mnt_id parent_mnt_id major:minor source target option propagation_type |
| 1924 | // ex) 33 260:19 / /apex rw,nosuid,nodev - |
| 1925 | if (tokens.size() >= 7 && tokens[4] == "/apex") { |
| 1926 | found_apex_mountpoint = true; |
| 1927 | // Make sure that propagation type is set to - which means private |
| 1928 | ASSERT_EQ("-", tokens[6]); |
| 1929 | } |
| 1930 | } |
| 1931 | ASSERT_TRUE(found_apex_mountpoint); |
| 1932 | } |
| 1933 | |
| 1934 | static const std::vector<const std::string> kEarlyApexes = { |
| 1935 | "/apex/com.android.runtime", |
| 1936 | "/apex/com.android.tzdata", |
| 1937 | }; |
| 1938 | |
| 1939 | TEST(ApexdTest, ApexesAreActivatedForEarlyProcesses) { |
| 1940 | for (const auto& name : kEarlyProcesses) { |
| 1941 | pid_t pid = GetPidOf(name); |
| 1942 | const std::string path = |
| 1943 | std::string("/proc/") + std::to_string(pid) + "/mountinfo"; |
| 1944 | std::string mountinfo; |
| 1945 | ASSERT_TRUE(android::base::ReadFileToString(path.c_str(), &mountinfo)); |
| 1946 | |
| 1947 | std::unordered_set<std::string> mountpoints; |
| 1948 | for (const auto& line : android::base::Split(mountinfo, "\n")) { |
| 1949 | std::vector<std::string> tokens = android::base::Split(line, " "); |
| 1950 | // line format: |
| 1951 | // mnt_id parent_mnt_id major:minor source target option propagation_type |
| 1952 | // ex) 69 33 7:40 / /apex/com.android.conscrypt ro,nodev,noatime - |
| 1953 | if (tokens.size() >= 5) { |
| 1954 | // token[4] is the target mount point |
| 1955 | mountpoints.emplace(tokens[4]); |
| 1956 | } |
| 1957 | } |
| 1958 | for (const auto& apex_name : kEarlyApexes) { |
| 1959 | ASSERT_NE(mountpoints.end(), mountpoints.find(apex_name)); |
| 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
Nikita Ioffe | df07441 | 2019-03-20 07:56:52 +0000 | [diff] [blame] | 1964 | class ApexShimUpdateTest : public ApexServiceTest { |
| 1965 | protected: |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 1966 | void SetUp() override { |
| 1967 | ApexServiceTest::SetUp(); |
| 1968 | |
Nikita Ioffe | fa7b5da | 2019-04-10 12:15:53 +0100 | [diff] [blame] | 1969 | // Assert that shim apex is pre-installed. |
| 1970 | std::vector<ApexInfo> list; |
| 1971 | ASSERT_TRUE(IsOk(service_->getAllPackages(&list))); |
| 1972 | ApexInfo expected; |
| 1973 | expected.packageName = "com.android.apex.cts.shim"; |
| 1974 | expected.packagePath = "/system/apex/com.android.apex.cts.shim.apex"; |
| 1975 | expected.versionCode = 1; |
| 1976 | expected.isFactory = true; |
| 1977 | expected.isActive = true; |
| 1978 | ASSERT_THAT(list, Contains(ApexInfoEq(expected))); |
Nikita Ioffe | df07441 | 2019-03-20 07:56:52 +0000 | [diff] [blame] | 1979 | } |
| 1980 | }; |
| 1981 | |
| 1982 | TEST_F(ApexShimUpdateTest, UpdateToV2Success) { |
| 1983 | PrepareTestApexForInstall installer( |
| 1984 | GetTestFile("com.android.apex.cts.shim.v2.apex")); |
| 1985 | |
| 1986 | if (!installer.Prepare()) { |
| 1987 | FAIL() << GetDebugStr(&installer); |
| 1988 | } |
| 1989 | |
| 1990 | bool success; |
| 1991 | ASSERT_TRUE(IsOk(service_->stagePackage(installer.test_file, &success))); |
| 1992 | ASSERT_TRUE(success); |
| 1993 | } |
| 1994 | |
Nikita Ioffe | d9a25d4 | 2019-03-26 01:37:03 +0000 | [diff] [blame] | 1995 | TEST_F(ApexShimUpdateTest, UpdateToV2FailureWrongSHA512) { |
| 1996 | PrepareTestApexForInstall installer( |
| 1997 | GetTestFile("com.android.apex.cts.shim.v2_wrong_sha.apex")); |
| 1998 | |
| 1999 | if (!installer.Prepare()) { |
| 2000 | FAIL() << GetDebugStr(&installer); |
| 2001 | } |
| 2002 | |
| 2003 | bool success; |
Nikita Ioffe | 98054d7 | 2019-04-03 14:18:22 +0100 | [diff] [blame] | 2004 | const auto& status = service_->stagePackage(installer.test_file, &success); |
| 2005 | ASSERT_FALSE(IsOk(status)); |
| 2006 | const std::string& error_message = |
| 2007 | std::string(status.exceptionMessage().c_str()); |
| 2008 | ASSERT_THAT(error_message, HasSubstr("has unexpected SHA512 hash")); |
Nikita Ioffe | d9a25d4 | 2019-03-26 01:37:03 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2011 | TEST_F(ApexShimUpdateTest, SubmitStagedSesssionFailureHasPreInstallHook) { |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2012 | PrepareTestApexForInstall installer( |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2013 | GetTestFile("com.android.apex.cts.shim.v2_with_pre_install_hook.apex"), |
| 2014 | "/data/app-staging/session_23", "staging_data_file"); |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2015 | |
| 2016 | if (!installer.Prepare()) { |
| 2017 | FAIL() << GetDebugStr(&installer); |
| 2018 | } |
| 2019 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2020 | ApexInfoList list; |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2021 | bool success; |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2022 | ASSERT_TRUE(IsOk(service_->submitStagedSession(23, {}, &list, &success))); |
| 2023 | ASSERT_FALSE(success); |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2026 | TEST_F(ApexShimUpdateTest, SubmitStagedSessionFailureHasPostInstallHook) { |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2027 | PrepareTestApexForInstall installer( |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2028 | GetTestFile("com.android.apex.cts.shim.v2_with_post_install_hook.apex"), |
| 2029 | "/data/app-staging/session_43", "staging_data_file"); |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2030 | |
| 2031 | if (!installer.Prepare()) { |
| 2032 | FAIL() << GetDebugStr(&installer); |
| 2033 | } |
| 2034 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2035 | ApexInfoList list; |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2036 | bool success; |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2037 | ASSERT_TRUE(IsOk(service_->submitStagedSession(43, {}, &list, &success))); |
| 2038 | ASSERT_FALSE(success); |
Nikita Ioffe | 69a1bc5 | 2019-03-29 14:55:29 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2041 | TEST_F(ApexShimUpdateTest, SubmitStagedSessionFailureAdditionalFile) { |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2042 | PrepareTestApexForInstall installer( |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2043 | GetTestFile("com.android.apex.cts.shim.v2_additional_file.apex"), |
| 2044 | "/data/app-staging/session_41", "staging_data_file"); |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2045 | if (!installer.Prepare()) { |
| 2046 | FAIL() << GetDebugStr(&installer); |
| 2047 | } |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2048 | |
| 2049 | ApexInfoList list; |
Nikita Ioffe | 98054d7 | 2019-04-03 14:18:22 +0100 | [diff] [blame] | 2050 | bool success; |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2051 | ASSERT_TRUE(IsOk(service_->submitStagedSession(41, {}, &list, &success))); |
| 2052 | ASSERT_FALSE(success); |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2055 | TEST_F(ApexShimUpdateTest, SubmitStagedSessionFailureAdditionalFolder) { |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2056 | PrepareTestApexForInstall installer( |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2057 | GetTestFile("com.android.apex.cts.shim.v2_additional_folder.apex"), |
| 2058 | "/data/app-staging/session_42", "staging_data_file"); |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2059 | if (!installer.Prepare()) { |
| 2060 | FAIL() << GetDebugStr(&installer); |
| 2061 | } |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2062 | |
| 2063 | ApexInfoList list; |
Nikita Ioffe | 98054d7 | 2019-04-03 14:18:22 +0100 | [diff] [blame] | 2064 | bool success; |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2065 | ASSERT_TRUE(IsOk(service_->submitStagedSession(42, {}, &list, &success))); |
| 2066 | ASSERT_FALSE(success); |
Nikita Ioffe | 2cbca0e | 2019-03-27 19:12:11 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2069 | TEST_F(ApexServiceTest, SubmitStagedSessionCorruptApexFails) { |
Martijn Coenen | 2caabb4 | 2019-04-02 11:42:02 +0200 | [diff] [blame] | 2070 | PrepareTestApexForInstall installer( |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2071 | GetTestFile("apex.apexd_test_corrupt_apex.apex"), |
| 2072 | "/data/app-staging/session_57", "staging_data_file"); |
Martijn Coenen | 2caabb4 | 2019-04-02 11:42:02 +0200 | [diff] [blame] | 2073 | |
| 2074 | if (!installer.Prepare()) { |
| 2075 | FAIL() << GetDebugStr(&installer); |
| 2076 | } |
| 2077 | |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2078 | ApexInfoList list; |
Martijn Coenen | 2caabb4 | 2019-04-02 11:42:02 +0200 | [diff] [blame] | 2079 | bool success; |
Nikita Ioffe | 54e24cd | 2019-04-05 17:04:11 +0100 | [diff] [blame] | 2080 | ASSERT_TRUE(IsOk(service_->submitStagedSession(57, {}, &list, &success))); |
| 2081 | ASSERT_FALSE(success); |
Martijn Coenen | 2caabb4 | 2019-04-02 11:42:02 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Nikita Ioffe | a67b3cd | 2019-04-29 11:24:40 +0100 | [diff] [blame] | 2084 | // Following test case piggybacks on logic in ApexServiceActivationSuccessTest |
| 2085 | // in order to use mounted apex as flattened one. |
| 2086 | TEST_F(ApexServiceActivationSuccessTest, StageFailsFlattenedApex) { |
| 2087 | ASSERT_TRUE(IsOk(service_->activatePackage(installer_->test_installed_file))) |
| 2088 | << GetDebugStr(installer_.get()); |
| 2089 | |
| 2090 | StatusOr<ApexFile> flattened_apex = |
| 2091 | ApexFile::Open(StringPrintf("/apex/%s", installer_->package.c_str())); |
| 2092 | ASSERT_TRUE(IsOk(flattened_apex)); |
| 2093 | ASSERT_TRUE(flattened_apex->IsFlattened()); |
| 2094 | |
| 2095 | bool success; |
| 2096 | const auto& status = |
| 2097 | service_->stagePackage(flattened_apex->GetPath(), &success); |
| 2098 | ASSERT_FALSE(IsOk(status)); |
| 2099 | const std::string& error_message = |
| 2100 | std::string(status.exceptionMessage().c_str()); |
| 2101 | ASSERT_THAT(error_message, HasSubstr("Can't upgrade flattened apex")); |
| 2102 | } |
| 2103 | |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 2104 | class LogTestToLogcat : public ::testing::EmptyTestEventListener { |
| 2105 | void OnTestStart(const ::testing::TestInfo& test_info) override { |
Andreas Gampe | 5ec47c5 | 2018-11-14 14:33:53 -0800 | [diff] [blame] | 2106 | #ifdef __ANDROID__ |
| 2107 | using base::LogId; |
| 2108 | using base::LogSeverity; |
| 2109 | using base::StringPrintf; |
| 2110 | base::LogdLogger l; |
| 2111 | std::string msg = |
| 2112 | StringPrintf("=== %s::%s (%s:%d)", test_info.test_case_name(), |
| 2113 | test_info.name(), test_info.file(), test_info.line()); |
| 2114 | l(LogId::MAIN, LogSeverity::INFO, "apexservice_test", __FILE__, __LINE__, |
| 2115 | msg.c_str()); |
| 2116 | #else |
Andreas Gampe | 74a8956 | 2018-11-16 14:41:47 -0800 | [diff] [blame] | 2117 | UNUSED(test_info); |
Andreas Gampe | 5ec47c5 | 2018-11-14 14:33:53 -0800 | [diff] [blame] | 2118 | #endif |
| 2119 | } |
| 2120 | }; |
| 2121 | |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 2122 | } // namespace apex |
| 2123 | } // namespace android |
| 2124 | |
| 2125 | int main(int argc, char** argv) { |
| 2126 | android::base::InitLogging(argv, &android::base::StderrLogger); |
| 2127 | ::testing::InitGoogleTest(&argc, argv); |
Nikita Ioffe | 61a9b53 | 2019-02-14 17:50:00 +0000 | [diff] [blame] | 2128 | ::testing::UnitTest::GetInstance()->listeners().Append( |
Andreas Gampe | 5ec47c5 | 2018-11-14 14:33:53 -0800 | [diff] [blame] | 2129 | new android::apex::LogTestToLogcat()); |
Andreas Gampe | 35e8093 | 2018-10-29 12:56:53 -0700 | [diff] [blame] | 2130 | return RUN_ALL_TESTS(); |
| 2131 | } |