• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 constexpr int BUF_SIZE = 1024;
6 
7 using namespace testing::ext;
8 
9 class FortifySprintfchkchkTest : public testing::Test {
SetUp()10     void SetUp() override {}
TearDown()11     void TearDown() override {}
12 };
13 
14 extern "C" int __sprintf_chk(char* dest, int flags, size_t dst_len_from_compiler, const char* format, ...);
15 
16 /**
17  * @tc.name: __sprintf_chk_001
18  * @tc.desc: Verify __sprintf_chk function handle different formatted strings and parameters as expected
19  *           to ensure the correctness and security of the function in actual use.
20  * @tc.type: FUNC
21  * */
22 HWTEST_F(FortifySprintfchkchkTest, __sprintf_chk_001, TestSize.Level1)
23 {
24     char str[BUF_SIZE];
25     int ret = 0;
26 
27     ret = __sprintf_chk(str, 0, BUF_SIZE, "test");
28     EXPECT_NE(0, ret);
29     EXPECT_STREQ(str, "test");
30 
31     ret = __sprintf_chk(str, 0, BUF_SIZE, "%c", '1');
32     EXPECT_NE(0, ret);
33     EXPECT_STREQ(str, "1");
34 
35     ret = __sprintf_chk(str, 0, BUF_SIZE, "%s,%s", "123", "456");
36     EXPECT_NE(0, ret);
37     EXPECT_STREQ(str, "123,456");
38 
39     ret = __sprintf_chk(str, 0, BUF_SIZE, "%%test");
40     EXPECT_NE(0, ret);
41     EXPECT_STREQ(str, "%test");
42 
43     ret = __sprintf_chk(str, 0, BUF_SIZE, "%3$d,%2$s,%1$c", 'a', "bc", 0);
44     EXPECT_NE(0, ret);
45     EXPECT_STREQ(str, "0,bc,a");
46 }