• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
17 #include <sys/mount.h>
18 #include "fs_manager/fs_manager.h"
19 #include "fs_manager/fs_manager_log.h"
20 #include "init_log.h"
21 #include "init_unittest.h"
22 #include "securec.h"
23 
24 using namespace testing::ext;
25 using namespace std;
26 
27 namespace init_ut {
28 class InnerkitsUnitTest : public testing::Test {
29 public:
SetUpTestCase(void)30     static void SetUpTestCase(void) {};
TearDownTestCase(void)31     static void TearDownTestCase(void) {};
SetUp()32     void SetUp() {};
TearDown()33     void TearDown() {};
34 };
35 
36 HWTEST_F(InnerkitsUnitTest, ReadFstabFromFile_unitest, TestSize.Level1)
37 {
38     Fstab *fstab = nullptr;
39     const std::string fstabFile1 = "/data/fstab.updater1";
40     fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
41     EXPECT_EQ(fstab, nullptr);
42     const std::string fstabFile2 = "/data/init_ut/mount_unitest/ReadFstabFromFile1.fstable";
43     fstab = ReadFstabFromFile(fstabFile2.c_str(), false);
44     EXPECT_NE(fstab, nullptr);
45     const std::string fstabFile3 = "/data/init_ut/mount_unitest/ReadFstabFromFile2.fstable";
46     fstab = ReadFstabFromFile(fstabFile3.c_str(), false);
47     EXPECT_NE(fstab, nullptr);
48     const std::string fstabFile4 = "/data/init_ut/mount_unitest/ReadFstabFromFile3.fstable";
49     fstab = ReadFstabFromFile(fstabFile4.c_str(), false);
50     EXPECT_NE(fstab, nullptr);
51     const std::string fstabFile5 = "/data/init_ut/mount_unitest/ReadFstabFromFile4.fstable";
52     fstab = ReadFstabFromFile(fstabFile5.c_str(), false);
53     EXPECT_NE(fstab, nullptr);
54     const std::string fstabFile6 = "/data/init_ut/mount_unitest/ReadFstabFromFile5.fstable";
55     fstab = ReadFstabFromFile(fstabFile6.c_str(), false);
56     EXPECT_NE(fstab, nullptr);
57     ReleaseFstab(fstab);
58 }
59 
60 HWTEST_F(InnerkitsUnitTest, FindFstabItemForPath_unitest, TestSize.Level1)
61 {
62     const std::string fstabFile1 = "/data/init_ut/mount_unitest/FindFstabItemForPath1.fstable";
63     Fstab *fstab = nullptr;
64     fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
65     ASSERT_NE(fstab, nullptr);
66     FstabItem* item = nullptr;
67     const std::string path1 = "";
68     item = FindFstabItemForPath(*fstab, path1.c_str());
69     if (item == nullptr) {
70         SUCCEED();
71     }
72     const std::string path2 = "/data";
73     item = FindFstabItemForPath(*fstab, path2.c_str());
74     if (item != nullptr) {
75         SUCCEED();
76     }
77     const std::string path3 = "/data2";
78     item = FindFstabItemForPath(*fstab, path3.c_str());
79     if (item == nullptr) {
80         SUCCEED();
81     }
82     const std::string path4 = "/data2/test";
83     item = FindFstabItemForPath(*fstab, path4.c_str());
84     if (item != nullptr) {
85         SUCCEED();
86     }
87     ReleaseFstab(fstab);
88     fstab = nullptr;
89 }
90 
91 HWTEST_F(InnerkitsUnitTest, FindFstabItemForMountPoint_unitest, TestSize.Level1)
92 {
93     const std::string fstabFile1 = "/data/init_ut/mount_unitest/FindFstabItemForMountPoint1.fstable";
94     Fstab *fstab = nullptr;
95     fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
96     ASSERT_NE(fstab, nullptr);
97     FstabItem* item = nullptr;
98     const std::string mp1 = "/data";
99     const std::string mp2 = "/data2";
100     item = FindFstabItemForMountPoint(*fstab, mp2.c_str());
101     if (item == nullptr) {
102         SUCCEED();
103     }
104     const std::string mp3 = "/data";
105     item = FindFstabItemForMountPoint(*fstab, mp3.c_str());
106     if (item != nullptr) {
107         SUCCEED();
108     }
109     ReleaseFstab(fstab);
110     fstab = nullptr;
111 }
112 
113 HWTEST_F(InnerkitsUnitTest, GetMountFlags_unitest, TestSize.Level1)
114 {
115     const std::string fstabFile1 = "/data/init_ut/mount_unitest/GetMountFlags1.fstable";
116     Fstab *fstab = nullptr;
117     fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
118     ASSERT_NE(fstab, nullptr);
119     struct FstabItem* item = nullptr;
120     const std::string mp = "/hos";
121     item = FindFstabItemForMountPoint(*fstab, mp.c_str());
122     if (item == nullptr) {
123         SUCCEED();
124     }
125     const int bufferSize = 512;
126     char fsSpecificOptions[bufferSize] = {0};
127     unsigned long flags = GetMountFlags(item->mountOptions, fsSpecificOptions, bufferSize);
128     EXPECT_EQ(flags, static_cast<unsigned long>(MS_NOSUID | MS_NODEV | MS_NOATIME));
129     ReleaseFstab(fstab);
130     fstab = nullptr;
131 }
132 
133 HWTEST_F(InnerkitsUnitTest, TestFsManagerLog, TestSize.Level1)
134 {
135     FsManagerLogInit(LOG_TO_KERNEL, FILE_NAME);
136     FSMGR_LOGE("Fsmanager log to kernel.");
137     FsManagerLogInit(LOG_TO_STDIO, "");
138     FSMGR_LOGE("Fsmanager log to stdio.");
139     string logPath = "/data/init_ut/fs_log.txt";
140     auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(logPath.c_str(), "at+"), fclose);
141     EXPECT_TRUE(fp != nullptr);
142     sync();
143     FsManagerLogInit(LOG_TO_FILE, logPath.c_str());
144     FSMGR_LOGE("Fsmanager log to file.");
145     FsManagerLogDeInit();
146 }
147 } // namespace init_ut
148