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 16#include "mount_unittest.h" 17#include <cerrno> 18#include <fcntl.h> 19#include <iostream> 20#include <map> 21#include <string> 22#include <sys/mount.h> 23#include <sys/stat.h> 24#include <sys/types.h> 25#include <sys/wait.h> 26#include <unistd.h> 27#include <vector> 28#include "fs_manager/fs_manager.h" 29#include "fs_manager/mount.h" 30#include "log/log.h" 31#include "utils.h" 32 33using namespace testing::ext; 34using namespace UpdaterUt; 35using namespace Updater; 36using namespace std; 37 38namespace UpdaterUt { 39void MountUnitTest::SetUp(void) 40{ 41 cout << "Updater Unit MountUnitTest Begin!" << endl; 42} 43 44// end 45void MountUnitTest::TearDown(void) 46{ 47 cout << "Updater Unit MountUnitTest End!" << endl; 48} 49 50// do something at the each function begining 51void MountUnitTest::SetUpTestCase(void) 52{ 53 mkdir("/misc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 54} 55 56// do something at the each function end 57void MountUnitTest::TearDownTestCase(void) 58{ 59 rmdir("/misc"); 60} 61 62HWTEST_F(MountUnitTest, FormatPartition_unitest, TestSize.Level1) 63{ 64 const std::string fstabFile1 = "/data/updater/mount_unitest/FormatPartition1.fstable"; 65 LoadSpecificFstab(fstabFile1); 66 const std::string path1 = ""; 67 int ret = FormatPartition(path1); 68 EXPECT_EQ(ret, -1); 69 const std::string path2 = "/"; 70 ret = FormatPartition(path2); 71 EXPECT_EQ(ret, 0); 72 const std::string path3 = "/data"; 73 ret = FormatPartition(path3); 74 EXPECT_EQ(ret, -1); 75 const std::string path4 = "/misc"; 76 ret = FormatPartition(path4); 77 EXPECT_EQ(ret, -1); 78} 79 80HWTEST_F(MountUnitTest, MountForPath_unitest, TestSize.Level1) 81{ 82 const std::string fstabFile1 = "/data/updater/mount_unitest/MountForPath1.fstable"; 83 LoadSpecificFstab(fstabFile1); 84 const std::string path1 = ""; 85 int ret = MountForPath(path1); 86 EXPECT_EQ(ret, -1); 87 const std::string path2 = "/vendor"; 88 ret = MountForPath(path2); 89 EXPECT_EQ(ret, 0); 90 ret = FormatPartition("/misc"); 91 EXPECT_EQ(ret, 0); 92 const std::string path3 = "/misc"; 93 ret = MountForPath(path3); 94 EXPECT_EQ(ret, 0); 95 96 const std::string path4 = "/data1"; 97 ret = MountForPath(path4); 98 EXPECT_EQ(ret, -1); 99} 100 101HWTEST_F(MountUnitTest, UmountForPath_unitest, TestSize.Level1) 102{ 103 const std::string fstabFile1 = "/data/updater/mount_unitest/UmountForPath1.fstable"; 104 LoadSpecificFstab(fstabFile1); 105 const std::string path1 = ""; 106 int ret = UmountForPath(path1); 107 EXPECT_EQ(ret, -1); 108 const std::string path2 = "/misc/mount2"; 109 ret = UmountForPath(path2); 110 EXPECT_EQ(ret, 0); 111 const std::string path3 = "/misc"; 112 ret = UmountForPath(path3); 113 EXPECT_EQ(ret, 0); 114} 115 116HWTEST_F(MountUnitTest, GetMountStatusForPath_unitest, TestSize.Level1) 117{ 118 const std::string fstabFile1 = "/data/updater/mount_unitest/GetMountStatusForPath1.fstable"; 119 LoadSpecificFstab(fstabFile1); 120 const std::string path1 = ""; 121 MountStatus ret = GetMountStatusForPath(path1); 122 EXPECT_EQ(ret, MountStatus::MOUNT_ERROR); 123 const std::string path2 = "/vendor"; 124 ret = GetMountStatusForPath(path2); 125 EXPECT_EQ(ret, MountStatus::MOUNT_MOUNTED); 126 const std::string path3 = "/data/mount2"; 127 ret = GetMountStatusForPath(path3); 128 EXPECT_EQ(ret, MountStatus::MOUNT_UMOUNTED); 129} 130 131HWTEST_F(MountUnitTest, SetupPartitions_unitest, TestSize.Level1) 132{ 133 const std::string fstabFile1 = "/data/updater/mount_unitest/SetupPartitions1.fstable"; 134 LoadSpecificFstab(fstabFile1); 135 int ret = SetupPartitions(); 136 EXPECT_EQ(ret, -1); 137 const std::string fstabFile2 = "/data/updater/mount_unitest/SetupPartitions2.fstable"; 138 LoadSpecificFstab(fstabFile2); 139 ret = SetupPartitions(); 140 EXPECT_EQ(ret, 0); 141} 142 143HWTEST_F(MountUnitTest, GetBlockDeviceByMountPoint_unitest, TestSize.Level1) 144{ 145 LoadFstab(); 146 const std::string fstabFile1 = "/data/updater/mount_unitest/GetBlockDeviceByMountPoint1.fstable"; 147 LoadSpecificFstab(fstabFile1); 148 unsigned long mountFlag = MS_REMOUNT; 149 const std::string tmpPath = "/vendor/etc/fstab.updater"; 150 std::string vendorSource = GetBlockDeviceByMountPoint(tmpPath); 151 // mount rootfs to read-write. 152 if (mount(vendorSource.c_str(), "/vendor", "ext4", mountFlag, nullptr) != 0) { 153 std::cout << "Cannot re-mount vendor\n"; 154 } 155 auto ret = open(tmpPath.c_str(), O_RDONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 156 if (ret < 0) { 157 std::cout << "Cannot open \"/vendor/etc/fstab.updater\" file: " << errno << "\n"; 158 return; 159 } 160 close(ret); 161 LoadFstab(); 162 unlink(tmpPath.c_str()); 163 164 LoadSpecificFstab(fstabFile1); 165 const std::string mountPoint1 = ""; 166 const std::string str1 = GetBlockDeviceByMountPoint(mountPoint1); 167 EXPECT_TRUE(str1.empty()); 168 const std::string mountPoint2 = "/data2"; 169 const std::string str2 = GetBlockDeviceByMountPoint(mountPoint2); 170 EXPECT_TRUE(str2.empty()); 171 const std::string mountPoint3 = "/data"; 172 const std::string str3 = GetBlockDeviceByMountPoint(mountPoint3); 173 EXPECT_FALSE(str3.empty()); 174} 175} // namespace updater_ut 176