blob: 2948b6a0f29374fb21e031b5b3ccf3b4745422ab [file] [log] [blame]
Songchun Fan3c82a302019-11-29 14:23:45 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android-base/file.h>
18#include <android-base/logging.h>
19#include <android-base/unique_fd.h>
20#include <binder/ParcelFileDescriptor.h>
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23#include <utils/Log.h>
24
25#include <future>
26
27#include "IncrementalService.h"
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -070028#include "IncrementalServiceValidation.h"
Songchun Fan3c82a302019-11-29 14:23:45 -080029#include "Metadata.pb.h"
30#include "ServiceWrappers.h"
31
32using namespace testing;
33using namespace android::incremental;
34using namespace std::literals;
35using testing::_;
36using testing::Invoke;
37using testing::NiceMock;
38
39#undef LOG_TAG
40#define LOG_TAG "IncrementalServiceTest"
41
42using namespace android::incfs;
43using namespace android::content::pm;
44
45namespace android::os::incremental {
46
47class MockVoldService : public VoldServiceWrapper {
48public:
49 MOCK_CONST_METHOD4(mountIncFs,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080050 binder::Status(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080051 int32_t flags,
52 IncrementalFileSystemControlParcel* _aidl_return));
53 MOCK_CONST_METHOD1(unmountIncFs, binder::Status(const std::string& dir));
54 MOCK_CONST_METHOD2(bindMount,
55 binder::Status(const std::string& sourceDir, const std::string& argetDir));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -070056 MOCK_CONST_METHOD2(setIncFsMountOptions,
57 binder::Status(const ::android::os::incremental::IncrementalFileSystemControlParcel&, bool));
Songchun Fan3c82a302019-11-29 14:23:45 -080058
59 void mountIncFsFails() {
60 ON_CALL(*this, mountIncFs(_, _, _, _))
61 .WillByDefault(
62 Return(binder::Status::fromExceptionCode(1, String8("failed to mount"))));
63 }
64 void mountIncFsInvalidControlParcel() {
65 ON_CALL(*this, mountIncFs(_, _, _, _))
66 .WillByDefault(Invoke(this, &MockVoldService::getInvalidControlParcel));
67 }
68 void mountIncFsSuccess() {
69 ON_CALL(*this, mountIncFs(_, _, _, _))
70 .WillByDefault(Invoke(this, &MockVoldService::incFsSuccess));
71 }
72 void bindMountFails() {
73 ON_CALL(*this, bindMount(_, _))
74 .WillByDefault(Return(
75 binder::Status::fromExceptionCode(1, String8("failed to bind-mount"))));
76 }
77 void bindMountSuccess() {
78 ON_CALL(*this, bindMount(_, _)).WillByDefault(Return(binder::Status::ok()));
79 }
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -070080 void setIncFsMountOptionsFails() const {
81 ON_CALL(*this, setIncFsMountOptions(_, _))
82 .WillByDefault(
83 Return(binder::Status::fromExceptionCode(1, String8("failed to set options"))));
84 }
85 void setIncFsMountOptionsSuccess() {
86 ON_CALL(*this, setIncFsMountOptions(_, _)).WillByDefault(Return(binder::Status::ok()));
87 }
Songchun Fan3c82a302019-11-29 14:23:45 -080088 binder::Status getInvalidControlParcel(const std::string& imagePath,
89 const std::string& targetDir, int32_t flags,
90 IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080091 _aidl_return = {};
Songchun Fan3c82a302019-11-29 14:23:45 -080092 return binder::Status::ok();
93 }
94 binder::Status incFsSuccess(const std::string& imagePath, const std::string& targetDir,
95 int32_t flags, IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080096 _aidl_return->pendingReads.reset(base::unique_fd(dup(STDIN_FILENO)));
97 _aidl_return->cmd.reset(base::unique_fd(dup(STDIN_FILENO)));
98 _aidl_return->log.reset(base::unique_fd(dup(STDIN_FILENO)));
Songchun Fan3c82a302019-11-29 14:23:45 -080099 return binder::Status::ok();
100 }
101
102private:
103 TemporaryFile cmdFile;
104 TemporaryFile logFile;
Songchun Fan3c82a302019-11-29 14:23:45 -0800105};
106
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700107class MockDataLoader : public IDataLoader {
Songchun Fan3c82a302019-11-29 14:23:45 -0800108public:
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700109 MockDataLoader() {
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700110 ON_CALL(*this, create(_, _, _, _)).WillByDefault(Invoke(this, &MockDataLoader::createOk));
111 ON_CALL(*this, start(_)).WillByDefault(Invoke(this, &MockDataLoader::startOk));
112 ON_CALL(*this, stop(_)).WillByDefault(Invoke(this, &MockDataLoader::stopOk));
113 ON_CALL(*this, destroy(_)).WillByDefault(Invoke(this, &MockDataLoader::destroyOk));
114 ON_CALL(*this, prepareImage(_, _, _))
115 .WillByDefault(Invoke(this, &MockDataLoader::prepareImageOk));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700116 }
Songchun Fan68645c42020-02-27 15:57:35 -0800117 IBinder* onAsBinder() override { return nullptr; }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700118 MOCK_METHOD4(create,
119 binder::Status(int32_t id, const DataLoaderParamsParcel& params,
120 const FileSystemControlParcel& control,
121 const sp<IDataLoaderStatusListener>& listener));
122 MOCK_METHOD1(start, binder::Status(int32_t id));
123 MOCK_METHOD1(stop, binder::Status(int32_t id));
124 MOCK_METHOD1(destroy, binder::Status(int32_t id));
125 MOCK_METHOD3(prepareImage,
126 binder::Status(int32_t id, const std::vector<InstallationFileParcel>& addedFiles,
127 const std::vector<std::string>& removedFiles));
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700128
129 void initializeCreateOkNoStatus() {
130 ON_CALL(*this, create(_, _, _, _))
131 .WillByDefault(Invoke(this, &MockDataLoader::createOkNoStatus));
132 }
133
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700134 binder::Status createOk(int32_t id, const content::pm::DataLoaderParamsParcel& params,
135 const content::pm::FileSystemControlParcel& control,
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700136 const sp<content::pm::IDataLoaderStatusListener>& listener) {
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700137 createOkNoStatus(id, params, control, listener);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700138 if (mListener) {
139 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_CREATED);
140 }
141 return binder::Status::ok();
142 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700143 binder::Status createOkNoStatus(int32_t id, const content::pm::DataLoaderParamsParcel& params,
144 const content::pm::FileSystemControlParcel& control,
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700145 const sp<content::pm::IDataLoaderStatusListener>& listener) {
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700146 mServiceConnector = control.service;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700147 mListener = listener;
148 return binder::Status::ok();
149 }
150 binder::Status startOk(int32_t id) {
151 if (mListener) {
152 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STARTED);
153 }
154 return binder::Status::ok();
155 }
156 binder::Status stopOk(int32_t id) {
157 if (mListener) {
158 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STOPPED);
159 }
160 return binder::Status::ok();
161 }
162 binder::Status destroyOk(int32_t id) {
163 if (mListener) {
164 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
165 }
166 mListener = nullptr;
167 return binder::Status::ok();
168 }
169 binder::Status prepareImageOk(int32_t id,
170 const ::std::vector<content::pm::InstallationFileParcel>&,
171 const ::std::vector<::std::string>&) {
172 if (mListener) {
173 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY);
174 }
175 return binder::Status::ok();
176 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700177 int32_t setStorageParams(bool enableReadLogs) {
178 int32_t result = -1;
179 EXPECT_NE(mServiceConnector.get(), nullptr);
180 EXPECT_TRUE(mServiceConnector->setStorageParams(enableReadLogs, &result).isOk());
181 return result;
182 }
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700183
184private:
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700185 sp<IIncrementalServiceConnector> mServiceConnector;
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700186 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800187};
188
189class MockDataLoaderManager : public DataLoaderManagerWrapper {
190public:
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700191 MockDataLoaderManager(sp<IDataLoader> dataLoader) : mDataLoaderHolder(std::move(dataLoader)) {
192 EXPECT_TRUE(mDataLoaderHolder != nullptr);
193 }
194
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700195 MOCK_CONST_METHOD4(bindToDataLoader,
Songchun Fan68645c42020-02-27 15:57:35 -0800196 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
Songchun Fan3c82a302019-11-29 14:23:45 -0800197 const sp<IDataLoaderStatusListener>& listener,
198 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800199 MOCK_CONST_METHOD2(getDataLoader,
200 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700201 MOCK_CONST_METHOD1(unbindFromDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800202
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700203 void bindToDataLoaderSuccess() {
204 ON_CALL(*this, bindToDataLoader(_, _, _, _))
205 .WillByDefault(Invoke(this, &MockDataLoaderManager::bindToDataLoaderOk));
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700206 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700207 void bindToDataLoaderFails() {
208 ON_CALL(*this, bindToDataLoader(_, _, _, _))
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700209 .WillByDefault(Return(
210 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
211 }
212 void getDataLoaderSuccess() {
213 ON_CALL(*this, getDataLoader(_, _))
214 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
215 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700216 void unbindFromDataLoaderSuccess() {
217 ON_CALL(*this, unbindFromDataLoader(_))
218 .WillByDefault(Invoke(this, &MockDataLoaderManager::unbindFromDataLoaderOk));
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700219 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700220 binder::Status bindToDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
221 const sp<IDataLoaderStatusListener>& listener,
222 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800223 mId = mountId;
224 mListener = listener;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700225 mDataLoader = mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800226 *_aidl_return = true;
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700227 if (mListener) {
228 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_BOUND);
229 }
230 return binder::Status::ok();
Songchun Fan3c82a302019-11-29 14:23:45 -0800231 }
Songchun Fan68645c42020-02-27 15:57:35 -0800232 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
233 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800234 return binder::Status::ok();
235 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700236 void setDataLoaderStatusCreated() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800237 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800238 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700239 void setDataLoaderStatusStarted() {
240 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_STARTED);
241 }
242 void setDataLoaderStatusDestroyed() {
243 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
244 }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700245 void setDataLoaderStatusUnavailable() {
246 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_UNAVAILABLE);
247 }
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700248 binder::Status unbindFromDataLoaderOk(int32_t id) {
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700249 if (mDataLoader) {
250 if (auto status = mDataLoader->destroy(id); !status.isOk()) {
251 return status;
252 }
253 mDataLoader = nullptr;
254 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700255 if (mListener) {
256 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
257 }
258 return binder::Status::ok();
259 }
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700260
Songchun Fan3c82a302019-11-29 14:23:45 -0800261private:
262 int mId;
263 sp<IDataLoaderStatusListener> mListener;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700264 sp<IDataLoader> mDataLoader;
265 sp<IDataLoader> mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800266};
267
268class MockIncFs : public IncFsWrapper {
269public:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700270 MOCK_CONST_METHOD1(listExistingMounts, void(const ExistingMountCallback& cb));
271 MOCK_CONST_METHOD1(openMount, Control(std::string_view path));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800272 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800273 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800274 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800275 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800276 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700277 MOCK_CONST_METHOD3(makeDirs,
278 ErrorCode(const Control& control, std::string_view path, int mode));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800279 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
280 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
281 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800282 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800283 ErrorCode(const Control& control, std::string_view from, std::string_view to));
284 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700285 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700286 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(std::span<const DataBlock> blocks));
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700287 MOCK_CONST_METHOD3(waitForPendingReads,
288 WaitResult(const Control& control, std::chrono::milliseconds timeout,
289 std::vector<incfs::ReadInfo>* pendingReadsBuffer));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700290
291 MockIncFs() { ON_CALL(*this, listExistingMounts(_)).WillByDefault(Return()); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800292
293 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
294 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700295 void openMountSuccess() {
296 ON_CALL(*this, openMount(_)).WillByDefault(Invoke(this, &MockIncFs::openMountForHealth));
297 }
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700298 void waitForPendingReadsSuccess() {
299 ON_CALL(*this, waitForPendingReads(_, _, _))
300 .WillByDefault(Invoke(this, &MockIncFs::waitForPendingReadsForHealth));
301 }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700302
303 static constexpr auto kPendingReadsFd = 42;
304 Control openMountForHealth(std::string_view) {
305 return UniqueControl(IncFs_CreateControl(-1, kPendingReadsFd, -1));
306 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700307
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700308 WaitResult waitForPendingReadsForHealth(
309 const Control& control, std::chrono::milliseconds timeout,
310 std::vector<incfs::ReadInfo>* pendingReadsBuffer) const {
311 pendingReadsBuffer->push_back({.bootClockTsUs = 0});
312 return android::incfs::WaitResult::HaveData;
313 }
314
Songchun Fan20d6ef22020-03-03 09:47:15 -0800315 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800316 metadata::Mount m;
317 m.mutable_storage()->set_id(100);
318 m.mutable_loader()->set_package_name("com.test");
319 m.mutable_loader()->set_arguments("com.uri");
320 const auto metadata = m.SerializeAsString();
321 m.mutable_loader()->release_arguments();
322 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800323 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800324 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800325 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800326 metadata::Storage st;
327 st.set_id(100);
328 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800329 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800330 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800331 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800332 metadata::BindPoint bp;
333 std::string destPath = "dest";
334 std::string srcPath = "src";
335 bp.set_storage_id(100);
336 bp.set_allocated_dest_path(&destPath);
337 bp.set_allocated_source_subdir(&srcPath);
338 const auto metadata = bp.SerializeAsString();
339 bp.release_source_subdir();
340 bp.release_dest_path();
341 return std::vector<char>(metadata.begin(), metadata.end());
342 }
343};
344
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700345class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700346public:
347 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700348 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700349 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
350
351 void checkPermissionSuccess() {
352 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
353 }
354 void checkPermissionFails() {
355 ON_CALL(*this, checkPermission(_, _, _))
356 .WillByDefault(
357 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
358 }
359 void initializeStartWatchingMode() {
360 ON_CALL(*this, startWatchingMode(_, _, _))
361 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
362 }
363 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
364 mStoredCallback = cb;
365 }
366
367 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700368};
369
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700370class MockJniWrapper : public JniWrapper {
371public:
372 MOCK_CONST_METHOD0(initializeForCurrentThread, void());
373
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700374 MockJniWrapper() { EXPECT_CALL(*this, initializeForCurrentThread()).Times(2); }
375};
376
377class MockLooperWrapper : public LooperWrapper {
378public:
379 MOCK_METHOD5(addFd, int(int, int, int, android::Looper_callbackFunc, void*));
380 MOCK_METHOD1(removeFd, int(int));
381 MOCK_METHOD0(wake, void());
382 MOCK_METHOD1(pollAll, int(int));
383
384 MockLooperWrapper() {
385 ON_CALL(*this, addFd(_, _, _, _, _))
386 .WillByDefault(Invoke(this, &MockLooperWrapper::storeCallback));
387 ON_CALL(*this, removeFd(_)).WillByDefault(Invoke(this, &MockLooperWrapper::clearCallback));
388 ON_CALL(*this, pollAll(_)).WillByDefault(Invoke(this, &MockLooperWrapper::sleepFor));
389 }
390
391 int storeCallback(int, int, int, android::Looper_callbackFunc callback, void* data) {
392 mCallback = callback;
393 mCallbackData = data;
394 return 0;
395 }
396
397 int clearCallback(int) {
398 mCallback = nullptr;
399 mCallbackData = nullptr;
400 return 0;
401 }
402
403 int sleepFor(int timeoutMillis) {
404 std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMillis));
405 return 0;
406 }
407
408 android::Looper_callbackFunc mCallback = nullptr;
409 void* mCallbackData = nullptr;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700410};
411
Songchun Fan3c82a302019-11-29 14:23:45 -0800412class MockServiceManager : public ServiceManagerWrapper {
413public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800414 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700415 std::unique_ptr<MockDataLoaderManager> dataLoaderManager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700416 std::unique_ptr<MockIncFs> incfs,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700417 std::unique_ptr<MockAppOpsManager> appOpsManager,
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700418 std::unique_ptr<MockJniWrapper> jni,
419 std::unique_ptr<MockLooperWrapper> looper)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800420 : mVold(std::move(vold)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700421 mDataLoaderManager(std::move(dataLoaderManager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700422 mIncFs(std::move(incfs)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700423 mAppOpsManager(std::move(appOpsManager)),
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700424 mJni(std::move(jni)),
425 mLooper(std::move(looper)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800426 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800427 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
428 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800429 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800430 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700431 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700432 std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700433 std::unique_ptr<LooperWrapper> getLooper() final { return std::move(mLooper); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800434
435private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800436 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800437 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800438 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700439 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700440 std::unique_ptr<MockJniWrapper> mJni;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700441 std::unique_ptr<MockLooperWrapper> mLooper;
Songchun Fan3c82a302019-11-29 14:23:45 -0800442};
443
444// --- IncrementalServiceTest ---
445
Songchun Fan3c82a302019-11-29 14:23:45 -0800446class IncrementalServiceTest : public testing::Test {
447public:
448 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800449 auto vold = std::make_unique<NiceMock<MockVoldService>>();
450 mVold = vold.get();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700451 sp<NiceMock<MockDataLoader>> dataLoader{new NiceMock<MockDataLoader>};
452 mDataLoader = dataLoader.get();
453 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>(dataLoader);
Songchun Fan68645c42020-02-27 15:57:35 -0800454 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800455 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
456 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700457 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
458 mAppOpsManager = appOps.get();
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700459 auto jni = std::make_unique<NiceMock<MockJniWrapper>>();
460 mJni = jni.get();
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700461 auto looper = std::make_unique<NiceMock<MockLooperWrapper>>();
462 mLooper = looper.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800463 mIncrementalService =
464 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700465 std::move(
466 dataloaderManager),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700467 std::move(incFs),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700468 std::move(appOps),
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700469 std::move(jni),
470 std::move(looper)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800471 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800472 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800473 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700474 mDataLoaderManager->unbindFromDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800475 mIncrementalService->onSystemReady();
476 }
477
478 void setUpExistingMountDir(const std::string& rootDir) {
479 const auto dir = rootDir + "/dir1";
480 const auto mountDir = dir + "/mount";
481 const auto backingDir = dir + "/backing_store";
482 const auto storageDir = mountDir + "/st0";
483 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
484 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
485 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
486 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
487 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
488 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
489 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
490 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800491 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
492 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
493 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
494 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
495 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
496 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800497 }
498
499protected:
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700500 NiceMock<MockVoldService>* mVold = nullptr;
501 NiceMock<MockIncFs>* mIncFs = nullptr;
502 NiceMock<MockDataLoaderManager>* mDataLoaderManager = nullptr;
503 NiceMock<MockAppOpsManager>* mAppOpsManager = nullptr;
504 NiceMock<MockJniWrapper>* mJni = nullptr;
505 NiceMock<MockLooperWrapper>* mLooper = nullptr;
506 NiceMock<MockDataLoader>* mDataLoader = nullptr;
Songchun Fan3c82a302019-11-29 14:23:45 -0800507 std::unique_ptr<IncrementalService> mIncrementalService;
508 TemporaryDir mRootDir;
509 DataLoaderParamsParcel mDataLoaderParcel;
510};
511
Songchun Fan3c82a302019-11-29 14:23:45 -0800512TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
513 mVold->mountIncFsFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700514 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800515 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700516 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
517 IncrementalService::CreateOptions::CreateNew,
518 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800519 ASSERT_LT(storageId, 0);
520}
521
522TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
523 mVold->mountIncFsInvalidControlParcel();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700524 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
525 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800526 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700527 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
528 IncrementalService::CreateOptions::CreateNew,
529 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800530 ASSERT_LT(storageId, 0);
531}
532
533TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
534 mVold->mountIncFsSuccess();
535 mIncFs->makeFileFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700536 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
537 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800538 EXPECT_CALL(*mVold, unmountIncFs(_));
539 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700540 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
541 IncrementalService::CreateOptions::CreateNew,
542 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800543 ASSERT_LT(storageId, 0);
544}
545
546TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
547 mVold->mountIncFsSuccess();
548 mIncFs->makeFileSuccess();
549 mVold->bindMountFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700550 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
551 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800552 EXPECT_CALL(*mVold, unmountIncFs(_));
553 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700554 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
555 IncrementalService::CreateOptions::CreateNew,
556 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800557 ASSERT_LT(storageId, 0);
558}
559
560TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
561 mVold->mountIncFsSuccess();
562 mIncFs->makeFileSuccess();
563 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700564 mDataLoaderManager->bindToDataLoaderFails();
565 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
566 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700567 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(0);
568 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
569 EXPECT_CALL(*mDataLoader, destroy(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800570 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
571 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700572 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
573 IncrementalService::CreateOptions::CreateNew,
574 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800575 ASSERT_LT(storageId, 0);
576}
577
578TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
579 mVold->mountIncFsSuccess();
580 mIncFs->makeFileSuccess();
581 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700582 mDataLoaderManager->bindToDataLoaderSuccess();
583 mDataLoaderManager->getDataLoaderSuccess();
584 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
585 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700586 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
587 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
588 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800589 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
590 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700591 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
592 IncrementalService::CreateOptions::CreateNew,
593 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800594 ASSERT_GE(storageId, 0);
595 mIncrementalService->deleteStorage(storageId);
596}
597
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700598TEST_F(IncrementalServiceTest, testDataLoaderDestroyed) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800599 mVold->mountIncFsSuccess();
600 mIncFs->makeFileSuccess();
601 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700602 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800603 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700604 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(2);
605 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700606 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
607 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
608 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800609 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
610 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700611 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
612 IncrementalService::CreateOptions::CreateNew,
613 {}, {}, {});
Songchun Fan3c82a302019-11-29 14:23:45 -0800614 ASSERT_GE(storageId, 0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700615 // Simulated crash/other connection breakage.
616 mDataLoaderManager->setDataLoaderStatusDestroyed();
617}
618
619TEST_F(IncrementalServiceTest, testStartDataLoaderCreate) {
620 mVold->mountIncFsSuccess();
621 mIncFs->makeFileSuccess();
622 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700623 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700624 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700625 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700626 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
627 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700628 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
629 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
630 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
631 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
632 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700633 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
634 IncrementalService::CreateOptions::CreateNew,
635 {}, {}, {});
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700636 ASSERT_GE(storageId, 0);
637 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800638 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700639 mDataLoaderManager->setDataLoaderStatusStarted();
640}
641
642TEST_F(IncrementalServiceTest, testStartDataLoaderPendingStart) {
643 mVold->mountIncFsSuccess();
644 mIncFs->makeFileSuccess();
645 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700646 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700647 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700648 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700649 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
650 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700651 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700652 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
653 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
654 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
655 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700656 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
657 IncrementalService::CreateOptions::CreateNew,
658 {}, {}, {});
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700659 ASSERT_GE(storageId, 0);
660 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
661 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800662}
663
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700664TEST_F(IncrementalServiceTest, testStartDataLoaderCreateUnavailable) {
665 mVold->mountIncFsSuccess();
666 mIncFs->makeFileSuccess();
667 mVold->bindMountSuccess();
668 mDataLoader->initializeCreateOkNoStatus();
669 mDataLoaderManager->bindToDataLoaderSuccess();
670 mDataLoaderManager->getDataLoaderSuccess();
671 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
672 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
673 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
674 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
675 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
676 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
677 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700678 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
679 IncrementalService::CreateOptions::CreateNew,
680 {}, {}, {});
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700681 ASSERT_GE(storageId, 0);
682 mDataLoaderManager->setDataLoaderStatusUnavailable();
683}
684
685TEST_F(IncrementalServiceTest, testStartDataLoaderRecreateOnPendingReads) {
686 mVold->mountIncFsSuccess();
687 mIncFs->makeFileSuccess();
688 mIncFs->openMountSuccess();
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700689 mIncFs->waitForPendingReadsSuccess();
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700690 mVold->bindMountSuccess();
691 mDataLoader->initializeCreateOkNoStatus();
692 mDataLoaderManager->bindToDataLoaderSuccess();
693 mDataLoaderManager->getDataLoaderSuccess();
694 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(2);
Alex Buynytskyy4dbc0602020-05-12 11:24:14 -0700695 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(2);
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700696 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
697 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
Alex Buynytskyy4dbc0602020-05-12 11:24:14 -0700698 EXPECT_CALL(*mDataLoader, destroy(_)).Times(2);
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700699 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
700 EXPECT_CALL(*mLooper, addFd(MockIncFs::kPendingReadsFd, _, _, _, _)).Times(1);
701 EXPECT_CALL(*mLooper, removeFd(MockIncFs::kPendingReadsFd)).Times(1);
702 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700703 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
704 IncrementalService::CreateOptions::CreateNew,
705 {}, {}, {});
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700706 ASSERT_GE(storageId, 0);
707 mDataLoaderManager->setDataLoaderStatusUnavailable();
708 ASSERT_NE(nullptr, mLooper->mCallback);
709 ASSERT_NE(nullptr, mLooper->mCallbackData);
710 mLooper->mCallback(-1, -1, mLooper->mCallbackData);
711}
712
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700713TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
714 mVold->mountIncFsSuccess();
715 mIncFs->makeFileSuccess();
716 mVold->bindMountSuccess();
717 mVold->setIncFsMountOptionsSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700718 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700719 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700720 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700721 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700722 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700723 // We are calling setIncFsMountOptions(true).
724 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
725 // After setIncFsMountOptions succeeded expecting to start watching.
726 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
727 // Not expecting callback removal.
728 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700729 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700730 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
731 IncrementalService::CreateOptions::CreateNew,
732 {}, {}, {});
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700733 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700734 ASSERT_GE(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700735}
736
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700737TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
738 mVold->mountIncFsSuccess();
739 mIncFs->makeFileSuccess();
740 mVold->bindMountSuccess();
741 mVold->setIncFsMountOptionsSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700742 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700743 mDataLoaderManager->getDataLoaderSuccess();
744 mAppOpsManager->checkPermissionSuccess();
745 mAppOpsManager->initializeStartWatchingMode();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700746 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700747 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
748 // We are calling setIncFsMountOptions(true).
749 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
750 // setIncFsMountOptions(false) is called on the callback.
751 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
752 // After setIncFsMountOptions succeeded expecting to start watching.
753 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
754 // After callback is called, disable read logs and remove callback.
755 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
756 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700757 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
758 IncrementalService::CreateOptions::CreateNew,
759 {}, {}, {});
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700760 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700761 ASSERT_GE(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700762 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
763 mAppOpsManager->mStoredCallback->opChanged(0, {});
764}
765
766TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
767 mVold->mountIncFsSuccess();
768 mIncFs->makeFileSuccess();
769 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700770 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700771 mDataLoaderManager->getDataLoaderSuccess();
772 mAppOpsManager->checkPermissionFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700773 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700774 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
775 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
776 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
777 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
778 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
779 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700780 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
781 IncrementalService::CreateOptions::CreateNew,
782 {}, {}, {});
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700783 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700784 ASSERT_LT(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700785}
786
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700787TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
788 mVold->mountIncFsSuccess();
789 mIncFs->makeFileSuccess();
790 mVold->bindMountSuccess();
791 mVold->setIncFsMountOptionsFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700792 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700793 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700794 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700795 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700796 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700797 // We are calling setIncFsMountOptions.
798 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
799 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
800 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
801 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700802 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700803 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
804 IncrementalService::CreateOptions::CreateNew,
805 {}, {}, {});
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700806 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700807 ASSERT_LT(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700808}
809
Songchun Fan3c82a302019-11-29 14:23:45 -0800810TEST_F(IncrementalServiceTest, testMakeDirectory) {
811 mVold->mountIncFsSuccess();
812 mIncFs->makeFileSuccess();
813 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700814 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800815 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800816 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700817 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
818 IncrementalService::CreateOptions::CreateNew,
819 {}, {}, {});
Songchun Fan103ba1d2020-02-03 17:32:32 -0800820 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800821
Songchun Fan103ba1d2020-02-03 17:32:32 -0800822 // Expecting incfs to call makeDir on a path like:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700823 // <root>/*/mount/<storage>/test
824 EXPECT_CALL(*mIncFs,
825 makeDir(_, Truly([&](std::string_view arg) {
826 return arg.starts_with(mRootDir.path) &&
827 arg.ends_with("/mount/st_1_0/" + dir_path);
828 }),
829 _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800830 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
831 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800832}
833
834TEST_F(IncrementalServiceTest, testMakeDirectories) {
835 mVold->mountIncFsSuccess();
836 mIncFs->makeFileSuccess();
837 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700838 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800839 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800840 TemporaryDir tempDir;
Alex Buynytskyy8ef61ae2020-05-08 16:18:52 -0700841 int storageId = mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel),
842 IncrementalService::CreateOptions::CreateNew,
843 {}, {}, {});
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800844 auto first = "first"sv;
845 auto second = "second"sv;
846 auto third = "third"sv;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700847 auto dir_path = std::string(first) + "/" + std::string(second) + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800848
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700849 EXPECT_CALL(*mIncFs,
850 makeDirs(_, Truly([&](std::string_view arg) {
851 return arg.starts_with(mRootDir.path) &&
852 arg.ends_with("/mount/st_1_0/" + dir_path);
853 }),
854 _));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700855 auto res = mIncrementalService->makeDirs(storageId, dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800856 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800857}
858} // namespace android::os::incremental