blob: 7a8560221beb56198afa5e6d7b02eefda25a052c [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
134 binder::Status createOk(int32_t id, const content::pm::DataLoaderParamsParcel&,
135 const content::pm::FileSystemControlParcel&,
136 const sp<content::pm::IDataLoaderStatusListener>& listener) {
137 mListener = listener;
138 if (mListener) {
139 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_CREATED);
140 }
141 return binder::Status::ok();
142 }
143 binder::Status createOkNoStatus(int32_t id, const content::pm::DataLoaderParamsParcel&,
144 const content::pm::FileSystemControlParcel&,
145 const sp<content::pm::IDataLoaderStatusListener>& listener) {
146 mListener = listener;
147 return binder::Status::ok();
148 }
149 binder::Status startOk(int32_t id) {
150 if (mListener) {
151 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STARTED);
152 }
153 return binder::Status::ok();
154 }
155 binder::Status stopOk(int32_t id) {
156 if (mListener) {
157 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STOPPED);
158 }
159 return binder::Status::ok();
160 }
161 binder::Status destroyOk(int32_t id) {
162 if (mListener) {
163 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
164 }
165 mListener = nullptr;
166 return binder::Status::ok();
167 }
168 binder::Status prepareImageOk(int32_t id,
169 const ::std::vector<content::pm::InstallationFileParcel>&,
170 const ::std::vector<::std::string>&) {
171 if (mListener) {
172 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY);
173 }
174 return binder::Status::ok();
175 }
176
177private:
178 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800179};
180
181class MockDataLoaderManager : public DataLoaderManagerWrapper {
182public:
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700183 MockDataLoaderManager(sp<IDataLoader> dataLoader) : mDataLoaderHolder(std::move(dataLoader)) {
184 EXPECT_TRUE(mDataLoaderHolder != nullptr);
185 }
186
Songchun Fan68645c42020-02-27 15:57:35 -0800187 MOCK_CONST_METHOD5(initializeDataLoader,
188 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
189 const FileSystemControlParcel& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800190 const sp<IDataLoaderStatusListener>& listener,
191 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800192 MOCK_CONST_METHOD2(getDataLoader,
193 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Songchun Fan3c82a302019-11-29 14:23:45 -0800194 MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800195
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700196 void initializeDataLoaderSuccess() {
197 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
198 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
199 }
200 void initializeDataLoaderFails() {
201 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
202 .WillByDefault(Return(
203 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
204 }
205 void getDataLoaderSuccess() {
206 ON_CALL(*this, getDataLoader(_, _))
207 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
208 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700209 void destroyDataLoaderSuccess() {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700210 ON_CALL(*this, destroyDataLoader(_))
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700211 .WillByDefault(Invoke(this, &MockDataLoaderManager::destroyDataLoaderOk));
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700212 }
Songchun Fan68645c42020-02-27 15:57:35 -0800213 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
214 const FileSystemControlParcel& control,
215 const sp<IDataLoaderStatusListener>& listener,
216 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800217 mId = mountId;
218 mListener = listener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700219 mServiceConnector = control.service;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700220 mDataLoader = mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800221 *_aidl_return = true;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700222 return mDataLoader->create(mountId, params, control, listener);
Songchun Fan3c82a302019-11-29 14:23:45 -0800223 }
Songchun Fan68645c42020-02-27 15:57:35 -0800224 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
225 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800226 return binder::Status::ok();
227 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700228 void setDataLoaderStatusCreated() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800229 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800230 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700231 void setDataLoaderStatusStarted() {
232 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_STARTED);
233 }
234 void setDataLoaderStatusDestroyed() {
235 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
236 }
237 binder::Status destroyDataLoaderOk(int32_t id) {
238 if (mDataLoader) {
239 if (auto status = mDataLoader->destroy(id); !status.isOk()) {
240 return status;
241 }
242 mDataLoader = nullptr;
243 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700244 if (mListener) {
245 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
246 }
247 return binder::Status::ok();
248 }
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700249 int32_t setStorageParams(bool enableReadLogs) {
250 int32_t result = -1;
251 EXPECT_NE(mServiceConnector.get(), nullptr);
252 EXPECT_TRUE(mServiceConnector->setStorageParams(enableReadLogs, &result).isOk());
253 return result;
254 }
255
Songchun Fan3c82a302019-11-29 14:23:45 -0800256private:
257 int mId;
258 sp<IDataLoaderStatusListener> mListener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700259 sp<IIncrementalServiceConnector> mServiceConnector;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700260 sp<IDataLoader> mDataLoader;
261 sp<IDataLoader> mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800262};
263
264class MockIncFs : public IncFsWrapper {
265public:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700266 MOCK_CONST_METHOD1(listExistingMounts, void(const ExistingMountCallback& cb));
267 MOCK_CONST_METHOD1(openMount, Control(std::string_view path));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800268 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800269 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800270 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800271 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800272 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700273 MOCK_CONST_METHOD3(makeDirs,
274 ErrorCode(const Control& control, std::string_view path, int mode));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800275 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
276 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
277 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800278 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800279 ErrorCode(const Control& control, std::string_view from, std::string_view to));
280 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700281 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700282 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(std::span<const DataBlock> blocks));
283
284 MockIncFs() { ON_CALL(*this, listExistingMounts(_)).WillByDefault(Return()); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800285
286 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
287 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700288
Songchun Fan20d6ef22020-03-03 09:47:15 -0800289 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800290 metadata::Mount m;
291 m.mutable_storage()->set_id(100);
292 m.mutable_loader()->set_package_name("com.test");
293 m.mutable_loader()->set_arguments("com.uri");
294 const auto metadata = m.SerializeAsString();
295 m.mutable_loader()->release_arguments();
296 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800297 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800298 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800299 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800300 metadata::Storage st;
301 st.set_id(100);
302 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800303 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800304 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800305 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800306 metadata::BindPoint bp;
307 std::string destPath = "dest";
308 std::string srcPath = "src";
309 bp.set_storage_id(100);
310 bp.set_allocated_dest_path(&destPath);
311 bp.set_allocated_source_subdir(&srcPath);
312 const auto metadata = bp.SerializeAsString();
313 bp.release_source_subdir();
314 bp.release_dest_path();
315 return std::vector<char>(metadata.begin(), metadata.end());
316 }
317};
318
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700319class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700320public:
321 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700322 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700323 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
324
325 void checkPermissionSuccess() {
326 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
327 }
328 void checkPermissionFails() {
329 ON_CALL(*this, checkPermission(_, _, _))
330 .WillByDefault(
331 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
332 }
333 void initializeStartWatchingMode() {
334 ON_CALL(*this, startWatchingMode(_, _, _))
335 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
336 }
337 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
338 mStoredCallback = cb;
339 }
340
341 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700342};
343
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700344class MockJniWrapper : public JniWrapper {
345public:
346 MOCK_CONST_METHOD0(initializeForCurrentThread, void());
347
348 MockJniWrapper() { EXPECT_CALL(*this, initializeForCurrentThread()).Times(1); }
349};
350
Songchun Fan3c82a302019-11-29 14:23:45 -0800351class MockServiceManager : public ServiceManagerWrapper {
352public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800353 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700354 std::unique_ptr<MockDataLoaderManager> dataLoaderManager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700355 std::unique_ptr<MockIncFs> incfs,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700356 std::unique_ptr<MockAppOpsManager> appOpsManager,
357 std::unique_ptr<MockJniWrapper> jni)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800358 : mVold(std::move(vold)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700359 mDataLoaderManager(std::move(dataLoaderManager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700360 mIncFs(std::move(incfs)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700361 mAppOpsManager(std::move(appOpsManager)),
362 mJni(std::move(jni)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800363 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800364 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
365 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800366 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800367 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700368 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700369 std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800370
371private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800372 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800373 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800374 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700375 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700376 std::unique_ptr<MockJniWrapper> mJni;
Songchun Fan3c82a302019-11-29 14:23:45 -0800377};
378
379// --- IncrementalServiceTest ---
380
Songchun Fan3c82a302019-11-29 14:23:45 -0800381class IncrementalServiceTest : public testing::Test {
382public:
383 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800384 auto vold = std::make_unique<NiceMock<MockVoldService>>();
385 mVold = vold.get();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700386 sp<NiceMock<MockDataLoader>> dataLoader{new NiceMock<MockDataLoader>};
387 mDataLoader = dataLoader.get();
388 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>(dataLoader);
Songchun Fan68645c42020-02-27 15:57:35 -0800389 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800390 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
391 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700392 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
393 mAppOpsManager = appOps.get();
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700394 auto jni = std::make_unique<NiceMock<MockJniWrapper>>();
395 mJni = jni.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800396 mIncrementalService =
397 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700398 std::move(
399 dataloaderManager),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700400 std::move(incFs),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700401 std::move(appOps),
402 std::move(jni)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800403 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800404 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800405 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700406 mDataLoaderManager->destroyDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800407 mIncrementalService->onSystemReady();
408 }
409
410 void setUpExistingMountDir(const std::string& rootDir) {
411 const auto dir = rootDir + "/dir1";
412 const auto mountDir = dir + "/mount";
413 const auto backingDir = dir + "/backing_store";
414 const auto storageDir = mountDir + "/st0";
415 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
416 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
417 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
418 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
419 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
420 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
421 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
422 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800423 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
424 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
425 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
426 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
427 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
428 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800429 }
430
431protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800432 NiceMock<MockVoldService>* mVold;
433 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800434 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700435 NiceMock<MockAppOpsManager>* mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700436 NiceMock<MockJniWrapper>* mJni;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700437 NiceMock<MockDataLoader>* mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800438 std::unique_ptr<IncrementalService> mIncrementalService;
439 TemporaryDir mRootDir;
440 DataLoaderParamsParcel mDataLoaderParcel;
441};
442
Songchun Fan3c82a302019-11-29 14:23:45 -0800443TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
444 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800445 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800446 TemporaryDir tempDir;
447 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800448 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800449 IncrementalService::CreateOptions::CreateNew);
450 ASSERT_LT(storageId, 0);
451}
452
453TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
454 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800455 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700456 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800457 TemporaryDir tempDir;
458 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800459 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800460 IncrementalService::CreateOptions::CreateNew);
461 ASSERT_LT(storageId, 0);
462}
463
464TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
465 mVold->mountIncFsSuccess();
466 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800467 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700468 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800469 EXPECT_CALL(*mVold, unmountIncFs(_));
470 TemporaryDir tempDir;
471 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800472 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800473 IncrementalService::CreateOptions::CreateNew);
474 ASSERT_LT(storageId, 0);
475}
476
477TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
478 mVold->mountIncFsSuccess();
479 mIncFs->makeFileSuccess();
480 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800481 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700482 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800483 EXPECT_CALL(*mVold, unmountIncFs(_));
484 TemporaryDir tempDir;
485 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800486 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800487 IncrementalService::CreateOptions::CreateNew);
488 ASSERT_LT(storageId, 0);
489}
490
491TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
492 mVold->mountIncFsSuccess();
493 mIncFs->makeFileSuccess();
494 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800495 mDataLoaderManager->initializeDataLoaderFails();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700496 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700497 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700498 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(0);
499 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
500 EXPECT_CALL(*mDataLoader, destroy(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800501 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
502 TemporaryDir tempDir;
503 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800504 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800505 IncrementalService::CreateOptions::CreateNew);
506 ASSERT_LT(storageId, 0);
507}
508
509TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
510 mVold->mountIncFsSuccess();
511 mIncFs->makeFileSuccess();
512 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800513 mDataLoaderManager->initializeDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700514 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700515 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700516 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
517 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
518 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800519 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
520 TemporaryDir tempDir;
521 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800522 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800523 IncrementalService::CreateOptions::CreateNew);
524 ASSERT_GE(storageId, 0);
525 mIncrementalService->deleteStorage(storageId);
526}
527
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700528TEST_F(IncrementalServiceTest, testDataLoaderDestroyed) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800529 mVold->mountIncFsSuccess();
530 mIncFs->makeFileSuccess();
531 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800532 mDataLoaderManager->initializeDataLoaderSuccess();
533 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700534 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
535 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
536 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
537 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
538 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800539 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
540 TemporaryDir tempDir;
541 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800542 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800543 IncrementalService::CreateOptions::CreateNew);
544 ASSERT_GE(storageId, 0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700545 // Simulated crash/other connection breakage.
546 mDataLoaderManager->setDataLoaderStatusDestroyed();
547}
548
549TEST_F(IncrementalServiceTest, testStartDataLoaderCreate) {
550 mVold->mountIncFsSuccess();
551 mIncFs->makeFileSuccess();
552 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700553 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700554 mDataLoaderManager->initializeDataLoaderSuccess();
555 mDataLoaderManager->getDataLoaderSuccess();
556 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
557 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
558 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
559 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
560 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
561 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
562 TemporaryDir tempDir;
563 int storageId =
564 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
565 IncrementalService::CreateOptions::CreateNew);
566 ASSERT_GE(storageId, 0);
567 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800568 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700569 mDataLoaderManager->setDataLoaderStatusStarted();
570}
571
572TEST_F(IncrementalServiceTest, testStartDataLoaderPendingStart) {
573 mVold->mountIncFsSuccess();
574 mIncFs->makeFileSuccess();
575 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700576 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700577 mDataLoaderManager->initializeDataLoaderSuccess();
578 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700579 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700580 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700581 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700582 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
583 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
584 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
585 TemporaryDir tempDir;
586 int storageId =
587 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
588 IncrementalService::CreateOptions::CreateNew);
589 ASSERT_GE(storageId, 0);
590 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
591 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800592}
593
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700594TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
595 mVold->mountIncFsSuccess();
596 mIncFs->makeFileSuccess();
597 mVold->bindMountSuccess();
598 mVold->setIncFsMountOptionsSuccess();
599 mDataLoaderManager->initializeDataLoaderSuccess();
600 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700601 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700602 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
603 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700604 // We are calling setIncFsMountOptions(true).
605 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
606 // After setIncFsMountOptions succeeded expecting to start watching.
607 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
608 // Not expecting callback removal.
609 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700610 TemporaryDir tempDir;
611 int storageId =
612 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
613 IncrementalService::CreateOptions::CreateNew);
614 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700615 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700616}
617
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700618TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
619 mVold->mountIncFsSuccess();
620 mIncFs->makeFileSuccess();
621 mVold->bindMountSuccess();
622 mVold->setIncFsMountOptionsSuccess();
623 mDataLoaderManager->initializeDataLoaderSuccess();
624 mDataLoaderManager->getDataLoaderSuccess();
625 mAppOpsManager->checkPermissionSuccess();
626 mAppOpsManager->initializeStartWatchingMode();
627 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
628 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
629 // We are calling setIncFsMountOptions(true).
630 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
631 // setIncFsMountOptions(false) is called on the callback.
632 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
633 // After setIncFsMountOptions succeeded expecting to start watching.
634 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
635 // After callback is called, disable read logs and remove callback.
636 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
637 TemporaryDir tempDir;
638 int storageId =
639 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
640 IncrementalService::CreateOptions::CreateNew);
641 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700642 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700643 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
644 mAppOpsManager->mStoredCallback->opChanged(0, {});
645}
646
647TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
648 mVold->mountIncFsSuccess();
649 mIncFs->makeFileSuccess();
650 mVold->bindMountSuccess();
651 mDataLoaderManager->initializeDataLoaderSuccess();
652 mDataLoaderManager->getDataLoaderSuccess();
653 mAppOpsManager->checkPermissionFails();
654 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
655 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
656 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
657 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
658 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
659 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
660 TemporaryDir tempDir;
661 int storageId =
662 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
663 IncrementalService::CreateOptions::CreateNew);
664 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700665 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700666}
667
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700668TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
669 mVold->mountIncFsSuccess();
670 mIncFs->makeFileSuccess();
671 mVold->bindMountSuccess();
672 mVold->setIncFsMountOptionsFails();
673 mDataLoaderManager->initializeDataLoaderSuccess();
674 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700675 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700676 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
677 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700678 // We are calling setIncFsMountOptions.
679 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
680 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
681 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
682 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700683 TemporaryDir tempDir;
684 int storageId =
685 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
686 IncrementalService::CreateOptions::CreateNew);
687 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700688 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700689}
690
Songchun Fan3c82a302019-11-29 14:23:45 -0800691TEST_F(IncrementalServiceTest, testMakeDirectory) {
692 mVold->mountIncFsSuccess();
693 mIncFs->makeFileSuccess();
694 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800695 mDataLoaderManager->initializeDataLoaderSuccess();
696 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800697 TemporaryDir tempDir;
698 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800699 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800700 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800701 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800702
Songchun Fan103ba1d2020-02-03 17:32:32 -0800703 // Expecting incfs to call makeDir on a path like:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700704 // <root>/*/mount/<storage>/test
705 EXPECT_CALL(*mIncFs,
706 makeDir(_, Truly([&](std::string_view arg) {
707 return arg.starts_with(mRootDir.path) &&
708 arg.ends_with("/mount/st_1_0/" + dir_path);
709 }),
710 _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800711 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
712 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800713}
714
715TEST_F(IncrementalServiceTest, testMakeDirectories) {
716 mVold->mountIncFsSuccess();
717 mIncFs->makeFileSuccess();
718 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800719 mDataLoaderManager->initializeDataLoaderSuccess();
720 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800721 TemporaryDir tempDir;
722 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800723 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800724 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800725 auto first = "first"sv;
726 auto second = "second"sv;
727 auto third = "third"sv;
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700728 auto dir_path = std::string(first) + "/" + std::string(second) + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800729
Yurii Zubrytskyiefebb452020-04-22 13:59:06 -0700730 EXPECT_CALL(*mIncFs,
731 makeDirs(_, Truly([&](std::string_view arg) {
732 return arg.starts_with(mRootDir.path) &&
733 arg.ends_with("/mount/st_1_0/" + dir_path);
734 }),
735 _));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700736 auto res = mIncrementalService->makeDirs(storageId, dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800737 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800738}
739} // namespace android::os::incremental