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