19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cstdio>
179762338dSopenharmony_ci#include <cstdlib>
189762338dSopenharmony_ci#include <fcntl.h>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <unistd.h>
219762338dSopenharmony_ci#include <vector>
229762338dSopenharmony_ci#include <gtest/gtest.h>
239762338dSopenharmony_ci#include <sys/stat.h>
249762338dSopenharmony_ci#include <sys/types.h>
259762338dSopenharmony_ci#include <sys/xattr.h>
269762338dSopenharmony_ci#include "securec.h"
279762338dSopenharmony_ci
289762338dSopenharmony_ciusing namespace testing::ext;
299762338dSopenharmony_ciusing namespace std;
309762338dSopenharmony_ci
319762338dSopenharmony_ciclass LremovexattrApiTest : public testing::Test {
329762338dSopenharmony_cipublic:
339762338dSopenharmony_ci    static void SetUpTestCase();
349762338dSopenharmony_ci    static void TearDownTestCase();
359762338dSopenharmony_ci    void SetUp();
369762338dSopenharmony_ci    void TearDown();
379762338dSopenharmony_ciprivate:
389762338dSopenharmony_ci};
399762338dSopenharmony_civoid LremovexattrApiTest::SetUp()
409762338dSopenharmony_ci{
419762338dSopenharmony_ci}
429762338dSopenharmony_civoid LremovexattrApiTest::TearDown()
439762338dSopenharmony_ci{
449762338dSopenharmony_ci}
459762338dSopenharmony_civoid LremovexattrApiTest::SetUpTestCase()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_civoid LremovexattrApiTest::TearDownTestCase()
499762338dSopenharmony_ci{
509762338dSopenharmony_ci}
519762338dSopenharmony_ci
529762338dSopenharmony_cistatic const char* OPEN_API_TEST_FILE = "/data/local/tmp";
539762338dSopenharmony_ciconst int BUFFER_SIZE = 128;
549762338dSopenharmony_ci
559762338dSopenharmony_ci/*
569762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0100
579762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteExtentAttSuccess_0001
589762338dSopenharmony_ci * @tc.desc   : delete the specified path extension attribute success.
599762338dSopenharmony_ci * @tc.size   : MediumTest
609762338dSopenharmony_ci * @tc.type   : Function
619762338dSopenharmony_ci * @tc.level  : Level 1
629762338dSopenharmony_ci */
639762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteExtentAttSuccess_0001, Function | MediumTest | Level1)
649762338dSopenharmony_ci{
659762338dSopenharmony_ci    const char* name = "user.example";
669762338dSopenharmony_ci    const char* setValue = "Hello World";
679762338dSopenharmony_ci    ssize_t size = strlen(setValue) + 1;
689762338dSopenharmony_ci    int result = setxattr(OPEN_API_TEST_FILE, name, setValue, size, 0);
699762338dSopenharmony_ci    EXPECT_TRUE(result != -1);
709762338dSopenharmony_ci
719762338dSopenharmony_ci    int ret = lremovexattr(OPEN_API_TEST_FILE, name);
729762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
739762338dSopenharmony_ci}
749762338dSopenharmony_ci
759762338dSopenharmony_ci/*
769762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0200
779762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteNonExtentAttrFailed_0002
789762338dSopenharmony_ci * @tc.desc   : delete the specified path non extension attribute failed.
799762338dSopenharmony_ci * @tc.size   : MediumTest
809762338dSopenharmony_ci * @tc.type   : Function
819762338dSopenharmony_ci * @tc.level  : Level 2
829762338dSopenharmony_ci */
839762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonExtentAttrFailed_0002, Function | MediumTest | Level2)
849762338dSopenharmony_ci{
859762338dSopenharmony_ci    const char* name = "user.example";
869762338dSopenharmony_ci    errno = 0;
879762338dSopenharmony_ci    int ret = lremovexattr(OPEN_API_TEST_FILE, name);
889762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
899762338dSopenharmony_ci    EXPECT_TRUE(errno == ENODATA);
909762338dSopenharmony_ci}
919762338dSopenharmony_ci
929762338dSopenharmony_ci/*
939762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0300
949762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteNonPathExtensionAttributeFailed_0003
959762338dSopenharmony_ci * @tc.desc   : delete the non path extension attribute failed.
969762338dSopenharmony_ci * @tc.size   : MediumTest
979762338dSopenharmony_ci * @tc.type   : Function
989762338dSopenharmony_ci * @tc.level  : Level 2
999762338dSopenharmony_ci */
1009762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonPathExtensionAttributeFailed_0003, Function | MediumTest | Level2)
1019762338dSopenharmony_ci{
1029762338dSopenharmony_ci    const char* name = "user.example";
1039762338dSopenharmony_ci    char tmpPath[BUFFER_SIZE] = {0};
1049762338dSopenharmony_ci    int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/non_existing_dir", OPEN_API_TEST_FILE);
1059762338dSopenharmony_ci    EXPECT_TRUE(num > 0);
1069762338dSopenharmony_ci
1079762338dSopenharmony_ci    errno = 0;
1089762338dSopenharmony_ci    int ret = lremovexattr(tmpPath, name);
1099762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1109762338dSopenharmony_ci    EXPECT_TRUE(errno == ENOENT);
1119762338dSopenharmony_ci}
1129762338dSopenharmony_ci
1139762338dSopenharmony_ci/*
1149762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0400
1159762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteFileExtentAttrSuccess_0004
1169762338dSopenharmony_ci * @tc.desc   : delete the specified file extension attribute success.
1179762338dSopenharmony_ci * @tc.size   : MediumTest
1189762338dSopenharmony_ci * @tc.type   : Function
1199762338dSopenharmony_ci * @tc.level  : Level 1
1209762338dSopenharmony_ci */
1219762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteFileExtentAttrSuccess_0004, Function | MediumTest | Level1)
1229762338dSopenharmony_ci{
1239762338dSopenharmony_ci    char tmpPath[BUFFER_SIZE] = {0};
1249762338dSopenharmony_ci    int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/existing_file.txt", OPEN_API_TEST_FILE);
1259762338dSopenharmony_ci    EXPECT_TRUE(num > 0);
1269762338dSopenharmony_ci
1279762338dSopenharmony_ci    int fd = open(tmpPath, O_WRONLY | O_CREAT, 0644);
1289762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1299762338dSopenharmony_ci    close(fd);
1309762338dSopenharmony_ci
1319762338dSopenharmony_ci    const char* name = "user.example";
1329762338dSopenharmony_ci    const char* setValue = "Hello World";
1339762338dSopenharmony_ci    ssize_t size = strlen(setValue) + 1;
1349762338dSopenharmony_ci    int result = setxattr(tmpPath, name, setValue, size, 0);
1359762338dSopenharmony_ci    EXPECT_TRUE(result != -1);
1369762338dSopenharmony_ci
1379762338dSopenharmony_ci    int ret = lremovexattr(tmpPath, name);
1389762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
1399762338dSopenharmony_ci
1409762338dSopenharmony_ci    remove(tmpPath);
1419762338dSopenharmony_ci}
1429762338dSopenharmony_ci
1439762338dSopenharmony_ci/*
1449762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0500
1459762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteSNonExtentAttrFailed_0005
1469762338dSopenharmony_ci * @tc.desc   : delete the specified file non extension attribute failed.
1479762338dSopenharmony_ci * @tc.size   : MediumTest
1489762338dSopenharmony_ci * @tc.type   : Function
1499762338dSopenharmony_ci * @tc.level  : Level 2
1509762338dSopenharmony_ci */
1519762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteSNonExtentAttrFailed_0005, Function | MediumTest | Level2)
1529762338dSopenharmony_ci{
1539762338dSopenharmony_ci    const char* name = "user.example";
1549762338dSopenharmony_ci
1559762338dSopenharmony_ci    char tmpPath[BUFFER_SIZE] = {0};
1569762338dSopenharmony_ci    int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/existing_file.txt", OPEN_API_TEST_FILE);
1579762338dSopenharmony_ci    EXPECT_TRUE(num > 0);
1589762338dSopenharmony_ci
1599762338dSopenharmony_ci    int fd = open(tmpPath, O_WRONLY | O_CREAT, 0644);
1609762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1619762338dSopenharmony_ci    close(fd);
1629762338dSopenharmony_ci
1639762338dSopenharmony_ci    errno = 0;
1649762338dSopenharmony_ci    int ret = lremovexattr(tmpPath, name);
1659762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1669762338dSopenharmony_ci    EXPECT_TRUE(errno == ENODATA);
1679762338dSopenharmony_ci    remove(tmpPath);
1689762338dSopenharmony_ci}
1699762338dSopenharmony_ci
1709762338dSopenharmony_ci/*
1719762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0600
1729762338dSopenharmony_ci * @tc.name   : LremovexattrDeleteNonFileExtensionAttributeFailed_0006
1739762338dSopenharmony_ci * @tc.desc   : delete the non file extension attribute failed.
1749762338dSopenharmony_ci * @tc.size   : MediumTest
1759762338dSopenharmony_ci * @tc.type   : Function
1769762338dSopenharmony_ci * @tc.level  : Level 2
1779762338dSopenharmony_ci */
1789762338dSopenharmony_ciHWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonFileExtensionAttributeFailed_0006, Function | MediumTest | Level2)
1799762338dSopenharmony_ci{
1809762338dSopenharmony_ci    const char* name = "user.example";
1819762338dSopenharmony_ci    char tmpPath[BUFFER_SIZE] = {0};
1829762338dSopenharmony_ci    int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/non_existing_file.txt", OPEN_API_TEST_FILE);
1839762338dSopenharmony_ci    EXPECT_TRUE(num > 0);
1849762338dSopenharmony_ci
1859762338dSopenharmony_ci    errno = 0;
1869762338dSopenharmony_ci    int ret = lremovexattr(tmpPath, name);
1879762338dSopenharmony_ci    EXPECT_TRUE(ret == -1);
1889762338dSopenharmony_ci    EXPECT_TRUE(errno == ENOENT);
1899762338dSopenharmony_ci}
1909762338dSopenharmony_ci
191