blob: 6002226f96307eacb52b6ad647b215c6f192b544 [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));
55
56 void mountIncFsFails() {
57 ON_CALL(*this, mountIncFs(_, _, _, _))
58 .WillByDefault(
59 Return(binder::Status::fromExceptionCode(1, String8("failed to mount"))));
60 }
61 void mountIncFsInvalidControlParcel() {
62 ON_CALL(*this, mountIncFs(_, _, _, _))
63 .WillByDefault(Invoke(this, &MockVoldService::getInvalidControlParcel));
64 }
65 void mountIncFsSuccess() {
66 ON_CALL(*this, mountIncFs(_, _, _, _))
67 .WillByDefault(Invoke(this, &MockVoldService::incFsSuccess));
68 }
69 void bindMountFails() {
70 ON_CALL(*this, bindMount(_, _))
71 .WillByDefault(Return(
72 binder::Status::fromExceptionCode(1, String8("failed to bind-mount"))));
73 }
74 void bindMountSuccess() {
75 ON_CALL(*this, bindMount(_, _)).WillByDefault(Return(binder::Status::ok()));
76 }
77 binder::Status getInvalidControlParcel(const std::string& imagePath,
78 const std::string& targetDir, int32_t flags,
79 IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080080 _aidl_return = {};
Songchun Fan3c82a302019-11-29 14:23:45 -080081 return binder::Status::ok();
82 }
83 binder::Status incFsSuccess(const std::string& imagePath, const std::string& targetDir,
84 int32_t flags, IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080085 _aidl_return->pendingReads.reset(base::unique_fd(dup(STDIN_FILENO)));
86 _aidl_return->cmd.reset(base::unique_fd(dup(STDIN_FILENO)));
87 _aidl_return->log.reset(base::unique_fd(dup(STDIN_FILENO)));
Songchun Fan3c82a302019-11-29 14:23:45 -080088 return binder::Status::ok();
89 }
90
91private:
92 TemporaryFile cmdFile;
93 TemporaryFile logFile;
Songchun Fan3c82a302019-11-29 14:23:45 -080094};
95
Songchun Fan68645c42020-02-27 15:57:35 -080096class FakeDataLoader : public IDataLoader {
Songchun Fan3c82a302019-11-29 14:23:45 -080097public:
Songchun Fan68645c42020-02-27 15:57:35 -080098 IBinder* onAsBinder() override { return nullptr; }
99 binder::Status create(int32_t, const DataLoaderParamsParcel&, const FileSystemControlParcel&,
100 const sp<IDataLoaderStatusListener>&) override {
101 return binder::Status::ok();
102 }
103 binder::Status start() override { return binder::Status::ok(); }
104 binder::Status stop() override { return binder::Status::ok(); }
105 binder::Status destroy() override { return binder::Status::ok(); }
106 binder::Status prepareImage(const std::vector<InstallationFileParcel>&,
107 const std::vector<std::string>&) override {
108 return binder::Status::ok();
109 }
110};
111
112class MockDataLoaderManager : public DataLoaderManagerWrapper {
113public:
114 MOCK_CONST_METHOD5(initializeDataLoader,
115 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
116 const FileSystemControlParcel& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800117 const sp<IDataLoaderStatusListener>& listener,
118 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800119 MOCK_CONST_METHOD2(getDataLoader,
120 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Songchun Fan3c82a302019-11-29 14:23:45 -0800121 MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800122
Songchun Fan68645c42020-02-27 15:57:35 -0800123 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
124 const FileSystemControlParcel& control,
125 const sp<IDataLoaderStatusListener>& listener,
126 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800127 mId = mountId;
128 mListener = listener;
129 *_aidl_return = true;
130 return binder::Status::ok();
131 }
132
Songchun Fan68645c42020-02-27 15:57:35 -0800133 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
134 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800135 return binder::Status::ok();
136 }
137
Songchun Fan68645c42020-02-27 15:57:35 -0800138 void initializeDataLoaderFails() {
139 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
Songchun Fan3c82a302019-11-29 14:23:45 -0800140 .WillByDefault(Return(
141 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
142 }
Songchun Fan68645c42020-02-27 15:57:35 -0800143 void initializeDataLoaderSuccess() {
144 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
145 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800146 }
Songchun Fan68645c42020-02-27 15:57:35 -0800147 void getDataLoaderSuccess() {
148 ON_CALL(*this, getDataLoader(_, _))
149 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800150 }
151 void setDataLoaderStatusNotReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800152 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800153 }
154 void setDataLoaderStatusReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800155 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800156 }
157
158private:
159 int mId;
160 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800161 sp<IDataLoader> mDataLoader = sp<IDataLoader>(new FakeDataLoader());
Songchun Fan3c82a302019-11-29 14:23:45 -0800162};
163
164class MockIncFs : public IncFsWrapper {
165public:
166 MOCK_CONST_METHOD5(makeFile,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800167 ErrorCode(Control control, std::string_view path, int mode, FileId id,
168 NewFileParams params));
169 MOCK_CONST_METHOD3(makeDir, ErrorCode(Control control, std::string_view path, int mode));
170 MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, FileId fileid));
171 MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, std::string_view path));
172 MOCK_CONST_METHOD2(getFileId, FileId(Control control, std::string_view path));
173 MOCK_CONST_METHOD3(link,
174 ErrorCode(Control control, std::string_view from, std::string_view to));
175 MOCK_CONST_METHOD2(unlink, ErrorCode(Control control, std::string_view path));
176 MOCK_CONST_METHOD2(openWrite, base::unique_fd(Control control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800177 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800178
179 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
180 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800181 RawMetadata getMountInfoMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800182 metadata::Mount m;
183 m.mutable_storage()->set_id(100);
184 m.mutable_loader()->set_package_name("com.test");
185 m.mutable_loader()->set_arguments("com.uri");
186 const auto metadata = m.SerializeAsString();
187 m.mutable_loader()->release_arguments();
188 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800189 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800190 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800191 RawMetadata getStorageMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800192 metadata::Storage st;
193 st.set_id(100);
194 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800195 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800196 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800197 RawMetadata getBindPointMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800198 metadata::BindPoint bp;
199 std::string destPath = "dest";
200 std::string srcPath = "src";
201 bp.set_storage_id(100);
202 bp.set_allocated_dest_path(&destPath);
203 bp.set_allocated_source_subdir(&srcPath);
204 const auto metadata = bp.SerializeAsString();
205 bp.release_source_subdir();
206 bp.release_dest_path();
207 return std::vector<char>(metadata.begin(), metadata.end());
208 }
209};
210
211class MockServiceManager : public ServiceManagerWrapper {
212public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800213 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800214 std::unique_ptr<MockDataLoaderManager> manager,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800215 std::unique_ptr<MockIncFs> incfs)
216 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800217 mDataLoaderManager(std::move(manager)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800218 mIncFs(std::move(incfs)) {}
219 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800220 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
221 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800222 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800223 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800224
225private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800226 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800227 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800228 std::unique_ptr<MockIncFs> mIncFs;
Songchun Fan3c82a302019-11-29 14:23:45 -0800229};
230
231// --- IncrementalServiceTest ---
232
Songchun Fan3c82a302019-11-29 14:23:45 -0800233class IncrementalServiceTest : public testing::Test {
234public:
235 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800236 auto vold = std::make_unique<NiceMock<MockVoldService>>();
237 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800238 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
239 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800240 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
241 mIncFs = incFs.get();
242 mIncrementalService =
243 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
244 std::move(
Songchun Fan68645c42020-02-27 15:57:35 -0800245 dataloaderManager),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800246 std::move(incFs)),
247 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800248 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800249 mDataLoaderParcel.arguments = "uri";
Songchun Fan3c82a302019-11-29 14:23:45 -0800250 mIncrementalService->onSystemReady();
251 }
252
253 void setUpExistingMountDir(const std::string& rootDir) {
254 const auto dir = rootDir + "/dir1";
255 const auto mountDir = dir + "/mount";
256 const auto backingDir = dir + "/backing_store";
257 const auto storageDir = mountDir + "/st0";
258 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
259 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
260 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
261 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
262 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
263 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
264 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
265 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800266 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
267 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
268 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
269 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
270 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
271 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800272 }
273
274protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800275 NiceMock<MockVoldService>* mVold;
276 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800277 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800278 std::unique_ptr<IncrementalService> mIncrementalService;
279 TemporaryDir mRootDir;
280 DataLoaderParamsParcel mDataLoaderParcel;
281};
282
Songchun Fan3c82a302019-11-29 14:23:45 -0800283TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
284 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800285 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800286 TemporaryDir tempDir;
287 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800288 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800289 IncrementalService::CreateOptions::CreateNew);
290 ASSERT_LT(storageId, 0);
291}
292
293TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
294 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800295 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800296 TemporaryDir tempDir;
297 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800298 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800299 IncrementalService::CreateOptions::CreateNew);
300 ASSERT_LT(storageId, 0);
301}
302
303TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
304 mVold->mountIncFsSuccess();
305 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800306 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
307 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800308 EXPECT_CALL(*mVold, unmountIncFs(_));
309 TemporaryDir tempDir;
310 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800311 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800312 IncrementalService::CreateOptions::CreateNew);
313 ASSERT_LT(storageId, 0);
314}
315
316TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
317 mVold->mountIncFsSuccess();
318 mIncFs->makeFileSuccess();
319 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800320 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
321 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800322 EXPECT_CALL(*mVold, unmountIncFs(_));
323 TemporaryDir tempDir;
324 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800325 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800326 IncrementalService::CreateOptions::CreateNew);
327 ASSERT_LT(storageId, 0);
328}
329
330TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
331 mVold->mountIncFsSuccess();
332 mIncFs->makeFileSuccess();
333 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800334 mDataLoaderManager->initializeDataLoaderFails();
335 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800336 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
337 TemporaryDir tempDir;
338 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800339 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800340 IncrementalService::CreateOptions::CreateNew);
341 ASSERT_LT(storageId, 0);
342}
343
344TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
345 mVold->mountIncFsSuccess();
346 mIncFs->makeFileSuccess();
347 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800348 mDataLoaderManager->initializeDataLoaderSuccess();
349 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800350 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
351 TemporaryDir tempDir;
352 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800353 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800354 IncrementalService::CreateOptions::CreateNew);
355 ASSERT_GE(storageId, 0);
356 mIncrementalService->deleteStorage(storageId);
357}
358
359TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
360 mVold->mountIncFsSuccess();
361 mIncFs->makeFileSuccess();
362 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800363 mDataLoaderManager->initializeDataLoaderSuccess();
364 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800365 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
366 TemporaryDir tempDir;
367 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800368 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800369 IncrementalService::CreateOptions::CreateNew);
370 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800371 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800372}
373
374TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
375 mVold->mountIncFsSuccess();
376 mIncFs->makeFileSuccess();
377 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800378 mDataLoaderManager->initializeDataLoaderSuccess();
379 mDataLoaderManager->getDataLoaderSuccess();
380 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800381 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
382 TemporaryDir tempDir;
383 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800384 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800385 IncrementalService::CreateOptions::CreateNew);
386 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800387 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800388 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
389}
390
391TEST_F(IncrementalServiceTest, testMakeDirectory) {
392 mVold->mountIncFsSuccess();
393 mIncFs->makeFileSuccess();
394 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800395 mDataLoaderManager->initializeDataLoaderSuccess();
396 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800397 TemporaryDir tempDir;
398 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800399 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800400 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800401 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800402
Songchun Fan103ba1d2020-02-03 17:32:32 -0800403 std::string tempPath(tempDir.path);
404 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800405 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800406 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800407
Songchun Fan103ba1d2020-02-03 17:32:32 -0800408 // Expecting incfs to call makeDir on a path like:
409 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
410 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800411 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
412 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800413}
414
415TEST_F(IncrementalServiceTest, testMakeDirectories) {
416 mVold->mountIncFsSuccess();
417 mIncFs->makeFileSuccess();
418 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800419 mDataLoaderManager->initializeDataLoaderSuccess();
420 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800421 TemporaryDir tempDir;
422 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800423 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800424 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800425 auto first = "first"sv;
426 auto second = "second"sv;
427 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800428
429 std::string tempPath(tempDir.path);
430 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800431 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800432
Songchun Fan3c82a302019-11-29 14:23:45 -0800433 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800434 auto parent_path = std::string(first) + "/" + std::string(second);
435 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800436
437 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
438 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
439 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
440
441 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
442 .WillOnce(Return(-ENOENT));
443 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
444 .WillOnce(Return(-ENOENT));
445 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
446 .WillOnce(Return(0));
447 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
448 .WillOnce(Return(0));
449 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
450 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800451 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800452}
453} // namespace android::os::incremental