1 /* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 #include "my_config.h"
24 #include <gtest/gtest.h>
25 #include <string>
26 
27 #include "m_string.h"
28 #include "mysql_com.h"
29 
30 namespace my_snprintf_unittest {
31 
32 using std::string;
33 
34 string sss= string(2 * MYSQL_ERRMSG_SIZE, 'a');
35 
36 class SnPrintfTest : public ::testing::Test
37 {
38 public:
SetUp()39   virtual void SetUp()
40   {
41     compile_time_assert(MYSQL_ERRMSG_SIZE == 512);
42     memset(m_errmsg_buf, 'x', sizeof(m_errmsg_buf));
43   }
44 
45   char m_errmsg_buf[MYSQL_ERRMSG_SIZE * 2];
46 };
47 
TEST_F(SnPrintfTest,FixedPrecisionOne)48 TEST_F(SnPrintfTest, FixedPrecisionOne)
49 {
50   EXPECT_EQ(1, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
51                            "%.1s", sss.data()));
52   EXPECT_EQ('a', m_errmsg_buf[0]);
53   EXPECT_EQ('\0', m_errmsg_buf[1]);
54   EXPECT_EQ('x', m_errmsg_buf[2]);
55 }
56 
TEST_F(SnPrintfTest,FixedPrecisionTwo)57 TEST_F(SnPrintfTest, FixedPrecisionTwo)
58 {
59   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
60                              "%.511s", sss.data()));
61   EXPECT_EQ('a', m_errmsg_buf[0]);
62   EXPECT_EQ('\0', m_errmsg_buf[511]);
63   EXPECT_EQ('x', m_errmsg_buf[512]);
64 }
65 
TEST_F(SnPrintfTest,FixedPrecisionThree)66 TEST_F(SnPrintfTest, FixedPrecisionThree)
67 {
68   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
69                              "%.512s", sss.data()));
70   EXPECT_EQ('a', m_errmsg_buf[0]);
71   EXPECT_EQ('\0', m_errmsg_buf[511]);
72   EXPECT_EQ('x', m_errmsg_buf[512]);
73 }
74 
TEST_F(SnPrintfTest,FixedPrecisionFour)75 TEST_F(SnPrintfTest, FixedPrecisionFour)
76 {
77   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
78                              "%.1000s", sss.data()));
79   EXPECT_EQ('a', m_errmsg_buf[0]);
80   EXPECT_EQ('\0', m_errmsg_buf[511]);
81   EXPECT_EQ('x', m_errmsg_buf[512]);
82 }
83 
TEST_F(SnPrintfTest,DynamicPrecisionOne)84 TEST_F(SnPrintfTest, DynamicPrecisionOne)
85 {
86   EXPECT_EQ(1, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
87                            "%.*s", 1, sss.data()));
88   EXPECT_EQ('a', m_errmsg_buf[0]);
89   EXPECT_EQ('\0', m_errmsg_buf[1]);
90   EXPECT_EQ('x', m_errmsg_buf[2]);
91 }
92 
TEST_F(SnPrintfTest,DynamicPrecisionTwo)93 TEST_F(SnPrintfTest, DynamicPrecisionTwo)
94 {
95   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
96                              "%.*s", 511, sss.data()));
97   EXPECT_EQ('a', m_errmsg_buf[0]);
98   EXPECT_EQ('\0', m_errmsg_buf[511]);
99   EXPECT_EQ('x', m_errmsg_buf[512]);
100 }
101 
TEST_F(SnPrintfTest,DynamicPrecisionThree)102 TEST_F(SnPrintfTest, DynamicPrecisionThree)
103 {
104   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
105                              "%.*s", 512, sss.data()));
106   EXPECT_EQ('a', m_errmsg_buf[0]);
107   EXPECT_EQ('\0', m_errmsg_buf[511]);
108   EXPECT_EQ('x', m_errmsg_buf[512]);
109 }
110 
TEST_F(SnPrintfTest,DynamicPrecisionFour)111 TEST_F(SnPrintfTest, DynamicPrecisionFour)
112 {
113   EXPECT_EQ(511, my_snprintf(m_errmsg_buf, MYSQL_ERRMSG_SIZE,
114                              "%.*s", 1000, sss.data()));
115   EXPECT_EQ('a', m_errmsg_buf[0]);
116   EXPECT_EQ('\0', m_errmsg_buf[511]);
117   EXPECT_EQ('x', m_errmsg_buf[512]);
118 }
119 
120 } // namespace
121