blob: 0635ae1692819121887249462cc36763618e22c7 [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 {
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700224public:
225 MOCK_CONST_METHOD3(checkPermission, binder::Status(const char*, const char*, const char*));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700226 MOCK_METHOD3(startWatchingMode, void(int32_t, const String16&, const sp<IAppOpsCallback>&));
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700227 MOCK_METHOD1(stopWatchingMode, void(const sp<IAppOpsCallback>&));
228
229 void checkPermissionSuccess() {
230 ON_CALL(*this, checkPermission(_, _, _)).WillByDefault(Return(android::incremental::Ok()));
231 }
232 void checkPermissionFails() {
233 ON_CALL(*this, checkPermission(_, _, _))
234 .WillByDefault(
235 Return(android::incremental::Exception(binder::Status::EX_SECURITY, {})));
236 }
237 void initializeStartWatchingMode() {
238 ON_CALL(*this, startWatchingMode(_, _, _))
239 .WillByDefault(Invoke(this, &MockAppOpsManager::storeCallback));
240 }
241 void storeCallback(int32_t, const String16&, const sp<IAppOpsCallback>& cb) {
242 mStoredCallback = cb;
243 }
244
245 sp<IAppOpsCallback> mStoredCallback;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700246};
247
Songchun Fan3c82a302019-11-29 14:23:45 -0800248class MockServiceManager : public ServiceManagerWrapper {
249public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800250 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800251 std::unique_ptr<MockDataLoaderManager> manager,
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700252 std::unique_ptr<MockIncFs> incfs,
253 std::unique_ptr<MockAppOpsManager> appOpsManager)
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800254 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800255 mDataLoaderManager(std::move(manager)),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700256 mIncFs(std::move(incfs)),
257 mAppOpsManager(std::move(appOpsManager)) {}
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800258 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800259 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
260 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800261 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800262 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700263 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final { return std::move(mAppOpsManager); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800264
265private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800266 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800267 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800268 std::unique_ptr<MockIncFs> mIncFs;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700269 std::unique_ptr<MockAppOpsManager> mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800270};
271
272// --- IncrementalServiceTest ---
273
Songchun Fan3c82a302019-11-29 14:23:45 -0800274class IncrementalServiceTest : public testing::Test {
275public:
276 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800277 auto vold = std::make_unique<NiceMock<MockVoldService>>();
278 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800279 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
280 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800281 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
282 mIncFs = incFs.get();
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700283 auto appOps = std::make_unique<NiceMock<MockAppOpsManager>>();
284 mAppOpsManager = appOps.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800285 mIncrementalService =
286 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700287 std::move(dataloaderManager),
288 std::move(incFs),
289 std::move(appOps)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800290 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800291 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800292 mDataLoaderParcel.arguments = "uri";
Songchun Fan3c82a302019-11-29 14:23:45 -0800293 mIncrementalService->onSystemReady();
294 }
295
296 void setUpExistingMountDir(const std::string& rootDir) {
297 const auto dir = rootDir + "/dir1";
298 const auto mountDir = dir + "/mount";
299 const auto backingDir = dir + "/backing_store";
300 const auto storageDir = mountDir + "/st0";
301 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
302 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
303 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
304 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
305 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
306 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
307 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
308 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800309 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
310 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
311 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
312 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
313 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
314 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800315 }
316
317protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800318 NiceMock<MockVoldService>* mVold;
319 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800320 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700321 NiceMock<MockAppOpsManager>* mAppOpsManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800322 std::unique_ptr<IncrementalService> mIncrementalService;
323 TemporaryDir mRootDir;
324 DataLoaderParamsParcel mDataLoaderParcel;
325};
326
Songchun Fan3c82a302019-11-29 14:23:45 -0800327TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
328 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800329 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800330 TemporaryDir tempDir;
331 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800332 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800333 IncrementalService::CreateOptions::CreateNew);
334 ASSERT_LT(storageId, 0);
335}
336
337TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
338 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800339 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800340 TemporaryDir tempDir;
341 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800342 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800343 IncrementalService::CreateOptions::CreateNew);
344 ASSERT_LT(storageId, 0);
345}
346
347TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
348 mVold->mountIncFsSuccess();
349 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800350 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
351 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800352 EXPECT_CALL(*mVold, unmountIncFs(_));
353 TemporaryDir tempDir;
354 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800355 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800356 IncrementalService::CreateOptions::CreateNew);
357 ASSERT_LT(storageId, 0);
358}
359
360TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
361 mVold->mountIncFsSuccess();
362 mIncFs->makeFileSuccess();
363 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800364 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
365 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800366 EXPECT_CALL(*mVold, unmountIncFs(_));
367 TemporaryDir tempDir;
368 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800369 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800370 IncrementalService::CreateOptions::CreateNew);
371 ASSERT_LT(storageId, 0);
372}
373
374TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
375 mVold->mountIncFsSuccess();
376 mIncFs->makeFileSuccess();
377 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800378 mDataLoaderManager->initializeDataLoaderFails();
379 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800380 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
381 TemporaryDir tempDir;
382 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800383 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800384 IncrementalService::CreateOptions::CreateNew);
385 ASSERT_LT(storageId, 0);
386}
387
388TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
389 mVold->mountIncFsSuccess();
390 mIncFs->makeFileSuccess();
391 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800392 mDataLoaderManager->initializeDataLoaderSuccess();
393 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800394 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
395 TemporaryDir tempDir;
396 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800397 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800398 IncrementalService::CreateOptions::CreateNew);
399 ASSERT_GE(storageId, 0);
400 mIncrementalService->deleteStorage(storageId);
401}
402
403TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
404 mVold->mountIncFsSuccess();
405 mIncFs->makeFileSuccess();
406 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800407 mDataLoaderManager->initializeDataLoaderSuccess();
408 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800409 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
410 TemporaryDir tempDir;
411 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800412 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800413 IncrementalService::CreateOptions::CreateNew);
414 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800415 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800416}
417
418TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
419 mVold->mountIncFsSuccess();
420 mIncFs->makeFileSuccess();
421 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800422 mDataLoaderManager->initializeDataLoaderSuccess();
423 mDataLoaderManager->getDataLoaderSuccess();
424 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800425 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
426 TemporaryDir tempDir;
427 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800428 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800429 IncrementalService::CreateOptions::CreateNew);
430 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800431 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800432 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
433}
434
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700435TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccess) {
436 mVold->mountIncFsSuccess();
437 mIncFs->makeFileSuccess();
438 mVold->bindMountSuccess();
439 mVold->setIncFsMountOptionsSuccess();
440 mDataLoaderManager->initializeDataLoaderSuccess();
441 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700442 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700443 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
444 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700445 // We are calling setIncFsMountOptions(true).
446 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
447 // After setIncFsMountOptions succeeded expecting to start watching.
448 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
449 // Not expecting callback removal.
450 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700451 TemporaryDir tempDir;
452 int storageId =
453 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
454 IncrementalService::CreateOptions::CreateNew);
455 ASSERT_GE(storageId, 0);
456 ASSERT_GE(mIncrementalService->setStorageParams(storageId, true), 0);
457}
458
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700459TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsSuccessAndPermissionChanged) {
460 mVold->mountIncFsSuccess();
461 mIncFs->makeFileSuccess();
462 mVold->bindMountSuccess();
463 mVold->setIncFsMountOptionsSuccess();
464 mDataLoaderManager->initializeDataLoaderSuccess();
465 mDataLoaderManager->getDataLoaderSuccess();
466 mAppOpsManager->checkPermissionSuccess();
467 mAppOpsManager->initializeStartWatchingMode();
468 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
469 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
470 // We are calling setIncFsMountOptions(true).
471 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
472 // setIncFsMountOptions(false) is called on the callback.
473 EXPECT_CALL(*mVold, setIncFsMountOptions(_, false)).Times(1);
474 // After setIncFsMountOptions succeeded expecting to start watching.
475 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(1);
476 // After callback is called, disable read logs and remove callback.
477 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(1);
478 TemporaryDir tempDir;
479 int storageId =
480 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
481 IncrementalService::CreateOptions::CreateNew);
482 ASSERT_GE(storageId, 0);
483 ASSERT_GE(mIncrementalService->setStorageParams(storageId, true), 0);
484 ASSERT_NE(nullptr, mAppOpsManager->mStoredCallback.get());
485 mAppOpsManager->mStoredCallback->opChanged(0, {});
486}
487
488TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsCheckPermissionFails) {
489 mVold->mountIncFsSuccess();
490 mIncFs->makeFileSuccess();
491 mVold->bindMountSuccess();
492 mDataLoaderManager->initializeDataLoaderSuccess();
493 mDataLoaderManager->getDataLoaderSuccess();
494 mAppOpsManager->checkPermissionFails();
495 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
496 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
497 // checkPermission fails, no calls to set opitions, start or stop WatchingMode.
498 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(0);
499 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
500 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
501 TemporaryDir tempDir;
502 int storageId =
503 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
504 IncrementalService::CreateOptions::CreateNew);
505 ASSERT_GE(storageId, 0);
506 ASSERT_LT(mIncrementalService->setStorageParams(storageId, true), 0);
507}
508
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700509TEST_F(IncrementalServiceTest, testSetIncFsMountOptionsFails) {
510 mVold->mountIncFsSuccess();
511 mIncFs->makeFileSuccess();
512 mVold->bindMountSuccess();
513 mVold->setIncFsMountOptionsFails();
514 mDataLoaderManager->initializeDataLoaderSuccess();
515 mDataLoaderManager->getDataLoaderSuccess();
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700516 mAppOpsManager->checkPermissionSuccess();
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700517 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
518 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
Alex Buynytskyy1d892162020-04-03 23:00:19 -0700519 // We are calling setIncFsMountOptions.
520 EXPECT_CALL(*mVold, setIncFsMountOptions(_, true)).Times(1);
521 // setIncFsMountOptions fails, no calls to start or stop WatchingMode.
522 EXPECT_CALL(*mAppOpsManager, startWatchingMode(_, _, _)).Times(0);
523 EXPECT_CALL(*mAppOpsManager, stopWatchingMode(_)).Times(0);
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700524 TemporaryDir tempDir;
525 int storageId =
526 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
527 IncrementalService::CreateOptions::CreateNew);
528 ASSERT_GE(storageId, 0);
529 ASSERT_LT(mIncrementalService->setStorageParams(storageId, true), 0);
530}
531
Songchun Fan3c82a302019-11-29 14:23:45 -0800532TEST_F(IncrementalServiceTest, testMakeDirectory) {
533 mVold->mountIncFsSuccess();
534 mIncFs->makeFileSuccess();
535 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800536 mDataLoaderManager->initializeDataLoaderSuccess();
537 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800538 TemporaryDir tempDir;
539 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800540 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800541 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800542 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800543
Songchun Fan103ba1d2020-02-03 17:32:32 -0800544 std::string tempPath(tempDir.path);
545 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800546 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800547 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800548
Songchun Fan103ba1d2020-02-03 17:32:32 -0800549 // Expecting incfs to call makeDir on a path like:
550 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
551 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800552 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
553 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800554}
555
556TEST_F(IncrementalServiceTest, testMakeDirectories) {
557 mVold->mountIncFsSuccess();
558 mIncFs->makeFileSuccess();
559 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800560 mDataLoaderManager->initializeDataLoaderSuccess();
561 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800562 TemporaryDir tempDir;
563 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800564 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800565 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800566 auto first = "first"sv;
567 auto second = "second"sv;
568 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800569
570 std::string tempPath(tempDir.path);
571 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800572 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800573
Songchun Fan3c82a302019-11-29 14:23:45 -0800574 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800575 auto parent_path = std::string(first) + "/" + std::string(second);
576 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800577
578 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
579 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
580 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
581
582 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
583 .WillOnce(Return(-ENOENT));
584 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
585 .WillOnce(Return(-ENOENT));
586 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
587 .WillOnce(Return(0));
588 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
589 .WillOnce(Return(0));
590 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
591 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800592 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800593}
594} // namespace android::os::incremental