1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <cerrno> 16 #include <unistd.h> 17 #include <fcntl.h> 18 #include "fs_manager/fs_manager.h" 19 #include "param_stub.h" 20 #include "init_mount.h" 21 #include "init.h" 22 #include "securec.h" 23 using namespace std; 24 using namespace testing::ext; 25 26 namespace init_ut { 27 class MountUnitTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void) {} TearDownTestCase(void)30 static void TearDownTestCase(void) {} SetUp()31 void SetUp() {}; TearDown()32 void TearDown() {}; 33 }; 34 35 HWTEST_F(MountUnitTest, TestMountRequriedPartitions, TestSize.Level0) 36 { 37 const char *fstabFiles = "/data/init_ut/etc/fstab.required"; 38 Fstab *fstab = NULL; 39 fstab = ReadFstabFromFile(fstabFiles, false); 40 if (fstab != NULL) { 41 #ifdef __MUSL__ 42 int ret = MountRequriedPartitions(fstab); 43 EXPECT_EQ(ret, -1); 44 #endif 45 ReleaseFstab(fstab); 46 } else { 47 Fstab fstab1; 48 fstab1.head = nullptr; 49 int ret = MountRequriedPartitions(&fstab1); 50 EXPECT_EQ(ret, -1); 51 } 52 ReleaseFstab(LoadRequiredFstab()); 53 } 54 HWTEST_F(MountUnitTest, TestGetBlockDevicePath, TestSize.Level1) 55 { 56 char path[20] = {0}; // 20 is path length 57 int fd = open("/bin/updater", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRWXU); 58 if (fd < 0) { 59 return; 60 } 61 GetBlockDevicePath("/test", path, sizeof(path)); 62 close(fd); 63 ReadConfig(); 64 unlink("/bin/updater"); 65 ReadConfig(); 66 int ret = GetBlockDeviceByMountPoint(nullptr, nullptr, nullptr, 0); 67 EXPECT_EQ(ret, -1); 68 FstabItem fstabitem = {(char *)"deviceName", (char *)"mountPoint", 69 (char *)"fsType", (char *)"mountOptions", 1, nullptr}; 70 Fstab fstab = {&fstabitem}; 71 char devicename[20]; // 20 is devicename length 72 ret = GetBlockDeviceByMountPoint("notmountpoint", &fstab, devicename, sizeof(devicename)); 73 EXPECT_EQ(ret, -1); 74 ret = GetBlockDeviceByMountPoint("mountPoint", &fstab, devicename, 0); 75 EXPECT_EQ(ret, -1); 76 ret = GetBlockDeviceByMountPoint("mountPoint", &fstab, devicename, sizeof(devicename)); 77 EXPECT_EQ(ret, 0); 78 } 79 } // namespace init_ut 80