1 #include <fcntl.h> 2 #include <gtest/gtest.h> 3 #include <string.h> 4 #define __FORTIFY_COMPILATION 5 #include <fortify/unistd.h> 6 7 using namespace testing::ext; 8 9 class FortifyReadlinkchkTest : public testing::Test { SetUp()10 void SetUp() override {} TearDown()11 void TearDown() override {} 12 }; 13 14 /** 15 * @tc.name: __readlinkat_chk_001 16 * @tc.desc: Verify the correctness of the readlink function for symbolic link files in a specific path. 17 * @tc.type: FUNC 18 * */ 19 HWTEST_F(FortifyReadlinkchkTest, __readlinkat_chk_001, TestSize.Level1) 20 { 21 int fd = open("/data/readlinkat_chk", O_RDWR | O_CREAT, 0644); 22 ASSERT_LE(0, fd); 23 char buf[] = "test"; 24 write(fd, buf, sizeof(buf)); 25 close(fd); 26 char linkpath[PATH_MAX] = "readlinkat_test"; 27 remove(linkpath); 28 EXPECT_EQ(0, symlink("/data/readlinkat_chk", linkpath)); 29 char path[PATH_MAX]; 30 ssize_t length = __readlinkat_chk(AT_FDCWD, "readlinkat_test", path, sizeof(path), sizeof(path)); 31 ASSERT_LT(0, length); 32 EXPECT_EQ("/data/readlinkat_chk", std::string(path, length)); 33 remove("/data/readlinkat_chk"); 34 remove("readlinkat_test"); 35 }