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 <string> 199762338dSopenharmony_ci#include <vector> 209762338dSopenharmony_ci#include <fcntl.h> 219762338dSopenharmony_ci#include <unistd.h> 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_cistatic const char* TEST_FILE = "/data/local/tmp/test_file.txt"; 329762338dSopenharmony_cistatic const int BUFFER_SIZE = 128; 339762338dSopenharmony_cistatic const int PATH_MAX_LEN = 128; 349762338dSopenharmony_ci 359762338dSopenharmony_ciclass GetxattrApiTest : public testing::Test { 369762338dSopenharmony_cipublic: 379762338dSopenharmony_ci static void SetUpTestCase(); 389762338dSopenharmony_ci static void TearDownTestCase(); 399762338dSopenharmony_ci void SetUp(); 409762338dSopenharmony_ci void TearDown(); 419762338dSopenharmony_ciprivate: 429762338dSopenharmony_ci}; 439762338dSopenharmony_civoid GetxattrApiTest::SetUp() 449762338dSopenharmony_ci{ 459762338dSopenharmony_ci int fd = open(TEST_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 469762338dSopenharmony_ci close(fd); 479762338dSopenharmony_ci} 489762338dSopenharmony_civoid GetxattrApiTest::TearDown() 499762338dSopenharmony_ci{ 509762338dSopenharmony_ci (void)remove(TEST_FILE); 519762338dSopenharmony_ci} 529762338dSopenharmony_civoid GetxattrApiTest::SetUpTestCase() 539762338dSopenharmony_ci{ 549762338dSopenharmony_ci} 559762338dSopenharmony_civoid GetxattrApiTest::TearDownTestCase() 569762338dSopenharmony_ci{ 579762338dSopenharmony_ci} 589762338dSopenharmony_ci 599762338dSopenharmony_ci/* 609762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_GETXATTR_0100 619762338dSopenharmony_ci * @tc.name : GetxattrGetExistAttrSuccess_0001 629762338dSopenharmony_ci * @tc.desc : Get the exist file attribute value success. 639762338dSopenharmony_ci * @tc.size : MediumTest 649762338dSopenharmony_ci * @tc.type : Function 659762338dSopenharmony_ci * @tc.level : Level 1 669762338dSopenharmony_ci */ 679762338dSopenharmony_ciHWTEST_F(GetxattrApiTest, GetxattrGetExistAttrSuccess_0001, Function | MediumTest | Level1) 689762338dSopenharmony_ci{ 699762338dSopenharmony_ci ssize_t size; 709762338dSopenharmony_ci const char* name = "user.example"; 719762338dSopenharmony_ci const char* setValue = "Hello World"; 729762338dSopenharmony_ci char value[PATH_MAX_LEN] = { 0 }; 739762338dSopenharmony_ci int ret = setxattr(TEST_FILE, name, setValue, strlen(setValue), 0); 749762338dSopenharmony_ci EXPECT_EQ(ret, 0); 759762338dSopenharmony_ci 769762338dSopenharmony_ci size = getxattr(TEST_FILE, name, value, sizeof(value) - 1); 779762338dSopenharmony_ci EXPECT_EQ(size, strlen(setValue)); 789762338dSopenharmony_ci EXPECT_STREQ(value, setValue); 799762338dSopenharmony_ci} 809762338dSopenharmony_ci 819762338dSopenharmony_ci/* 829762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_GETXATTR_0200 839762338dSopenharmony_ci * @tc.name : GetxattrGetUnknownAttrValueFailed_0002 849762338dSopenharmony_ci * @tc.desc : Get the unknown file attribute value failed, errno ENODATA. 859762338dSopenharmony_ci * @tc.size : MediumTest 869762338dSopenharmony_ci * @tc.type : Function 879762338dSopenharmony_ci * @tc.level : Level 2 889762338dSopenharmony_ci */ 899762338dSopenharmony_ciHWTEST_F(GetxattrApiTest, GetxattrGetUnknownAttrValueFailed_0002, Function | MediumTest | Level2) 909762338dSopenharmony_ci{ 919762338dSopenharmony_ci const char* name = "user.example1"; 929762338dSopenharmony_ci char value[BUFFER_SIZE] = {0}; 939762338dSopenharmony_ci errno = 0; 949762338dSopenharmony_ci ssize_t size = getxattr(TEST_FILE, name, value, sizeof(value) - 1); 959762338dSopenharmony_ci EXPECT_TRUE(size == -1); 969762338dSopenharmony_ci EXPECT_TRUE(errno == ENODATA); 979762338dSopenharmony_ci}