blob: f5b88d93c8de69f405346a1506234fd3a90c3261 [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 }
Alex Buynytskyyb6e02f72020-03-17 18:12:23 -0700103 binder::Status start(int32_t) override { return binder::Status::ok(); }
104 binder::Status stop(int32_t) override { return binder::Status::ok(); }
105 binder::Status destroy(int32_t) override { return binder::Status::ok(); }
106 binder::Status prepareImage(int32_t,
107 const std::vector<InstallationFileParcel>&,
Songchun Fan68645c42020-02-27 15:57:35 -0800108 const std::vector<std::string>&) override {
109 return binder::Status::ok();
110 }
111};
112
113class MockDataLoaderManager : public DataLoaderManagerWrapper {
114public:
115 MOCK_CONST_METHOD5(initializeDataLoader,
116 binder::Status(int32_t mountId, const DataLoaderParamsParcel& params,
117 const FileSystemControlParcel& control,
Songchun Fan3c82a302019-11-29 14:23:45 -0800118 const sp<IDataLoaderStatusListener>& listener,
119 bool* _aidl_return));
Songchun Fan68645c42020-02-27 15:57:35 -0800120 MOCK_CONST_METHOD2(getDataLoader,
121 binder::Status(int32_t mountId, sp<IDataLoader>* _aidl_return));
Songchun Fan3c82a302019-11-29 14:23:45 -0800122 MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId));
Songchun Fan3c82a302019-11-29 14:23:45 -0800123
Songchun Fan68645c42020-02-27 15:57:35 -0800124 binder::Status initializeDataLoaderOk(int32_t mountId, const DataLoaderParamsParcel& params,
125 const FileSystemControlParcel& control,
126 const sp<IDataLoaderStatusListener>& listener,
127 bool* _aidl_return) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800128 mId = mountId;
129 mListener = listener;
130 *_aidl_return = true;
131 return binder::Status::ok();
132 }
133
Songchun Fan68645c42020-02-27 15:57:35 -0800134 binder::Status getDataLoaderOk(int32_t mountId, sp<IDataLoader>* _aidl_return) {
135 *_aidl_return = mDataLoader;
Songchun Fan3c82a302019-11-29 14:23:45 -0800136 return binder::Status::ok();
137 }
138
Songchun Fan68645c42020-02-27 15:57:35 -0800139 void initializeDataLoaderFails() {
140 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
Songchun Fan3c82a302019-11-29 14:23:45 -0800141 .WillByDefault(Return(
142 (binder::Status::fromExceptionCode(1, String8("failed to prepare")))));
143 }
Songchun Fan68645c42020-02-27 15:57:35 -0800144 void initializeDataLoaderSuccess() {
145 ON_CALL(*this, initializeDataLoader(_, _, _, _, _))
146 .WillByDefault(Invoke(this, &MockDataLoaderManager::initializeDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800147 }
Songchun Fan68645c42020-02-27 15:57:35 -0800148 void getDataLoaderSuccess() {
149 ON_CALL(*this, getDataLoader(_, _))
150 .WillByDefault(Invoke(this, &MockDataLoaderManager::getDataLoaderOk));
Songchun Fan3c82a302019-11-29 14:23:45 -0800151 }
152 void setDataLoaderStatusNotReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800153 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800154 }
155 void setDataLoaderStatusReady() {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800156 mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED);
Songchun Fan3c82a302019-11-29 14:23:45 -0800157 }
158
159private:
160 int mId;
161 sp<IDataLoaderStatusListener> mListener;
Songchun Fan68645c42020-02-27 15:57:35 -0800162 sp<IDataLoader> mDataLoader = sp<IDataLoader>(new FakeDataLoader());
Songchun Fan3c82a302019-11-29 14:23:45 -0800163};
164
165class MockIncFs : public IncFsWrapper {
166public:
167 MOCK_CONST_METHOD5(makeFile,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800168 ErrorCode(Control control, std::string_view path, int mode, FileId id,
169 NewFileParams params));
170 MOCK_CONST_METHOD3(makeDir, ErrorCode(Control control, std::string_view path, int mode));
171 MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, FileId fileid));
172 MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, std::string_view path));
173 MOCK_CONST_METHOD2(getFileId, FileId(Control control, std::string_view path));
174 MOCK_CONST_METHOD3(link,
175 ErrorCode(Control control, std::string_view from, std::string_view to));
176 MOCK_CONST_METHOD2(unlink, ErrorCode(Control control, std::string_view path));
177 MOCK_CONST_METHOD2(openWrite, base::unique_fd(Control control, FileId id));
Songchun Fan9b753082020-02-26 13:08:06 -0800178 MOCK_CONST_METHOD1(writeBlocks, ErrorCode(Span<const DataBlock> blocks));
Songchun Fan3c82a302019-11-29 14:23:45 -0800179
180 void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); }
181 void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800182 RawMetadata getMountInfoMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800183 metadata::Mount m;
184 m.mutable_storage()->set_id(100);
185 m.mutable_loader()->set_package_name("com.test");
186 m.mutable_loader()->set_arguments("com.uri");
187 const auto metadata = m.SerializeAsString();
188 m.mutable_loader()->release_arguments();
189 m.mutable_loader()->release_package_name();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800190 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800191 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800192 RawMetadata getStorageMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800193 metadata::Storage st;
194 st.set_id(100);
195 auto metadata = st.SerializeAsString();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800196 return {metadata.begin(), metadata.end()};
Songchun Fan3c82a302019-11-29 14:23:45 -0800197 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800198 RawMetadata getBindPointMetadata(Control control, std::string_view path) {
Songchun Fan3c82a302019-11-29 14:23:45 -0800199 metadata::BindPoint bp;
200 std::string destPath = "dest";
201 std::string srcPath = "src";
202 bp.set_storage_id(100);
203 bp.set_allocated_dest_path(&destPath);
204 bp.set_allocated_source_subdir(&srcPath);
205 const auto metadata = bp.SerializeAsString();
206 bp.release_source_subdir();
207 bp.release_dest_path();
208 return std::vector<char>(metadata.begin(), metadata.end());
209 }
210};
211
212class MockServiceManager : public ServiceManagerWrapper {
213public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800214 MockServiceManager(std::unique_ptr<MockVoldService> vold,
Songchun Fan68645c42020-02-27 15:57:35 -0800215 std::unique_ptr<MockDataLoaderManager> manager,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800216 std::unique_ptr<MockIncFs> incfs)
217 : mVold(std::move(vold)),
Songchun Fan68645c42020-02-27 15:57:35 -0800218 mDataLoaderManager(std::move(manager)),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800219 mIncFs(std::move(incfs)) {}
220 std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); }
Songchun Fan68645c42020-02-27 15:57:35 -0800221 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final {
222 return std::move(mDataLoaderManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800223 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800224 std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); }
Songchun Fan3c82a302019-11-29 14:23:45 -0800225
226private:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800227 std::unique_ptr<MockVoldService> mVold;
Songchun Fan68645c42020-02-27 15:57:35 -0800228 std::unique_ptr<MockDataLoaderManager> mDataLoaderManager;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800229 std::unique_ptr<MockIncFs> mIncFs;
Songchun Fan3c82a302019-11-29 14:23:45 -0800230};
231
232// --- IncrementalServiceTest ---
233
Songchun Fan3c82a302019-11-29 14:23:45 -0800234class IncrementalServiceTest : public testing::Test {
235public:
236 void SetUp() override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800237 auto vold = std::make_unique<NiceMock<MockVoldService>>();
238 mVold = vold.get();
Songchun Fan68645c42020-02-27 15:57:35 -0800239 auto dataloaderManager = std::make_unique<NiceMock<MockDataLoaderManager>>();
240 mDataLoaderManager = dataloaderManager.get();
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800241 auto incFs = std::make_unique<NiceMock<MockIncFs>>();
242 mIncFs = incFs.get();
243 mIncrementalService =
244 std::make_unique<IncrementalService>(MockServiceManager(std::move(vold),
245 std::move(
Songchun Fan68645c42020-02-27 15:57:35 -0800246 dataloaderManager),
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800247 std::move(incFs)),
248 mRootDir.path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800249 mDataLoaderParcel.packageName = "com.test";
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800250 mDataLoaderParcel.arguments = "uri";
Songchun Fan3c82a302019-11-29 14:23:45 -0800251 mIncrementalService->onSystemReady();
252 }
253
254 void setUpExistingMountDir(const std::string& rootDir) {
255 const auto dir = rootDir + "/dir1";
256 const auto mountDir = dir + "/mount";
257 const auto backingDir = dir + "/backing_store";
258 const auto storageDir = mountDir + "/st0";
259 ASSERT_EQ(0, mkdir(dir.c_str(), 0755));
260 ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755));
261 ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755));
262 ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755));
263 const auto mountInfoFile = rootDir + "/dir1/mount/.info";
264 const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd";
265 ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile));
266 ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800267 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile)))
268 .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata));
269 ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile)))
270 .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata));
271 ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0")))
272 .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata));
Songchun Fan3c82a302019-11-29 14:23:45 -0800273 }
274
275protected:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800276 NiceMock<MockVoldService>* mVold;
277 NiceMock<MockIncFs>* mIncFs;
Songchun Fan68645c42020-02-27 15:57:35 -0800278 NiceMock<MockDataLoaderManager>* mDataLoaderManager;
Songchun Fan3c82a302019-11-29 14:23:45 -0800279 std::unique_ptr<IncrementalService> mIncrementalService;
280 TemporaryDir mRootDir;
281 DataLoaderParamsParcel mDataLoaderParcel;
282};
283
Songchun Fan3c82a302019-11-29 14:23:45 -0800284TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) {
285 mVold->mountIncFsFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800286 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800287 TemporaryDir tempDir;
288 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800289 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800290 IncrementalService::CreateOptions::CreateNew);
291 ASSERT_LT(storageId, 0);
292}
293
294TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) {
295 mVold->mountIncFsInvalidControlParcel();
Songchun Fan68645c42020-02-27 15:57:35 -0800296 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800297 TemporaryDir tempDir;
298 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800299 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800300 IncrementalService::CreateOptions::CreateNew);
301 ASSERT_LT(storageId, 0);
302}
303
304TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) {
305 mVold->mountIncFsSuccess();
306 mIncFs->makeFileFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800307 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
308 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800309 EXPECT_CALL(*mVold, unmountIncFs(_));
310 TemporaryDir tempDir;
311 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800312 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800313 IncrementalService::CreateOptions::CreateNew);
314 ASSERT_LT(storageId, 0);
315}
316
317TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) {
318 mVold->mountIncFsSuccess();
319 mIncFs->makeFileSuccess();
320 mVold->bindMountFails();
Songchun Fan68645c42020-02-27 15:57:35 -0800321 EXPECT_CALL(*mDataLoaderManager, initializeDataLoader(_, _, _, _, _)).Times(0);
322 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800323 EXPECT_CALL(*mVold, unmountIncFs(_));
324 TemporaryDir tempDir;
325 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800326 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800327 IncrementalService::CreateOptions::CreateNew);
328 ASSERT_LT(storageId, 0);
329}
330
331TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) {
332 mVold->mountIncFsSuccess();
333 mIncFs->makeFileSuccess();
334 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800335 mDataLoaderManager->initializeDataLoaderFails();
336 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800337 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
338 TemporaryDir tempDir;
339 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800340 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800341 IncrementalService::CreateOptions::CreateNew);
342 ASSERT_LT(storageId, 0);
343}
344
345TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) {
346 mVold->mountIncFsSuccess();
347 mIncFs->makeFileSuccess();
348 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800349 mDataLoaderManager->initializeDataLoaderSuccess();
350 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800351 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
352 TemporaryDir tempDir;
353 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800354 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800355 IncrementalService::CreateOptions::CreateNew);
356 ASSERT_GE(storageId, 0);
357 mIncrementalService->deleteStorage(storageId);
358}
359
360TEST_F(IncrementalServiceTest, testOnStatusNotReady) {
361 mVold->mountIncFsSuccess();
362 mIncFs->makeFileSuccess();
363 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800364 mDataLoaderManager->initializeDataLoaderSuccess();
365 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800366 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
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_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800372 mDataLoaderManager->setDataLoaderStatusNotReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800373}
374
375TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) {
376 mVold->mountIncFsSuccess();
377 mIncFs->makeFileSuccess();
378 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800379 mDataLoaderManager->initializeDataLoaderSuccess();
380 mDataLoaderManager->getDataLoaderSuccess();
381 EXPECT_CALL(*mDataLoaderManager, destroyDataLoader(_));
Songchun Fan3c82a302019-11-29 14:23:45 -0800382 EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2);
383 TemporaryDir tempDir;
384 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800385 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800386 IncrementalService::CreateOptions::CreateNew);
387 ASSERT_GE(storageId, 0);
Songchun Fan68645c42020-02-27 15:57:35 -0800388 mDataLoaderManager->setDataLoaderStatusReady();
Songchun Fan3c82a302019-11-29 14:23:45 -0800389 ASSERT_TRUE(mIncrementalService->startLoading(storageId));
390}
391
392TEST_F(IncrementalServiceTest, testMakeDirectory) {
393 mVold->mountIncFsSuccess();
394 mIncFs->makeFileSuccess();
395 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800396 mDataLoaderManager->initializeDataLoaderSuccess();
397 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800398 TemporaryDir tempDir;
399 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800400 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800401 IncrementalService::CreateOptions::CreateNew);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800402 std::string dir_path("test");
Songchun Fan3c82a302019-11-29 14:23:45 -0800403
Songchun Fan103ba1d2020-02-03 17:32:32 -0800404 std::string tempPath(tempDir.path);
405 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800406 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800407 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800408
Songchun Fan103ba1d2020-02-03 17:32:32 -0800409 // Expecting incfs to call makeDir on a path like:
410 // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test
411 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _));
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800412 auto res = mIncrementalService->makeDir(storageId, dir_path, 0555);
413 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800414}
415
416TEST_F(IncrementalServiceTest, testMakeDirectories) {
417 mVold->mountIncFsSuccess();
418 mIncFs->makeFileSuccess();
419 mVold->bindMountSuccess();
Songchun Fan68645c42020-02-27 15:57:35 -0800420 mDataLoaderManager->initializeDataLoaderSuccess();
421 mDataLoaderManager->getDataLoaderSuccess();
Songchun Fan3c82a302019-11-29 14:23:45 -0800422 TemporaryDir tempDir;
423 int storageId =
Alex Buynytskyy04f73912020-02-10 08:34:18 -0800424 mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), {},
Songchun Fan3c82a302019-11-29 14:23:45 -0800425 IncrementalService::CreateOptions::CreateNew);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800426 auto first = "first"sv;
427 auto second = "second"sv;
428 auto third = "third"sv;
Songchun Fan103ba1d2020-02-03 17:32:32 -0800429
430 std::string tempPath(tempDir.path);
431 std::replace(tempPath.begin(), tempPath.end(), '/', '_');
Songchun Fan1124fd32020-02-10 12:49:41 -0800432 std::string mount_dir = std::string(mRootDir.path) + "/MT_" + tempPath.substr(1);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800433
Songchun Fan3c82a302019-11-29 14:23:45 -0800434 InSequence seq;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800435 auto parent_path = std::string(first) + "/" + std::string(second);
436 auto dir_path = parent_path + "/" + std::string(third);
Songchun Fan103ba1d2020-02-03 17:32:32 -0800437
438 std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first);
439 std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path;
440 std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path;
441
442 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _))
443 .WillOnce(Return(-ENOENT));
444 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
445 .WillOnce(Return(-ENOENT));
446 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _))
447 .WillOnce(Return(0));
448 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _))
449 .WillOnce(Return(0));
450 EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0));
451 auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555);
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800452 ASSERT_EQ(res, 0);
Songchun Fan3c82a302019-11-29 14:23:45 -0800453}
454} // namespace android::os::incremental