blob: 991131950531bf401744569d57d15599f2d35ac0 [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
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700134 void initializeDataLoaderSuccess() {
135 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
136 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
137 }
138 void initializeDataLoaderFails() {
139 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
140 .WillByDefault(Return(
141 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
142 }
143 void getDataLoaderSuccess() {
144 ON_CALL(*this, getDataLoader(_, _))
145 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
146 }
147 void destroyDataLoaderOk() {
148 ON_CALL(*this, destroyDataLoader(_))
149 .WillByDefault(Invoke(this, &MockDataLoaderManager::setDataLoaderStatusDestroyed));
150 }
Songchun Fan68645c42020-02-27 15:57:35 -0800151 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
152 const FileSystemControlParcel& control,
153 const sp<IDataLoaderStatusListener>& listener,
154 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800155 mId = mountId;
156 mListener = listener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700157 mServiceConnector = control.service;
Songchun Fan3c82a302019-11-29 14:23:45 -0800158 *_aidl_return = true;
159 return binder::Status::ok();
160 }
Songchun Fan68645c42020-02-27 15:57:35 -0800161 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
162 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800163 return binder::Status::ok();
164 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800165 void setDataLoaderStatusNotReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800166 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800167 }
168 void setDataLoaderStatusReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800169 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800170 }
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700171 binder::Status setDataLoaderStatusDestroyed(int32_t id) {
172 if (mListener) {
173 mListener->onStatusChanged(id, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
174 }
175 return binder::Status::ok();
176 }
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700177 int32_t setStorageParams(bool enableReadLogs) {
178 int32_t result = -1;
179 EXPECT_NE(mServiceConnector.get(), nullptr);
180 EXPECT_TRUE(mServiceConnector->setStorageParams(enableReadLogs, &result).isOk());
181 return result;
182 }
183
Songchun Fan3c82a302019-11-29 14:23:45 -0800184private:
185 int mId;
186 sp<IDataLoaderStatusListener> mListener;
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700187 sp<IIncrementalServiceConnector> mServiceConnector;
Songchun Fan68645c42020-02-27 15:57:35 -0800188 sp<IDataLoader> mDataLoader = sp<IDataLoader>(new FakeDataLoader());
Songchun Fan3c82a302019-11-29 14:23:45 -0800189};
190
191class MockIncFs : public IncFsWrapper {
192public:
Songchun Fan20d6ef22020-03-03 09:47:15 -0800193 MOCK_CONST_METHOD3(createControl, Control(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs));
Songchun Fan3c82a302019-11-29 14:23:45 -0800194 MOCK_CONST_METHOD5(makeFile,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800195 ErrorCode(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800196 NewFileParams params));
Songchun Fan20d6ef22020-03-03 09:47:15 -0800197 MOCK_CONST_METHOD3(makeDir, ErrorCode(const Control& control, std::string_view path, int mode));
198 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, FileId fileid));
199 MOCK_CONST_METHOD2(getMetadata, RawMetadata(const Control& control, std::string_view path));
200 MOCK_CONST_METHOD2(getFileId, FileId(const Control& control, std::string_view path));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800201 MOCK_CONST_METHOD3(link,
Songchun Fan20d6ef22020-03-03 09:47:15 -0800202 ErrorCode(const Control& control, std::string_view from, std::string_view to));
203 MOCK_CONST_METHOD2(unlink, ErrorCode(const Control& control, std::string_view path));
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700204 MOCK_CONST_METHOD2(openForSpecialOps, base::unique_fd(const Control& control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800205 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800206
207 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
208 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800209 RawMetadata getMountInfoMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800210 metadata::Mount m;
211 m.mutable_storage()->set_id(100);
212 m.mutable_loader()->set_package_name("com.test");
213 m.mutable_loader()->set_arguments("com.uri");
214 const auto metadata = m.SerializeAsString();
215 m.mutable_loader()->release_arguments();
216 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800217 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800218 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800219 RawMetadata getStorageMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800220 metadata::Storage st;
221 st.set_id(100);
222 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800223 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800224 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800225 RawMetadata getBindPointMetadata(const Control& control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800226 metadata::BindPoint bp;
227 std::string destPath = "dest";
228 std::string srcPath = "src";
229 bp.set_storage_id(100);
230 bp.set_allocated_dest_path(&destPath);
231 bp.set_allocated_source_subdir(&srcPath);
232 const auto metadata = bp.SerializeAsString();
233 bp.release_source_subdir();
234 bp.release_dest_path();
235 return std::vector<char>(metadata.begin(), metadata.end());
236 }
237};
238
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700239class MockAppOpsManager : public AppOpsManagerWrapper {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700240public:
241 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700242 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700243 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
244
245 void checkPermissionSuccess() {
246 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
247 }
248 void checkPermissionFails() {
249 ON_CALL(*this, checkPermission(_, _, _))
250 .WillByDefault(
251 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
252 }
253 void initializeStartWatchingMode() {
254 ON_CALL(*this, startWatchingMode(_, _, _))
255 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
256 }
257 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
258 mStoredCallback = cb;
259 }
260
261 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700262};
263
Songchun Fan3c82a302019-11-29 14:23:45 -0800264class MockServiceManager : public ServiceManagerWrapper {
265public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800266 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800267 std::unique_ptr<MockDataLoaderManager> manager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700268 std::unique_ptr<MockIncFs> incfs,
269 std::unique_ptr<MockAppOpsManager> appOpsManager)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800270 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800271 mDataLoaderManager(std::move(manager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700272 mIncFs(std::move(incfs)),
273 mAppOpsManager(std::move(appOpsManager)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800274 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800275 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
276 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800277 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800278 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700279 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800280
281private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800282 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800283 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800284 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700285 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800286};
287
288// --- IncrementalServiceTest ---
289
Songchun Fan3c82a302019-11-29 14:23:45 -0800290class IncrementalServiceTest : public testing::Test {
291public:
292 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800293 auto vold = std::make_unique<NiceMock<MockVoldService>>();
294 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800295 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
296 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800297 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
298 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700299 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
300 mAppOpsManager = appOps.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800301 mIncrementalService =
302 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700303 std::move(dataloaderManager),
304 std::move(incFs),
305 std::move(appOps)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800306 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800307 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800308 mDataLoaderParcel.arguments = "uri";
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700309 mDataLoaderManager->destroyDataLoaderOk();
Songchun Fan3c82a302019-11-29 14:23:45 -0800310 mIncrementalService->onSystemReady();
311 }
312
313 void setUpExistingMountDir(const std::string& rootDir) {
314 const auto dir = rootDir + "/dir1";
315 const auto mountDir = dir + "/mount";
316 const auto backingDir = dir + "/backing_store";
317 const auto storageDir = mountDir + "/st0";
318 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
319 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
320 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
321 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
322 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
323 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
324 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
325 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800326 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
327 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
328 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
329 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
330 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
331 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800332 }
333
334protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800335 NiceMock<MockVoldService>* mVold;
336 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800337 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700338 NiceMock<MockAppOpsManager>* mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800339 std::unique_ptr<IncrementalService> mIncrementalService;
340 TemporaryDir mRootDir;
341 DataLoaderParamsParcel mDataLoaderParcel;
342};
343
Songchun Fan3c82a302019-11-29 14:23:45 -0800344TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
345 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800346 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800347 TemporaryDir tempDir;
348 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800349 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800350 IncrementalService::CreateOptions::CreateNew);
351 ASSERT_LT(storageId, 0);
352}
353
354TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
355 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800356 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700357 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800358 TemporaryDir tempDir;
359 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800360 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800361 IncrementalService::CreateOptions::CreateNew);
362 ASSERT_LT(storageId, 0);
363}
364
365TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
366 mVold->mountIncFsSuccess();
367 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800368 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700369 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800370 EXPECT_CALL(*mVold, unmountIncFs(_));
371 TemporaryDir tempDir;
372 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800373 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800374 IncrementalService::CreateOptions::CreateNew);
375 ASSERT_LT(storageId, 0);
376}
377
378TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
379 mVold->mountIncFsSuccess();
380 mIncFs->makeFileSuccess();
381 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800382 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700383 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800384 EXPECT_CALL(*mVold, unmountIncFs(_));
385 TemporaryDir tempDir;
386 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800387 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800388 IncrementalService::CreateOptions::CreateNew);
389 ASSERT_LT(storageId, 0);
390}
391
392TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
393 mVold->mountIncFsSuccess();
394 mIncFs->makeFileSuccess();
395 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800396 mDataLoaderManager->initializeDataLoaderFails();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700397 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800398 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
399 TemporaryDir tempDir;
400 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800401 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800402 IncrementalService::CreateOptions::CreateNew);
403 ASSERT_LT(storageId, 0);
404}
405
406TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
407 mVold->mountIncFsSuccess();
408 mIncFs->makeFileSuccess();
409 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800410 mDataLoaderManager->initializeDataLoaderSuccess();
Alex Buynytskyy0ea4ff42020-04-09 17:25:42 -0700411 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_)).Times(1);
Songchun Fan3c82a302019-11-29 14:23:45 -0800412 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
413 TemporaryDir tempDir;
414 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800415 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800416 IncrementalService::CreateOptions::CreateNew);
417 ASSERT_GE(storageId, 0);
418 mIncrementalService->deleteStorage(storageId);
419}
420
421TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
422 mVold->mountIncFsSuccess();
423 mIncFs->makeFileSuccess();
424 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800425 mDataLoaderManager->initializeDataLoaderSuccess();
426 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800427 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
428 TemporaryDir tempDir;
429 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800430 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800431 IncrementalService::CreateOptions::CreateNew);
432 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800433 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800434}
435
436TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
437 mVold->mountIncFsSuccess();
438 mIncFs->makeFileSuccess();
439 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800440 mDataLoaderManager->initializeDataLoaderSuccess();
441 mDataLoaderManager->getDataLoaderSuccess();
442 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800443 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
444 TemporaryDir tempDir;
445 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800446 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800447 IncrementalService::CreateOptions::CreateNew);
448 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800449 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800450 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
451}
452
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700453TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
454 mVold->mountIncFsSuccess();
455 mIncFs->makeFileSuccess();
456 mVold->bindMountSuccess();
457 mVold->setIncFsMountOptionsSuccess();
458 mDataLoaderManager->initializeDataLoaderSuccess();
459 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700460 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700461 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
462 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700463 // We are calling setIncFsMountOptions(true).
464 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
465 // After setIncFsMountOptions succeeded expecting to start watching.
466 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
467 // Not expecting callback removal.
468 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700469 TemporaryDir tempDir;
470 int storageId =
471 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
472 IncrementalService::CreateOptions::CreateNew);
473 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700474 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700475}
476
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700477TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
478 mVold->mountIncFsSuccess();
479 mIncFs->makeFileSuccess();
480 mVold->bindMountSuccess();
481 mVold->setIncFsMountOptionsSuccess();
482 mDataLoaderManager->initializeDataLoaderSuccess();
483 mDataLoaderManager->getDataLoaderSuccess();
484 mAppOpsManager->checkPermissionSuccess();
485 mAppOpsManager->initializeStartWatchingMode();
486 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
487 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
488 // We are calling setIncFsMountOptions(true).
489 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
490 // setIncFsMountOptions(false) is called on the callback.
491 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
492 // After setIncFsMountOptions succeeded expecting to start watching.
493 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
494 // After callback is called, disable read logs and remove callback.
495 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
496 TemporaryDir tempDir;
497 int storageId =
498 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
499 IncrementalService::CreateOptions::CreateNew);
500 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700501 ASSERT_GE(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700502 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
503 mAppOpsManager->mStoredCallback->opChanged(0, {});
504}
505
506TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
507 mVold->mountIncFsSuccess();
508 mIncFs->makeFileSuccess();
509 mVold->bindMountSuccess();
510 mDataLoaderManager->initializeDataLoaderSuccess();
511 mDataLoaderManager->getDataLoaderSuccess();
512 mAppOpsManager->checkPermissionFails();
513 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
514 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
515 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
516 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
517 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
518 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
519 TemporaryDir tempDir;
520 int storageId =
521 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
522 IncrementalService::CreateOptions::CreateNew);
523 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700524 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700525}
526
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700527TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
528 mVold->mountIncFsSuccess();
529 mIncFs->makeFileSuccess();
530 mVold->bindMountSuccess();
531 mVold->setIncFsMountOptionsFails();
532 mDataLoaderManager->initializeDataLoaderSuccess();
533 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700534 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700535 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
536 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700537 // We are calling setIncFsMountOptions.
538 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
539 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
540 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
541 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700542 TemporaryDir tempDir;
543 int storageId =
544 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
545 IncrementalService::CreateOptions::CreateNew);
546 ASSERT_GE(storageId, 0);
Alex Buynytskyy0a646ca2020-04-08 12:18:01 -0700547 ASSERT_LT(mDataLoaderManager->setStorageParams(true), 0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700548}
549
Songchun Fan3c82a302019-11-29 14:23:45 -0800550TEST_F(IncrementalServiceTest, testMakeDirectory) {
551 mVold->mountIncFsSuccess();
552 mIncFs->makeFileSuccess();
553 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800554 mDataLoaderManager->initializeDataLoaderSuccess();
555 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800556 TemporaryDir tempDir;
557 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800558 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800559 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800560 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800561
Songchun Fan103ba1d2020-02-03 17:32:32 -0800562 std::string tempPath(tempDir.path);
563 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800564 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800565 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800566
Songchun Fan103ba1d2020-02-03 17:32:32 -0800567 // Expecting incfs to call makeDir on a path like:
568 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
569 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800570 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
571 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800572}
573
574TEST_F(IncrementalServiceTest, testMakeDirectories) {
575 mVold->mountIncFsSuccess();
576 mIncFs->makeFileSuccess();
577 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800578 mDataLoaderManager->initializeDataLoaderSuccess();
579 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800580 TemporaryDir tempDir;
581 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800582 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800583 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800584 auto first = "first"sv;
585 auto second = "second"sv;
586 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800587
588 std::string tempPath(tempDir.path);
589 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800590 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800591
Songchun Fan3c82a302019-11-29 14:23:45 -0800592 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800593 auto parent_path = std::string(first) + "/" + std::string(second);
594 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800595
596 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
597 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
598 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
599
600 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
601 .WillOnce(Return(-ENOENT));
602 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
603 .WillOnce(Return(-ENOENT));
604 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
605 .WillOnce(Return(0));
606 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
607 .WillOnce(Return(0));
608 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
609 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800610 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800611}
612} // namespace android::os::incremental