1 /*
2   Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 */
24 
25 #include <thread>
26 
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 
30 #include "common.h"
31 
32 using ::testing::StrEq;
33 
TEST(TestCommon,truncate_string)34 TEST(TestCommon, truncate_string) {
35   // This test tests truncate_string() and truncate_string_r(). Since both
36   // utilize the same backend, common functionality tests are ran only on
37   // truncate_string().
38 
39   using mysql_harness::truncate_string;
40   using mysql_harness::truncate_string_r;
41 
42   constexpr size_t kMinMaxLen = 6;  // max_len less than this triggers assertion
43 
44   // simple case
45   {
46     const std::string s = "1234567890";
47     size_t len = s.size();
48     EXPECT_TRUE(truncate_string(s, len + 1) == s);
49     EXPECT_TRUE(truncate_string(s, len + 0) == s);
50     EXPECT_THAT(truncate_string(s, len - 1), StrEq("123456..."));
51   }
52 
53 // max_len too short
54 // This test doesn't work in Windows or FreeBSD, because of how ASSERT_DEATH
55 // works It also fails on release version
56 #if !defined(_WIN32) && !defined(__FreeBSD__) && !defined(NDEBUG)
57   { ASSERT_DEATH(truncate_string("123456", kMinMaxLen - 1), ""); }
58 #endif
59 
60   // string len = kMinMaxLen
61   {
62     const std::string s = "123456";
63     size_t len = s.size();
64     EXPECT_TRUE(truncate_string(s, len + 1) == s);
65     EXPECT_TRUE(truncate_string(s, len + 0) == s);
66     // testing with len-1 would trigger assertion, as shown in the previous test
67   }
68 
69   // short string
70   {
71     const std::string s = "1";
72     EXPECT_TRUE(truncate_string(s, kMinMaxLen) == s);
73   }
74 
75   // empty string
76   {
77     const std::string s = "";
78     EXPECT_TRUE(truncate_string(s, kMinMaxLen) == s);
79   }
80 
81   // thread-safety test
82   {
83     const std::string &r1 = truncate_string("1234567890", 8);
84 
85     std::thread([]() {
86       const std::string &r2 = truncate_string("abcdefghij", 8);
87       EXPECT_THAT(r2, StrEq("abcde..."));
88     }).join();
89 
90     // call to truncate_string() in another thread should not overwrite this
91     // result
92     EXPECT_THAT(r1, StrEq("12345..."));
93 
94     // but calling it in this thread will (this funcionality is not a
95     // requirement, the test only demonstrates the weakness)
96     truncate_string("blablabla", 8);
97     EXPECT_THAT(r1, StrEq("blabl..."));
98   }
99 
100   // re-entry test (using truncate_string_r() instead of truncate_string())
101   {
102     const std::string &r1 = truncate_string_r("1234567890", 8);
103 
104     std::thread([]() {
105       const std::string &r2 = truncate_string_r("abcdefghij", 8);
106       EXPECT_THAT(r2, StrEq("abcde..."));
107     }).join();
108 
109     // call to truncate_string_r() in another thread should not overwrite this
110     // result
111     EXPECT_THAT(r1, StrEq("12345..."));
112 
113     // call to truncate_string_r() in this thread should not overwrite this
114     // result
115     truncate_string_r("blablabla", 8);
116     EXPECT_THAT(r1, StrEq("12345..."));
117   }
118 }
119 
TEST(TestCommon,SerialComma)120 TEST(TestCommon, SerialComma) {
121   using mysql_harness::serial_comma;
122 
123   auto expect_output = [](int count, const std::string &expect) {
124     constexpr int primes[]{2, 3, 5, 7, 11};
125 
126     std::string res = "Primes are ";
127     res += serial_comma(&primes[0], &primes[count]);
128     EXPECT_EQ(res, "Primes are " + expect);
129   };
130 
131   expect_output(1, "2");
132   expect_output(2, "2 and 3");
133   expect_output(3, "2, 3, and 5");
134   expect_output(5, "2, 3, 5, 7, and 11");
135 }
136 
main(int argc,char ** argv)137 int main(int argc, char **argv) {
138   ::testing::InitGoogleTest(&argc, argv);
139   return RUN_ALL_TESTS();
140 }
141