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_cistatic const char *TEST_VALID_PATH = "/data/local/tmp/attr_test_dir"; 329762338dSopenharmony_cistatic const char *TEST_VALID_FILE = "/data/local/tmp/attr_test_dir/attr_test.txt"; 339762338dSopenharmony_cistatic const char *TEST_INVALID_PATH = "/data/local/abcd"; 349762338dSopenharmony_cistatic const int MAX_LEN = 128; 359762338dSopenharmony_ci 369762338dSopenharmony_ciclass AttrApiTest : public testing::Test { 379762338dSopenharmony_cipublic: 389762338dSopenharmony_ci static void SetUpTestCase(); 399762338dSopenharmony_ci static void TearDownTestCase(); 409762338dSopenharmony_ci void SetUp(); 419762338dSopenharmony_ci void TearDown(); 429762338dSopenharmony_ciprivate: 439762338dSopenharmony_ci}; 449762338dSopenharmony_civoid AttrApiTest::SetUp() 459762338dSopenharmony_ci{ 469762338dSopenharmony_ci int fd = -1; 479762338dSopenharmony_ci if (access(TEST_VALID_PATH, F_OK) == 0) { 489762338dSopenharmony_ci (void)remove(TEST_VALID_PATH); 499762338dSopenharmony_ci } 509762338dSopenharmony_ci (void)mkdir(TEST_VALID_PATH, S_IWUSR | S_IRUSR | S_IXUSR); 519762338dSopenharmony_ci fd = open(TEST_VALID_FILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 529762338dSopenharmony_ci close(fd); 539762338dSopenharmony_ci} 549762338dSopenharmony_civoid AttrApiTest::TearDown() 559762338dSopenharmony_ci{ 569762338dSopenharmony_ci (void)remove(TEST_VALID_FILE); 579762338dSopenharmony_ci if (access(TEST_VALID_PATH, F_OK) == 0) { 589762338dSopenharmony_ci (void)remove(TEST_VALID_PATH); 599762338dSopenharmony_ci } 609762338dSopenharmony_ci} 619762338dSopenharmony_civoid AttrApiTest::SetUpTestCase() 629762338dSopenharmony_ci{ 639762338dSopenharmony_ci} 649762338dSopenharmony_civoid AttrApiTest::TearDownTestCase() 659762338dSopenharmony_ci{ 669762338dSopenharmony_ci} 679762338dSopenharmony_ci 689762338dSopenharmony_ci/* 699762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_XATTR_0100 709762338dSopenharmony_ci * @tc.name : LsetAndListxattrValidFileTestSuccess_0001 719762338dSopenharmony_ci * @tc.desc : lsetxattr and listxattr test valid path and file extend properties success. 729762338dSopenharmony_ci * @tc.size : MediumTest 739762338dSopenharmony_ci * @tc.type : Function 749762338dSopenharmony_ci * @tc.level : Level 1 759762338dSopenharmony_ci */ 769762338dSopenharmony_ciHWTEST_F(AttrApiTest, ListxattrFileExtendPropertiesTest_0001, Function | MediumTest | Level1) 779762338dSopenharmony_ci{ 789762338dSopenharmony_ci char list[MAX_LEN] = { 0 }; 799762338dSopenharmony_ci const char *name = "user.path_attr"; 809762338dSopenharmony_ci const char *value = "path_attr_value"; 819762338dSopenharmony_ci size_t size = strlen(value); 829762338dSopenharmony_ci 839762338dSopenharmony_ci // set path xattr, success 849762338dSopenharmony_ci int ret = lsetxattr(TEST_VALID_PATH, name, value, size, 0); 859762338dSopenharmony_ci EXPECT_EQ(ret, 0); 869762338dSopenharmony_ci 879762338dSopenharmony_ci // get path xattr list, success 889762338dSopenharmony_ci size = listxattr(TEST_VALID_PATH, list, sizeof(list)); 899762338dSopenharmony_ci EXPECT_TRUE(size > 0); 909762338dSopenharmony_ci 919762338dSopenharmony_ci // set file xattr, success 929762338dSopenharmony_ci name = "user.file_attr"; 939762338dSopenharmony_ci value = "file_attr_value"; 949762338dSopenharmony_ci size = strlen(value); 959762338dSopenharmony_ci ret = lsetxattr(TEST_VALID_FILE, name, value, size, 0); 969762338dSopenharmony_ci EXPECT_EQ(ret, 0); 979762338dSopenharmony_ci 989762338dSopenharmony_ci // get file xattr list, success 999762338dSopenharmony_ci size = listxattr(TEST_VALID_FILE, list, sizeof(list)); 1009762338dSopenharmony_ci EXPECT_TRUE(size >= 0); 1019762338dSopenharmony_ci 1029762338dSopenharmony_ci // only get xattr size, success 1039762338dSopenharmony_ci size = listxattr(TEST_VALID_FILE, nullptr, 0); 1049762338dSopenharmony_ci EXPECT_TRUE(size >= 0); 1059762338dSopenharmony_ci} 1069762338dSopenharmony_ci 1079762338dSopenharmony_ci/* 1089762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_XATTR_0200 1099762338dSopenharmony_ci * @tc.name : LsetxattrInvalidPropertyFailed_0002 1109762338dSopenharmony_ci * @tc.desc : lsetxattr set invalid xattr failed, errno EOPNOTSUPP. 1119762338dSopenharmony_ci * @tc.size : MediumTest 1129762338dSopenharmony_ci * @tc.type : Function 1139762338dSopenharmony_ci * @tc.level : Level 2 1149762338dSopenharmony_ci */ 1159762338dSopenharmony_ciHWTEST_F(AttrApiTest, LsetxattrInvalidPropertyFailed_0002, Function | MediumTest | Level2) 1169762338dSopenharmony_ci{ 1179762338dSopenharmony_ci const char *name = "invalid_attr"; 1189762338dSopenharmony_ci const char *value = "invalid_attr_value"; 1199762338dSopenharmony_ci size_t size = strlen(value); 1209762338dSopenharmony_ci 1219762338dSopenharmony_ci // set invalid property 1229762338dSopenharmony_ci errno = 0; 1239762338dSopenharmony_ci int ret = lsetxattr(TEST_VALID_PATH, name, value, size, 0); 1249762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1259762338dSopenharmony_ci EXPECT_EQ(errno, EOPNOTSUPP); 1269762338dSopenharmony_ci} 1279762338dSopenharmony_ci 1289762338dSopenharmony_ci/* 1299762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_XATTR_0300 1309762338dSopenharmony_ci * @tc.name : LsetAndListxattrInvalidFileFailed_0003 1319762338dSopenharmony_ci * @tc.desc : lsetxattr and listxattr test use invalid path or file failed, errno ENOENT. 1329762338dSopenharmony_ci * @tc.size : MediumTest 1339762338dSopenharmony_ci * @tc.type : Function 1349762338dSopenharmony_ci * @tc.level : Level 2 1359762338dSopenharmony_ci */ 1369762338dSopenharmony_ciHWTEST_F(AttrApiTest, LsetAndListxattrInvalidFileFailed_0003, Function | MediumTest | Level2) 1379762338dSopenharmony_ci{ 1389762338dSopenharmony_ci char list[MAX_LEN] = { 0 }; 1399762338dSopenharmony_ci const char *name = "user.path_attr"; 1409762338dSopenharmony_ci const char *value = "path_attr_value"; 1419762338dSopenharmony_ci size_t size = strlen(value); 1429762338dSopenharmony_ci 1439762338dSopenharmony_ci errno = 0; 1449762338dSopenharmony_ci int ret = lsetxattr(TEST_INVALID_PATH, name, value, size, 0); 1459762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1469762338dSopenharmony_ci EXPECT_EQ(errno, ENOENT); 1479762338dSopenharmony_ci 1489762338dSopenharmony_ci errno = 0; 1499762338dSopenharmony_ci size = listxattr(TEST_INVALID_PATH, list, sizeof(list)); 1509762338dSopenharmony_ci EXPECT_EQ(size, -1); 1519762338dSopenharmony_ci EXPECT_EQ(errno, ENOENT); 1529762338dSopenharmony_ci} 1539762338dSopenharmony_ci 1549762338dSopenharmony_ci/* 1559762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_XATTR_0400 1569762338dSopenharmony_ci * @tc.name : RemovexattrRemoveNotExistAttrFailed_0004 1579762338dSopenharmony_ci * @tc.desc : Removexattr removes attribute that does not exist failed, errno ENOENT. 1589762338dSopenharmony_ci * @tc.size : MediumTest 1599762338dSopenharmony_ci * @tc.type : Function 1609762338dSopenharmony_ci * @tc.level : Level 2 1619762338dSopenharmony_ci */ 1629762338dSopenharmony_ciHWTEST_F(AttrApiTest, RemovexattrRemoveNotExistAttrFailed_0004, Function | MediumTest | Level2) 1639762338dSopenharmony_ci{ 1649762338dSopenharmony_ci const char *name = "user.invalid_attr"; 1659762338dSopenharmony_ci errno = 0; 1669762338dSopenharmony_ci int ret = removexattr(TEST_VALID_PATH, name); 1679762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1689762338dSopenharmony_ci EXPECT_EQ(errno, ENODATA); 1699762338dSopenharmony_ci} 1709762338dSopenharmony_ci 1719762338dSopenharmony_ci/* 1729762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_XATTR_0500 1739762338dSopenharmony_ci * @tc.name : RemovexattrExistAttrSuccess_0005 1749762338dSopenharmony_ci * @tc.desc : Removexattr removes exist attribute success. 1759762338dSopenharmony_ci * @tc.size : MediumTest 1769762338dSopenharmony_ci * @tc.type : Function 1779762338dSopenharmony_ci * @tc.level : Level 1 1789762338dSopenharmony_ci */ 1799762338dSopenharmony_ciHWTEST_F(AttrApiTest, RemovexattrExistAttrSuccess_0005, Function | MediumTest | Level1) 1809762338dSopenharmony_ci{ 1819762338dSopenharmony_ci const char *name = "user.path_attr"; 1829762338dSopenharmony_ci const char *value = "path_attr_value"; 1839762338dSopenharmony_ci size_t size = strlen(value); 1849762338dSopenharmony_ci 1859762338dSopenharmony_ci // set path xattr, success 1869762338dSopenharmony_ci int ret = lsetxattr(TEST_VALID_PATH, name, value, size, 0); 1879762338dSopenharmony_ci EXPECT_EQ(ret, 0); 1889762338dSopenharmony_ci 1899762338dSopenharmony_ci // remove exist xattr success 1909762338dSopenharmony_ci ret = removexattr(TEST_VALID_PATH, name); 1919762338dSopenharmony_ci EXPECT_EQ(ret, 0); 1929762338dSopenharmony_ci}