1/* 2 * Copyright (C) 2024 HiHope Open Source Organization. 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 <cerrno> 17#include <cstdio> 18#include <cstdlib> 19#include <string> 20#include <vector> 21#include <fcntl.h> 22#include <unistd.h> 23#include <gtest/gtest.h> 24#include <sys/file.h> 25#include <sys/stat.h> 26#include <sys/types.h> 27#include <sys/xattr.h> 28#include "securec.h" 29 30using namespace testing::ext; 31using namespace std; 32 33static const char *TEST_FILE = "/data/local/tmp/fremovexattr.txt"; 34mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 35static const char *XATTR_NAME = "user.author"; 36static const char *AUTHOR_NAME = "sun"; 37size_t g_length = strlen(AUTHOR_NAME) + 1; 38 39class FremovexattrApiTest : public testing::Test { 40public: 41 static void SetUpTestCase(); 42 static void TearDownTestCase(); 43 void SetUp(); 44 void TearDown(); 45private: 46}; 47void FremovexattrApiTest::SetUp() 48{ 49} 50void FremovexattrApiTest::TearDown() 51{ 52} 53void FremovexattrApiTest::SetUpTestCase() 54{ 55} 56void FremovexattrApiTest::TearDownTestCase() 57{ 58} 59 60/* 61 * @tc.number : SUB_KERNEL_SYSCALL_FREMOVEXATTR_0100 62 * @tc.name : FremovexattrValidFdAttrSuccess_0001 63 * @tc.desc : fremovexattr remove valid file attribute success. 64 * @tc.size : MediumTest 65 * @tc.type : Function 66 * @tc.level : Level 1 67 */ 68HWTEST_F(FremovexattrApiTest, FremovexattrValidFdAttrSuccess_0001, Function | MediumTest | Level1) 69{ 70 ssize_t ret = -1; 71 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644); 72 EXPECT_TRUE(fd > 0); 73 ret = setxattr(TEST_FILE, XATTR_NAME, AUTHOR_NAME, g_length, 0); 74 EXPECT_EQ(ret, 0); 75 76 ret = fremovexattr(fd, XATTR_NAME); 77 EXPECT_EQ(ret, 0); 78 79 close(fd); 80 remove(TEST_FILE); 81} 82 83/* 84 * @tc.number : SUB_KERNEL_SYSCALL_FREMOVEXATTR_0200 85 * @tc.name : FremovexattrInvalidFdAttrFail_0002 86 * @tc.desc : fremovexattr remove invalid file attribute fail, errno EBADF. 87 * @tc.size : MediumTest 88 * @tc.type : Function 89 * @tc.level : Level 2 90 */ 91HWTEST_F(FremovexattrApiTest, FremovexattrInvalidFdAttrFail_0002, Function | MediumTest | Level2) 92{ 93 int invalidFd = -1; 94 errno = 0; 95 ssize_t ret = fremovexattr(invalidFd, XATTR_NAME); 96 EXPECT_EQ(ret, -1); 97 EXPECT_EQ(errno, EBADF); 98} 99 100/* 101 * @tc.number : SUB_KERNEL_SYSCALL_FREMOVEXATTR_0300 102 * @tc.name : FremovexattrRemoveNonExistAttrFail_0003 103 * @tc.desc : fremovexattr remove file non-exist attribute fail, errno ENODATA. 104 * @tc.size : MediumTest 105 * @tc.type : Function 106 * @tc.level : Level 2 107 */ 108HWTEST_F(FremovexattrApiTest, FremovexattrRemoveNonExistAttrFail_0003, Function | MediumTest | Level2) 109{ 110 ssize_t ret = -1; 111 int fd = open(TEST_FILE, O_RDONLY | O_CREAT, MODE_0644); 112 EXPECT_TRUE(fd > 0); 113 114 errno = 0; 115 ret = fremovexattr(fd, XATTR_NAME); 116 EXPECT_EQ(ret, -1); 117 EXPECT_EQ(errno, ENODATA); 118 119 close(fd); 120 remove(TEST_FILE); 121} 122