• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "fstab_mount_test.h"
16 #include <gtest/gtest.h>
17 #include "func_wrapper.h"
18 #include <sys/stat.h>
19 #include <sys/mount.h>
20 #include "init_utils.h"
21 #include "securec.h"
22 using namespace testing;
23 using namespace testing::ext;
24 #define STRSIZE 64
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31 
SnprintfSReturnZero(char * strDest,size_t destMax,size_t count,const char * format,va_list args)32 size_t SnprintfSReturnZero(char *strDest, size_t destMax, size_t count, const char *format, va_list args)
33 {
34     return 0;
35 }
36 
37 #ifdef __cplusplus
38 #if __cplusplus
39 }
40 #endif
41 #endif
42 
43 
44 namespace OHOS {
45 class FstabMountTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 };
52 
SetUpTestCase()53 void FstabMountTest::SetUpTestCase()
54 {
55     GTEST_LOG_(INFO) << "FstabMountTest SetUpTestCase";
56 }
57 
TearDownTestCase()58 void FstabMountTest::TearDownTestCase()
59 {
60     GTEST_LOG_(INFO) << "FstabMountTest TearDownTestCase";
61 }
62 
SetUp()63 void FstabMountTest::SetUp()
64 {
65     GTEST_LOG_(INFO) << "FstabMountTest SetUp";
66 }
67 
TearDown()68 void FstabMountTest::TearDown()
69 {
70     GTEST_LOG_(INFO) << "FstabMountTest TearDown";
71 }
72 
73 HWTEST_F(FstabMountTest, DoOneMountItem_001, TestSize.Level0)
74 {
75     char deviceName[STRSIZE]  = "deviceName";
76     char mountPoint[STRSIZE] = "mountPoint";
77     char fsType[STRSIZE] = "fstype";
78     char mountOptions[STRSIZE] = "op1,op2,op3,op4,op5";
79     unsigned int fsManagerFlags = 0;
80 
81     FstabItem item = {
82         .deviceName = deviceName,
83         .mountPoint = mountPoint,
84         .fsType = fsType,
85         .mountOptions = mountOptions,
86         .fsManagerFlags = fsManagerFlags,
87         .next = NULL,
88     };
89 
90     int rc = DoMountOneItem(&item);
91     EXPECT_NE(rc, 0);
92 }
93 
94 HWTEST_F(FstabMountTest, DoOneMountItem_002, TestSize.Level0)
95 {
96     char deviceName[STRSIZE]  = "deviceName";
97     char mountPoint[STRSIZE] = "mountPoint";
98     char fsType[STRSIZE] = "hmfs";
99     char mountOptions[STRSIZE] = "op1,op2,op3,op4,op5,checkpoint=disable";
100     unsigned int fsManagerFlags = 0;
101 
102     FstabItem item = {
103         .deviceName = deviceName,
104         .mountPoint = mountPoint,
105         .fsType = fsType,
106         .mountOptions = mountOptions,
107         .fsManagerFlags = fsManagerFlags,
108         .next = NULL,
109     };
110 
111     int rc = DoMountOneItem(&item);
112     EXPECT_EQ(rc, -1);
113 }
114 
115 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_001, TestSize.Level0)
116 {
117     int rc =  GetDataWithoutCheckpoint(nullptr, 0, nullptr, 0);
118     EXPECT_EQ(rc, -1);
119 }
120 
121 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_002, TestSize.Level0)
122 {
123     char fsData[STRSIZE] = "checkpoint=disable,op1,op2,op3";
124     int rc =  GetDataWithoutCheckpoint(fsData, STRSIZE, nullptr, 0);
125     EXPECT_EQ(rc, -1);
126 
127     char result[STRSIZE] = {0};
128     rc = GetDataWithoutCheckpoint(fsData, STRSIZE, result, STRSIZE);
129     EXPECT_EQ(rc, 0);
130 }
131 
132 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_003, TestSize.Level0)
133 {
134     char fsData[STRSIZE] = "checkpoint=disable,op1,op2,op3,op4,op5";
135     char result[STRSIZE] = {0};
136     StrdupFunc func = [](const char *path) -> char *
__anonb65c97420102(const char *path) 137     {
138         return nullptr;
139     };
140     UpdateStrdupFunc(func);
141     int rc = GetDataWithoutCheckpoint(fsData, STRSIZE, result, STRSIZE);
142     UpdateStrdupFunc(nullptr);
143     EXPECT_EQ(rc, -1);
144 }
145 
146 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_004, TestSize.Level0)
147 {
148     char fsData[STRSIZE] = "checkpoint=disable,op1,op2,op3,op4,op5";
149     char result[STRSIZE] = {0};
__anonb65c97420202(size_t size) 150     MallocFunc func = [](size_t size) -> void* {
151         return nullptr;
152     };
153     UpdateMallocFunc(func);
154     int rc = GetDataWithoutCheckpoint(fsData, STRSIZE, result, STRSIZE);
155     UpdateMallocFunc(nullptr);
156     EXPECT_EQ(rc, -1);
157 }
158 
159 
160 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_005, TestSize.Level0)
161 {
162     char fsData[STRSIZE] = "checkpoint=disable,op1,op2,op3,op4,op5";
163     char result[STRSIZE] = {0};
__anonb65c97420302(char * strDest, size_t destMax, const char * strSrc, size_t count) 164     StrncatSFunc func = [](char * strDest, size_t destMax, const char * strSrc, size_t count) -> int {
165         if (strcmp(strSrc, ",") == 0) {
166             return -1;
167         }
168         return __real_strncat_s(strDest, destMax, strSrc, count);
169     };
170     UpdateStrncatSFunc(func);
171     int rc = GetDataWithoutCheckpoint(fsData, STRSIZE, result, STRSIZE);
172     UpdateStrncatSFunc(nullptr);
173     EXPECT_EQ(rc, -1);
174 }
175 
176 HWTEST_F(FstabMountTest, GetDataWithoutCheckpoint_006, TestSize.Level0)
177 {
178     char fsData[STRSIZE] = "checkpoint=disable,op1,op2,op3,op4,op5";
179     char result[STRSIZE] = {0};
__anonb65c97420402(char *, size_t, const char *, size_t) 180     StrncatSFunc func = [](char *, size_t, const char *, size_t) -> int {
181         return -1;
182     };
183     UpdateStrncatSFunc(func);
184     int rc = GetDataWithoutCheckpoint(fsData, STRSIZE, result, STRSIZE);
185     UpdateStrncatSFunc(nullptr);
186     EXPECT_EQ(rc, -1);
187 }
188 
189 HWTEST_F(FstabMountTest, MountWithCheckpoint_001, TestSize.Level0)
190 {
__anonb65c97420502(const char* path, struct stat *) 191     StatFunc func = [](const char* path, struct stat *) -> int {
192         return 0;
193     };
194     UpdateStatFunc(func);
195     int rc = MountWithCheckpoint("/source", "/target", "fsType", 0, "op1,op2,op3,op4,op5");
196     UpdateStatFunc(nullptr);
197     EXPECT_EQ(rc, -1);
198 }
199 
200 HWTEST_F(FstabMountTest, MountWithCheckpoint_002, TestSize.Level0)
201 {
__anonb65c97420602(const char* path, struct stat *) 202     StatFunc func = [](const char* path, struct stat *) -> int {
203         return 0;
204     };
205     UpdateStatFunc(func);
206 
__anonb65c97420702(const char*, mode_t) 207     MkdirFunc mkdirFunc = [](const char*, mode_t) -> int {
208         return 0;
209     };
210     UpdateMkdirFunc(mkdirFunc);
211     UpdateSnprintfSFunc(SnprintfSReturnZero);
212 
213     int rc = MountWithCheckpoint("/source", "/target", "fsType", 0, "op1,op2,op3,op4,op5");
214     UpdateStatFunc(nullptr);
215     UpdateMkdirFunc(nullptr);
216     UpdateSnprintfSFunc(nullptr);
217 
218     EXPECT_EQ(rc, -1);
219 }
220 
221 HWTEST_F(FstabMountTest, MountWithCheckpoint_003, TestSize.Level0)
222 {
__anonb65c97420802(const char* path, struct stat *) 223     StatFunc func = [](const char* path, struct stat *) -> int {
224         return 0;
225     };
226     UpdateStatFunc(func);
227 
__anonb65c97420902(const char*, mode_t) 228     MkdirFunc mkdirFunc = [](const char*, mode_t) -> int {
229         return 0;
230     };
231     UpdateMkdirFunc(mkdirFunc);
232 
__anonb65c97420a02(const char *, const char *, const char *, unsigned long, const void *) 233     MountFunc mountFunc = [](const char *, const char *, const char *, unsigned long, const void *) -> int {
234         errno = EBUSY;
235         return -1;
236     };
237     UpdateMountFunc(mountFunc);
238 
239     int rc = MountWithCheckpoint("/source", "/target", "fsType", 0, "op1,op2,op3,op4,op5");
240     UpdateStatFunc(nullptr);
241     UpdateMkdirFunc(nullptr);
242     UpdateMountFunc(nullptr);
243 
244     EXPECT_EQ(rc, 0);
245 }
246 
247 }