blob: 18ae4b5af4351c87e7838b77a9d5af0210e86926 [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;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700140 mServiceConnector = control.service;
Songchun Fan3c82a302019-11-29 14:23:45 -0800141 *_aidl_return = true;
142 return binder::Status::ok();
143 }
144
Songchun Fan68645c42020-02-27 15:57:35 -0800145 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
146 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800147 return binder::Status::ok();
148 }
149
Songchun Fan68645c42020-02-27 15:57:35 -0800150 void initializeDataLoaderFails() {
151 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
Songchun Fan3c82a302019-11-29 14:23:45 -0800152 .WillByDefault(Return(
153 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
154 }
Songchun Fan68645c42020-02-27 15:57:35 -0800155 void initializeDataLoaderSuccess() {
156 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
157 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800158 }
Songchun Fan68645c42020-02-27 15:57:35 -0800159 void getDataLoaderSuccess() {
160 ON_CALL(*this, getDataLoader(_, _))
161 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800162 }
163 void setDataLoaderStatusNotReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800164 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800165 }
166 void setDataLoaderStatusReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800167 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800168 }
169
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700170 int32_t setStorageParams(bool enableReadLogs) {
171 int32_t result = -1;
172 EXPECT_NE(mServiceConnector.get(), nullptr);
173 EXPECT_TRUE(mServiceConnector->setStorageParams(enableReadLogs, &result).isOk());
174 return result;
175 }
176
Songchun Fan3c82a302019-11-29 14:23:45 -0800177private:
178 int mId;
179 sp<IDataLoaderStatusListener> mListener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700180 sp<IIncrementalServiceConnector> mServiceConnector;
Songchun Fan68645c42020-02-27 15:57:35 -0800181 sp<IDataLoader> mDataLoader = sp<IDataLoader>(new FakeDataLoader());
Songchun Fan3c82a302019-11-29 14:23:45 -0800182};
183
184class MockIncFs : public IncFsWrapper {
185public:
Songchun Fan20d6ef22020-03-03 09:47:15 -0800186 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800187 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800188 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800189 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800190 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
191 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
192 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
193 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800194 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800195 ErrorCode(const Control& control, std::string_view from, std::string_view to));
196 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700197 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800198 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800199
200 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
201 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800202 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800203 metadata::Mount m;
204 m.mutable_storage()->set_id(100);
205 m.mutable_loader()->set_package_name("com.test");
206 m.mutable_loader()->set_arguments("com.uri");
207 const auto metadata = m.SerializeAsString();
208 m.mutable_loader()->release_arguments();
209 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800210 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800211 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800212 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800213 metadata::Storage st;
214 st.set_id(100);
215 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800216 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800217 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800218 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800219 metadata::BindPoint bp;
220 std::string destPath = "dest";
221 std::string srcPath = "src";
222 bp.set_storage_id(100);
223 bp.set_allocated_dest_path(&destPath);
224 bp.set_allocated_source_subdir(&srcPath);
225 const auto metadata = bp.SerializeAsString();
226 bp.release_source_subdir();
227 bp.release_dest_path();
228 return std::vector<char>(metadata.begin(), metadata.end());
229 }
230};
231
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700232class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700233public:
234 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700235 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700236 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
237
238 void checkPermissionSuccess() {
239 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
240 }
241 void checkPermissionFails() {
242 ON_CALL(*this, checkPermission(_, _, _))
243 .WillByDefault(
244 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
245 }
246 void initializeStartWatchingMode() {
247 ON_CALL(*this, startWatchingMode(_, _, _))
248 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
249 }
250 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
251 mStoredCallback = cb;
252 }
253
254 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700255};
256
Songchun Fan3c82a302019-11-29 14:23:45 -0800257class MockServiceManager : public ServiceManagerWrapper {
258public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800259 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800260 std::unique_ptr<MockDataLoaderManager> manager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700261 std::unique_ptr<MockIncFs> incfs,
262 std::unique_ptr<MockAppOpsManager> appOpsManager)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800263 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800264 mDataLoaderManager(std::move(manager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700265 mIncFs(std::move(incfs)),
266 mAppOpsManager(std::move(appOpsManager)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800267 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800268 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
269 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800270 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800271 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700272 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800273
274private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800275 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800276 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800277 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700278 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800279};
280
281// --- IncrementalServiceTest ---
282
Songchun Fan3c82a302019-11-29 14:23:45 -0800283class IncrementalServiceTest : public testing::Test {
284public:
285 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800286 auto vold = std::make_unique<NiceMock<MockVoldService>>();
287 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800288 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
289 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800290 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
291 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700292 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
293 mAppOpsManager = appOps.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800294 mIncrementalService =
295 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700296 std::move(dataloaderManager),
297 std::move(incFs),
298 std::move(appOps)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800299 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800300 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800301 mDataLoaderParcel.arguments = "uri";
Songchun Fan3c82a302019-11-29 14:23:45 -0800302 mIncrementalService->onSystemReady();
303 }
304
305 void setUpExistingMountDir(const std::string& rootDir) {
306 const auto dir = rootDir + "/dir1";
307 const auto mountDir = dir + "/mount";
308 const auto backingDir = dir + "/backing_store";
309 const auto storageDir = mountDir + "/st0";
310 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
311 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
312 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
313 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
314 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
315 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
316 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
317 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800318 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
319 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
320 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
321 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
322 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
323 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800324 }
325
326protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800327 NiceMock<MockVoldService>* mVold;
328 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800329 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700330 NiceMock<MockAppOpsManager>* mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800331 std::unique_ptr<IncrementalService> mIncrementalService;
332 TemporaryDir mRootDir;
333 DataLoaderParamsParcel mDataLoaderParcel;
334};
335
Songchun Fan3c82a302019-11-29 14:23:45 -0800336TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
337 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800338 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800339 TemporaryDir tempDir;
340 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800341 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800342 IncrementalService::CreateOptions::CreateNew);
343 ASSERT_LT(storageId, 0);
344}
345
346TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
347 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800348 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800349 TemporaryDir tempDir;
350 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800351 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800352 IncrementalService::CreateOptions::CreateNew);
353 ASSERT_LT(storageId, 0);
354}
355
356TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
357 mVold->mountIncFsSuccess();
358 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800359 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
360 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800361 EXPECT_CALL(*mVold, unmountIncFs(_));
362 TemporaryDir tempDir;
363 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800364 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800365 IncrementalService::CreateOptions::CreateNew);
366 ASSERT_LT(storageId, 0);
367}
368
369TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
370 mVold->mountIncFsSuccess();
371 mIncFs->makeFileSuccess();
372 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800373 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
374 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800375 EXPECT_CALL(*mVold, unmountIncFs(_));
376 TemporaryDir tempDir;
377 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800378 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800379 IncrementalService::CreateOptions::CreateNew);
380 ASSERT_LT(storageId, 0);
381}
382
383TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
384 mVold->mountIncFsSuccess();
385 mIncFs->makeFileSuccess();
386 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800387 mDataLoaderManager->initializeDataLoaderFails();
388 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800389 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
390 TemporaryDir tempDir;
391 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800392 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800393 IncrementalService::CreateOptions::CreateNew);
394 ASSERT_LT(storageId, 0);
395}
396
397TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
398 mVold->mountIncFsSuccess();
399 mIncFs->makeFileSuccess();
400 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800401 mDataLoaderManager->initializeDataLoaderSuccess();
402 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800403 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
404 TemporaryDir tempDir;
405 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800406 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800407 IncrementalService::CreateOptions::CreateNew);
408 ASSERT_GE(storageId, 0);
409 mIncrementalService->deleteStorage(storageId);
410}
411
412TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
413 mVold->mountIncFsSuccess();
414 mIncFs->makeFileSuccess();
415 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800416 mDataLoaderManager->initializeDataLoaderSuccess();
417 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800418 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
419 TemporaryDir tempDir;
420 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800421 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800422 IncrementalService::CreateOptions::CreateNew);
423 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800424 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800425}
426
427TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
428 mVold->mountIncFsSuccess();
429 mIncFs->makeFileSuccess();
430 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800431 mDataLoaderManager->initializeDataLoaderSuccess();
432 mDataLoaderManager->getDataLoaderSuccess();
433 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800434 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
435 TemporaryDir tempDir;
436 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800437 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800438 IncrementalService::CreateOptions::CreateNew);
439 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800440 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800441 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
442}
443
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700444TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
445 mVold->mountIncFsSuccess();
446 mIncFs->makeFileSuccess();
447 mVold->bindMountSuccess();
448 mVold->setIncFsMountOptionsSuccess();
449 mDataLoaderManager->initializeDataLoaderSuccess();
450 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700451 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700452 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
453 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700454 // We are calling setIncFsMountOptions(true).
455 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
456 // After setIncFsMountOptions succeeded expecting to start watching.
457 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
458 // Not expecting callback removal.
459 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700460 TemporaryDir tempDir;
461 int storageId =
462 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
463 IncrementalService::CreateOptions::CreateNew);
464 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700465 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700466}
467
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700468TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
469 mVold->mountIncFsSuccess();
470 mIncFs->makeFileSuccess();
471 mVold->bindMountSuccess();
472 mVold->setIncFsMountOptionsSuccess();
473 mDataLoaderManager->initializeDataLoaderSuccess();
474 mDataLoaderManager->getDataLoaderSuccess();
475 mAppOpsManager->checkPermissionSuccess();
476 mAppOpsManager->initializeStartWatchingMode();
477 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
478 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
479 // We are calling setIncFsMountOptions(true).
480 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
481 // setIncFsMountOptions(false) is called on the callback.
482 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
483 // After setIncFsMountOptions succeeded expecting to start watching.
484 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
485 // After callback is called, disable read logs and remove callback.
486 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
487 TemporaryDir tempDir;
488 int storageId =
489 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
490 IncrementalService::CreateOptions::CreateNew);
491 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700492 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700493 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
494 mAppOpsManager->mStoredCallback->opChanged(0, {});
495}
496
497TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
498 mVold->mountIncFsSuccess();
499 mIncFs->makeFileSuccess();
500 mVold->bindMountSuccess();
501 mDataLoaderManager->initializeDataLoaderSuccess();
502 mDataLoaderManager->getDataLoaderSuccess();
503 mAppOpsManager->checkPermissionFails();
504 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
505 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
506 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
507 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
508 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
509 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
510 TemporaryDir tempDir;
511 int storageId =
512 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
513 IncrementalService::CreateOptions::CreateNew);
514 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700515 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700516}
517
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700518TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
519 mVold->mountIncFsSuccess();
520 mIncFs->makeFileSuccess();
521 mVold->bindMountSuccess();
522 mVold->setIncFsMountOptionsFails();
523 mDataLoaderManager->initializeDataLoaderSuccess();
524 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700525 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700526 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
527 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700528 // We are calling setIncFsMountOptions.
529 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
530 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
531 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
532 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700533 TemporaryDir tempDir;
534 int storageId =
535 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
536 IncrementalService::CreateOptions::CreateNew);
537 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700538 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700539}
540
Songchun Fan3c82a302019-11-29 14:23:45 -0800541TEST_F(IncrementalServiceTest, testMakeDirectory) {
542 mVold->mountIncFsSuccess();
543 mIncFs->makeFileSuccess();
544 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800545 mDataLoaderManager->initializeDataLoaderSuccess();
546 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800547 TemporaryDir tempDir;
548 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800549 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800550 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800551 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800552
Songchun Fan103ba1d2020-02-03 17:32:32 -0800553 std::string tempPath(tempDir.path);
554 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800555 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800556 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800557
Songchun Fan103ba1d2020-02-03 17:32:32 -0800558 // Expecting incfs to call makeDir on a path like:
559 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
560 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800561 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
562 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800563}
564
565TEST_F(IncrementalServiceTest, testMakeDirectories) {
566 mVold->mountIncFsSuccess();
567 mIncFs->makeFileSuccess();
568 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800569 mDataLoaderManager->initializeDataLoaderSuccess();
570 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800571 TemporaryDir tempDir;
572 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800573 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800574 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800575 auto first = "first"sv;
576 auto second = "second"sv;
577 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800578
579 std::string tempPath(tempDir.path);
580 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800581 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800582
Songchun Fan3c82a302019-11-29 14:23:45 -0800583 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800584 auto parent_path = std::string(first) + "/" + std::string(second);
585 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800586
587 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
588 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
589 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
590
591 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
592 .WillOnce(Return(-ENOENT));
593 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
594 .WillOnce(Return(-ENOENT));
595 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
596 .WillOnce(Return(0));
597 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
598 .WillOnce(Return(0));
599 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
600 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800601 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800602}
603} // namespace android::os::incremental