blob: 5553f688060a87ae6d432fd4ef97f7eaead3414c [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
Songchun Fan68645c42020-02-27 15:57:35 -0800106class FakeDataLoader : public IDataLoader {
Songchun Fan3c82a302019-11-29 14:23:45 -0800107public:
Songchun Fan68645c42020-02-27 15:57:35 -0800108 IBinder* onAsBinder() override { return nullptr; }
109 binder::Status create(int32_t, const DataLoaderParamsParcel&, const FileSystemControlParcel&,
110 const sp<IDataLoaderStatusListener>&) override {
111 return binder::Status::ok();
112 }
Alex Buynytskyyb6e02f72020-03-17 18:12:23 -0700113 binder::Status start(int32_t) override { return binder::Status::ok(); }
114 binder::Status stop(int32_t) override { return binder::Status::ok(); }
115 binder::Status destroy(int32_t) override { return binder::Status::ok(); }
116 binder::Status prepareImage(int32_t,
117 const std::vector<InstallationFileParcel>&,
Songchun Fan68645c42020-02-27 15:57:35 -0800118 const std::vector<std::string>&) override {
119 return binder::Status::ok();
120 }
121};
122
123class MockDataLoaderManager : public DataLoaderManagerWrapper {
124public:
125 MOCK_CONST_METHOD5(initializeDataLoader,
126 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
127 const FileSystemControlParcel& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800128 const sp<IDataLoaderStatusListener>& listener,
129 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800130 MOCK_CONST_METHOD2(getDataLoader,
131 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Songchun Fan3c82a302019-11-29 14:23:45 -0800132 MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800133
Songchun Fan68645c42020-02-27 15:57:35 -0800134 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
135 const FileSystemControlParcel& control,
136 const sp<IDataLoaderStatusListener>& listener,
137 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800138 mId = mountId;
139 mListener = listener;
140 *_aidl_return = true;
141 return binder::Status::ok();
142 }
143
Songchun Fan68645c42020-02-27 15:57:35 -0800144 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
145 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800146 return binder::Status::ok();
147 }
148
Songchun Fan68645c42020-02-27 15:57:35 -0800149 void initializeDataLoaderFails() {
150 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
Songchun Fan3c82a302019-11-29 14:23:45 -0800151 .WillByDefault(Return(
152 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
153 }
Songchun Fan68645c42020-02-27 15:57:35 -0800154 void initializeDataLoaderSuccess() {
155 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
156 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800157 }
Songchun Fan68645c42020-02-27 15:57:35 -0800158 void getDataLoaderSuccess() {
159 ON_CALL(*this, getDataLoader(_, _))
160 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800161 }
162 void setDataLoaderStatusNotReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800163 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800164 }
165 void setDataLoaderStatusReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800166 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800167 }
168
169private:
170 int mId;
171 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800172 sp<IDataLoader> mDataLoader = sp<IDataLoader>(new FakeDataLoader());
Songchun Fan3c82a302019-11-29 14:23:45 -0800173};
174
175class MockIncFs : public IncFsWrapper {
176public:
Songchun Fan20d6ef22020-03-03 09:47:15 -0800177 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800178 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800179 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800180 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800181 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
182 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
183 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
184 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800185 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800186 ErrorCode(const Control& control, std::string_view from, std::string_view to));
187 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700188 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800189 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800190
191 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
192 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800193 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800194 metadata::Mount m;
195 m.mutable_storage()->set_id(100);
196 m.mutable_loader()->set_package_name("com.test");
197 m.mutable_loader()->set_arguments("com.uri");
198 const auto metadata = m.SerializeAsString();
199 m.mutable_loader()->release_arguments();
200 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800201 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800202 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800203 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800204 metadata::Storage st;
205 st.set_id(100);
206 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800207 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800208 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800209 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800210 metadata::BindPoint bp;
211 std::string destPath = "dest";
212 std::string srcPath = "src";
213 bp.set_storage_id(100);
214 bp.set_allocated_dest_path(&destPath);
215 bp.set_allocated_source_subdir(&srcPath);
216 const auto metadata = bp.SerializeAsString();
217 bp.release_source_subdir();
218 bp.release_dest_path();
219 return std::vector<char>(metadata.begin(), metadata.end());
220 }
221};
222
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700223class MockAppOpsManager : public AppOpsManagerWrapper {
224 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
225};
226
Songchun Fan3c82a302019-11-29 14:23:45 -0800227class MockServiceManager : public ServiceManagerWrapper {
228public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800229 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800230 std::unique_ptr<MockDataLoaderManager> manager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700231 std::unique_ptr<MockIncFs> incfs,
232 std::unique_ptr<MockAppOpsManager> appOpsManager)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800233 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800234 mDataLoaderManager(std::move(manager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700235 mIncFs(std::move(incfs)),
236 mAppOpsManager(std::move(appOpsManager)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800237 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800238 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
239 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800240 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800241 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700242 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800243
244private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800245 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800246 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800247 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700248 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800249};
250
251// --- IncrementalServiceTest ---
252
Songchun Fan3c82a302019-11-29 14:23:45 -0800253class IncrementalServiceTest : public testing::Test {
254public:
255 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800256 auto vold = std::make_unique<NiceMock<MockVoldService>>();
257 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800258 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
259 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800260 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
261 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700262 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
263 mAppOpsManager = appOps.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800264 mIncrementalService =
265 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700266 std::move(dataloaderManager),
267 std::move(incFs),
268 std::move(appOps)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800269 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800270 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800271 mDataLoaderParcel.arguments = "uri";
Songchun Fan3c82a302019-11-29 14:23:45 -0800272 mIncrementalService->onSystemReady();
273 }
274
275 void setUpExistingMountDir(const std::string& rootDir) {
276 const auto dir = rootDir + "/dir1";
277 const auto mountDir = dir + "/mount";
278 const auto backingDir = dir + "/backing_store";
279 const auto storageDir = mountDir + "/st0";
280 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
281 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
282 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
283 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
284 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
285 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
286 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
287 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800288 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
289 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
290 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
291 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
292 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
293 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800294 }
295
296protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800297 NiceMock<MockVoldService>* mVold;
298 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800299 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700300 NiceMock<MockAppOpsManager>* mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800301 std::unique_ptr<IncrementalService> mIncrementalService;
302 TemporaryDir mRootDir;
303 DataLoaderParamsParcel mDataLoaderParcel;
304};
305
Songchun Fan3c82a302019-11-29 14:23:45 -0800306TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
307 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800308 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800309 TemporaryDir tempDir;
310 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800311 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800312 IncrementalService::CreateOptions::CreateNew);
313 ASSERT_LT(storageId, 0);
314}
315
316TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
317 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800318 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800319 TemporaryDir tempDir;
320 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800321 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800322 IncrementalService::CreateOptions::CreateNew);
323 ASSERT_LT(storageId, 0);
324}
325
326TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
327 mVold->mountIncFsSuccess();
328 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800329 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
330 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800331 EXPECT_CALL(*mVold, unmountIncFs(_));
332 TemporaryDir tempDir;
333 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800334 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800335 IncrementalService::CreateOptions::CreateNew);
336 ASSERT_LT(storageId, 0);
337}
338
339TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
340 mVold->mountIncFsSuccess();
341 mIncFs->makeFileSuccess();
342 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800343 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
344 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800345 EXPECT_CALL(*mVold, unmountIncFs(_));
346 TemporaryDir tempDir;
347 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800348 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800349 IncrementalService::CreateOptions::CreateNew);
350 ASSERT_LT(storageId, 0);
351}
352
353TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
354 mVold->mountIncFsSuccess();
355 mIncFs->makeFileSuccess();
356 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800357 mDataLoaderManager->initializeDataLoaderFails();
358 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800359 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
360 TemporaryDir tempDir;
361 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800362 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800363 IncrementalService::CreateOptions::CreateNew);
364 ASSERT_LT(storageId, 0);
365}
366
367TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
368 mVold->mountIncFsSuccess();
369 mIncFs->makeFileSuccess();
370 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800371 mDataLoaderManager->initializeDataLoaderSuccess();
372 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800373 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
374 TemporaryDir tempDir;
375 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800376 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800377 IncrementalService::CreateOptions::CreateNew);
378 ASSERT_GE(storageId, 0);
379 mIncrementalService->deleteStorage(storageId);
380}
381
382TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
383 mVold->mountIncFsSuccess();
384 mIncFs->makeFileSuccess();
385 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800386 mDataLoaderManager->initializeDataLoaderSuccess();
387 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800388 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
389 TemporaryDir tempDir;
390 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800391 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800392 IncrementalService::CreateOptions::CreateNew);
393 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800394 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800395}
396
397TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
398 mVold->mountIncFsSuccess();
399 mIncFs->makeFileSuccess();
400 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800401 mDataLoaderManager->initializeDataLoaderSuccess();
402 mDataLoaderManager->getDataLoaderSuccess();
403 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800404 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
405 TemporaryDir tempDir;
406 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800407 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800408 IncrementalService::CreateOptions::CreateNew);
409 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800410 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800411 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
412}
413
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700414TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
415 mVold->mountIncFsSuccess();
416 mIncFs->makeFileSuccess();
417 mVold->bindMountSuccess();
418 mVold->setIncFsMountOptionsSuccess();
419 mDataLoaderManager->initializeDataLoaderSuccess();
420 mDataLoaderManager->getDataLoaderSuccess();
421 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
422 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
423 EXPECT_CALL(*mVold, setIncFsMountOptions(_, _));
424 TemporaryDir tempDir;
425 int storageId =
426 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
427 IncrementalService::CreateOptions::CreateNew);
428 ASSERT_GE(storageId, 0);
429 ASSERT_GE(mIncrementalService->setStorageParams(storageId, true), 0);
430}
431
432TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
433 mVold->mountIncFsSuccess();
434 mIncFs->makeFileSuccess();
435 mVold->bindMountSuccess();
436 mVold->setIncFsMountOptionsFails();
437 mDataLoaderManager->initializeDataLoaderSuccess();
438 mDataLoaderManager->getDataLoaderSuccess();
439 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
440 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
441 EXPECT_CALL(*mVold, setIncFsMountOptions(_, _));
442 TemporaryDir tempDir;
443 int storageId =
444 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
445 IncrementalService::CreateOptions::CreateNew);
446 ASSERT_GE(storageId, 0);
447 ASSERT_LT(mIncrementalService->setStorageParams(storageId, true), 0);
448}
449
Songchun Fan3c82a302019-11-29 14:23:45 -0800450TEST_F(IncrementalServiceTest, testMakeDirectory) {
451 mVold->mountIncFsSuccess();
452 mIncFs->makeFileSuccess();
453 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800454 mDataLoaderManager->initializeDataLoaderSuccess();
455 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800456 TemporaryDir tempDir;
457 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800458 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800459 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800460 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800461
Songchun Fan103ba1d2020-02-03 17:32:32 -0800462 std::string tempPath(tempDir.path);
463 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800464 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800465 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800466
Songchun Fan103ba1d2020-02-03 17:32:32 -0800467 // Expecting incfs to call makeDir on a path like:
468 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
469 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800470 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
471 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800472}
473
474TEST_F(IncrementalServiceTest, testMakeDirectories) {
475 mVold->mountIncFsSuccess();
476 mIncFs->makeFileSuccess();
477 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800478 mDataLoaderManager->initializeDataLoaderSuccess();
479 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800480 TemporaryDir tempDir;
481 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800482 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800483 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800484 auto first = "first"sv;
485 auto second = "second"sv;
486 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800487
488 std::string tempPath(tempDir.path);
489 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800490 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800491
Songchun Fan3c82a302019-11-29 14:23:45 -0800492 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800493 auto parent_path = std::string(first) + "/" + std::string(second);
494 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800495
496 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
497 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
498 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
499
500 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
501 .WillOnce(Return(-ENOENT));
502 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
503 .WillOnce(Return(-ENOENT));
504 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
505 .WillOnce(Return(0));
506 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
507 .WillOnce(Return(0));
508 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
509 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800510 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800511}
512} // namespace android::os::incremental