• 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 <gtest/gtest.h>
20 #include <sys/xattr.h>
21 
22 #include "xattr_core.h"
23 
24 #define MAX_LEN 4096
25 
26 namespace OHOS::FileManagement::ModuleFileIO::Test {
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 
31 class XattrCoreTest : 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 };
39 
40 filesystem::path XattrCoreTest::tempFilePath;
41 
SetUpTestCase(void)42 void XattrCoreTest::SetUpTestCase(void)
43 {
44     tempFilePath = "/data/local/tmp/xattr_test_file.txt";
45     ofstream tempfile(tempFilePath);
46     tempfile << "Test content\n123\n456";
47     tempfile.close();
48     GTEST_LOG_(INFO) << "SetUpTestCase";
49 }
50 
TearDownTestCase(void)51 void XattrCoreTest::TearDownTestCase(void)
52 {
53     filesystem::remove(tempFilePath);
54     GTEST_LOG_(INFO) << "TearDownTestCase";
55 }
56 
SetUp(void)57 void XattrCoreTest::SetUp(void)
58 {
59     GTEST_LOG_(INFO) << "SetUp";
60 }
61 
TearDown(void)62 void XattrCoreTest::TearDown(void)
63 {
64     GTEST_LOG_(INFO) << "TearDown";
65 }
66 
67 /**
68  * @tc.name: XattrCoreTest_DoSetXattr_001
69  * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED.
70  * @tc.size: MEDIUM
71  * @tc.type: FUNC
72  * @tc.level Level 1
73  */
74 HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_001, testing::ext::TestSize.Level1)
75 {
76     GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoSetXattr_001";
77 
78     string path = tempFilePath.string();
79     std::string key = "test_key";
80     std::string value(MAX_LEN + 1, 'a');
81 
82     auto ret = XattrCore::DoSetXattr(path, key, value);
83 
84     EXPECT_FALSE(ret.IsSuccess());
85     auto err = ret.GetError();
86     int errCode = err.GetErrNo();
87     EXPECT_EQ(errCode, 13900020);
88     auto msg = err.GetErrMsg();
89     EXPECT_EQ(msg, "Invalid argument");
90 
91     GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoSetXattr_001";
92 }
93 
94 /**
95  * @tc.name: XattrCoreTest_DoSetXattr_002
96  * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED.
97  * @tc.size: MEDIUM
98  * @tc.type: FUNC
99  * @tc.level Level 1
100  */
101 HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_002, testing::ext::TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoSetXattr_002";
104 
105     string path = tempFilePath.string();
106     std::string key(MAX_LEN + 1, 'a');
107     std::string value = "test_value";
108 
109     auto ret = XattrCore::DoSetXattr(path, key, value);
110 
111     EXPECT_FALSE(ret.IsSuccess());
112     auto err = ret.GetError();
113     int errCode = err.GetErrNo();
114     EXPECT_EQ(errCode, 13900020);
115     auto msg = err.GetErrMsg();
116     EXPECT_EQ(msg, "Invalid argument");
117 
118     GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoSetXattr_002";
119 }
120 
121 /**
122  * @tc.name: XattrCoreTest_DoGetXattr_001
123  * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS.
124  * @tc.size: MEDIUM
125  * @tc.type: FUNC
126  * @tc.level Level 1
127  */
128 HWTEST_F(XattrCoreTest, XattrCoreTest_DoGetXattr_001, testing::ext::TestSize.Level1)
129 {
130     GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoGetXattr_001";
131 
132     std::string path = "/data/local/tmp/nonexistent_file";
133     string key = "test_key";
134 
135     auto ret = XattrCore::DoGetXattr(path, key);
136 
137     ASSERT_TRUE(ret.IsSuccess());
138     EXPECT_EQ(ret.GetData().value(), "");
139 
140     GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoGetXattr_001";
141 }
142 
143 } // namespace OHOS::FileManagement::ModuleFileIO::Test