• 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 
16 #include <filesystem>
17 #include <fstream>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <sys/xattr.h>
22 
23 #include "system_mock.h"
24 #include "xattr_core.h"
25 
26 namespace OHOS::FileManagement::ModuleFileIO::Test {
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 
31 class XattrCoreMockTest : public testing::Test {
32 public:
33     static filesystem::path tempFilePath;
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38     static inline shared_ptr<SystemMock> sys = nullptr;
39 };
40 
41 filesystem::path XattrCoreMockTest::tempFilePath;
42 
SetUpTestCase(void)43 void XattrCoreMockTest::SetUpTestCase(void)
44 {
45     GTEST_LOG_(INFO) << "SetUpTestCase";
46     tempFilePath = "/data/local/tmp/xattr_test_file.txt";
47     ofstream tempfile(tempFilePath);
48     tempfile << "Test content\n123\n456";
49     tempfile.close();
50     sys = make_shared<SystemMock>();
51     System::ins = sys;
52 }
53 
TearDownTestCase(void)54 void XattrCoreMockTest::TearDownTestCase(void)
55 {
56     GTEST_LOG_(INFO) << "TearDownTestCase";
57     filesystem::remove(tempFilePath);
58     System::ins = nullptr;
59     sys = nullptr;
60 }
61 
SetUp(void)62 void XattrCoreMockTest::SetUp(void)
63 {
64     GTEST_LOG_(INFO) << "SetUp";
65 }
66 
TearDown(void)67 void XattrCoreMockTest::TearDown(void)
68 {
69     GTEST_LOG_(INFO) << "TearDown";
70 }
71 
72 /**
73  * @tc.name: XattrCoreMockTest_DoSetXattr_001
74  * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED.
75  * @tc.size: MEDIUM
76  * @tc.type: FUNC
77  * @tc.level Level 1
78  */
79 HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_001, TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoSetXattr_001";
82 
83     string path = tempFilePath.string();
84     string key = "test_key";
85     string value = "test_value";
86 
87     EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(SetErrnoAndReturn(EIO, -1));
88     auto ret = XattrCore::DoSetXattr(path, key, value);
89     EXPECT_FALSE(ret.IsSuccess());
90 
91     GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoSetXattr_001";
92 }
93 
94 /**
95  * @tc.name: XattrCoreMockTest_DoSetXattr_002
96  * @tc.desc: Test function of XattrCore::DoSetXattr interface for SUCCESS.
97  * @tc.size: MEDIUM
98  * @tc.type: FUNC
99  * @tc.level Level 1
100  */
101 HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_002, TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoSetXattr_002";
104 
105     string path = tempFilePath.string();
106     string key = "test_key";
107     string value = "test_value";
108 
109     EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(0));
110     auto ret = XattrCore::DoSetXattr(path, key, value);
111     EXPECT_TRUE(ret.IsSuccess());
112 
113     GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoSetXattr_002";
114 }
115 
116 /**
117  * @tc.name: XattrCoreMockTest_DoGetXattr_001
118  * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS.
119  * @tc.size: MEDIUM
120  * @tc.type: FUNC
121  * @tc.level Level 1
122  */
123 HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_001, TestSize.Level1)
124 {
125     GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_001";
126 
127     string path = tempFilePath.string();
128     string key = "test_key";
129 
130     EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillRepeatedly(SetErrnoAndReturn(EIO, -1));
131     auto ret = XattrCore::DoGetXattr(path, key);
132     EXPECT_TRUE(ret.IsSuccess());
133 
134     GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_001";
135 }
136 
137 /**
138  * @tc.name: XattrCoreMockTest_DoGetXattr_002
139  * @tc.desc: Test function of XattrCore::DoGetXattr interface for FAILED.
140  * @tc.size: MEDIUM
141  * @tc.type: FUNC
142  * @tc.level Level 1
143  */
144 HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_002, TestSize.Level1)
145 {
146     GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_002";
147 
148     string path = tempFilePath.string();
149     string key = "test_key";
150 
151     EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(SetErrnoAndReturn(EIO, -1));
152     auto ret = XattrCore::DoGetXattr(path, key);
153     EXPECT_FALSE(ret.IsSuccess());
154 
155     GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_002";
156 }
157 
158 /**
159  * @tc.name: XattrCoreMockTest_DoGetXattr_003
160  * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS.
161  * @tc.size: MEDIUM
162  * @tc.type: FUNC
163  * @tc.level Level 1
164  */
165 HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_003, TestSize.Level1)
166 {
167     GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_003";
168 
169     string path = tempFilePath.string();
170     string key = "test_key";
171 
172     EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(Return(1));
173     auto ret = XattrCore::DoGetXattr(path, key);
174     EXPECT_TRUE(ret.IsSuccess());
175 
176     GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_003";
177 }
178 
179 } // namespace OHOS::FileManagement::ModuleFileIO::Test