Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | using namespace testing; |
| 32 | using namespace android::incremental; |
| 33 | using namespace std::literals; |
| 34 | using testing::_; |
| 35 | using testing::Invoke; |
| 36 | using testing::NiceMock; |
| 37 | |
| 38 | #undef LOG_TAG |
| 39 | #define LOG_TAG "IncrementalServiceTest" |
| 40 | |
| 41 | using namespace android::incfs; |
| 42 | using namespace android::content::pm; |
| 43 | |
| 44 | namespace android::os::incremental { |
| 45 | |
| 46 | class MockVoldService : public VoldServiceWrapper { |
| 47 | public: |
| 48 | MOCK_CONST_METHOD4(mountIncFs, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 49 | binder::Status(const std::string& backingPath, const std::string& targetDir, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 50 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 80 | _aidl_return = {}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 81 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 85 | _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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 88 | return binder::Status::ok(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | TemporaryFile cmdFile; |
| 93 | TemporaryFile logFile; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | class MockIncrementalManager : public IncrementalManagerWrapper { |
| 97 | public: |
| 98 | MOCK_CONST_METHOD5(prepareDataLoader, |
| 99 | binder::Status(int32_t mountId, const FileSystemControlParcel& control, |
| 100 | const DataLoaderParamsParcel& params, |
| 101 | const sp<IDataLoaderStatusListener>& listener, |
| 102 | bool* _aidl_return)); |
| 103 | MOCK_CONST_METHOD2(startDataLoader, binder::Status(int32_t mountId, bool* _aidl_return)); |
| 104 | MOCK_CONST_METHOD1(destroyDataLoader, binder::Status(int32_t mountId)); |
| 105 | MOCK_CONST_METHOD3(newFileForDataLoader, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 106 | binder::Status(int32_t mountId, FileId fileId, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 107 | const ::std::vector<uint8_t>& metadata)); |
| 108 | MOCK_CONST_METHOD1(showHealthBlockedUI, binder::Status(int32_t mountId)); |
| 109 | |
| 110 | binder::Status prepareDataLoaderOk(int32_t mountId, const FileSystemControlParcel& control, |
| 111 | const DataLoaderParamsParcel& params, |
| 112 | const sp<IDataLoaderStatusListener>& listener, |
| 113 | bool* _aidl_return) { |
| 114 | mId = mountId; |
| 115 | mListener = listener; |
| 116 | *_aidl_return = true; |
| 117 | return binder::Status::ok(); |
| 118 | } |
| 119 | |
| 120 | binder::Status startDataLoaderOk(int32_t mountId, bool* _aidl_return) { |
| 121 | *_aidl_return = true; |
| 122 | return binder::Status::ok(); |
| 123 | } |
| 124 | |
| 125 | void prepareDataLoaderFails() { |
| 126 | ON_CALL(*this, prepareDataLoader(_, _, _, _, _)) |
| 127 | .WillByDefault(Return( |
| 128 | (binder::Status::fromExceptionCode(1, String8("failed to prepare"))))); |
| 129 | } |
| 130 | void prepareDataLoaderSuccess() { |
| 131 | ON_CALL(*this, prepareDataLoader(_, _, _, _, _)) |
| 132 | .WillByDefault(Invoke(this, &MockIncrementalManager::prepareDataLoaderOk)); |
| 133 | } |
| 134 | void startDataLoaderSuccess() { |
| 135 | ON_CALL(*this, startDataLoader(_, _)) |
| 136 | .WillByDefault(Invoke(this, &MockIncrementalManager::startDataLoaderOk)); |
| 137 | } |
| 138 | void setDataLoaderStatusNotReady() { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 139 | mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_DESTROYED); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 140 | } |
| 141 | void setDataLoaderStatusReady() { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 142 | mListener->onStatusChanged(mId, IDataLoaderStatusListener::DATA_LOADER_CREATED); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | int mId; |
| 147 | sp<IDataLoaderStatusListener> mListener; |
| 148 | }; |
| 149 | |
| 150 | class MockIncFs : public IncFsWrapper { |
| 151 | public: |
| 152 | MOCK_CONST_METHOD5(makeFile, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 153 | ErrorCode(Control control, std::string_view path, int mode, FileId id, |
| 154 | NewFileParams params)); |
| 155 | MOCK_CONST_METHOD3(makeDir, ErrorCode(Control control, std::string_view path, int mode)); |
| 156 | MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, FileId fileid)); |
| 157 | MOCK_CONST_METHOD2(getMetadata, RawMetadata(Control control, std::string_view path)); |
| 158 | MOCK_CONST_METHOD2(getFileId, FileId(Control control, std::string_view path)); |
| 159 | MOCK_CONST_METHOD3(link, |
| 160 | ErrorCode(Control control, std::string_view from, std::string_view to)); |
| 161 | MOCK_CONST_METHOD2(unlink, ErrorCode(Control control, std::string_view path)); |
| 162 | MOCK_CONST_METHOD2(openWrite, base::unique_fd(Control control, FileId id)); |
| 163 | MOCK_CONST_METHOD1(writeBlocks, ErrorCode(std::span<const DataBlock> blocks)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 164 | |
| 165 | void makeFileFails() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(-1)); } |
| 166 | void makeFileSuccess() { ON_CALL(*this, makeFile(_, _, _, _, _)).WillByDefault(Return(0)); } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 167 | RawMetadata getMountInfoMetadata(Control control, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 168 | metadata::Mount m; |
| 169 | m.mutable_storage()->set_id(100); |
| 170 | m.mutable_loader()->set_package_name("com.test"); |
| 171 | m.mutable_loader()->set_arguments("com.uri"); |
| 172 | const auto metadata = m.SerializeAsString(); |
| 173 | m.mutable_loader()->release_arguments(); |
| 174 | m.mutable_loader()->release_package_name(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 175 | return {metadata.begin(), metadata.end()}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 176 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 177 | RawMetadata getStorageMetadata(Control control, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 178 | metadata::Storage st; |
| 179 | st.set_id(100); |
| 180 | auto metadata = st.SerializeAsString(); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 181 | return {metadata.begin(), metadata.end()}; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 183 | RawMetadata getBindPointMetadata(Control control, std::string_view path) { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 184 | metadata::BindPoint bp; |
| 185 | std::string destPath = "dest"; |
| 186 | std::string srcPath = "src"; |
| 187 | bp.set_storage_id(100); |
| 188 | bp.set_allocated_dest_path(&destPath); |
| 189 | bp.set_allocated_source_subdir(&srcPath); |
| 190 | const auto metadata = bp.SerializeAsString(); |
| 191 | bp.release_source_subdir(); |
| 192 | bp.release_dest_path(); |
| 193 | return std::vector<char>(metadata.begin(), metadata.end()); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | class MockServiceManager : public ServiceManagerWrapper { |
| 198 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 199 | MockServiceManager(std::unique_ptr<MockVoldService> vold, |
| 200 | std::unique_ptr<MockIncrementalManager> manager, |
| 201 | std::unique_ptr<MockIncFs> incfs) |
| 202 | : mVold(std::move(vold)), |
| 203 | mIncrementalManager(std::move(manager)), |
| 204 | mIncFs(std::move(incfs)) {} |
| 205 | std::unique_ptr<VoldServiceWrapper> getVoldService() final { return std::move(mVold); } |
| 206 | std::unique_ptr<IncrementalManagerWrapper> getIncrementalManager() final { |
| 207 | return std::move(mIncrementalManager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 208 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 209 | std::unique_ptr<IncFsWrapper> getIncFs() final { return std::move(mIncFs); } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 210 | |
| 211 | private: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 212 | std::unique_ptr<MockVoldService> mVold; |
| 213 | std::unique_ptr<MockIncrementalManager> mIncrementalManager; |
| 214 | std::unique_ptr<MockIncFs> mIncFs; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | // --- IncrementalServiceTest --- |
| 218 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 219 | class IncrementalServiceTest : public testing::Test { |
| 220 | public: |
| 221 | void SetUp() override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 222 | auto vold = std::make_unique<NiceMock<MockVoldService>>(); |
| 223 | mVold = vold.get(); |
| 224 | auto incrementalManager = std::make_unique<NiceMock<MockIncrementalManager>>(); |
| 225 | mIncrementalManager = incrementalManager.get(); |
| 226 | auto incFs = std::make_unique<NiceMock<MockIncFs>>(); |
| 227 | mIncFs = incFs.get(); |
| 228 | mIncrementalService = |
| 229 | std::make_unique<IncrementalService>(MockServiceManager(std::move(vold), |
| 230 | std::move( |
| 231 | incrementalManager), |
| 232 | std::move(incFs)), |
| 233 | mRootDir.path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 234 | mDataLoaderParcel.packageName = "com.test"; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 235 | mDataLoaderParcel.arguments = "uri"; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 236 | mIncrementalService->onSystemReady(); |
| 237 | } |
| 238 | |
| 239 | void setUpExistingMountDir(const std::string& rootDir) { |
| 240 | const auto dir = rootDir + "/dir1"; |
| 241 | const auto mountDir = dir + "/mount"; |
| 242 | const auto backingDir = dir + "/backing_store"; |
| 243 | const auto storageDir = mountDir + "/st0"; |
| 244 | ASSERT_EQ(0, mkdir(dir.c_str(), 0755)); |
| 245 | ASSERT_EQ(0, mkdir(mountDir.c_str(), 0755)); |
| 246 | ASSERT_EQ(0, mkdir(backingDir.c_str(), 0755)); |
| 247 | ASSERT_EQ(0, mkdir(storageDir.c_str(), 0755)); |
| 248 | const auto mountInfoFile = rootDir + "/dir1/mount/.info"; |
| 249 | const auto mountPointsFile = rootDir + "/dir1/mount/.mountpoint.abcd"; |
| 250 | ASSERT_TRUE(base::WriteStringToFile("info", mountInfoFile)); |
| 251 | ASSERT_TRUE(base::WriteStringToFile("mounts", mountPointsFile)); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 252 | ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountInfoFile))) |
| 253 | .WillByDefault(Invoke(mIncFs, &MockIncFs::getMountInfoMetadata)); |
| 254 | ON_CALL(*mIncFs, getMetadata(_, std::string_view(mountPointsFile))) |
| 255 | .WillByDefault(Invoke(mIncFs, &MockIncFs::getBindPointMetadata)); |
| 256 | ON_CALL(*mIncFs, getMetadata(_, std::string_view(rootDir + "/dir1/mount/st0"))) |
| 257 | .WillByDefault(Invoke(mIncFs, &MockIncFs::getStorageMetadata)); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | protected: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 261 | NiceMock<MockVoldService>* mVold; |
| 262 | NiceMock<MockIncFs>* mIncFs; |
| 263 | NiceMock<MockIncrementalManager>* mIncrementalManager; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 264 | std::unique_ptr<IncrementalService> mIncrementalService; |
| 265 | TemporaryDir mRootDir; |
| 266 | DataLoaderParamsParcel mDataLoaderParcel; |
| 267 | }; |
| 268 | |
| 269 | /* |
| 270 | TEST_F(IncrementalServiceTest, testBootMountExistingImagesSuccess) { |
| 271 | TemporaryDir tempDir; |
| 272 | setUpExistingMountDir(tempDir.path); |
| 273 | mVold->mountIncFsSuccess(); |
| 274 | mVold->bindMountSuccess(); |
| 275 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 276 | ON_CALL(*mIncrementalManager, destroyDataLoader(_)).WillByDefault(Return(binder::Status::ok())); |
| 277 | |
| 278 | EXPECT_CALL(*mVold, mountIncFs(_, _, _, _)).Times(1); |
| 279 | EXPECT_CALL(*mIncrementalManager, prepareDataLoader(_, _, _, _, _)).Times(1); |
| 280 | |
| 281 | MockServiceManager serviceManager = MockServiceManager(mVold, mIncrementalManager, mIncFs); |
| 282 | std::unique_ptr<IncrementalService> incrementalService = |
| 283 | std::make_unique<IncrementalService>(serviceManager, tempDir.path); |
| 284 | auto finished = incrementalService->onSystemReady(); |
| 285 | if (finished) { |
| 286 | finished->wait(); |
| 287 | } |
| 288 | } |
| 289 | */ |
| 290 | |
| 291 | TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsFails) { |
| 292 | mVold->mountIncFsFails(); |
| 293 | EXPECT_CALL(*mIncrementalManager, prepareDataLoader(_, _, _, _, _)).Times(0); |
| 294 | TemporaryDir tempDir; |
| 295 | int storageId = |
| 296 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 297 | IncrementalService::CreateOptions::CreateNew); |
| 298 | ASSERT_LT(storageId, 0); |
| 299 | } |
| 300 | |
| 301 | TEST_F(IncrementalServiceTest, testCreateStorageMountIncFsInvalidControlParcel) { |
| 302 | mVold->mountIncFsInvalidControlParcel(); |
| 303 | EXPECT_CALL(*mIncrementalManager, prepareDataLoader(_, _, _, _, _)).Times(0); |
| 304 | TemporaryDir tempDir; |
| 305 | int storageId = |
| 306 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 307 | IncrementalService::CreateOptions::CreateNew); |
| 308 | ASSERT_LT(storageId, 0); |
| 309 | } |
| 310 | |
| 311 | TEST_F(IncrementalServiceTest, testCreateStorageMakeFileFails) { |
| 312 | mVold->mountIncFsSuccess(); |
| 313 | mIncFs->makeFileFails(); |
| 314 | EXPECT_CALL(*mIncrementalManager, prepareDataLoader(_, _, _, _, _)).Times(0); |
| 315 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 316 | EXPECT_CALL(*mVold, unmountIncFs(_)); |
| 317 | TemporaryDir tempDir; |
| 318 | int storageId = |
| 319 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 320 | IncrementalService::CreateOptions::CreateNew); |
| 321 | ASSERT_LT(storageId, 0); |
| 322 | } |
| 323 | |
| 324 | TEST_F(IncrementalServiceTest, testCreateStorageBindMountFails) { |
| 325 | mVold->mountIncFsSuccess(); |
| 326 | mIncFs->makeFileSuccess(); |
| 327 | mVold->bindMountFails(); |
| 328 | EXPECT_CALL(*mIncrementalManager, prepareDataLoader(_, _, _, _, _)).Times(0); |
| 329 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 330 | EXPECT_CALL(*mVold, unmountIncFs(_)); |
| 331 | TemporaryDir tempDir; |
| 332 | int storageId = |
| 333 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 334 | IncrementalService::CreateOptions::CreateNew); |
| 335 | ASSERT_LT(storageId, 0); |
| 336 | } |
| 337 | |
| 338 | TEST_F(IncrementalServiceTest, testCreateStoragePrepareDataLoaderFails) { |
| 339 | mVold->mountIncFsSuccess(); |
| 340 | mIncFs->makeFileSuccess(); |
| 341 | mVold->bindMountSuccess(); |
| 342 | mIncrementalManager->prepareDataLoaderFails(); |
| 343 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 344 | EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2); |
| 345 | TemporaryDir tempDir; |
| 346 | int storageId = |
| 347 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 348 | IncrementalService::CreateOptions::CreateNew); |
| 349 | ASSERT_LT(storageId, 0); |
| 350 | } |
| 351 | |
| 352 | TEST_F(IncrementalServiceTest, testDeleteStorageSuccess) { |
| 353 | mVold->mountIncFsSuccess(); |
| 354 | mIncFs->makeFileSuccess(); |
| 355 | mVold->bindMountSuccess(); |
| 356 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 357 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 358 | EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2); |
| 359 | TemporaryDir tempDir; |
| 360 | int storageId = |
| 361 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 362 | IncrementalService::CreateOptions::CreateNew); |
| 363 | ASSERT_GE(storageId, 0); |
| 364 | mIncrementalService->deleteStorage(storageId); |
| 365 | } |
| 366 | |
| 367 | TEST_F(IncrementalServiceTest, testOnStatusNotReady) { |
| 368 | mVold->mountIncFsSuccess(); |
| 369 | mIncFs->makeFileSuccess(); |
| 370 | mVold->bindMountSuccess(); |
| 371 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 372 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 373 | EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2); |
| 374 | TemporaryDir tempDir; |
| 375 | int storageId = |
| 376 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 377 | IncrementalService::CreateOptions::CreateNew); |
| 378 | ASSERT_GE(storageId, 0); |
| 379 | mIncrementalManager->setDataLoaderStatusNotReady(); |
| 380 | } |
| 381 | |
| 382 | TEST_F(IncrementalServiceTest, testStartDataLoaderSuccess) { |
| 383 | mVold->mountIncFsSuccess(); |
| 384 | mIncFs->makeFileSuccess(); |
| 385 | mVold->bindMountSuccess(); |
| 386 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 387 | mIncrementalManager->startDataLoaderSuccess(); |
| 388 | EXPECT_CALL(*mIncrementalManager, destroyDataLoader(_)); |
| 389 | EXPECT_CALL(*mVold, unmountIncFs(_)).Times(2); |
| 390 | TemporaryDir tempDir; |
| 391 | int storageId = |
| 392 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 393 | IncrementalService::CreateOptions::CreateNew); |
| 394 | ASSERT_GE(storageId, 0); |
| 395 | mIncrementalManager->setDataLoaderStatusReady(); |
| 396 | ASSERT_TRUE(mIncrementalService->startLoading(storageId)); |
| 397 | } |
| 398 | |
| 399 | TEST_F(IncrementalServiceTest, testMakeDirectory) { |
| 400 | mVold->mountIncFsSuccess(); |
| 401 | mIncFs->makeFileSuccess(); |
| 402 | mVold->bindMountSuccess(); |
| 403 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 404 | mIncrementalManager->startDataLoaderSuccess(); |
| 405 | TemporaryDir tempDir; |
| 406 | int storageId = |
| 407 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 408 | IncrementalService::CreateOptions::CreateNew); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 409 | std::string dir_path("test"); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 410 | |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 411 | std::string tempPath(tempDir.path); |
| 412 | std::replace(tempPath.begin(), tempPath.end(), '/', '_'); |
| 413 | std::string mount_dir = std::string(mRootDir.path) + "/" + tempPath.substr(1); |
| 414 | std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 415 | |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 416 | // Expecting incfs to call makeDir on a path like: |
| 417 | // /data/local/tmp/TemporaryDir-06yixG/data_local_tmp_TemporaryDir-xwdFhT/mount/st_1_0/test |
| 418 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 419 | auto res = mIncrementalService->makeDir(storageId, dir_path, 0555); |
| 420 | ASSERT_EQ(res, 0); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | TEST_F(IncrementalServiceTest, testMakeDirectories) { |
| 424 | mVold->mountIncFsSuccess(); |
| 425 | mIncFs->makeFileSuccess(); |
| 426 | mVold->bindMountSuccess(); |
| 427 | mIncrementalManager->prepareDataLoaderSuccess(); |
| 428 | mIncrementalManager->startDataLoaderSuccess(); |
| 429 | TemporaryDir tempDir; |
| 430 | int storageId = |
| 431 | mIncrementalService->createStorage(tempDir.path, std::move(mDataLoaderParcel), |
| 432 | IncrementalService::CreateOptions::CreateNew); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 433 | auto first = "first"sv; |
| 434 | auto second = "second"sv; |
| 435 | auto third = "third"sv; |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 436 | |
| 437 | std::string tempPath(tempDir.path); |
| 438 | std::replace(tempPath.begin(), tempPath.end(), '/', '_'); |
| 439 | std::string mount_dir = std::string(mRootDir.path) + "/" + tempPath.substr(1); |
| 440 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 441 | InSequence seq; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 442 | auto parent_path = std::string(first) + "/" + std::string(second); |
| 443 | auto dir_path = parent_path + "/" + std::string(third); |
Songchun Fan | 103ba1d | 2020-02-03 17:32:32 -0800 | [diff] [blame] | 444 | |
| 445 | std::string normalized_first_path = mount_dir + "/mount/st_1_0/" + std::string(first); |
| 446 | std::string normalized_parent_path = mount_dir + "/mount/st_1_0/" + parent_path; |
| 447 | std::string normalized_dir_path = mount_dir + "/mount/st_1_0/" + dir_path; |
| 448 | |
| 449 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)) |
| 450 | .WillOnce(Return(-ENOENT)); |
| 451 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _)) |
| 452 | .WillOnce(Return(-ENOENT)); |
| 453 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_first_path), _)) |
| 454 | .WillOnce(Return(0)); |
| 455 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_parent_path), _)) |
| 456 | .WillOnce(Return(0)); |
| 457 | EXPECT_CALL(*mIncFs, makeDir(_, std::string_view(normalized_dir_path), _)).WillOnce(Return(0)); |
| 458 | auto res = mIncrementalService->makeDirs(storageId, normalized_dir_path, 0555); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 459 | ASSERT_EQ(res, 0); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 460 | } |
| 461 | } // namespace android::os::incremental |