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 <arpa/inet.h> 249762338dSopenharmony_ci#include <gtest/gtest.h> 259762338dSopenharmony_ci#include <netinet/in.h> 269762338dSopenharmony_ci#include <sys/stat.h> 279762338dSopenharmony_ci#include <sys/socket.h> 289762338dSopenharmony_ci#include <sys/types.h> 299762338dSopenharmony_ci#include "securec.h" 309762338dSopenharmony_ci 319762338dSopenharmony_ciusing namespace testing::ext; 329762338dSopenharmony_ci 339762338dSopenharmony_cistatic const char *TEST_VALID_PATH = "/data/local/tmp/test_dir"; 349762338dSopenharmony_cistatic const char *TEST_VALID_FILE = "/data/local/tmp/test_dir/test.txt"; 359762338dSopenharmony_cistatic const char *TEST_LINK_PATH = "/data/local/tmp/symlinkat_test_dir"; 369762338dSopenharmony_cistatic const char *TEST_LINK_FILE = "/data/local/tmp/test_dir/symlinkat_test.txt"; 379762338dSopenharmony_cistatic const char *TEST_NOACCESS_PATH = "/data/local/abcd/abcd"; 389762338dSopenharmony_ci 399762338dSopenharmony_ciclass HatsSymlinkatTest : public testing::Test { 409762338dSopenharmony_cipublic: 419762338dSopenharmony_cistatic void SetUpTestCase(); 429762338dSopenharmony_cistatic void TearDownTestCase(); 439762338dSopenharmony_civoid SetUp(); 449762338dSopenharmony_civoid TearDown(); 459762338dSopenharmony_ciprivate: 469762338dSopenharmony_ci}; 479762338dSopenharmony_civoid HatsSymlinkatTest::SetUp() 489762338dSopenharmony_ci{ 499762338dSopenharmony_ci int fd = -1; 509762338dSopenharmony_ci if (access(TEST_VALID_PATH, F_OK) == 0) { 519762338dSopenharmony_ci (void)remove(TEST_VALID_PATH); 529762338dSopenharmony_ci } 539762338dSopenharmony_ci (void)mkdir(TEST_VALID_PATH, S_IWUSR | S_IRUSR | S_IXUSR); 549762338dSopenharmony_ci fd = open(TEST_VALID_FILE, O_WRONLY | O_CREAT, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 559762338dSopenharmony_ci close(fd); 569762338dSopenharmony_ci} 579762338dSopenharmony_civoid HatsSymlinkatTest::TearDown() 589762338dSopenharmony_ci{ 599762338dSopenharmony_ci (void)remove(TEST_VALID_FILE); 609762338dSopenharmony_ci (void)remove(TEST_LINK_FILE); 619762338dSopenharmony_ci (void)remove(TEST_VALID_PATH); 629762338dSopenharmony_ci (void)remove(TEST_LINK_PATH); 639762338dSopenharmony_ci} 649762338dSopenharmony_civoid HatsSymlinkatTest::SetUpTestCase() 659762338dSopenharmony_ci{ 669762338dSopenharmony_ci} 679762338dSopenharmony_civoid HatsSymlinkatTest::TearDownTestCase() 689762338dSopenharmony_ci{ 699762338dSopenharmony_ci} 709762338dSopenharmony_ci 719762338dSopenharmony_ci/* 729762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0100 739762338dSopenharmony_ci * @tc.name : SymlinkatFileAndPathSuccess_0001 749762338dSopenharmony_ci * @tc.desc : symlinkat create file and path symlink success. 759762338dSopenharmony_ci * @tc.size : MediumTest 769762338dSopenharmony_ci * @tc.type : Function 779762338dSopenharmony_ci * @tc.level : Level 1 789762338dSopenharmony_ci */ 799762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatFileAndPathSuccess_0001, Function | MediumTest | Level1) 809762338dSopenharmony_ci{ 819762338dSopenharmony_ci // symlinkat create file symlink success 829762338dSopenharmony_ci int ret = symlinkat(TEST_VALID_FILE, AT_FDCWD, TEST_LINK_FILE); 839762338dSopenharmony_ci EXPECT_EQ(ret, 0); 849762338dSopenharmony_ci EXPECT_EQ(access(TEST_LINK_FILE, F_OK), 0); 859762338dSopenharmony_ci 869762338dSopenharmony_ci // symlinkat create path symlink success 879762338dSopenharmony_ci ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_LINK_PATH); 889762338dSopenharmony_ci EXPECT_EQ(ret, 0); 899762338dSopenharmony_ci EXPECT_EQ(access(TEST_LINK_PATH, F_OK), 0); 909762338dSopenharmony_ci} 919762338dSopenharmony_ci 929762338dSopenharmony_ci/* 939762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0200 949762338dSopenharmony_ci * @tc.name : SymlinkatNoExistLinkPathFailed_0002 959762338dSopenharmony_ci * @tc.desc : symlinkat create path symlink which is no exist path failed, errno ENOENT. 969762338dSopenharmony_ci * @tc.size : MediumTest 979762338dSopenharmony_ci * @tc.type : Function 989762338dSopenharmony_ci * @tc.level : Level 1 999762338dSopenharmony_ci */ 1009762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatNoPermissionPathFailed_0002, Function | MediumTest | Level1) 1019762338dSopenharmony_ci{ 1029762338dSopenharmony_ci errno = 0; 1039762338dSopenharmony_ci int ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_NOACCESS_PATH); 1049762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1059762338dSopenharmony_ci EXPECT_EQ(errno, ENOENT); 1069762338dSopenharmony_ci} 1079762338dSopenharmony_ci 1089762338dSopenharmony_ci/* 1099762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0300 1109762338dSopenharmony_ci * @tc.name : SymlinkatExistPathFailed_0003 1119762338dSopenharmony_ci * @tc.desc : symlinkat create path symlink which is already exist failed, errno EEXIST. 1129762338dSopenharmony_ci * @tc.size : MediumTest 1139762338dSopenharmony_ci * @tc.type : Function 1149762338dSopenharmony_ci * @tc.level : Level 1 1159762338dSopenharmony_ci */ 1169762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatExistPathFailed_0003, Function | MediumTest | Level1) 1179762338dSopenharmony_ci{ 1189762338dSopenharmony_ci int ret; 1199762338dSopenharmony_ci if (access(TEST_LINK_PATH, F_OK) != 0) { 1209762338dSopenharmony_ci ret = mkdir(TEST_LINK_PATH, S_IWUSR | S_IRUSR | S_IXUSR); 1219762338dSopenharmony_ci EXPECT_EQ(ret, 0); 1229762338dSopenharmony_ci } 1239762338dSopenharmony_ci 1249762338dSopenharmony_ci errno = 0; 1259762338dSopenharmony_ci ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_LINK_PATH); 1269762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1279762338dSopenharmony_ci EXPECT_EQ(errno, EEXIST); 1289762338dSopenharmony_ci} 1299762338dSopenharmony_ci 1309762338dSopenharmony_ci/* 1319762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0400 1329762338dSopenharmony_ci * @tc.name : SymlinkatLinkPathLongerFailed_0004 1339762338dSopenharmony_ci * @tc.desc : symlinkat create path symlink which the name is too longer failed, errno ENAMETOOLONG. 1349762338dSopenharmony_ci * @tc.size : MediumTest 1359762338dSopenharmony_ci * @tc.type : Function 1369762338dSopenharmony_ci * @tc.level : Level 1 1379762338dSopenharmony_ci */ 1389762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathLongerFailed_0004, Function | MediumTest | Level1) 1399762338dSopenharmony_ci{ 1409762338dSopenharmony_ci int ret; 1419762338dSopenharmony_ci char name[256]; 1429762338dSopenharmony_ci (void)memset_s(name, sizeof(name), 's', sizeof(name)); 1439762338dSopenharmony_ci 1449762338dSopenharmony_ci errno = 0; 1459762338dSopenharmony_ci ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, name); 1469762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1479762338dSopenharmony_ci EXPECT_EQ(errno, ENAMETOOLONG); 1489762338dSopenharmony_ci} 1499762338dSopenharmony_ci 1509762338dSopenharmony_ci/* 1519762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0500 1529762338dSopenharmony_ci * @tc.name : SymlinkatLinkPathNullptrFailed_0005 1539762338dSopenharmony_ci * @tc.desc : symlinkat create path symlink is nullptr failed, errno EFAULT. 1549762338dSopenharmony_ci * @tc.size : MediumTest 1559762338dSopenharmony_ci * @tc.type : Function 1569762338dSopenharmony_ci * @tc.level : Level 1 1579762338dSopenharmony_ci */ 1589762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathNullptrFailed_0005, Function | MediumTest | Level1) 1599762338dSopenharmony_ci{ 1609762338dSopenharmony_ci errno = 0; 1619762338dSopenharmony_ci int ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, nullptr); 1629762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1639762338dSopenharmony_ci EXPECT_EQ(errno, EFAULT); 1649762338dSopenharmony_ci} 1659762338dSopenharmony_ci 1669762338dSopenharmony_ci/* 1679762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0600 1689762338dSopenharmony_ci * @tc.name : SymlinkatInvalidFdFailed_0006 1699762338dSopenharmony_ci * @tc.desc : symlinkat used invalid fd failed, errno EBADF. 1709762338dSopenharmony_ci * @tc.size : MediumTest 1719762338dSopenharmony_ci * @tc.type : Function 1729762338dSopenharmony_ci * @tc.level : Level 1 1739762338dSopenharmony_ci */ 1749762338dSopenharmony_ciHWTEST_F(HatsSymlinkatTest, SymlinkatInvalidFdFailed_0006, Function | MediumTest | Level1) 1759762338dSopenharmony_ci{ 1769762338dSopenharmony_ci errno = 0; 1779762338dSopenharmony_ci int ret = symlinkat("test_path", -1, "symlinkat_test_dir"); 1789762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1799762338dSopenharmony_ci EXPECT_EQ(errno, EBADF); 1809762338dSopenharmony_ci} 181