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
16 #include "fs_stat.h"
17
18 #include <gtest/gtest.h>
19
20
21 namespace OHOS::FileManagement::ModuleFileIO::Test {
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace std;
25
26 class FsStatTest : public testing::Test {
27 public:
28 static void SetUpTestCase(void);
29 static void TearDownTestCase(void);
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void FsStatTest::SetUpTestCase(void)
35 {
36 GTEST_LOG_(INFO) << "SetUpTestCase";
37 }
38
TearDownTestCase(void)39 void FsStatTest::TearDownTestCase(void)
40 {
41 GTEST_LOG_(INFO) << "TearDownTestCase";
42 }
43
SetUp(void)44 void FsStatTest::SetUp(void)
45 {
46 GTEST_LOG_(INFO) << "SetUp";
47 }
48
TearDown(void)49 void FsStatTest::TearDown(void)
50 {
51 GTEST_LOG_(INFO) << "TearDown";
52 }
53
54 /**
55 * @tc.name: FsStatTest_Constructor_001
56 * @tc.desc: Test FsStat::Constructor for success case
57 * @tc.size: SMALL
58 * @tc.type: FUNC
59 * @tc.level Level 1
60 */
61 HWTEST_F(FsStatTest, FsStatTest_Constructor_001, testing::ext::TestSize.Level1)
62 {
63 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_Constructor_001";
64
65 auto stat = FsStat::Constructor();
66 EXPECT_NE(stat, nullptr);
67 delete stat;
68
69 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_Constructor_001";
70 }
71
72 /**
73 * @tc.name: FsStatTest_IsBlockDevice_001
74 * @tc.desc: Test FsStat::IsBlockDevice for directory
75 * @tc.size: SMALL
76 * @tc.type: FUNC
77 * @tc.level Level 1
78 */
79 HWTEST_F(FsStatTest, FsStatTest_IsBlockDevice_001, testing::ext::TestSize.Level1)
80 {
81 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsBlockDevice_001";
82
83 auto stat = FsStat::Constructor();
84 ASSERT_NE(stat, nullptr);
85
86 stat->entity->stat_.st_mode = S_IFBLK;
87 EXPECT_TRUE(stat->IsBlockDevice());
88 EXPECT_FALSE(stat->IsFile());
89 delete stat;
90
91 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsBlockDevice_001";
92 }
93
94 /**
95 * @tc.name: FsStatTest_IsCharacterDevice_001
96 * @tc.desc: Test FsStat::IsCharacterDevice for directory
97 * @tc.size: SMALL
98 * @tc.type: FUNC
99 * @tc.level Level 1
100 */
101 HWTEST_F(FsStatTest, FsStatTest_IsCharacterDevice_001, testing::ext::TestSize.Level1)
102 {
103 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsCharacterDevice_001";
104
105 auto stat = FsStat::Constructor();
106 ASSERT_NE(stat, nullptr);
107
108 stat->entity->stat_.st_mode = S_IFCHR;
109 EXPECT_TRUE(stat->IsCharacterDevice());
110 EXPECT_FALSE(stat->IsFile());
111 delete stat;
112
113 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsCharacterDevice_001";
114 }
115
116 /**
117 * @tc.name: FsStatTest_IsDirectory_001
118 * @tc.desc: Test FsStat::IsDirectory for directory
119 * @tc.size: SMALL
120 * @tc.type: FUNC
121 * @tc.level Level 1
122 */
123 HWTEST_F(FsStatTest, FsStatTest_IsDirectory_001, testing::ext::TestSize.Level1)
124 {
125 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsDirectory_001";
126
127 auto stat = FsStat::Constructor();
128 ASSERT_NE(stat, nullptr);
129
130 stat->entity->stat_.st_mode = S_IFDIR | 0755;
131 EXPECT_TRUE(stat->IsDirectory());
132 EXPECT_FALSE(stat->IsFile());
133 delete stat;
134
135 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsDirectory_001";
136 }
137
138 /**
139 * @tc.name: FsStatTest_IsFIFO_001
140 * @tc.desc: Test FsStat::IsFIFO for directory
141 * @tc.size: SMALL
142 * @tc.type: FUNC
143 * @tc.level Level 1
144 */
145 HWTEST_F(FsStatTest, FsStatTest_IsFIFO_001, testing::ext::TestSize.Level1)
146 {
147 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsFIFO_001";
148
149 auto stat = FsStat::Constructor();
150 ASSERT_NE(stat, nullptr);
151
152 stat->entity->stat_.st_mode = S_IFIFO;
153 EXPECT_TRUE(stat->IsFIFO());
154 EXPECT_FALSE(stat->IsFile());
155 delete stat;
156
157 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsFIFO_001";
158 }
159
160 /**
161 * @tc.name: FsStatTest_IsFile_001
162 * @tc.desc: Test FsStat::IsFile for regular file
163 * @tc.size: SMALL
164 * @tc.type: FUNC
165 * @tc.level Level 1
166 */
167 HWTEST_F(FsStatTest, FsStatTest_IsFile_001, testing::ext::TestSize.Level1)
168 {
169 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsFile_001";
170
171 auto stat = FsStat::Constructor();
172 ASSERT_NE(stat, nullptr);
173
174 stat->entity->stat_.st_mode = S_IFREG | 0644;
175 EXPECT_TRUE(stat->IsFile());
176 EXPECT_FALSE(stat->IsDirectory());
177 delete stat;
178
179 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsFile_001";
180 }
181
182 /**
183 * @tc.name: FsStatTest_IsSocket_001
184 * @tc.desc: Test FsStat::IsSocket for symbolic link
185 * @tc.size: SMALL
186 * @tc.type: FUNC
187 * @tc.level Level 1
188 */
189 HWTEST_F(FsStatTest, FsStatTest_IsSocket_001, testing::ext::TestSize.Level1)
190 {
191 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsSocket_001";
192
193 auto stat = FsStat::Constructor();
194 ASSERT_NE(stat, nullptr);
195
196 stat->entity->stat_.st_mode = S_IFSOCK;
197 EXPECT_TRUE(stat->IsSocket());
198 EXPECT_FALSE(stat->IsDirectory());
199 delete stat;
200
201 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsSocket_001";
202 }
203
204 /**
205 * @tc.name: FsStatTest_IsSymbolicLink_001
206 * @tc.desc: Test FsStat::IsSymbolicLink for symbolic link
207 * @tc.size: SMALL
208 * @tc.type: FUNC
209 * @tc.level Level 1
210 */
211 HWTEST_F(FsStatTest, FsStatTest_IsSymbolicLink_001, testing::ext::TestSize.Level1)
212 {
213 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsSymbolicLink_001";
214
215 auto stat = FsStat::Constructor();
216 ASSERT_NE(stat, nullptr);
217
218 stat->entity->stat_.st_mode = S_IFLNK | 0777;
219 EXPECT_TRUE(stat->IsSymbolicLink());
220 EXPECT_FALSE(stat->IsDirectory());
221 delete stat;
222
223 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsSymbolicLink_001";
224 }
225
226 /**
227 * @tc.name: FsStatTest_GetIno_001
228 * @tc.desc: Test FsStat::GetIno for valid inode number
229 * @tc.size: SMALL
230 * @tc.type: FUNC
231 * @tc.level Level 1
232 */
233 HWTEST_F(FsStatTest, FsStatTest_GetIno_001, testing::ext::TestSize.Level1)
234 {
235 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetIno_001";
236
237 auto stat = FsStat::Constructor();
238 ASSERT_NE(stat, nullptr);
239
240 stat->entity->stat_.st_ino = 123456789;
241 EXPECT_EQ(stat->GetIno(), 123456789);
242 delete stat;
243
244 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetIno_001";
245 }
246
247 /**
248 * @tc.name: FsStatTest_GetMode_001
249 * @tc.desc: Test FsStat::GetMode for permission bits
250 * @tc.size: SMALL
251 * @tc.type: FUNC
252 * @tc.level Level 1
253 */
254 HWTEST_F(FsStatTest, FsStatTest_GetMode_001, testing::ext::TestSize.Level1)
255 {
256 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMode_001";
257
258 auto stat = FsStat::Constructor();
259 ASSERT_NE(stat, nullptr);
260
261 stat->entity->stat_.st_mode = S_IFREG | 0755;
262 EXPECT_EQ(stat->GetMode(), 0755);
263 delete stat;
264
265 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMode_001";
266 }
267
268 /**
269 * @tc.name: FsStatTest_GetUid_001
270 * @tc.desc: Test FsStat::GetUid for user ID
271 * @tc.size: SMALL
272 * @tc.type: FUNC
273 * @tc.level Level 1
274 */
275 HWTEST_F(FsStatTest, FsStatTest_GetUid_001, testing::ext::TestSize.Level1)
276 {
277 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetUid_001";
278
279 auto stat = FsStat::Constructor();
280 ASSERT_NE(stat, nullptr);
281
282 stat->entity->stat_.st_uid = 1000;
283 EXPECT_EQ(stat->GetUid(), 1000);
284 delete stat;
285
286 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetUid_001";
287 }
288
289 /**
290 * @tc.name: FsStatTest_GetGid_001
291 * @tc.desc: Test FsStat::GetGid for group ID
292 * @tc.size: SMALL
293 * @tc.type: FUNC
294 * @tc.level Level 1
295 */
296 HWTEST_F(FsStatTest, FsStatTest_GetGid_001, testing::ext::TestSize.Level1)
297 {
298 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetGid_001";
299
300 auto stat = FsStat::Constructor();
301 ASSERT_NE(stat, nullptr);
302
303 stat->entity->stat_.st_gid = 1000;
304 EXPECT_EQ(stat->GetGid(), 1000);
305 delete stat;
306
307 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetGid_001";
308 }
309
310 /**
311 * @tc.name: FsStatTest_GetSize_001
312 * @tc.desc: Test FsStat::GetSize for file size
313 * @tc.size: SMALL
314 * @tc.type: FUNC
315 * @tc.level Level 1
316 */
317 HWTEST_F(FsStatTest, FsStatTest_GetSize_001, testing::ext::TestSize.Level1)
318 {
319 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetSize_001";
320
321 auto stat = FsStat::Constructor();
322 ASSERT_NE(stat, nullptr);
323
324 stat->entity->stat_.st_size = 123456789;
325 EXPECT_EQ(stat->GetSize(), 123456789);
326 delete stat;
327
328 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetSize_001";
329 }
330
331 /**
332 * @tc.name: FsStatTest_GetAtime_001
333 * @tc.desc: Test FsStat::GetAtime for access time in seconds
334 * @tc.size: SMALL
335 * @tc.type: FUNC
336 * @tc.level Level 1
337 */
338 HWTEST_F(FsStatTest, FsStatTest_GetAtime_001, testing::ext::TestSize.Level1)
339 {
340 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetAtime_001";
341
342 auto stat = FsStat::Constructor();
343 ASSERT_NE(stat, nullptr);
344
345 stat->entity->stat_.st_atim.tv_sec = 1630473600;
346 stat->entity->stat_.st_atim.tv_nsec = 500000000;
347
348 EXPECT_EQ(stat->GetAtime(), 1630473600);
349 delete stat;
350
351 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetAtime_001";
352 }
353
354 /**
355 * @tc.name: FsStatTest_GetMtime_001
356 * @tc.desc: Test FsStat::GetMtime for modification time in seconds
357 * @tc.size: SMALL
358 * @tc.type: FUNC
359 * @tc.level Level 1
360 */
361 HWTEST_F(FsStatTest, FsStatTest_GetMtime_001, testing::ext::TestSize.Level1)
362 {
363 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMtime_001";
364
365 auto stat = FsStat::Constructor();
366 ASSERT_NE(stat, nullptr);
367
368 stat->entity->stat_.st_mtim.tv_sec = 1630473601;
369 stat->entity->stat_.st_mtim.tv_nsec = 500000000;
370
371 EXPECT_EQ(stat->GetMtime(), 1630473601);
372 delete stat;
373
374 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMtime_001";
375 }
376
377 /**
378 * @tc.name: FsStatTest_GetCtime_001
379 * @tc.desc: Test FsStat::GetCtime for change time in seconds
380 * @tc.size: SMALL
381 * @tc.type: FUNC
382 * @tc.level Level 1
383 */
384 HWTEST_F(FsStatTest, FsStatTest_GetCtime_001, testing::ext::TestSize.Level1)
385 {
386 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetCtime_001";
387
388 auto stat = FsStat::Constructor();
389 ASSERT_NE(stat, nullptr);
390
391 stat->entity->stat_.st_ctim.tv_sec = 1630473602;
392 stat->entity->stat_.st_ctim.tv_nsec = 500000000;
393
394 EXPECT_EQ(stat->GetCtime(), 1630473602);
395 delete stat;
396
397 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetCtime_001";
398 }
399
400 /**
401 * @tc.name: FsStatTest_GetAtimeNs_001
402 * @tc.desc: Test FsStat::GetAtimeNs for access time in nanoseconds
403 * @tc.size: SMALL
404 * @tc.type: FUNC
405 * @tc.level Level 1
406 */
407 HWTEST_F(FsStatTest, FsStatTest_GetAtimeNs_001, testing::ext::TestSize.Level1)
408 {
409 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetAtimeNs_001";
410
411 auto stat = FsStat::Constructor();
412 ASSERT_NE(stat, nullptr);
413
414 stat->entity->stat_.st_atim.tv_sec = 1630473600;
415 stat->entity->stat_.st_atim.tv_nsec = 500000000;
416
417 int64_t expected = 1630473600LL * 1000000000 + 500000000;
418 EXPECT_EQ(stat->GetAtimeNs(), expected);
419 delete stat;
420
421 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetAtimeNs_001";
422 }
423
424 /**
425 * @tc.name: FsStatTest_GetMtimeNs_001
426 * @tc.desc: Test FsStat::GetMtimeNs for modification time in nanoseconds
427 * @tc.size: SMALL
428 * @tc.type: FUNC
429 * @tc.level Level 1
430 */
431 HWTEST_F(FsStatTest, FsStatTest_GetMtimeNs_001, testing::ext::TestSize.Level1)
432 {
433 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMtimeNs_001";
434
435 auto stat = FsStat::Constructor();
436 ASSERT_NE(stat, nullptr);
437
438 stat->entity->stat_.st_mtim.tv_sec = 1630473601;
439 stat->entity->stat_.st_mtim.tv_nsec = 500000000;
440
441 int64_t expected = 1630473601LL * 1000000000 + 500000000;
442 EXPECT_EQ(stat->GetMtimeNs(), expected);
443 delete stat;
444
445 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMtimeNs_001";
446 }
447
448 /**
449 * @tc.name: FsStatTest_GetCtimeNs_001
450 * @tc.desc: Test FsStat::GetCtimeNs for change time in nanoseconds
451 * @tc.size: SMALL
452 * @tc.type: FUNC
453 * @tc.level Level 1
454 */
455 HWTEST_F(FsStatTest, FsStatTest_GetCtimeNs_001, testing::ext::TestSize.Level1)
456 {
457 GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetCtimeNs_001";
458
459 auto stat = FsStat::Constructor();
460 ASSERT_NE(stat, nullptr);
461
462 stat->entity->stat_.st_ctim.tv_sec = 1630473602;
463 stat->entity->stat_.st_ctim.tv_nsec = 500000000;
464
465 int64_t expected = 1630473602LL * 1000000000 + 500000000;
466 EXPECT_EQ(stat->GetCtimeNs(), expected);
467 delete stat;
468
469 GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetCtimeNs_001";
470 }
471
472 }