1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "mount_unittest.h" 17fb299fa2Sopenharmony_ci#include <cerrno> 18fb299fa2Sopenharmony_ci#include <fcntl.h> 19fb299fa2Sopenharmony_ci#include <iostream> 20fb299fa2Sopenharmony_ci#include <map> 21fb299fa2Sopenharmony_ci#include <string> 22fb299fa2Sopenharmony_ci#include <sys/mount.h> 23fb299fa2Sopenharmony_ci#include <sys/stat.h> 24fb299fa2Sopenharmony_ci#include <sys/types.h> 25fb299fa2Sopenharmony_ci#include <sys/wait.h> 26fb299fa2Sopenharmony_ci#include <unistd.h> 27fb299fa2Sopenharmony_ci#include <vector> 28fb299fa2Sopenharmony_ci#include "fs_manager/fs_manager.h" 29fb299fa2Sopenharmony_ci#include "fs_manager/mount.h" 30fb299fa2Sopenharmony_ci#include "log/log.h" 31fb299fa2Sopenharmony_ci#include "utils.h" 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_ciusing namespace testing::ext; 34fb299fa2Sopenharmony_ciusing namespace UpdaterUt; 35fb299fa2Sopenharmony_ciusing namespace Updater; 36fb299fa2Sopenharmony_ciusing namespace std; 37fb299fa2Sopenharmony_ci 38fb299fa2Sopenharmony_cinamespace UpdaterUt { 39fb299fa2Sopenharmony_civoid MountUnitTest::SetUp(void) 40fb299fa2Sopenharmony_ci{ 41fb299fa2Sopenharmony_ci cout << "Updater Unit MountUnitTest Begin!" << endl; 42fb299fa2Sopenharmony_ci} 43fb299fa2Sopenharmony_ci 44fb299fa2Sopenharmony_ci// end 45fb299fa2Sopenharmony_civoid MountUnitTest::TearDown(void) 46fb299fa2Sopenharmony_ci{ 47fb299fa2Sopenharmony_ci cout << "Updater Unit MountUnitTest End!" << endl; 48fb299fa2Sopenharmony_ci} 49fb299fa2Sopenharmony_ci 50fb299fa2Sopenharmony_ci// do something at the each function begining 51fb299fa2Sopenharmony_civoid MountUnitTest::SetUpTestCase(void) 52fb299fa2Sopenharmony_ci{ 53fb299fa2Sopenharmony_ci mkdir("/misc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 54fb299fa2Sopenharmony_ci} 55fb299fa2Sopenharmony_ci 56fb299fa2Sopenharmony_ci// do something at the each function end 57fb299fa2Sopenharmony_civoid MountUnitTest::TearDownTestCase(void) 58fb299fa2Sopenharmony_ci{ 59fb299fa2Sopenharmony_ci rmdir("/misc"); 60fb299fa2Sopenharmony_ci} 61fb299fa2Sopenharmony_ci 62fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, FormatPartition_unitest, TestSize.Level1) 63fb299fa2Sopenharmony_ci{ 64fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/FormatPartition1.fstable"; 65fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 66fb299fa2Sopenharmony_ci const std::string path1 = ""; 67fb299fa2Sopenharmony_ci int ret = FormatPartition(path1); 68fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 69fb299fa2Sopenharmony_ci const std::string path2 = "/"; 70fb299fa2Sopenharmony_ci ret = FormatPartition(path2); 71fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 72fb299fa2Sopenharmony_ci const std::string path3 = "/data"; 73fb299fa2Sopenharmony_ci ret = FormatPartition(path3); 74fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 75fb299fa2Sopenharmony_ci const std::string path4 = "/misc"; 76fb299fa2Sopenharmony_ci ret = FormatPartition(path4); 77fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 78fb299fa2Sopenharmony_ci} 79fb299fa2Sopenharmony_ci 80fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, MountForPath_unitest, TestSize.Level1) 81fb299fa2Sopenharmony_ci{ 82fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/MountForPath1.fstable"; 83fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 84fb299fa2Sopenharmony_ci const std::string path1 = ""; 85fb299fa2Sopenharmony_ci int ret = MountForPath(path1); 86fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 87fb299fa2Sopenharmony_ci const std::string path2 = "/vendor"; 88fb299fa2Sopenharmony_ci ret = MountForPath(path2); 89fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 90fb299fa2Sopenharmony_ci ret = FormatPartition("/misc"); 91fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 92fb299fa2Sopenharmony_ci const std::string path3 = "/misc"; 93fb299fa2Sopenharmony_ci ret = MountForPath(path3); 94fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 95fb299fa2Sopenharmony_ci 96fb299fa2Sopenharmony_ci const std::string path4 = "/data1"; 97fb299fa2Sopenharmony_ci ret = MountForPath(path4); 98fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 99fb299fa2Sopenharmony_ci} 100fb299fa2Sopenharmony_ci 101fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, UmountForPath_unitest, TestSize.Level1) 102fb299fa2Sopenharmony_ci{ 103fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/UmountForPath1.fstable"; 104fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 105fb299fa2Sopenharmony_ci const std::string path1 = ""; 106fb299fa2Sopenharmony_ci int ret = UmountForPath(path1); 107fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 108fb299fa2Sopenharmony_ci const std::string path2 = "/misc/mount2"; 109fb299fa2Sopenharmony_ci ret = UmountForPath(path2); 110fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 111fb299fa2Sopenharmony_ci const std::string path3 = "/misc"; 112fb299fa2Sopenharmony_ci ret = UmountForPath(path3); 113fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 114fb299fa2Sopenharmony_ci} 115fb299fa2Sopenharmony_ci 116fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, GetMountStatusForPath_unitest, TestSize.Level1) 117fb299fa2Sopenharmony_ci{ 118fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/GetMountStatusForPath1.fstable"; 119fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 120fb299fa2Sopenharmony_ci const std::string path1 = ""; 121fb299fa2Sopenharmony_ci MountStatus ret = GetMountStatusForPath(path1); 122fb299fa2Sopenharmony_ci EXPECT_EQ(ret, MountStatus::MOUNT_ERROR); 123fb299fa2Sopenharmony_ci const std::string path2 = "/vendor"; 124fb299fa2Sopenharmony_ci ret = GetMountStatusForPath(path2); 125fb299fa2Sopenharmony_ci EXPECT_EQ(ret, MountStatus::MOUNT_MOUNTED); 126fb299fa2Sopenharmony_ci const std::string path3 = "/data/mount2"; 127fb299fa2Sopenharmony_ci ret = GetMountStatusForPath(path3); 128fb299fa2Sopenharmony_ci EXPECT_EQ(ret, MountStatus::MOUNT_UMOUNTED); 129fb299fa2Sopenharmony_ci} 130fb299fa2Sopenharmony_ci 131fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, SetupPartitions_unitest, TestSize.Level1) 132fb299fa2Sopenharmony_ci{ 133fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/SetupPartitions1.fstable"; 134fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 135fb299fa2Sopenharmony_ci int ret = SetupPartitions(); 136fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 137fb299fa2Sopenharmony_ci const std::string fstabFile2 = "/data/updater/mount_unitest/SetupPartitions2.fstable"; 138fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile2); 139fb299fa2Sopenharmony_ci ret = SetupPartitions(); 140fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 141fb299fa2Sopenharmony_ci} 142fb299fa2Sopenharmony_ci 143fb299fa2Sopenharmony_ciHWTEST_F(MountUnitTest, GetBlockDeviceByMountPoint_unitest, TestSize.Level1) 144fb299fa2Sopenharmony_ci{ 145fb299fa2Sopenharmony_ci LoadFstab(); 146fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/GetBlockDeviceByMountPoint1.fstable"; 147fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 148fb299fa2Sopenharmony_ci unsigned long mountFlag = MS_REMOUNT; 149fb299fa2Sopenharmony_ci const std::string tmpPath = "/vendor/etc/fstab.updater"; 150fb299fa2Sopenharmony_ci std::string vendorSource = GetBlockDeviceByMountPoint(tmpPath); 151fb299fa2Sopenharmony_ci // mount rootfs to read-write. 152fb299fa2Sopenharmony_ci if (mount(vendorSource.c_str(), "/vendor", "ext4", mountFlag, nullptr) != 0) { 153fb299fa2Sopenharmony_ci std::cout << "Cannot re-mount vendor\n"; 154fb299fa2Sopenharmony_ci } 155fb299fa2Sopenharmony_ci auto ret = open(tmpPath.c_str(), O_RDONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 156fb299fa2Sopenharmony_ci if (ret < 0) { 157fb299fa2Sopenharmony_ci std::cout << "Cannot open \"/vendor/etc/fstab.updater\" file: " << errno << "\n"; 158fb299fa2Sopenharmony_ci return; 159fb299fa2Sopenharmony_ci } 160fb299fa2Sopenharmony_ci close(ret); 161fb299fa2Sopenharmony_ci LoadFstab(); 162fb299fa2Sopenharmony_ci unlink(tmpPath.c_str()); 163fb299fa2Sopenharmony_ci 164fb299fa2Sopenharmony_ci LoadSpecificFstab(fstabFile1); 165fb299fa2Sopenharmony_ci const std::string mountPoint1 = ""; 166fb299fa2Sopenharmony_ci const std::string str1 = GetBlockDeviceByMountPoint(mountPoint1); 167fb299fa2Sopenharmony_ci EXPECT_TRUE(str1.empty()); 168fb299fa2Sopenharmony_ci const std::string mountPoint2 = "/data2"; 169fb299fa2Sopenharmony_ci const std::string str2 = GetBlockDeviceByMountPoint(mountPoint2); 170fb299fa2Sopenharmony_ci EXPECT_TRUE(str2.empty()); 171fb299fa2Sopenharmony_ci const std::string mountPoint3 = "/data"; 172fb299fa2Sopenharmony_ci const std::string str3 = GetBlockDeviceByMountPoint(mountPoint3); 173fb299fa2Sopenharmony_ci EXPECT_FALSE(str3.empty()); 174fb299fa2Sopenharmony_ci} 175fb299fa2Sopenharmony_ci} // namespace updater_ut 176