1 #include <gtest/gtest.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 constexpr int BUF_SIZE = 100;
6 constexpr int EXPECT_NUM = 3;
7
8 using namespace testing::ext;
9
10 class FortifyVsprintfchkTest : public testing::Test {
SetUp()11 void SetUp() override {}
TearDown()12 void TearDown() override {}
13 };
14
15 extern "C" int __vsprintf_chk(char* dest, int flags, size_t dst_len_from_compiler, const char* format, va_list va);
16
VsprintfHelper(char * str,size_t size,const char * format,...)17 static int VsprintfHelper(char* str, size_t size, const char* format, ...)
18 {
19 va_list args;
20 va_start(args, format);
21 int result = __vsprintf_chk(str, 0, size - 1, format, args);
22 va_end(args);
23 return result;
24 }
25
26 /**
27 * @tc.name: __strcat_chk_001
28 * @tc.desc: Verify whether the specified content will be copied after the output of this function.
29 * @tc.type: FUNC
30 * */
31 HWTEST_F(FortifyVsprintfchkTest, __vsprintf_chk_001, TestSize.Level1)
32 {
33 char buffer[BUF_SIZE];
34 const char* format = "%d";
35 int value = 666;
36
37 int result = VsprintfHelper(buffer, sizeof(buffer), format, value);
38 EXPECT_EQ(EXPECT_NUM, result);
39 EXPECT_STREQ("666", buffer);
40 }