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 <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <vector>
219762338dSopenharmony_ci#include <fcntl.h>
229762338dSopenharmony_ci#include <unistd.h>
239762338dSopenharmony_ci#include <gtest/gtest.h>
249762338dSopenharmony_ci#include <sys/file.h>
259762338dSopenharmony_ci#include <sys/stat.h>
269762338dSopenharmony_ci#include <sys/types.h>
279762338dSopenharmony_ci#include <sys/xattr.h>
289762338dSopenharmony_ci#include "securec.h"
299762338dSopenharmony_ci
309762338dSopenharmony_ciusing namespace testing::ext;
319762338dSopenharmony_ciusing namespace std;
329762338dSopenharmony_ci
339762338dSopenharmony_ci#define BUFFER_SIZE 128
349762338dSopenharmony_cistatic const char *TEST_FILE = "/data/local/tmp/fgetxattr.txt";
359762338dSopenharmony_cimode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
369762338dSopenharmony_cimode_t MODE_0400 = S_IRUSR;
379762338dSopenharmony_cistatic const char *XATTR_NAME = "user.author";
389762338dSopenharmony_cistatic const char *AUTHOR_NAME = "sun";
399762338dSopenharmony_cisize_t g_length = strlen(AUTHOR_NAME) + 1;
409762338dSopenharmony_ci
419762338dSopenharmony_ciclass FgetxattrApiTest : public testing::Test {
429762338dSopenharmony_cipublic:
439762338dSopenharmony_ci    static void SetUpTestCase();
449762338dSopenharmony_ci    static void TearDownTestCase();
459762338dSopenharmony_ci    void SetUp();
469762338dSopenharmony_ci    void TearDown();
479762338dSopenharmony_ciprivate:
489762338dSopenharmony_ci};
499762338dSopenharmony_civoid FgetxattrApiTest::SetUp()
509762338dSopenharmony_ci{
519762338dSopenharmony_ci}
529762338dSopenharmony_civoid FgetxattrApiTest::TearDown()
539762338dSopenharmony_ci{
549762338dSopenharmony_ci}
559762338dSopenharmony_civoid FgetxattrApiTest::SetUpTestCase()
569762338dSopenharmony_ci{
579762338dSopenharmony_ci}
589762338dSopenharmony_civoid FgetxattrApiTest::TearDownTestCase()
599762338dSopenharmony_ci{
609762338dSopenharmony_ci}
619762338dSopenharmony_ci
629762338dSopenharmony_ci/*
639762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_FGETXATTR_0100
649762338dSopenharmony_ci * @tc.name   : FgetxattrValidFdAttrSuccess_0001
659762338dSopenharmony_ci * @tc.desc   : fgetxattr get valid file attribute success.
669762338dSopenharmony_ci * @tc.size   : MediumTest
679762338dSopenharmony_ci * @tc.type   : Function
689762338dSopenharmony_ci * @tc.level  : Level 1
699762338dSopenharmony_ci */
709762338dSopenharmony_ciHWTEST_F(FgetxattrApiTest, FgetxattrValidFdAttrSuccess_0001, Function | MediumTest | Level1)
719762338dSopenharmony_ci{
729762338dSopenharmony_ci    ssize_t ret = -1;
739762338dSopenharmony_ci    char buf[BUFFER_SIZE] = {0};
749762338dSopenharmony_ci    int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
759762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
769762338dSopenharmony_ci    ret = setxattr(TEST_FILE, XATTR_NAME, AUTHOR_NAME, g_length, 0);
779762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
789762338dSopenharmony_ci    ret = fgetxattr(fd, XATTR_NAME, buf, g_length);
799762338dSopenharmony_ci    EXPECT_EQ(ret, g_length);
809762338dSopenharmony_ci    EXPECT_STREQ(buf, AUTHOR_NAME);
819762338dSopenharmony_ci
829762338dSopenharmony_ci    close(fd);
839762338dSopenharmony_ci    remove(TEST_FILE);
849762338dSopenharmony_ci}
859762338dSopenharmony_ci
869762338dSopenharmony_ci/*
879762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_FGETXATTR_0200
889762338dSopenharmony_ci * @tc.name   : FgetxattrInvalidFdAttrFail_0002
899762338dSopenharmony_ci * @tc.desc   : fgetxattr get invalid fd attribute fail, errno EBADF.
909762338dSopenharmony_ci * @tc.size   : MediumTest
919762338dSopenharmony_ci * @tc.type   : Function
929762338dSopenharmony_ci * @tc.level  : Level 2
939762338dSopenharmony_ci */
949762338dSopenharmony_ciHWTEST_F(FgetxattrApiTest, FgetxattrInvalidFdAttrFail_0002, Function | MediumTest | Level2)
959762338dSopenharmony_ci{
969762338dSopenharmony_ci    ssize_t ret = -1;
979762338dSopenharmony_ci    int invalidFd = -1;
989762338dSopenharmony_ci    char buf[BUFFER_SIZE] = {0};
999762338dSopenharmony_ci    errno = 0;
1009762338dSopenharmony_ci    ret = fgetxattr(invalidFd, XATTR_NAME, buf, sizeof(buf));
1019762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1029762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1039762338dSopenharmony_ci}
1049762338dSopenharmony_ci
1059762338dSopenharmony_ci/*
1069762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_FGETXATTR_0300
1079762338dSopenharmony_ci * @tc.name   : FgetxattrNonExistFileAttrFail_0003
1089762338dSopenharmony_ci * @tc.desc   : fgetxattr get non-exist file attribute fail, errno ENODATA.
1099762338dSopenharmony_ci * @tc.size   : MediumTest
1109762338dSopenharmony_ci * @tc.type   : Function
1119762338dSopenharmony_ci * @tc.level  : Level 2
1129762338dSopenharmony_ci */
1139762338dSopenharmony_ciHWTEST_F(FgetxattrApiTest, FgetxattrNonExistFileAttrFail_0003, Function | MediumTest | Level2)
1149762338dSopenharmony_ci{
1159762338dSopenharmony_ci    ssize_t ret = -1;
1169762338dSopenharmony_ci    char buf[BUFFER_SIZE] = {0};
1179762338dSopenharmony_ci    int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
1189762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1199762338dSopenharmony_ci
1209762338dSopenharmony_ci    errno = 0;
1219762338dSopenharmony_ci    ret = fgetxattr(fd, XATTR_NAME, buf, g_length);
1229762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1239762338dSopenharmony_ci    EXPECT_EQ(errno, ENODATA);
1249762338dSopenharmony_ci
1259762338dSopenharmony_ci    close(fd);
1269762338dSopenharmony_ci    remove(TEST_FILE);
1279762338dSopenharmony_ci}
1289762338dSopenharmony_ci
1299762338dSopenharmony_ci/*
1309762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_FGETXATTR_0400
1319762338dSopenharmony_ci * @tc.name   : FgetxattrGetShorterAttrFail_0004
1329762338dSopenharmony_ci * @tc.desc   : fgetxattr get file specific attribute length short than actual fail, errno ERANGE.
1339762338dSopenharmony_ci * @tc.size   : MediumTest
1349762338dSopenharmony_ci * @tc.type   : Function
1359762338dSopenharmony_ci * @tc.level  : Level 2
1369762338dSopenharmony_ci */
1379762338dSopenharmony_ciHWTEST_F(FgetxattrApiTest, FgetxattrAttrFail_0004, Function | MediumTest | Level2)
1389762338dSopenharmony_ci{
1399762338dSopenharmony_ci    ssize_t ret = -1;
1409762338dSopenharmony_ci    char buf[BUFFER_SIZE] = {0};
1419762338dSopenharmony_ci    int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
1429762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1439762338dSopenharmony_ci    ret = setxattr(TEST_FILE, XATTR_NAME, AUTHOR_NAME, g_length, 0);
1449762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
1459762338dSopenharmony_ci    errno = 0;
1469762338dSopenharmony_ci    ret = fgetxattr(fd, XATTR_NAME, buf, 1);
1479762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1489762338dSopenharmony_ci    EXPECT_STRNE(buf, AUTHOR_NAME);
1499762338dSopenharmony_ci    EXPECT_EQ(errno, ERANGE);
1509762338dSopenharmony_ci
1519762338dSopenharmony_ci    close(fd);
1529762338dSopenharmony_ci    remove(TEST_FILE);
1539762338dSopenharmony_ci}
154