blob: 8a0605348aab05f2eb9d123ca7d50f819958587b [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));
273 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
274 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
275 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800276 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800277 ErrorCode(const Control& control, std::string_view from, std::string_view to));
278 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700279 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700280 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(std::span<const DataBlock> blocks));
281
282 MockIncFs() { ON_CALL(*this, listExistingMounts(_)).WillByDefault(Return()); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800283
284 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
285 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700286
Songchun Fan20d6ef22020-03-03 09:47:15 -0800287 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800288 metadata::Mount m;
289 m.mutable_storage()->set_id(100);
290 m.mutable_loader()->set_package_name("com.test");
291 m.mutable_loader()->set_arguments("com.uri");
292 const auto metadata = m.SerializeAsString();
293 m.mutable_loader()->release_arguments();
294 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800295 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800296 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800297 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800298 metadata::Storage st;
299 st.set_id(100);
300 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800301 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800302 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800303 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800304 metadata::BindPoint bp;
305 std::string destPath = "dest";
306 std::string srcPath = "src";
307 bp.set_storage_id(100);
308 bp.set_allocated_dest_path(&destPath);
309 bp.set_allocated_source_subdir(&srcPath);
310 const auto metadata = bp.SerializeAsString();
311 bp.release_source_subdir();
312 bp.release_dest_path();
313 return std::vector<char>(metadata.begin(), metadata.end());
314 }
315};
316
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700317class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700318public:
319 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700320 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700321 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
322
323 void checkPermissionSuccess() {
324 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
325 }
326 void checkPermissionFails() {
327 ON_CALL(*this, checkPermission(_, _, _))
328 .WillByDefault(
329 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
330 }
331 void initializeStartWatchingMode() {
332 ON_CALL(*this, startWatchingMode(_, _, _))
333 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
334 }
335 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
336 mStoredCallback = cb;
337 }
338
339 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700340};
341
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700342class MockJniWrapper : public JniWrapper {
343public:
344 MOCK_CONST_METHOD0(initializeForCurrentThread, void());
345
346 MockJniWrapper() { EXPECT_CALL(*this, initializeForCurrentThread()).Times(1); }
347};
348
Songchun Fan3c82a302019-11-29 14:23:45 -0800349class MockServiceManager : public ServiceManagerWrapper {
350public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800351 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700352 std::unique_ptr<MockDataLoaderManager> dataLoaderManager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700353 std::unique_ptr<MockIncFs> incfs,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700354 std::unique_ptr<MockAppOpsManager> appOpsManager,
355 std::unique_ptr<MockJniWrapper> jni)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800356 : mVold(std::move(vold)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700357 mDataLoaderManager(std::move(dataLoaderManager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700358 mIncFs(std::move(incfs)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700359 mAppOpsManager(std::move(appOpsManager)),
360 mJni(std::move(jni)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800361 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800362 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
363 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800364 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800365 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700366 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700367 std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800368
369private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800370 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800371 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800372 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700373 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700374 std::unique_ptr<MockJniWrapper> mJni;
Songchun Fan3c82a302019-11-29 14:23:45 -0800375};
376
377// --- IncrementalServiceTest ---
378
Songchun Fan3c82a302019-11-29 14:23:45 -0800379class IncrementalServiceTest : public testing::Test {
380public:
381 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800382 auto vold = std::make_unique<NiceMock<MockVoldService>>();
383 mVold = vold.get();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700384 sp<NiceMock<MockDataLoader>> dataLoader{new NiceMock<MockDataLoader>};
385 mDataLoader = dataLoader.get();
386 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>(dataLoader);
Songchun Fan68645c42020-02-27 15:57:35 -0800387 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800388 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
389 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700390 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
391 mAppOpsManager = appOps.get();
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700392 auto jni = std::make_unique<NiceMock<MockJniWrapper>>();
393 mJni = jni.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800394 mIncrementalService =
395 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700396 std::move(
397 dataloaderManager),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700398 std::move(incFs),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700399 std::move(appOps),
400 std::move(jni)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800401 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800402 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800403 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700404 mDataLoaderManager->destroyDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800405 mIncrementalService->onSystemReady();
406 }
407
408 void setUpExistingMountDir(const std::string& rootDir) {
409 const auto dir = rootDir + "/dir1";
410 const auto mountDir = dir + "/mount";
411 const auto backingDir = dir + "/backing_store";
412 const auto storageDir = mountDir + "/st0";
413 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
414 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
415 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
416 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
417 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
418 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
419 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
420 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800421 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
422 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
423 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
424 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
425 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
426 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800427 }
428
429protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800430 NiceMock<MockVoldService>* mVold;
431 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800432 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700433 NiceMock<MockAppOpsManager>* mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700434 NiceMock<MockJniWrapper>* mJni;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700435 NiceMock<MockDataLoader>* mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800436 std::unique_ptr<IncrementalService> mIncrementalService;
437 TemporaryDir mRootDir;
438 DataLoaderParamsParcel mDataLoaderParcel;
439};
440
Songchun Fan3c82a302019-11-29 14:23:45 -0800441TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
442 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800443 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800444 TemporaryDir tempDir;
445 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800446 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800447 IncrementalService::CreateOptions::CreateNew);
448 ASSERT_LT(storageId, 0);
449}
450
451TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
452 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800453 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700454 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800455 TemporaryDir tempDir;
456 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800457 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800458 IncrementalService::CreateOptions::CreateNew);
459 ASSERT_LT(storageId, 0);
460}
461
462TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
463 mVold->mountIncFsSuccess();
464 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800465 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700466 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800467 EXPECT_CALL(*mVold, unmountIncFs(_));
468 TemporaryDir tempDir;
469 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800470 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800471 IncrementalService::CreateOptions::CreateNew);
472 ASSERT_LT(storageId, 0);
473}
474
475TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
476 mVold->mountIncFsSuccess();
477 mIncFs->makeFileSuccess();
478 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800479 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700480 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800481 EXPECT_CALL(*mVold, unmountIncFs(_));
482 TemporaryDir tempDir;
483 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800484 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800485 IncrementalService::CreateOptions::CreateNew);
486 ASSERT_LT(storageId, 0);
487}
488
489TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
490 mVold->mountIncFsSuccess();
491 mIncFs->makeFileSuccess();
492 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800493 mDataLoaderManager->initializeDataLoaderFails();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700494 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700495 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700496 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(0);
497 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
498 EXPECT_CALL(*mDataLoader, destroy(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800499 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
500 TemporaryDir tempDir;
501 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800502 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800503 IncrementalService::CreateOptions::CreateNew);
504 ASSERT_LT(storageId, 0);
505}
506
507TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
508 mVold->mountIncFsSuccess();
509 mIncFs->makeFileSuccess();
510 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800511 mDataLoaderManager->initializeDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700512 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700513 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700514 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
515 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
516 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800517 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
518 TemporaryDir tempDir;
519 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800520 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800521 IncrementalService::CreateOptions::CreateNew);
522 ASSERT_GE(storageId, 0);
523 mIncrementalService->deleteStorage(storageId);
524}
525
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700526TEST_F(IncrementalServiceTest, testDataLoaderDestroyed) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800527 mVold->mountIncFsSuccess();
528 mIncFs->makeFileSuccess();
529 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800530 mDataLoaderManager->initializeDataLoaderSuccess();
531 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700532 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
533 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
534 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
535 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
536 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800537 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
538 TemporaryDir tempDir;
539 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800540 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800541 IncrementalService::CreateOptions::CreateNew);
542 ASSERT_GE(storageId, 0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700543 // Simulated crash/other connection breakage.
544 mDataLoaderManager->setDataLoaderStatusDestroyed();
545}
546
547TEST_F(IncrementalServiceTest, testStartDataLoaderCreate) {
548 mVold->mountIncFsSuccess();
549 mIncFs->makeFileSuccess();
550 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700551 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700552 mDataLoaderManager->initializeDataLoaderSuccess();
553 mDataLoaderManager->getDataLoaderSuccess();
554 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
555 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
556 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
557 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
558 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
559 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
560 TemporaryDir tempDir;
561 int storageId =
562 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
563 IncrementalService::CreateOptions::CreateNew);
564 ASSERT_GE(storageId, 0);
565 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800566 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700567 mDataLoaderManager->setDataLoaderStatusStarted();
568}
569
570TEST_F(IncrementalServiceTest, testStartDataLoaderPendingStart) {
571 mVold->mountIncFsSuccess();
572 mIncFs->makeFileSuccess();
573 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700574 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700575 mDataLoaderManager->initializeDataLoaderSuccess();
576 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700577 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700578 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700579 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700580 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
581 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
582 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
583 TemporaryDir tempDir;
584 int storageId =
585 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
586 IncrementalService::CreateOptions::CreateNew);
587 ASSERT_GE(storageId, 0);
588 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
589 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800590}
591
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700592TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
593 mVold->mountIncFsSuccess();
594 mIncFs->makeFileSuccess();
595 mVold->bindMountSuccess();
596 mVold->setIncFsMountOptionsSuccess();
597 mDataLoaderManager->initializeDataLoaderSuccess();
598 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700599 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700600 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
601 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700602 // We are calling setIncFsMountOptions(true).
603 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
604 // After setIncFsMountOptions succeeded expecting to start watching.
605 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
606 // Not expecting callback removal.
607 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700608 TemporaryDir tempDir;
609 int storageId =
610 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
611 IncrementalService::CreateOptions::CreateNew);
612 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700613 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700614}
615
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700616TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
617 mVold->mountIncFsSuccess();
618 mIncFs->makeFileSuccess();
619 mVold->bindMountSuccess();
620 mVold->setIncFsMountOptionsSuccess();
621 mDataLoaderManager->initializeDataLoaderSuccess();
622 mDataLoaderManager->getDataLoaderSuccess();
623 mAppOpsManager->checkPermissionSuccess();
624 mAppOpsManager->initializeStartWatchingMode();
625 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
626 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
627 // We are calling setIncFsMountOptions(true).
628 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
629 // setIncFsMountOptions(false) is called on the callback.
630 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
631 // After setIncFsMountOptions succeeded expecting to start watching.
632 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
633 // After callback is called, disable read logs and remove callback.
634 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
635 TemporaryDir tempDir;
636 int storageId =
637 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
638 IncrementalService::CreateOptions::CreateNew);
639 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700640 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700641 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
642 mAppOpsManager->mStoredCallback->opChanged(0, {});
643}
644
645TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
646 mVold->mountIncFsSuccess();
647 mIncFs->makeFileSuccess();
648 mVold->bindMountSuccess();
649 mDataLoaderManager->initializeDataLoaderSuccess();
650 mDataLoaderManager->getDataLoaderSuccess();
651 mAppOpsManager->checkPermissionFails();
652 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
653 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
654 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
655 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
656 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
657 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
658 TemporaryDir tempDir;
659 int storageId =
660 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
661 IncrementalService::CreateOptions::CreateNew);
662 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700663 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700664}
665
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700666TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
667 mVold->mountIncFsSuccess();
668 mIncFs->makeFileSuccess();
669 mVold->bindMountSuccess();
670 mVold->setIncFsMountOptionsFails();
671 mDataLoaderManager->initializeDataLoaderSuccess();
672 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700673 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700674 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
675 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700676 // We are calling setIncFsMountOptions.
677 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
678 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
679 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
680 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700681 TemporaryDir tempDir;
682 int storageId =
683 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
684 IncrementalService::CreateOptions::CreateNew);
685 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700686 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700687}
688
Songchun Fan3c82a302019-11-29 14:23:45 -0800689TEST_F(IncrementalServiceTest, testMakeDirectory) {
690 mVold->mountIncFsSuccess();
691 mIncFs->makeFileSuccess();
692 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800693 mDataLoaderManager->initializeDataLoaderSuccess();
694 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800695 TemporaryDir tempDir;
696 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800697 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800698 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800699 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800700
Songchun Fan103ba1d2020-02-03 17:32:32 -0800701 // Expecting incfs to call makeDir on a path like:
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700702 // <root>/*/mount/<storage>/test
703 EXPECT_CALL(*mIncFs,
704 makeDir(_, Truly([&](std::string_view arg) {
705 return arg.starts_with(mRootDir.path) &&
706 arg.ends_with("/mount/st_1_0/" + dir_path);
707 }),
708 _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800709 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
710 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800711}
712
713TEST_F(IncrementalServiceTest, testMakeDirectories) {
714 mVold->mountIncFsSuccess();
715 mIncFs->makeFileSuccess();
716 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800717 mDataLoaderManager->initializeDataLoaderSuccess();
718 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800719 TemporaryDir tempDir;
720 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800721 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800722 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800723 auto first = "first"sv;
724 auto second = "second"sv;
725 auto third = "third"sv;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800726 auto parent_path = std::string(first) + "/" + std::string(second);
727 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800728
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700729 auto checkArgFor = [&](std::string_view expected, std::string_view arg) {
730 return arg.starts_with(mRootDir.path) && arg.ends_with("/mount/st_1_0/"s += expected);
731 };
Songchun Fan103ba1d2020-02-03 17:32:32 -0800732
Yurii Zubrytskyi629051fd2020-04-17 23:13:47 -0700733 {
734 InSequence seq;
735 EXPECT_CALL(*mIncFs,
736 makeDir(_, Truly([&](auto arg) { return checkArgFor(dir_path, arg); }), _))
737 .WillOnce(Return(-ENOENT));
738 EXPECT_CALL(*mIncFs,
739 makeDir(_, Truly([&](auto arg) { return checkArgFor(parent_path, arg); }), _))
740 .WillOnce(Return(-ENOENT));
741 EXPECT_CALL(*mIncFs,
742 makeDir(_, Truly([&](auto arg) { return checkArgFor(first, arg); }), _))
743 .WillOnce(Return(0));
744 EXPECT_CALL(*mIncFs,
745 makeDir(_, Truly([&](auto arg) { return checkArgFor(parent_path, arg); }), _))
746 .WillOnce(Return(0));
747 EXPECT_CALL(*mIncFs,
748 makeDir(_, Truly([&](auto arg) { return checkArgFor(dir_path, arg); }), _))
749 .WillOnce(Return(0));
750 }
751 auto res = mIncrementalService->makeDirs(storageId, dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800752 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800753}
754} // namespace android::os::incremental