blob: 325218dfa6a5c853efefb57462eed584ebd8748a [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));
287
288 MockIncFs() { ON_CALL(*this, listExistingMounts(_)).WillByDefault(Return()); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800289
290 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
291 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700292 void openMountSuccess() {
293 ON_CALL(*this, openMount(_)).WillByDefault(Invoke(this, &MockIncFs::openMountForHealth));
294 }
295
296 static constexpr auto kPendingReadsFd = 42;
297 Control openMountForHealth(std::string_view) {
298 return UniqueControl(IncFs_CreateControl(-1, kPendingReadsFd, -1));
299 }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700300
Songchun Fan20d6ef22020-03-03 09:47:15 -0800301 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800302 metadata::Mount m;
303 m.mutable_storage()->set_id(100);
304 m.mutable_loader()->set_package_name("com.test");
305 m.mutable_loader()->set_arguments("com.uri");
306 const auto metadata = m.SerializeAsString();
307 m.mutable_loader()->release_arguments();
308 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800309 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800310 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800311 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800312 metadata::Storage st;
313 st.set_id(100);
314 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800315 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800316 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800317 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800318 metadata::BindPoint bp;
319 std::string destPath = "dest";
320 std::string srcPath = "src";
321 bp.set_storage_id(100);
322 bp.set_allocated_dest_path(&destPath);
323 bp.set_allocated_source_subdir(&srcPath);
324 const auto metadata = bp.SerializeAsString();
325 bp.release_source_subdir();
326 bp.release_dest_path();
327 return std::vector<char>(metadata.begin(), metadata.end());
328 }
329};
330
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700331class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700332public:
333 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700334 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700335 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
336
337 void checkPermissionSuccess() {
338 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
339 }
340 void checkPermissionFails() {
341 ON_CALL(*this, checkPermission(_, _, _))
342 .WillByDefault(
343 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
344 }
345 void initializeStartWatchingMode() {
346 ON_CALL(*this, startWatchingMode(_, _, _))
347 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
348 }
349 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
350 mStoredCallback = cb;
351 }
352
353 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700354};
355
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700356class MockJniWrapper : public JniWrapper {
357public:
358 MOCK_CONST_METHOD0(initializeForCurrentThread, void());
359
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700360 MockJniWrapper() { EXPECT_CALL(*this, initializeForCurrentThread()).Times(2); }
361};
362
363class MockLooperWrapper : public LooperWrapper {
364public:
365 MOCK_METHOD5(addFd, int(int, int, int, android::Looper_callbackFunc, void*));
366 MOCK_METHOD1(removeFd, int(int));
367 MOCK_METHOD0(wake, void());
368 MOCK_METHOD1(pollAll, int(int));
369
370 MockLooperWrapper() {
371 ON_CALL(*this, addFd(_, _, _, _, _))
372 .WillByDefault(Invoke(this, &MockLooperWrapper::storeCallback));
373 ON_CALL(*this, removeFd(_)).WillByDefault(Invoke(this, &MockLooperWrapper::clearCallback));
374 ON_CALL(*this, pollAll(_)).WillByDefault(Invoke(this, &MockLooperWrapper::sleepFor));
375 }
376
377 int storeCallback(int, int, int, android::Looper_callbackFunc callback, void* data) {
378 mCallback = callback;
379 mCallbackData = data;
380 return 0;
381 }
382
383 int clearCallback(int) {
384 mCallback = nullptr;
385 mCallbackData = nullptr;
386 return 0;
387 }
388
389 int sleepFor(int timeoutMillis) {
390 std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMillis));
391 return 0;
392 }
393
394 android::Looper_callbackFunc mCallback = nullptr;
395 void* mCallbackData = nullptr;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700396};
397
Songchun Fan3c82a302019-11-29 14:23:45 -0800398class MockServiceManager : public ServiceManagerWrapper {
399public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800400 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700401 std::unique_ptr<MockDataLoaderManager> dataLoaderManager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700402 std::unique_ptr<MockIncFs> incfs,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700403 std::unique_ptr<MockAppOpsManager> appOpsManager,
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700404 std::unique_ptr<MockJniWrapper> jni,
405 std::unique_ptr<MockLooperWrapper> looper)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800406 : mVold(std::move(vold)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700407 mDataLoaderManager(std::move(dataLoaderManager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700408 mIncFs(std::move(incfs)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700409 mAppOpsManager(std::move(appOpsManager)),
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700410 mJni(std::move(jni)),
411 mLooper(std::move(looper)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800412 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800413 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
414 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800415 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800416 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700417 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700418 std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700419 std::unique_ptr<LooperWrapper> getLooper() final { return std::move(mLooper); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800420
421private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800422 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800423 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800424 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700425 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700426 std::unique_ptr<MockJniWrapper> mJni;
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700427 std::unique_ptr<MockLooperWrapper> mLooper;
Songchun Fan3c82a302019-11-29 14:23:45 -0800428};
429
430// --- IncrementalServiceTest ---
431
Songchun Fan3c82a302019-11-29 14:23:45 -0800432class IncrementalServiceTest : public testing::Test {
433public:
434 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800435 auto vold = std::make_unique<NiceMock<MockVoldService>>();
436 mVold = vold.get();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700437 sp<NiceMock<MockDataLoader>> dataLoader{new NiceMock<MockDataLoader>};
438 mDataLoader = dataLoader.get();
439 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>(dataLoader);
Songchun Fan68645c42020-02-27 15:57:35 -0800440 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800441 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
442 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700443 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
444 mAppOpsManager = appOps.get();
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700445 auto jni = std::make_unique<NiceMock<MockJniWrapper>>();
446 mJni = jni.get();
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700447 auto looper = std::make_unique<NiceMock<MockLooperWrapper>>();
448 mLooper = looper.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800449 mIncrementalService =
450 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700451 std::move(
452 dataloaderManager),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700453 std::move(incFs),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700454 std::move(appOps),
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700455 std::move(jni),
456 std::move(looper)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800457 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800458 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800459 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700460 mDataLoaderManager->unbindFromDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800461 mIncrementalService->onSystemReady();
462 }
463
464 void setUpExistingMountDir(const std::string& rootDir) {
465 const auto dir = rootDir + "/dir1";
466 const auto mountDir = dir + "/mount";
467 const auto backingDir = dir + "/backing_store";
468 const auto storageDir = mountDir + "/st0";
469 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
470 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
471 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
472 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
473 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
474 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
475 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
476 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800477 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
478 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
479 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
480 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
481 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
482 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800483 }
484
485protected:
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700486 NiceMock<MockVoldService>* mVold = nullptr;
487 NiceMock<MockIncFs>* mIncFs = nullptr;
488 NiceMock<MockDataLoaderManager>* mDataLoaderManager = nullptr;
489 NiceMock<MockAppOpsManager>* mAppOpsManager = nullptr;
490 NiceMock<MockJniWrapper>* mJni = nullptr;
491 NiceMock<MockLooperWrapper>* mLooper = nullptr;
492 NiceMock<MockDataLoader>* mDataLoader = nullptr;
Songchun Fan3c82a302019-11-29 14:23:45 -0800493 std::unique_ptr<IncrementalService> mIncrementalService;
494 TemporaryDir mRootDir;
495 DataLoaderParamsParcel mDataLoaderParcel;
496};
497
Songchun Fan3c82a302019-11-29 14:23:45 -0800498TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
499 mVold->mountIncFsFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700500 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800501 TemporaryDir tempDir;
502 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800503 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800504 IncrementalService::CreateOptions::CreateNew);
505 ASSERT_LT(storageId, 0);
506}
507
508TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
509 mVold->mountIncFsInvalidControlParcel();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700510 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
511 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800512 TemporaryDir tempDir;
513 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800514 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800515 IncrementalService::CreateOptions::CreateNew);
516 ASSERT_LT(storageId, 0);
517}
518
519TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
520 mVold->mountIncFsSuccess();
521 mIncFs->makeFileFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700522 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(0);
523 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800524 EXPECT_CALL(*mVold, unmountIncFs(_));
525 TemporaryDir tempDir;
526 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800527 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800528 IncrementalService::CreateOptions::CreateNew);
529 ASSERT_LT(storageId, 0);
530}
531
532TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
533 mVold->mountIncFsSuccess();
534 mIncFs->makeFileSuccess();
535 mVold->bindMountFails();
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;
540 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800541 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800542 IncrementalService::CreateOptions::CreateNew);
543 ASSERT_LT(storageId, 0);
544}
545
546TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
547 mVold->mountIncFsSuccess();
548 mIncFs->makeFileSuccess();
549 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700550 mDataLoaderManager->bindToDataLoaderFails();
551 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
552 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700553 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(0);
554 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
555 EXPECT_CALL(*mDataLoader, destroy(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800556 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
557 TemporaryDir tempDir;
558 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800559 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800560 IncrementalService::CreateOptions::CreateNew);
561 ASSERT_LT(storageId, 0);
562}
563
564TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
565 mVold->mountIncFsSuccess();
566 mIncFs->makeFileSuccess();
567 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700568 mDataLoaderManager->bindToDataLoaderSuccess();
569 mDataLoaderManager->getDataLoaderSuccess();
570 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
571 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700572 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
573 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
574 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800575 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
576 TemporaryDir tempDir;
577 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800578 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800579 IncrementalService::CreateOptions::CreateNew);
580 ASSERT_GE(storageId, 0);
581 mIncrementalService->deleteStorage(storageId);
582}
583
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700584TEST_F(IncrementalServiceTest, testDataLoaderDestroyed) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800585 mVold->mountIncFsSuccess();
586 mIncFs->makeFileSuccess();
587 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700588 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800589 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700590 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(2);
591 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700592 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
593 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
594 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800595 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
596 TemporaryDir tempDir;
597 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800598 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800599 IncrementalService::CreateOptions::CreateNew);
600 ASSERT_GE(storageId, 0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700601 // Simulated crash/other connection breakage.
602 mDataLoaderManager->setDataLoaderStatusDestroyed();
603}
604
605TEST_F(IncrementalServiceTest, testStartDataLoaderCreate) {
606 mVold->mountIncFsSuccess();
607 mIncFs->makeFileSuccess();
608 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700609 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700610 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700611 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700612 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
613 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700614 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
615 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
616 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
617 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
618 TemporaryDir tempDir;
619 int storageId =
620 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
621 IncrementalService::CreateOptions::CreateNew);
622 ASSERT_GE(storageId, 0);
623 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800624 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700625 mDataLoaderManager->setDataLoaderStatusStarted();
626}
627
628TEST_F(IncrementalServiceTest, testStartDataLoaderPendingStart) {
629 mVold->mountIncFsSuccess();
630 mIncFs->makeFileSuccess();
631 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700632 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700633 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700634 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700635 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
636 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700637 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700638 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
639 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
640 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
641 TemporaryDir tempDir;
642 int storageId =
643 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
644 IncrementalService::CreateOptions::CreateNew);
645 ASSERT_GE(storageId, 0);
646 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
647 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800648}
649
Alex Buynytskyycca2c112020-05-05 12:48:41 -0700650TEST_F(IncrementalServiceTest, testStartDataLoaderCreateUnavailable) {
651 mVold->mountIncFsSuccess();
652 mIncFs->makeFileSuccess();
653 mVold->bindMountSuccess();
654 mDataLoader->initializeCreateOkNoStatus();
655 mDataLoaderManager->bindToDataLoaderSuccess();
656 mDataLoaderManager->getDataLoaderSuccess();
657 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(1);
658 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
659 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
660 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
661 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
662 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
663 TemporaryDir tempDir;
664 int storageId =
665 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
666 IncrementalService::CreateOptions::CreateNew);
667 ASSERT_GE(storageId, 0);
668 mDataLoaderManager->setDataLoaderStatusUnavailable();
669}
670
671TEST_F(IncrementalServiceTest, testStartDataLoaderRecreateOnPendingReads) {
672 mVold->mountIncFsSuccess();
673 mIncFs->makeFileSuccess();
674 mIncFs->openMountSuccess();
675 mVold->bindMountSuccess();
676 mDataLoader->initializeCreateOkNoStatus();
677 mDataLoaderManager->bindToDataLoaderSuccess();
678 mDataLoaderManager->getDataLoaderSuccess();
679 EXPECT_CALL(*mDataLoaderManager, bindToDataLoader(_, _, _, _)).Times(2);
680 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_)).Times(1);
681 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
682 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
683 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
684 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
685 EXPECT_CALL(*mLooper, addFd(MockIncFs::kPendingReadsFd, _, _, _, _)).Times(1);
686 EXPECT_CALL(*mLooper, removeFd(MockIncFs::kPendingReadsFd)).Times(1);
687 TemporaryDir tempDir;
688 int storageId =
689 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
690 IncrementalService::CreateOptions::CreateNew);
691 ASSERT_GE(storageId, 0);
692 mDataLoaderManager->setDataLoaderStatusUnavailable();
693 ASSERT_NE(nullptr, mLooper->mCallback);
694 ASSERT_NE(nullptr, mLooper->mCallbackData);
695 mLooper->mCallback(-1, -1, mLooper->mCallbackData);
696}
697
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700698TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
699 mVold->mountIncFsSuccess();
700 mIncFs->makeFileSuccess();
701 mVold->bindMountSuccess();
702 mVold->setIncFsMountOptionsSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700703 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700704 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700705 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700706 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700707 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700708 // We are calling setIncFsMountOptions(true).
709 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
710 // After setIncFsMountOptions succeeded expecting to start watching.
711 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
712 // Not expecting callback removal.
713 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700714 TemporaryDir tempDir;
715 int storageId =
716 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
717 IncrementalService::CreateOptions::CreateNew);
718 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700719 ASSERT_GE(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700720}
721
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700722TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
723 mVold->mountIncFsSuccess();
724 mIncFs->makeFileSuccess();
725 mVold->bindMountSuccess();
726 mVold->setIncFsMountOptionsSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700727 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700728 mDataLoaderManager->getDataLoaderSuccess();
729 mAppOpsManager->checkPermissionSuccess();
730 mAppOpsManager->initializeStartWatchingMode();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700731 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700732 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
733 // We are calling setIncFsMountOptions(true).
734 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
735 // setIncFsMountOptions(false) is called on the callback.
736 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
737 // After setIncFsMountOptions succeeded expecting to start watching.
738 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
739 // After callback is called, disable read logs and remove callback.
740 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
741 TemporaryDir tempDir;
742 int storageId =
743 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
744 IncrementalService::CreateOptions::CreateNew);
745 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700746 ASSERT_GE(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700747 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
748 mAppOpsManager->mStoredCallback->opChanged(0, {});
749}
750
751TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
752 mVold->mountIncFsSuccess();
753 mIncFs->makeFileSuccess();
754 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700755 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700756 mDataLoaderManager->getDataLoaderSuccess();
757 mAppOpsManager->checkPermissionFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700758 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700759 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
760 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
761 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
762 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
763 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
764 TemporaryDir tempDir;
765 int storageId =
766 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
767 IncrementalService::CreateOptions::CreateNew);
768 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700769 ASSERT_LT(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700770}
771
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700772TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
773 mVold->mountIncFsSuccess();
774 mIncFs->makeFileSuccess();
775 mVold->bindMountSuccess();
776 mVold->setIncFsMountOptionsFails();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700777 mDataLoaderManager->bindToDataLoaderSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700778 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700779 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700780 EXPECT_CALL(*mDataLoaderManager, unbindFromDataLoader(_));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700781 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700782 // We are calling setIncFsMountOptions.
783 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
784 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
785 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
786 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700787 TemporaryDir tempDir;
788 int storageId =
789 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
790 IncrementalService::CreateOptions::CreateNew);
791 ASSERT_GE(storageId, 0);
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700792 ASSERT_LT(mDataLoader->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700793}
794
Songchun Fan3c82a302019-11-29 14:23:45 -0800795TEST_F(IncrementalServiceTest, testMakeDirectory) {
796 mVold->mountIncFsSuccess();
797 mIncFs->makeFileSuccess();
798 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700799 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800800 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800801 TemporaryDir tempDir;
802 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800803 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800804 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800805 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800806
Songchun Fan103ba1d2020-02-03 17:32:32 -0800807 // Expecting incfs to call makeDir on a path like:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700808 // <root>/*/mount/<storage>/test
809 EXPECT_CALL(*mIncFs,
810 makeDir(_, Truly([&](std::string_view arg) {
811 return arg.starts_with(mRootDir.path) &&
812 arg.ends_with("/mount/st_1_0/" + dir_path);
813 }),
814 _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800815 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
816 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800817}
818
819TEST_F(IncrementalServiceTest, testMakeDirectories) {
820 mVold->mountIncFsSuccess();
821 mIncFs->makeFileSuccess();
822 mVold->bindMountSuccess();
Alex Buynytskyyea1390f2020-04-22 16:08:50 -0700823 mDataLoaderManager->bindToDataLoaderSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800824 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800825 TemporaryDir tempDir;
826 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800827 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800828 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800829 auto first = "first"sv;
830 auto second = "second"sv;
831 auto third = "third"sv;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700832 auto dir_path = std::string(first) + "/" + std::string(second) + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800833
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700834 EXPECT_CALL(*mIncFs,
835 makeDirs(_, Truly([&](std::string_view arg) {
836 return arg.starts_with(mRootDir.path) &&
837 arg.ends_with("/mount/st_1_0/" + dir_path);
838 }),
839 _));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700840 auto res = mIncrementalService->makeDirs(storageId, dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800841 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800842}
843} // namespace android::os::incremental