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 "fstabapi_unittest.h" 17fb299fa2Sopenharmony_ci#include <cctype> 18fb299fa2Sopenharmony_ci#include <cstdio> 19fb299fa2Sopenharmony_ci#include <fstream> 20fb299fa2Sopenharmony_ci#include <iostream> 21fb299fa2Sopenharmony_ci#include <memory> 22fb299fa2Sopenharmony_ci#include <string> 23fb299fa2Sopenharmony_ci#include <sys/mount.h> 24fb299fa2Sopenharmony_ci#include <unistd.h> 25fb299fa2Sopenharmony_ci#include <vector> 26fb299fa2Sopenharmony_ci#include "fs_manager/fs_manager.h" 27fb299fa2Sopenharmony_ci#include "fs_manager/mount.h" 28fb299fa2Sopenharmony_ci#include "log/log.h" 29fb299fa2Sopenharmony_ci#include "utils.h" 30fb299fa2Sopenharmony_ci 31fb299fa2Sopenharmony_ciusing namespace testing::ext; 32fb299fa2Sopenharmony_ciusing namespace UpdaterUt; 33fb299fa2Sopenharmony_ciusing namespace Updater; 34fb299fa2Sopenharmony_ciusing namespace std; 35fb299fa2Sopenharmony_ci 36fb299fa2Sopenharmony_cinamespace UpdaterUt { 37fb299fa2Sopenharmony_civoid FstabApiUnitTest::SetUp(void) 38fb299fa2Sopenharmony_ci{ 39fb299fa2Sopenharmony_ci cout << "Updater Unit MountUnitTest Begin!" << endl; 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ci// end 43fb299fa2Sopenharmony_civoid FstabApiUnitTest::TearDown(void) 44fb299fa2Sopenharmony_ci{ 45fb299fa2Sopenharmony_ci cout << "Updater Unit MountUnitTest End!" << endl; 46fb299fa2Sopenharmony_ci} 47fb299fa2Sopenharmony_ci 48fb299fa2Sopenharmony_ci// do something at the each function begining 49fb299fa2Sopenharmony_civoid FstabApiUnitTest::SetUpTestCase(void) {} 50fb299fa2Sopenharmony_ci 51fb299fa2Sopenharmony_ci// do something at the each function end 52fb299fa2Sopenharmony_civoid FstabApiUnitTest::TearDownTestCase(void) {} 53fb299fa2Sopenharmony_ci 54fb299fa2Sopenharmony_ciHWTEST_F(FstabApiUnitTest, ReadFstabFromFile_unitest, TestSize.Level1) 55fb299fa2Sopenharmony_ci{ 56fb299fa2Sopenharmony_ci Fstab *fstab = nullptr; 57fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/fstab.updater1"; 58fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile1.c_str(), false); 59fb299fa2Sopenharmony_ci EXPECT_EQ(fstab, nullptr); 60fb299fa2Sopenharmony_ci const std::string fstabFile2 = "/data/updater/mount_unitest/ReadFstabFromFile1.fstable"; 61fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile2.c_str(), false); 62fb299fa2Sopenharmony_ci EXPECT_EQ(fstab, nullptr); 63fb299fa2Sopenharmony_ci const std::string fstabFile3 = "/data/updater/mount_unitest/ReadFstabFromFile2.fstable"; 64fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile3.c_str(), false); 65fb299fa2Sopenharmony_ci EXPECT_EQ(fstab, nullptr); 66fb299fa2Sopenharmony_ci const std::string fstabFile4 = "/data/updater/mount_unitest/ReadFstabFromFile3.fstable"; 67fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile4.c_str(), false); 68fb299fa2Sopenharmony_ci EXPECT_EQ(fstab, nullptr); 69fb299fa2Sopenharmony_ci const std::string fstabFile5 = "/data/updater/mount_unitest/ReadFstabFromFile4.fstable"; 70fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile5.c_str(), false); 71fb299fa2Sopenharmony_ci EXPECT_EQ(fstab, nullptr); 72fb299fa2Sopenharmony_ci const std::string fstabFile6 = "/data/updater/mount_unitest/ReadFstabFromFile5.fstable"; 73fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile6.c_str(), false); 74fb299fa2Sopenharmony_ci EXPECT_NE(fstab, nullptr); 75fb299fa2Sopenharmony_ci ReleaseFstab(fstab); 76fb299fa2Sopenharmony_ci} 77fb299fa2Sopenharmony_ci 78fb299fa2Sopenharmony_ciHWTEST_F(FstabApiUnitTest, FindFstabItemForPath_unitest, TestSize.Level1) 79fb299fa2Sopenharmony_ci{ 80fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/FindFstabItemForPath1.fstable"; 81fb299fa2Sopenharmony_ci Fstab *fstab = nullptr; 82fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile1.c_str(), false); 83fb299fa2Sopenharmony_ci ASSERT_NE(fstab, nullptr); 84fb299fa2Sopenharmony_ci FstabItem* item = nullptr; 85fb299fa2Sopenharmony_ci const std::string path1 = ""; 86fb299fa2Sopenharmony_ci item = FindFstabItemForPath(*fstab, path1.c_str()); 87fb299fa2Sopenharmony_ci if (item == nullptr) { 88fb299fa2Sopenharmony_ci SUCCEED(); 89fb299fa2Sopenharmony_ci } 90fb299fa2Sopenharmony_ci const std::string path2 = "/data"; 91fb299fa2Sopenharmony_ci item = FindFstabItemForPath(*fstab, path2.c_str()); 92fb299fa2Sopenharmony_ci if (item != nullptr) { 93fb299fa2Sopenharmony_ci SUCCEED(); 94fb299fa2Sopenharmony_ci } 95fb299fa2Sopenharmony_ci const std::string path3 = "/data2"; 96fb299fa2Sopenharmony_ci item = FindFstabItemForPath(*fstab, path3.c_str()); 97fb299fa2Sopenharmony_ci if (item == nullptr) { 98fb299fa2Sopenharmony_ci SUCCEED(); 99fb299fa2Sopenharmony_ci } 100fb299fa2Sopenharmony_ci const std::string path4 = "/data2/test"; 101fb299fa2Sopenharmony_ci item = FindFstabItemForPath(*fstab, path4.c_str()); 102fb299fa2Sopenharmony_ci if (item != nullptr) { 103fb299fa2Sopenharmony_ci SUCCEED(); 104fb299fa2Sopenharmony_ci } 105fb299fa2Sopenharmony_ci ReleaseFstab(fstab); 106fb299fa2Sopenharmony_ci fstab = nullptr; 107fb299fa2Sopenharmony_ci} 108fb299fa2Sopenharmony_ci 109fb299fa2Sopenharmony_ciHWTEST_F(FstabApiUnitTest, FindFstabItemForMountPoint_unitest, TestSize.Level1) 110fb299fa2Sopenharmony_ci{ 111fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/FindFstabItemForMountPoint1.fstable"; 112fb299fa2Sopenharmony_ci Fstab *fstab = nullptr; 113fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile1.c_str(), false); 114fb299fa2Sopenharmony_ci ASSERT_NE(fstab, nullptr); 115fb299fa2Sopenharmony_ci FstabItem* item = nullptr; 116fb299fa2Sopenharmony_ci const std::string mp1 = "/data"; 117fb299fa2Sopenharmony_ci const std::string mp2 = "/data2"; 118fb299fa2Sopenharmony_ci item = FindFstabItemForMountPoint(*fstab, mp2.c_str()); 119fb299fa2Sopenharmony_ci if (item == nullptr) { 120fb299fa2Sopenharmony_ci SUCCEED(); 121fb299fa2Sopenharmony_ci } 122fb299fa2Sopenharmony_ci const std::string mp3 = "/data"; 123fb299fa2Sopenharmony_ci item = FindFstabItemForMountPoint(*fstab, mp3.c_str()); 124fb299fa2Sopenharmony_ci if (item != nullptr) { 125fb299fa2Sopenharmony_ci SUCCEED(); 126fb299fa2Sopenharmony_ci } 127fb299fa2Sopenharmony_ci ReleaseFstab(fstab); 128fb299fa2Sopenharmony_ci fstab = nullptr; 129fb299fa2Sopenharmony_ci} 130fb299fa2Sopenharmony_ci 131fb299fa2Sopenharmony_ciHWTEST_F(FstabApiUnitTest, GetMountFlags_unitest, TestSize.Level1) 132fb299fa2Sopenharmony_ci{ 133fb299fa2Sopenharmony_ci const std::string fstabFile1 = "/data/updater/mount_unitest/GetMountFlags1.fstable"; 134fb299fa2Sopenharmony_ci Fstab *fstab = nullptr; 135fb299fa2Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile1.c_str(), false); 136fb299fa2Sopenharmony_ci ASSERT_NE(fstab, nullptr); 137fb299fa2Sopenharmony_ci struct FstabItem* item = nullptr; 138fb299fa2Sopenharmony_ci const std::string mp = "/hos"; 139fb299fa2Sopenharmony_ci item = FindFstabItemForMountPoint(*fstab, mp.c_str()); 140fb299fa2Sopenharmony_ci if (item == nullptr) { 141fb299fa2Sopenharmony_ci SUCCEED(); 142fb299fa2Sopenharmony_ci } 143fb299fa2Sopenharmony_ci const int bufferSize = 512; 144fb299fa2Sopenharmony_ci char fsSpecificOptions[bufferSize] = {0}; 145fb299fa2Sopenharmony_ci unsigned long flags = GetMountFlags(item->mountOptions, fsSpecificOptions, bufferSize, mp.c_str()); 146fb299fa2Sopenharmony_ci EXPECT_EQ(flags, static_cast<unsigned long>(MS_NOSUID | MS_NODEV | MS_NOATIME)); 147fb299fa2Sopenharmony_ci ReleaseFstab(fstab); 148fb299fa2Sopenharmony_ci fstab = nullptr; 149fb299fa2Sopenharmony_ci} 150fb299fa2Sopenharmony_ci} // namespace updater_ut 151