blob: bfe92f4f2080d9d4b696b3b358763ce003fb6791 [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"
28#include "Metadata.pb.h"
29#include "ServiceWrappers.h"
30
31using namespace testing;
32using namespace android::incremental;
33using namespace std::literals;
34using testing::_;
35using testing::Invoke;
36using testing::NiceMock;
37
38#undef LOG_TAG
39#define LOG_TAG "IncrementalServiceTest"
40
41using namespace android::incfs;
42using namespace android::content::pm;
43
44namespace android::os::incremental {
45
46class MockVoldService : public VoldServiceWrapper {
47public:
48 MOCK_CONST_METHOD4(mountIncFs,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080049 binder::Status(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080050 int32_t flags,
51 IncrementalFileSystemControlParcel* _aidl_return));
52 MOCK_CONST_METHOD1(unmountIncFs, binder::Status(const std::string& dir));
53 MOCK_CONST_METHOD2(bindMount,
54 binder::Status(const std::string& sourceDir, const std::string& argetDir));
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -070055 MOCK_CONST_METHOD2(setIncFsMountOptions,
56 binder::Status(const ::android::os::incremental::IncrementalFileSystemControlParcel&, bool));
Songchun Fan3c82a302019-11-29 14:23:45 -080057
58 void mountIncFsFails() {
59 ON_CALL(*this, mountIncFs(_, _, _, _))
60 .WillByDefault(
61 Return(binder::Status::fromExceptionCode(1, String8("failed to mount"))));
62 }
63 void mountIncFsInvalidControlParcel() {
64 ON_CALL(*this, mountIncFs(_, _, _, _))
65 .WillByDefault(Invoke(this, &MockVoldService::getInvalidControlParcel));
66 }
67 void mountIncFsSuccess() {
68 ON_CALL(*this, mountIncFs(_, _, _, _))
69 .WillByDefault(Invoke(this, &MockVoldService::incFsSuccess));
70 }
71 void bindMountFails() {
72 ON_CALL(*this, bindMount(_, _))
73 .WillByDefault(Return(
74 binder::Status::fromExceptionCode(1, String8("failed to bind-mount"))));
75 }
76 void bindMountSuccess() {
77 ON_CALL(*this, bindMount(_, _)).WillByDefault(Return(binder::Status::ok()));
78 }
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -070079 void setIncFsMountOptionsFails() const {
80 ON_CALL(*this, setIncFsMountOptions(_, _))
81 .WillByDefault(
82 Return(binder::Status::fromExceptionCode(1, String8("failed to set options"))));
83 }
84 void setIncFsMountOptionsSuccess() {
85 ON_CALL(*this, setIncFsMountOptions(_, _)).WillByDefault(Return(binder::Status::ok()));
86 }
Songchun Fan3c82a302019-11-29 14:23:45 -080087 binder::Status getInvalidControlParcel(const std::string& imagePath,
88 const std::string& targetDir, int32_t flags,
89 IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080090 _aidl_return = {};
Songchun Fan3c82a302019-11-29 14:23:45 -080091 return binder::Status::ok();
92 }
93 binder::Status incFsSuccess(const std::string& imagePath, const std::string& targetDir,
94 int32_t flags, IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080095 _aidl_return->pendingReads.reset(base::unique_fd(dup(STDIN_FILENO)));
96 _aidl_return->cmd.reset(base::unique_fd(dup(STDIN_FILENO)));
97 _aidl_return->log.reset(base::unique_fd(dup(STDIN_FILENO)));
Songchun Fan3c82a302019-11-29 14:23:45 -080098 return binder::Status::ok();
99 }
100
101private:
102 TemporaryFile cmdFile;
103 TemporaryFile logFile;
Songchun Fan3c82a302019-11-29 14:23:45 -0800104};
105
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700106class MockDataLoader : public IDataLoader {
Songchun Fan3c82a302019-11-29 14:23:45 -0800107public:
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700108 MockDataLoader() {
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700109 ON_CALL(*this, create(_, _, _, _)).WillByDefault(Invoke(this, &MockDataLoader::createOk));
110 ON_CALL(*this, start(_)).WillByDefault(Invoke(this, &MockDataLoader::startOk));
111 ON_CALL(*this, stop(_)).WillByDefault(Invoke(this, &MockDataLoader::stopOk));
112 ON_CALL(*this, destroy(_)).WillByDefault(Invoke(this, &MockDataLoader::destroyOk));
113 ON_CALL(*this, prepareImage(_, _, _))
114 .WillByDefault(Invoke(this, &MockDataLoader::prepareImageOk));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700115 }
Songchun Fan68645c42020-02-27 15:57:35 -0800116 IBinder* onAsBinder() override { return nullptr; }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700117 MOCK_METHOD4(create,
118 binder::Status(int32_t id, const DataLoaderParamsParcel& params,
119 const FileSystemControlParcel& control,
120 const sp<IDataLoaderStatusListener>& listener));
121 MOCK_METHOD1(start, binder::Status(int32_t id));
122 MOCK_METHOD1(stop, binder::Status(int32_t id));
123 MOCK_METHOD1(destroy, binder::Status(int32_t id));
124 MOCK_METHOD3(prepareImage,
125 binder::Status(int32_t id, const std::vector<InstallationFileParcel>& addedFiles,
126 const std::vector<std::string>& removedFiles));
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700127
128 void initializeCreateOkNoStatus() {
129 ON_CALL(*this, create(_, _, _, _))
130 .WillByDefault(Invoke(this, &MockDataLoader::createOkNoStatus));
131 }
132
133 binder::Status createOk(int32_t id, const content::pm::DataLoaderParamsParcel&,
134 const content::pm::FileSystemControlParcel&,
135 const sp<content::pm::IDataLoaderStatusListener>& listener) {
136 mListener = listener;
137 if (mListener) {
138 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_CREATED);
139 }
140 return binder::Status::ok();
141 }
142 binder::Status createOkNoStatus(int32_t id, const content::pm::DataLoaderParamsParcel&,
143 const content::pm::FileSystemControlParcel&,
144 const sp<content::pm::IDataLoaderStatusListener>& listener) {
145 mListener = listener;
146 return binder::Status::ok();
147 }
148 binder::Status startOk(int32_t id) {
149 if (mListener) {
150 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STARTED);
151 }
152 return binder::Status::ok();
153 }
154 binder::Status stopOk(int32_t id) {
155 if (mListener) {
156 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_STOPPED);
157 }
158 return binder::Status::ok();
159 }
160 binder::Status destroyOk(int32_t id) {
161 if (mListener) {
162 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
163 }
164 mListener = nullptr;
165 return binder::Status::ok();
166 }
167 binder::Status prepareImageOk(int32_t id,
168 const ::std::vector<content::pm::InstallationFileParcel>&,
169 const ::std::vector<::std::string>&) {
170 if (mListener) {
171 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_IMAGE_READY);
172 }
173 return binder::Status::ok();
174 }
175
176private:
177 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800178};
179
180class MockDataLoaderManager : public DataLoaderManagerWrapper {
181public:
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700182 MockDataLoaderManager(sp<IDataLoader> dataLoader) : mDataLoaderHolder(std::move(dataLoader)) {
183 EXPECT_TRUE(mDataLoaderHolder != nullptr);
184 }
185
Songchun Fan68645c42020-02-27 15:57:35 -0800186 MOCK_CONST_METHOD5(initializeDataLoader,
187 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
188 const FileSystemControlParcel& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800189 const sp<IDataLoaderStatusListener>& listener,
190 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800191 MOCK_CONST_METHOD2(getDataLoader,
192 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Songchun Fan3c82a302019-11-29 14:23:45 -0800193 MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800194
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700195 void initializeDataLoaderSuccess() {
196 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
197 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
198 }
199 void initializeDataLoaderFails() {
200 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
201 .WillByDefault(Return(
202 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
203 }
204 void getDataLoaderSuccess() {
205 ON_CALL(*this, getDataLoader(_, _))
206 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
207 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700208 void destroyDataLoaderSuccess() {
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700209 ON_CALL(*this, destroyDataLoader(_))
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700210 .WillByDefault(Invoke(this, &MockDataLoaderManager::destroyDataLoaderOk));
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700211 }
Songchun Fan68645c42020-02-27 15:57:35 -0800212 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
213 const FileSystemControlParcel& control,
214 const sp<IDataLoaderStatusListener>& listener,
215 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800216 mId = mountId;
217 mListener = listener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700218 mServiceConnector = control.service;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700219 mDataLoader = mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800220 *_aidl_return = true;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700221 return mDataLoader->create(mountId, params, control, listener);
Songchun Fan3c82a302019-11-29 14:23:45 -0800222 }
Songchun Fan68645c42020-02-27 15:57:35 -0800223 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
224 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800225 return binder::Status::ok();
226 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700227 void setDataLoaderStatusCreated() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800228 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800229 }
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700230 void setDataLoaderStatusStarted() {
231 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_STARTED);
232 }
233 void setDataLoaderStatusDestroyed() {
234 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
235 }
236 binder::Status destroyDataLoaderOk(int32_t id) {
237 if (mDataLoader) {
238 if (auto status = mDataLoader->destroy(id); !status.isOk()) {
239 return status;
240 }
241 mDataLoader = nullptr;
242 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700243 if (mListener) {
244 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
245 }
246 return binder::Status::ok();
247 }
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700248 int32_t setStorageParams(bool enableReadLogs) {
249 int32_t result = -1;
250 EXPECT_NE(mServiceConnector.get(), nullptr);
251 EXPECT_TRUE(mServiceConnector->setStorageParams(enableReadLogs, &result).isOk());
252 return result;
253 }
254
Songchun Fan3c82a302019-11-29 14:23:45 -0800255private:
256 int mId;
257 sp<IDataLoaderStatusListener> mListener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700258 sp<IIncrementalServiceConnector> mServiceConnector;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700259 sp<IDataLoader> mDataLoader;
260 sp<IDataLoader> mDataLoaderHolder;
Songchun Fan3c82a302019-11-29 14:23:45 -0800261};
262
263class MockIncFs : public IncFsWrapper {
264public:
Songchun Fan20d6ef22020-03-03 09:47:15 -0800265 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800266 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800267 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800268 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800269 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
270 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
271 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
272 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800273 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800274 ErrorCode(const Control& control, std::string_view from, std::string_view to));
275 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700276 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800277 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800278
279 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
280 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800281 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800282 metadata::Mount m;
283 m.mutable_storage()->set_id(100);
284 m.mutable_loader()->set_package_name("com.test");
285 m.mutable_loader()->set_arguments("com.uri");
286 const auto metadata = m.SerializeAsString();
287 m.mutable_loader()->release_arguments();
288 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800289 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800290 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800291 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800292 metadata::Storage st;
293 st.set_id(100);
294 auto metadata = st.SerializeAsString();
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 getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800298 metadata::BindPoint bp;
299 std::string destPath = "dest";
300 std::string srcPath = "src";
301 bp.set_storage_id(100);
302 bp.set_allocated_dest_path(&destPath);
303 bp.set_allocated_source_subdir(&srcPath);
304 const auto metadata = bp.SerializeAsString();
305 bp.release_source_subdir();
306 bp.release_dest_path();
307 return std::vector<char>(metadata.begin(), metadata.end());
308 }
309};
310
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700311class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700312public:
313 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700314 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700315 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
316
317 void checkPermissionSuccess() {
318 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
319 }
320 void checkPermissionFails() {
321 ON_CALL(*this, checkPermission(_, _, _))
322 .WillByDefault(
323 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
324 }
325 void initializeStartWatchingMode() {
326 ON_CALL(*this, startWatchingMode(_, _, _))
327 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
328 }
329 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
330 mStoredCallback = cb;
331 }
332
333 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700334};
335
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700336class MockJniWrapper : public JniWrapper {
337public:
338 MOCK_CONST_METHOD0(initializeForCurrentThread, void());
339
340 MockJniWrapper() { EXPECT_CALL(*this, initializeForCurrentThread()).Times(1); }
341};
342
Songchun Fan3c82a302019-11-29 14:23:45 -0800343class MockServiceManager : public ServiceManagerWrapper {
344public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800345 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700346 std::unique_ptr<MockDataLoaderManager> dataLoaderManager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700347 std::unique_ptr<MockIncFs> incfs,
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700348 std::unique_ptr<MockAppOpsManager> appOpsManager,
349 std::unique_ptr<MockJniWrapper> jni)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800350 : mVold(std::move(vold)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700351 mDataLoaderManager(std::move(dataLoaderManager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700352 mIncFs(std::move(incfs)),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700353 mAppOpsManager(std::move(appOpsManager)),
354 mJni(std::move(jni)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800355 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800356 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
357 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800358 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800359 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700360 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700361 std::unique_ptr<JniWrapper> getJni() final { return std::move(mJni); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800362
363private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800364 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800365 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800366 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700367 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700368 std::unique_ptr<MockJniWrapper> mJni;
Songchun Fan3c82a302019-11-29 14:23:45 -0800369};
370
371// --- IncrementalServiceTest ---
372
Songchun Fan3c82a302019-11-29 14:23:45 -0800373class IncrementalServiceTest : public testing::Test {
374public:
375 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800376 auto vold = std::make_unique<NiceMock<MockVoldService>>();
377 mVold = vold.get();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700378 sp<NiceMock<MockDataLoader>> dataLoader{new NiceMock<MockDataLoader>};
379 mDataLoader = dataLoader.get();
380 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>(dataLoader);
Songchun Fan68645c42020-02-27 15:57:35 -0800381 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800382 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
383 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700384 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
385 mAppOpsManager = appOps.get();
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700386 auto jni = std::make_unique<NiceMock<MockJniWrapper>>();
387 mJni = jni.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800388 mIncrementalService =
389 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700390 std::move(
391 dataloaderManager),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700392 std::move(incFs),
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700393 std::move(appOps),
394 std::move(jni)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800395 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800396 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800397 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700398 mDataLoaderManager->destroyDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800399 mIncrementalService->onSystemReady();
400 }
401
402 void setUpExistingMountDir(const std::string& rootDir) {
403 const auto dir = rootDir + "/dir1";
404 const auto mountDir = dir + "/mount";
405 const auto backingDir = dir + "/backing_store";
406 const auto storageDir = mountDir + "/st0";
407 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
408 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
409 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
410 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
411 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
412 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
413 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
414 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800415 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
416 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
417 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
418 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
419 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
420 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800421 }
422
423protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800424 NiceMock<MockVoldService>* mVold;
425 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800426 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700427 NiceMock<MockAppOpsManager>* mAppOpsManager;
Yurii Zubrytskyi86321402020-04-09 19:22:30 -0700428 NiceMock<MockJniWrapper>* mJni;
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700429 NiceMock<MockDataLoader>* mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800430 std::unique_ptr<IncrementalService> mIncrementalService;
431 TemporaryDir mRootDir;
432 DataLoaderParamsParcel mDataLoaderParcel;
433};
434
Songchun Fan3c82a302019-11-29 14:23:45 -0800435TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
436 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800437 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800438 TemporaryDir tempDir;
439 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800440 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800441 IncrementalService::CreateOptions::CreateNew);
442 ASSERT_LT(storageId, 0);
443}
444
445TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
446 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800447 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700448 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800449 TemporaryDir tempDir;
450 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800451 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800452 IncrementalService::CreateOptions::CreateNew);
453 ASSERT_LT(storageId, 0);
454}
455
456TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
457 mVold->mountIncFsSuccess();
458 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800459 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700460 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800461 EXPECT_CALL(*mVold, unmountIncFs(_));
462 TemporaryDir tempDir;
463 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800464 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800465 IncrementalService::CreateOptions::CreateNew);
466 ASSERT_LT(storageId, 0);
467}
468
469TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
470 mVold->mountIncFsSuccess();
471 mIncFs->makeFileSuccess();
472 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800473 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700474 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800475 EXPECT_CALL(*mVold, unmountIncFs(_));
476 TemporaryDir tempDir;
477 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800478 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800479 IncrementalService::CreateOptions::CreateNew);
480 ASSERT_LT(storageId, 0);
481}
482
483TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
484 mVold->mountIncFsSuccess();
485 mIncFs->makeFileSuccess();
486 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800487 mDataLoaderManager->initializeDataLoaderFails();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700488 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700489 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700490 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(0);
491 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
492 EXPECT_CALL(*mDataLoader, destroy(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800493 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
494 TemporaryDir tempDir;
495 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800496 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800497 IncrementalService::CreateOptions::CreateNew);
498 ASSERT_LT(storageId, 0);
499}
500
501TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
502 mVold->mountIncFsSuccess();
503 mIncFs->makeFileSuccess();
504 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800505 mDataLoaderManager->initializeDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700506 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700507 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700508 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
509 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
510 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800511 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
512 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_GE(storageId, 0);
517 mIncrementalService->deleteStorage(storageId);
518}
519
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700520TEST_F(IncrementalServiceTest, testDataLoaderDestroyed) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800521 mVold->mountIncFsSuccess();
522 mIncFs->makeFileSuccess();
523 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800524 mDataLoaderManager->initializeDataLoaderSuccess();
525 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700526 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
527 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
528 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
529 EXPECT_CALL(*mDataLoader, start(_)).Times(0);
530 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800531 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
532 TemporaryDir tempDir;
533 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800534 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800535 IncrementalService::CreateOptions::CreateNew);
536 ASSERT_GE(storageId, 0);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700537 // Simulated crash/other connection breakage.
538 mDataLoaderManager->setDataLoaderStatusDestroyed();
539}
540
541TEST_F(IncrementalServiceTest, testStartDataLoaderCreate) {
542 mVold->mountIncFsSuccess();
543 mIncFs->makeFileSuccess();
544 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700545 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700546 mDataLoaderManager->initializeDataLoaderSuccess();
547 mDataLoaderManager->getDataLoaderSuccess();
548 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(1);
549 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
550 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(1);
551 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
552 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
553 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
554 TemporaryDir tempDir;
555 int storageId =
556 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
557 IncrementalService::CreateOptions::CreateNew);
558 ASSERT_GE(storageId, 0);
559 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800560 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700561 mDataLoaderManager->setDataLoaderStatusStarted();
562}
563
564TEST_F(IncrementalServiceTest, testStartDataLoaderPendingStart) {
565 mVold->mountIncFsSuccess();
566 mIncFs->makeFileSuccess();
567 mVold->bindMountSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700568 mDataLoader->initializeCreateOkNoStatus();
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700569 mDataLoaderManager->initializeDataLoaderSuccess();
570 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700571 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700572 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Alex Buynytskyyab65cb12020-04-17 10:01:47 -0700573 EXPECT_CALL(*mDataLoader, create(_, _, _, _)).Times(2);
Alex Buynytskyy0b202662020-04-13 09:53:04 -0700574 EXPECT_CALL(*mDataLoader, start(_)).Times(1);
575 EXPECT_CALL(*mDataLoader, destroy(_)).Times(1);
576 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
577 TemporaryDir tempDir;
578 int storageId =
579 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
580 IncrementalService::CreateOptions::CreateNew);
581 ASSERT_GE(storageId, 0);
582 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
583 mDataLoaderManager->setDataLoaderStatusCreated();
Songchun Fan3c82a302019-11-29 14:23:45 -0800584}
585
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700586TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
587 mVold->mountIncFsSuccess();
588 mIncFs->makeFileSuccess();
589 mVold->bindMountSuccess();
590 mVold->setIncFsMountOptionsSuccess();
591 mDataLoaderManager->initializeDataLoaderSuccess();
592 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700593 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700594 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
595 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700596 // We are calling setIncFsMountOptions(true).
597 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
598 // After setIncFsMountOptions succeeded expecting to start watching.
599 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
600 // Not expecting callback removal.
601 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700602 TemporaryDir tempDir;
603 int storageId =
604 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
605 IncrementalService::CreateOptions::CreateNew);
606 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700607 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700608}
609
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700610TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
611 mVold->mountIncFsSuccess();
612 mIncFs->makeFileSuccess();
613 mVold->bindMountSuccess();
614 mVold->setIncFsMountOptionsSuccess();
615 mDataLoaderManager->initializeDataLoaderSuccess();
616 mDataLoaderManager->getDataLoaderSuccess();
617 mAppOpsManager->checkPermissionSuccess();
618 mAppOpsManager->initializeStartWatchingMode();
619 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
620 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
621 // We are calling setIncFsMountOptions(true).
622 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
623 // setIncFsMountOptions(false) is called on the callback.
624 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
625 // After setIncFsMountOptions succeeded expecting to start watching.
626 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
627 // After callback is called, disable read logs and remove callback.
628 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
629 TemporaryDir tempDir;
630 int storageId =
631 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
632 IncrementalService::CreateOptions::CreateNew);
633 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700634 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700635 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
636 mAppOpsManager->mStoredCallback->opChanged(0, {});
637}
638
639TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
640 mVold->mountIncFsSuccess();
641 mIncFs->makeFileSuccess();
642 mVold->bindMountSuccess();
643 mDataLoaderManager->initializeDataLoaderSuccess();
644 mDataLoaderManager->getDataLoaderSuccess();
645 mAppOpsManager->checkPermissionFails();
646 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
647 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
648 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
649 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
650 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
651 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
652 TemporaryDir tempDir;
653 int storageId =
654 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
655 IncrementalService::CreateOptions::CreateNew);
656 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700657 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700658}
659
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700660TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
661 mVold->mountIncFsSuccess();
662 mIncFs->makeFileSuccess();
663 mVold->bindMountSuccess();
664 mVold->setIncFsMountOptionsFails();
665 mDataLoaderManager->initializeDataLoaderSuccess();
666 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700667 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700668 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
669 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700670 // We are calling setIncFsMountOptions.
671 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
672 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
673 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
674 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700675 TemporaryDir tempDir;
676 int storageId =
677 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
678 IncrementalService::CreateOptions::CreateNew);
679 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700680 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700681}
682
Songchun Fan3c82a302019-11-29 14:23:45 -0800683TEST_F(IncrementalServiceTest, testMakeDirectory) {
684 mVold->mountIncFsSuccess();
685 mIncFs->makeFileSuccess();
686 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800687 mDataLoaderManager->initializeDataLoaderSuccess();
688 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800689 TemporaryDir tempDir;
690 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800691 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800692 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800693 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800694
Songchun Fan103ba1d2020-02-03 17:32:32 -0800695 std::string tempPath(tempDir.path);
696 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800697 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800698 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800699
Songchun Fan103ba1d2020-02-03 17:32:32 -0800700 // Expecting incfs to call makeDir on a path like:
701 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
702 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800703 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
704 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800705}
706
707TEST_F(IncrementalServiceTest, testMakeDirectories) {
708 mVold->mountIncFsSuccess();
709 mIncFs->makeFileSuccess();
710 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800711 mDataLoaderManager->initializeDataLoaderSuccess();
712 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800713 TemporaryDir tempDir;
714 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800715 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800716 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800717 auto first = "first"sv;
718 auto second = "second"sv;
719 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800720
721 std::string tempPath(tempDir.path);
722 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800723 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800724
Songchun Fan3c82a302019-11-29 14:23:45 -0800725 InSequence seq;
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
729 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
730 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
731 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
732
733 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
734 .WillOnce(Return(-ENOENT));
735 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
736 .WillOnce(Return(-ENOENT));
737 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
738 .WillOnce(Return(0));
739 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
740 .WillOnce(Return(0));
741 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
742 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800743 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800744}
745} // namespace android::os::incremental