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 LsetxattrApiTest : 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 LsetxattrApiTest::SetUp() 409762338dSopenharmony_ci{ 419762338dSopenharmony_ci} 429762338dSopenharmony_civoid LsetxattrApiTest::TearDown() 439762338dSopenharmony_ci{ 449762338dSopenharmony_ci} 459762338dSopenharmony_civoid LsetxattrApiTest::SetUpTestCase() 469762338dSopenharmony_ci{ 479762338dSopenharmony_ci} 489762338dSopenharmony_civoid LsetxattrApiTest::TearDownTestCase() 499762338dSopenharmony_ci{ 509762338dSopenharmony_ci} 519762338dSopenharmony_ci 529762338dSopenharmony_ciint SetExtendedAttribute(const char* linkPath, const char* attrName, const char* attrValue, int flags = 0) 539762338dSopenharmony_ci{ 549762338dSopenharmony_ci size_t attrSize = strlen(attrValue) + 1; 559762338dSopenharmony_ci int ret = lsetxattr(linkPath, attrName, attrValue, attrSize, flags); 569762338dSopenharmony_ci if (ret == -1) { 579762338dSopenharmony_ci return -1; 589762338dSopenharmony_ci } 599762338dSopenharmony_ci return 0; 609762338dSopenharmony_ci} 619762338dSopenharmony_ci 629762338dSopenharmony_ciint GetExtendedAttribute(const char* linkPath, const char* attrName, char* buffer, size_t bufferSize) 639762338dSopenharmony_ci{ 649762338dSopenharmony_ci ssize_t ret = lgetxattr(linkPath, attrName, buffer, bufferSize); 659762338dSopenharmony_ci if (ret == -1) { 669762338dSopenharmony_ci return -1; 679762338dSopenharmony_ci } 689762338dSopenharmony_ci buffer[ret] = '\0'; 699762338dSopenharmony_ci return 0; 709762338dSopenharmony_ci} 719762338dSopenharmony_ci 729762338dSopenharmony_ciint RemoveExtendedAttribute(const char* linkPath, const char* attrName) 739762338dSopenharmony_ci{ 749762338dSopenharmony_ci int ret = lremovexattr(linkPath, attrName); 759762338dSopenharmony_ci if (ret == -1) { 769762338dSopenharmony_ci return -1; 779762338dSopenharmony_ci } 789762338dSopenharmony_ci return 0; 799762338dSopenharmony_ci} 809762338dSopenharmony_ci 819762338dSopenharmony_cistatic const char* OPEN_API_TEST_FILE = "/data/local/tmp"; 829762338dSopenharmony_ciconst int BUFFER_SIZE = 128; 839762338dSopenharmony_ci 849762338dSopenharmony_ci/* 859762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0100 869762338dSopenharmony_ci * @tc.name : LsetxattrSetExtAttrSuccess_0001 879762338dSopenharmony_ci * @tc.desc : set file extended attribute success. 889762338dSopenharmony_ci * @tc.size : MediumTest 899762338dSopenharmony_ci * @tc.type : Function 909762338dSopenharmony_ci * @tc.level : Level 1 919762338dSopenharmony_ci */ 929762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetExtAttrSuccess_0001, Function | MediumTest | Level1) 939762338dSopenharmony_ci{ 949762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 959762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 969762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 979762338dSopenharmony_ci EXPECT_TRUE(num > 0); 989762338dSopenharmony_ci 999762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 1009762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 1019762338dSopenharmony_ci close(fd); 1029762338dSopenharmony_ci 1039762338dSopenharmony_ci const char* attrName = "user.myattr"; 1049762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 1059762338dSopenharmony_ci int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue); 1069762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1079762338dSopenharmony_ci 1089762338dSopenharmony_ci char buffer[64] = {0}; 1099762338dSopenharmony_ci ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer)); 1109762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1119762338dSopenharmony_ci 1129762338dSopenharmony_ci ret = strncmp(attrValue, buffer, strlen(attrValue)); 1139762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1149762338dSopenharmony_ci 1159762338dSopenharmony_ci remove(targetFilePath); 1169762338dSopenharmony_ci} 1179762338dSopenharmony_ci 1189762338dSopenharmony_ci/* 1199762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0200 1209762338dSopenharmony_ci * @tc.name : LsetxattrSetMoreExtAttrSuccess_0002 1219762338dSopenharmony_ci * @tc.desc : set file more extended attribute success. 1229762338dSopenharmony_ci * @tc.size : MediumTest 1239762338dSopenharmony_ci * @tc.type : Function 1249762338dSopenharmony_ci * @tc.level : Level 1 1259762338dSopenharmony_ci */ 1269762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetMoreExtAttrSuccess_0002, Function | MediumTest | Level1) 1279762338dSopenharmony_ci{ 1289762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 1299762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 1309762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 1319762338dSopenharmony_ci EXPECT_TRUE(num > 0); 1329762338dSopenharmony_ci 1339762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 1349762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 1359762338dSopenharmony_ci close(fd); 1369762338dSopenharmony_ci 1379762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 1389762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 1399762338dSopenharmony_ci int ret = SetExtendedAttribute(targetFilePath, attrName1, attrValue1); 1409762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1419762338dSopenharmony_ci char buffer[64] = {0}; 1429762338dSopenharmony_ci ret = GetExtendedAttribute(targetFilePath, attrName1, buffer, sizeof(buffer)); 1439762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1449762338dSopenharmony_ci 1459762338dSopenharmony_ci ret = strncmp(attrValue1, buffer, strlen(attrValue1)); 1469762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1479762338dSopenharmony_ci 1489762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 1499762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 1509762338dSopenharmony_ci ret = SetExtendedAttribute(targetFilePath, attrName2, attrValue2); 1519762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1529762338dSopenharmony_ci 1539762338dSopenharmony_ci memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer)); 1549762338dSopenharmony_ci ret = GetExtendedAttribute(targetFilePath, attrName2, buffer, sizeof(buffer)); 1559762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1569762338dSopenharmony_ci 1579762338dSopenharmony_ci ret = strncmp(attrValue2, buffer, strlen(attrValue2)); 1589762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1599762338dSopenharmony_ci 1609762338dSopenharmony_ci remove(targetFilePath); 1619762338dSopenharmony_ci} 1629762338dSopenharmony_ci 1639762338dSopenharmony_ci/* 1649762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0300 1659762338dSopenharmony_ci * @tc.name : LsetxattrCreateExtAttrSuccess_0003 1669762338dSopenharmony_ci * @tc.desc : create file extended attribute success. 1679762338dSopenharmony_ci * @tc.size : MediumTest 1689762338dSopenharmony_ci * @tc.type : Function 1699762338dSopenharmony_ci * @tc.level : Level 1 1709762338dSopenharmony_ci */ 1719762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateExtAttrSuccess_0003, Function | MediumTest | Level1) 1729762338dSopenharmony_ci{ 1739762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 1749762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 1759762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 1769762338dSopenharmony_ci EXPECT_TRUE(num > 0); 1779762338dSopenharmony_ci 1789762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 1799762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 1809762338dSopenharmony_ci close(fd); 1819762338dSopenharmony_ci 1829762338dSopenharmony_ci const char* attrName = "user.myattr"; 1839762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 1849762338dSopenharmony_ci int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue, XATTR_CREATE); 1859762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1869762338dSopenharmony_ci 1879762338dSopenharmony_ci char buffer[64] = {0}; 1889762338dSopenharmony_ci ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer)); 1899762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1909762338dSopenharmony_ci 1919762338dSopenharmony_ci ret = strncmp(attrValue, buffer, strlen(attrValue)); 1929762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1939762338dSopenharmony_ci 1949762338dSopenharmony_ci remove(targetFilePath); 1959762338dSopenharmony_ci} 1969762338dSopenharmony_ci 1979762338dSopenharmony_ci/* 1989762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0400 1999762338dSopenharmony_ci * @tc.name : LsetxattrCreateMoreExtAttrSuccess_0004 2009762338dSopenharmony_ci * @tc.desc : create file more extended attribute success. 2019762338dSopenharmony_ci * @tc.size : MediumTest 2029762338dSopenharmony_ci * @tc.type : Function 2039762338dSopenharmony_ci * @tc.level : Level 1 2049762338dSopenharmony_ci */ 2059762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateMoreExtAttrSuccess_0004, Function | MediumTest | Level1) 2069762338dSopenharmony_ci{ 2079762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 2089762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 2099762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 2109762338dSopenharmony_ci EXPECT_TRUE(num > 0); 2119762338dSopenharmony_ci 2129762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 2139762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 2149762338dSopenharmony_ci close(fd); 2159762338dSopenharmony_ci 2169762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 2179762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 2189762338dSopenharmony_ci int ret = SetExtendedAttribute(targetFilePath, attrName1, attrValue1, XATTR_CREATE); 2199762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2209762338dSopenharmony_ci 2219762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 2229762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 2239762338dSopenharmony_ci ret = SetExtendedAttribute(targetFilePath, attrName2, attrValue2, XATTR_CREATE); 2249762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2259762338dSopenharmony_ci 2269762338dSopenharmony_ci remove(targetFilePath); 2279762338dSopenharmony_ci} 2289762338dSopenharmony_ci 2299762338dSopenharmony_ci/* 2309762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0500 2319762338dSopenharmony_ci * @tc.name : LsetxattrReplaceExtAttrSuccess_0005 2329762338dSopenharmony_ci * @tc.desc : replace file more extended attribute success. 2339762338dSopenharmony_ci * @tc.size : MediumTest 2349762338dSopenharmony_ci * @tc.type : Function 2359762338dSopenharmony_ci * @tc.level : Level 1 2369762338dSopenharmony_ci */ 2379762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrReplaceExtAttrSuccess_0005, Function | MediumTest | Level1) 2389762338dSopenharmony_ci{ 2399762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 2409762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 2419762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 2429762338dSopenharmony_ci EXPECT_TRUE(num > 0); 2439762338dSopenharmony_ci 2449762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 2459762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 2469762338dSopenharmony_ci close(fd); 2479762338dSopenharmony_ci 2489762338dSopenharmony_ci const char* attrName = "user.myattr"; 2499762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 2509762338dSopenharmony_ci int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue1, XATTR_CREATE); 2519762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2529762338dSopenharmony_ci 2539762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 2549762338dSopenharmony_ci ret = SetExtendedAttribute(targetFilePath, attrName, attrValue2, XATTR_REPLACE); 2559762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2569762338dSopenharmony_ci 2579762338dSopenharmony_ci char buffer[64] = {0}; 2589762338dSopenharmony_ci ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer)); 2599762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2609762338dSopenharmony_ci 2619762338dSopenharmony_ci ret = strncmp(attrValue2, buffer, strlen(attrValue2)); 2629762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2639762338dSopenharmony_ci 2649762338dSopenharmony_ci remove(targetFilePath); 2659762338dSopenharmony_ci} 2669762338dSopenharmony_ci 2679762338dSopenharmony_ci/* 2689762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0600 2699762338dSopenharmony_ci * @tc.name : LsetxattrSetLExtAttrSuccess_0006 2709762338dSopenharmony_ci * @tc.desc : set link file extended attribute success. 2719762338dSopenharmony_ci * @tc.size : MediumTest 2729762338dSopenharmony_ci * @tc.type : Function 2739762338dSopenharmony_ci * @tc.level : Level 1 2749762338dSopenharmony_ci */ 2759762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetLExtAttrSuccess_0006, Function | MediumTest | Level1) 2769762338dSopenharmony_ci{ 2779762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 2789762338dSopenharmony_ci const char* linkPath = "link_to_file.txt"; 2799762338dSopenharmony_ci 2809762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 2819762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 2829762338dSopenharmony_ci EXPECT_TRUE(num > 0); 2839762338dSopenharmony_ci 2849762338dSopenharmony_ci char linkFilePath[BUFFER_SIZE] = {0}; 2859762338dSopenharmony_ci num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath); 2869762338dSopenharmony_ci EXPECT_TRUE(num > 0); 2879762338dSopenharmony_ci 2889762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 2899762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 2909762338dSopenharmony_ci close(fd); 2919762338dSopenharmony_ci 2929762338dSopenharmony_ci int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644); 2939762338dSopenharmony_ci EXPECT_TRUE(newFd > 0); 2949762338dSopenharmony_ci 2959762338dSopenharmony_ci int ret = linkat(newFd, targetFile, newFd, linkPath, 0); 2969762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 2979762338dSopenharmony_ci 2989762338dSopenharmony_ci const char* attrName = "user.myattr"; 2999762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 3009762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName, attrValue, 0); 3019762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3029762338dSopenharmony_ci 3039762338dSopenharmony_ci remove(targetFilePath); 3049762338dSopenharmony_ci remove(linkFilePath); 3059762338dSopenharmony_ci close(newFd); 3069762338dSopenharmony_ci} 3079762338dSopenharmony_ci 3089762338dSopenharmony_ci/* 3099762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0700 3109762338dSopenharmony_ci * @tc.name : LsetxattrSetLMoreExtAttrSuccess_0007 3119762338dSopenharmony_ci * @tc.desc : set link file more extended attribute success. 3129762338dSopenharmony_ci * @tc.size : MediumTest 3139762338dSopenharmony_ci * @tc.type : Function 3149762338dSopenharmony_ci * @tc.level : Level 1 3159762338dSopenharmony_ci */ 3169762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetLMoreExtAttrSuccess_0007, Function | MediumTest | Level1) 3179762338dSopenharmony_ci{ 3189762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 3199762338dSopenharmony_ci const char* linkPath = "link_to_file.txt"; 3209762338dSopenharmony_ci 3219762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 3229762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 3239762338dSopenharmony_ci EXPECT_TRUE(num > 0); 3249762338dSopenharmony_ci 3259762338dSopenharmony_ci char linkFilePath[BUFFER_SIZE] = {0}; 3269762338dSopenharmony_ci num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath); 3279762338dSopenharmony_ci EXPECT_TRUE(num > 0); 3289762338dSopenharmony_ci 3299762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 3309762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 3319762338dSopenharmony_ci close(fd); 3329762338dSopenharmony_ci 3339762338dSopenharmony_ci int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644); 3349762338dSopenharmony_ci EXPECT_TRUE(newFd > 0); 3359762338dSopenharmony_ci 3369762338dSopenharmony_ci int ret = linkat(newFd, targetFile, newFd, linkPath, 0); 3379762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3389762338dSopenharmony_ci 3399762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 3409762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 3419762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName1, attrValue1); 3429762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3439762338dSopenharmony_ci 3449762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 3459762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 3469762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName2, attrValue2); 3479762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3489762338dSopenharmony_ci 3499762338dSopenharmony_ci remove(targetFilePath); 3509762338dSopenharmony_ci remove(linkFilePath); 3519762338dSopenharmony_ci close(newFd); 3529762338dSopenharmony_ci} 3539762338dSopenharmony_ci 3549762338dSopenharmony_ci/* 3559762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0800 3569762338dSopenharmony_ci * @tc.name : LsetxattrCreateLExtAttrSuccess_0008 3579762338dSopenharmony_ci * @tc.desc : create link file extended attribute success. 3589762338dSopenharmony_ci * @tc.size : MediumTest 3599762338dSopenharmony_ci * @tc.type : Function 3609762338dSopenharmony_ci * @tc.level : Level 1 3619762338dSopenharmony_ci */ 3629762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateLExtAttrSuccess_0008, Function | MediumTest | Level1) 3639762338dSopenharmony_ci{ 3649762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 3659762338dSopenharmony_ci const char* linkPath = "link_to_file.txt"; 3669762338dSopenharmony_ci 3679762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 3689762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 3699762338dSopenharmony_ci EXPECT_TRUE(num > 0); 3709762338dSopenharmony_ci 3719762338dSopenharmony_ci char linkFilePath[BUFFER_SIZE] = {0}; 3729762338dSopenharmony_ci num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath); 3739762338dSopenharmony_ci EXPECT_TRUE(num > 0); 3749762338dSopenharmony_ci 3759762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 3769762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 3779762338dSopenharmony_ci close(fd); 3789762338dSopenharmony_ci 3799762338dSopenharmony_ci int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644); 3809762338dSopenharmony_ci EXPECT_TRUE(newFd > 0); 3819762338dSopenharmony_ci 3829762338dSopenharmony_ci int ret = linkat(newFd, targetFile, newFd, linkPath, 0); 3839762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3849762338dSopenharmony_ci 3859762338dSopenharmony_ci const char* attrName = "user.myattr"; 3869762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 3879762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName, attrValue, XATTR_CREATE); 3889762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 3899762338dSopenharmony_ci 3909762338dSopenharmony_ci remove(targetFilePath); 3919762338dSopenharmony_ci remove(linkFilePath); 3929762338dSopenharmony_ci close(newFd); 3939762338dSopenharmony_ci} 3949762338dSopenharmony_ci 3959762338dSopenharmony_ci/* 3969762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0900 3979762338dSopenharmony_ci * @tc.name : LsetxattrCreateLMoreExtAttrSuccess_0009 3989762338dSopenharmony_ci * @tc.desc : create link file more extended attribute success. 3999762338dSopenharmony_ci * @tc.size : MediumTest 4009762338dSopenharmony_ci * @tc.type : Function 4019762338dSopenharmony_ci * @tc.level : Level 1 4029762338dSopenharmony_ci */ 4039762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateLMoreExtAttrSuccess_0009, Function | MediumTest | Level1) 4049762338dSopenharmony_ci{ 4059762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 4069762338dSopenharmony_ci const char* linkPath = "link_to_file.txt"; 4079762338dSopenharmony_ci 4089762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 4099762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 4109762338dSopenharmony_ci EXPECT_TRUE(num > 0); 4119762338dSopenharmony_ci 4129762338dSopenharmony_ci char linkFilePath[BUFFER_SIZE] = {0}; 4139762338dSopenharmony_ci num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath); 4149762338dSopenharmony_ci EXPECT_TRUE(num > 0); 4159762338dSopenharmony_ci 4169762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 4179762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 4189762338dSopenharmony_ci close(fd); 4199762338dSopenharmony_ci 4209762338dSopenharmony_ci int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644); 4219762338dSopenharmony_ci EXPECT_TRUE(newFd > 0); 4229762338dSopenharmony_ci 4239762338dSopenharmony_ci int ret = linkat(newFd, targetFile, newFd, linkPath, 0); 4249762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4259762338dSopenharmony_ci 4269762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 4279762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 4289762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName1, attrValue1, XATTR_CREATE); 4299762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4309762338dSopenharmony_ci 4319762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 4329762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 4339762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName2, attrValue2, XATTR_CREATE); 4349762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4359762338dSopenharmony_ci 4369762338dSopenharmony_ci remove(targetFilePath); 4379762338dSopenharmony_ci remove(linkFilePath); 4389762338dSopenharmony_ci close(newFd); 4399762338dSopenharmony_ci} 4409762338dSopenharmony_ci 4419762338dSopenharmony_ci/* 4429762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1000 4439762338dSopenharmony_ci * @tc.name : LsetxattrReplaceLExtAttrSuccess_0010 4449762338dSopenharmony_ci * @tc.desc : replace link file more extended attribute success. 4459762338dSopenharmony_ci * @tc.size : MediumTest 4469762338dSopenharmony_ci * @tc.type : Function 4479762338dSopenharmony_ci * @tc.level : Level 1 4489762338dSopenharmony_ci */ 4499762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrReplaceLExtAttrSuccess_0010, Function | MediumTest | Level1) 4509762338dSopenharmony_ci{ 4519762338dSopenharmony_ci const char* targetFile = "target_file.txt"; 4529762338dSopenharmony_ci const char* linkPath = "link_to_file.txt"; 4539762338dSopenharmony_ci 4549762338dSopenharmony_ci char targetFilePath[BUFFER_SIZE] = {0}; 4559762338dSopenharmony_ci int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile); 4569762338dSopenharmony_ci EXPECT_TRUE(num > 0); 4579762338dSopenharmony_ci 4589762338dSopenharmony_ci char linkFilePath[BUFFER_SIZE] = {0}; 4599762338dSopenharmony_ci num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath); 4609762338dSopenharmony_ci EXPECT_TRUE(num > 0); 4619762338dSopenharmony_ci 4629762338dSopenharmony_ci int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644); 4639762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 4649762338dSopenharmony_ci close(fd); 4659762338dSopenharmony_ci 4669762338dSopenharmony_ci int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644); 4679762338dSopenharmony_ci EXPECT_TRUE(newFd > 0); 4689762338dSopenharmony_ci 4699762338dSopenharmony_ci int ret = linkat(newFd, targetFile, newFd, linkPath, 0); 4709762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4719762338dSopenharmony_ci 4729762338dSopenharmony_ci const char* attrName = "user.myattr"; 4739762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 4749762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName, attrValue1, XATTR_CREATE); 4759762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4769762338dSopenharmony_ci 4779762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 4789762338dSopenharmony_ci ret = SetExtendedAttribute(linkFilePath, attrName, attrValue2, XATTR_REPLACE); 4799762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 4809762338dSopenharmony_ci 4819762338dSopenharmony_ci remove(targetFilePath); 4829762338dSopenharmony_ci remove(linkFilePath); 4839762338dSopenharmony_ci close(newFd); 4849762338dSopenharmony_ci} 4859762338dSopenharmony_ci 4869762338dSopenharmony_ci/* 4879762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1100 4889762338dSopenharmony_ci * @tc.name : LsetxattrSetPExtAttrSuccess_0011 4899762338dSopenharmony_ci * @tc.desc : set path extended attribute success. 4909762338dSopenharmony_ci * @tc.size : MediumTest 4919762338dSopenharmony_ci * @tc.type : Function 4929762338dSopenharmony_ci * @tc.level : Level 1 4939762338dSopenharmony_ci */ 4949762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetPExtAttrSuccess_0011, Function | MediumTest | Level1) 4959762338dSopenharmony_ci{ 4969762338dSopenharmony_ci const char* attrName = "user.myattr"; 4979762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 4989762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue); 4999762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5009762338dSopenharmony_ci 5019762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName); 5029762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5039762338dSopenharmony_ci} 5049762338dSopenharmony_ci 5059762338dSopenharmony_ci/* 5069762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1200 5079762338dSopenharmony_ci * @tc.name : LsetxattrSetPMoreExtAttrSuccess_0012 5089762338dSopenharmony_ci * @tc.desc : set path more extended attribute success. 5099762338dSopenharmony_ci * @tc.size : MediumTest 5109762338dSopenharmony_ci * @tc.type : Function 5119762338dSopenharmony_ci * @tc.level : Level 1 5129762338dSopenharmony_ci */ 5139762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrSetPMoreExtAttrSuccess_0012, Function | MediumTest | Level1) 5149762338dSopenharmony_ci{ 5159762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 5169762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 5179762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName1, attrValue1); 5189762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5199762338dSopenharmony_ci 5209762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 5219762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 5229762338dSopenharmony_ci ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName2, attrValue2); 5239762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5249762338dSopenharmony_ci 5259762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName1); 5269762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5279762338dSopenharmony_ci 5289762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName2); 5299762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5309762338dSopenharmony_ci} 5319762338dSopenharmony_ci 5329762338dSopenharmony_ci/* 5339762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1300 5349762338dSopenharmony_ci * @tc.name : LsetxattrCreatePExtAttrSuccess_0013 5359762338dSopenharmony_ci * @tc.desc : create path extended attribute success. 5369762338dSopenharmony_ci * @tc.size : MediumTest 5379762338dSopenharmony_ci * @tc.type : Function 5389762338dSopenharmony_ci * @tc.level : Level 1 5399762338dSopenharmony_ci */ 5409762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreatePExtAttrSuccess_0013, Function | MediumTest | Level1) 5419762338dSopenharmony_ci{ 5429762338dSopenharmony_ci const char* attrName = "user.myattr"; 5439762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 5449762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE); 5459762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5469762338dSopenharmony_ci 5479762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName); 5489762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5499762338dSopenharmony_ci} 5509762338dSopenharmony_ci 5519762338dSopenharmony_ci/* 5529762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1400 5539762338dSopenharmony_ci * @tc.name : LsetxattrCreatePMoreExtAttrSuccess_0014 5549762338dSopenharmony_ci * @tc.desc : create path more extended attribute success. 5559762338dSopenharmony_ci * @tc.size : MediumTest 5569762338dSopenharmony_ci * @tc.type : Function 5579762338dSopenharmony_ci * @tc.level : Level 1 5589762338dSopenharmony_ci */ 5599762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreatePMoreExtAttrSuccess_0014, Function | MediumTest | Level1) 5609762338dSopenharmony_ci{ 5619762338dSopenharmony_ci const char* attrName1 = "user.myattr1"; 5629762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 5639762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName1, attrValue1, XATTR_CREATE); 5649762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5659762338dSopenharmony_ci 5669762338dSopenharmony_ci const char* attrName2 = "user.myattr2"; 5679762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 5689762338dSopenharmony_ci ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName2, attrValue2, XATTR_CREATE); 5699762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5709762338dSopenharmony_ci 5719762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName1); 5729762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5739762338dSopenharmony_ci 5749762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName2); 5759762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5769762338dSopenharmony_ci} 5779762338dSopenharmony_ci 5789762338dSopenharmony_ci/* 5799762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1500 5809762338dSopenharmony_ci * @tc.name : LsetxattrReplacePExtAttrSuccess_0015 5819762338dSopenharmony_ci * @tc.desc : replace path more extended attribute success. 5829762338dSopenharmony_ci * @tc.size : MediumTest 5839762338dSopenharmony_ci * @tc.type : Function 5849762338dSopenharmony_ci * @tc.level : Level 1 5859762338dSopenharmony_ci */ 5869762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrReplacePExtAttrSuccess_0015, Function | MediumTest | Level1) 5879762338dSopenharmony_ci{ 5889762338dSopenharmony_ci const char* attrName = "user.myattr"; 5899762338dSopenharmony_ci const char* attrValue1 = "Hello, xattr1!"; 5909762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue1, XATTR_CREATE); 5919762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5929762338dSopenharmony_ci 5939762338dSopenharmony_ci char buffer[64] = {0}; 5949762338dSopenharmony_ci ret = GetExtendedAttribute(OPEN_API_TEST_FILE, attrName, buffer, sizeof(buffer)); 5959762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5969762338dSopenharmony_ci 5979762338dSopenharmony_ci ret = strncmp(attrValue1, buffer, strlen(attrValue1)); 5989762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 5999762338dSopenharmony_ci 6009762338dSopenharmony_ci const char* attrValue2 = "Hello, xattr2!"; 6019762338dSopenharmony_ci ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue2, XATTR_REPLACE); 6029762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6039762338dSopenharmony_ci 6049762338dSopenharmony_ci memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer)); 6059762338dSopenharmony_ci ret = GetExtendedAttribute(OPEN_API_TEST_FILE, attrName, buffer, sizeof(buffer)); 6069762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6079762338dSopenharmony_ci 6089762338dSopenharmony_ci ret = strncmp(attrValue2, buffer, strlen(attrValue2)); 6099762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6109762338dSopenharmony_ci 6119762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName); 6129762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6139762338dSopenharmony_ci} 6149762338dSopenharmony_ci 6159762338dSopenharmony_ci/* 6169762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1600 6179762338dSopenharmony_ci * @tc.name : LsetxattrCreateExtAttrFailed_0016 6189762338dSopenharmony_ci * @tc.desc : create non path extended attribute failed. 6199762338dSopenharmony_ci * @tc.size : MediumTest 6209762338dSopenharmony_ci * @tc.type : Function 6219762338dSopenharmony_ci * @tc.level : Level 2 6229762338dSopenharmony_ci */ 6239762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateExtAttrFailed_0016, Function | MediumTest | Level2) 6249762338dSopenharmony_ci{ 6259762338dSopenharmony_ci const char* nonDirPath = "non_existing_dir"; 6269762338dSopenharmony_ci char tmpNonDirPath[BUFFER_SIZE] = {0}; 6279762338dSopenharmony_ci int num = sprintf_s(tmpNonDirPath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, nonDirPath); 6289762338dSopenharmony_ci EXPECT_TRUE(num > 0); 6299762338dSopenharmony_ci 6309762338dSopenharmony_ci errno = 0; 6319762338dSopenharmony_ci const char* attrName = "user.myattr"; 6329762338dSopenharmony_ci const char* attrValue = "Hello, xattr!"; 6339762338dSopenharmony_ci int ret = SetExtendedAttribute(tmpNonDirPath, attrName, attrValue, XATTR_CREATE); 6349762338dSopenharmony_ci EXPECT_TRUE(ret == -1); 6359762338dSopenharmony_ci EXPECT_TRUE(errno == ENOENT); 6369762338dSopenharmony_ci} 6379762338dSopenharmony_ci 6389762338dSopenharmony_ci/* 6399762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1700 6409762338dSopenharmony_ci * @tc.name : LsetxattrCreateSameExtAttrFailed_0017 6419762338dSopenharmony_ci * @tc.desc : create file same extended attribute failed. 6429762338dSopenharmony_ci * @tc.size : MediumTest 6439762338dSopenharmony_ci * @tc.type : Function 6449762338dSopenharmony_ci * @tc.level : Level 2 6459762338dSopenharmony_ci */ 6469762338dSopenharmony_ciHWTEST_F(LsetxattrApiTest, LsetxattrCreateSameExtAttrFailed_0017, Function | MediumTest | Level2) 6479762338dSopenharmony_ci{ 6489762338dSopenharmony_ci const char* attrName = "user.myattr"; 6499762338dSopenharmony_ci const char* attrValue = "Hello, xattr1!"; 6509762338dSopenharmony_ci int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE); 6519762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6529762338dSopenharmony_ci 6539762338dSopenharmony_ci errno = 0; 6549762338dSopenharmony_ci ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE); 6559762338dSopenharmony_ci EXPECT_TRUE(ret == -1); 6569762338dSopenharmony_ci EXPECT_TRUE(errno == EEXIST); 6579762338dSopenharmony_ci 6589762338dSopenharmony_ci ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName); 6599762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 6609762338dSopenharmony_ci}