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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "fs_stat_entity.h"
20 #include "fs_stat.h"
21 #include "securec.h"
22 #include "system_mock.h"
23
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28
29 class FsStatMockTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 static inline shared_ptr<SystemMock> sys = nullptr;
36 };
37
SetUpTestCase(void)38 void FsStatMockTest::SetUpTestCase(void)
39 {
40 GTEST_LOG_(INFO) << "SetUpTestCase";
41 sys = make_shared<SystemMock>();
42 System::ins = sys;
43 }
44
TearDownTestCase(void)45 void FsStatMockTest::TearDownTestCase(void)
46 {
47 GTEST_LOG_(INFO) << "TearDownTestCase";
48 System::ins = nullptr;
49 sys = nullptr;
50 }
51
SetUp(void)52 void FsStatMockTest::SetUp(void)
53 {
54 GTEST_LOG_(INFO) << "SetUp";
55 }
56
TearDown(void)57 void FsStatMockTest::TearDown(void)
58 {
59 GTEST_LOG_(INFO) << "TearDown";
60 }
61
62 /**
63 * @tc.name: FsStatMockTest_GetLocation_001
64 * @tc.desc: Test function of GetLocation() interface for SUCCESS.
65 * @tc.size: MEDIUM
66 * @tc.type: FUNC
67 * @tc.level Level 1
68 */
69 HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize.Level1)
70 {
71 GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_001";
72
73 unique_ptr<StatEntity> statEntity;
74 unique_ptr<FsStat> fsStat;
75 statEntity = make_unique<StatEntity>();
76 statEntity->fileInfo_ = make_unique<FileInfo>();
77 statEntity->fileInfo_->isPath = true;
78 int length = 100;
79 string testPath = "/test/stat_path";
80 statEntity->fileInfo_->path = make_unique<char[]>(length);
81 strncpy_s(statEntity->fileInfo_->path.get(), length, testPath.c_str(), testPath.size());
82 statEntity->fileInfo_->path.get()[99] = '\0';
83 fsStat = make_unique<FsStat>(move(statEntity));
84
85 EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1));
86 EXPECT_EQ(fsStat->GetLocation(), 1);
87
88 GTEST_LOG_(INFO) << "FsStatMockTes-end FsStatMockTest_GetLocation_001";
89 }
90
91 /**
92 * @tc.name: FsStatMockTest_GetLocation_002
93 * @tc.desc: Test function of GetLocation() interface for SUCCESS.
94 * @tc.size: MEDIUM
95 * @tc.type: FUNC
96 * @tc.level Level 1
97 */
98 HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_002, testing::ext::TestSize.Level1)
99 {
100 GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_002";
101
102 unique_ptr<StatEntity> statEntity;
103 unique_ptr<FsStat> fsStat;
104 statEntity = make_unique<StatEntity>();
105 statEntity->fileInfo_ = make_unique<FileInfo>();
106 statEntity->fileInfo_->isPath = false;
107 const int fdValue = 3;
108 const bool isClosed = false;
109 statEntity->fileInfo_->fdg = make_unique<DistributedFS::FDGuard>(fdValue, isClosed);
110 fsStat = make_unique<FsStat>(move(statEntity));
111
112 EXPECT_CALL(*sys, fgetxattr(_, _, _, _)).WillOnce(Return(1));
113 EXPECT_EQ(fsStat->GetLocation(), 1);
114
115 GTEST_LOG_(INFO) << "FsStatMockTes-end FsStatMockTest_GetLocation_002";
116 }
117
118 }