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