1 #include <gtest/gtest.h> 2 #include <string.h> 3 #define __FORTIFY_COMPILATION 4 #include <fortify/stdio.h> 5 6 constexpr int STR_SIZE = 100; 7 8 using namespace testing::ext; 9 10 class FortifyFgetschkTest : public testing::Test { SetUp()11 void SetUp() override {} TearDown()12 void TearDown() override {} 13 }; 14 15 /** 16 * @tc.name: __fgets_chk_001 17 * @tc.desc: Verify if the __fgets_chk is functioning properly. 18 * @tc.type: FUNC 19 * */ 20 HWTEST_F(FortifyFgetschkTest, __fgets_chk_001, TestSize.Level1) 21 { 22 char str[STR_SIZE]; 23 const char* ptr = "fgetstest.txt"; 24 const char* wrstring = "this is a test\n"; 25 FILE* fptr = fopen(ptr, "wr+"); 26 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 27 fflush(fptr); 28 fseek(fptr, 0L, SEEK_SET); 29 char* content = __fgets_chk(str, STR_SIZE, fptr, STR_SIZE); 30 int result = strcmp(content, "this is a test\n"); 31 EXPECT_EQ(0, result); 32 33 fclose(fptr); 34 remove(ptr); 35 fptr = nullptr; 36 ptr = nullptr; 37 wrstring = nullptr; 38 }