1
/* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
// Always include (the generated) my_config.h, to get correct platform defines.
17
#include "my_config.h"
18
#include <gtest/gtest.h>
20
#include "test_utils.h"
21
#include "my_stacktrace.h"
23
#include "hash_filo.h"
25
namespace segfault_unittest {
27
using my_testing::Server_initializer;
28
using my_testing::Mock_error_handler;
30
class FatalSignalDeathTest : public ::testing::Test
35
::testing::FLAGS_gtest_death_test_style = "threadsafe";
38
virtual void TearDown() { initializer.TearDown(); }
40
Server_initializer initializer;
44
TEST_F(FatalSignalDeathTest, Abort)
47
EXPECT_DEATH_IF_SUPPORTED(abort(), ".* UTC - mysqld got exception.*");
49
EXPECT_DEATH_IF_SUPPORTED(abort(), ".* UTC - mysqld got signal 6.*");
54
TEST_F(FatalSignalDeathTest, Segfault)
59
After upgrading from gtest 1.5 to 1.6 this segfault is no longer
60
caught by handle_fatal_signal(). We get an empty error message from the
61
gtest library instead.
63
EXPECT_DEATH_IF_SUPPORTED(*pint= 42, "");
64
#elif defined(__SANITIZE_ADDRESS__)
65
/* gcc 4.8.1 with '-fsanitize=address -O1' */
66
EXPECT_DEATH_IF_SUPPORTED(*pint= 42, ".*ASAN:SIGSEGV.*");
69
On most platforms we get SIGSEGV == 11, but SIGBUS == 10 is also possible.
70
And on Mac OsX we can get SIGILL == 4 (but only in optmized mode).
72
EXPECT_DEATH_IF_SUPPORTED(*pint= 42, ".* UTC - mysqld got signal .*");
77
// A simple helper function to determine array size.
78
template <class T, int size>
79
int array_size(const T (&)[size])
85
// Verifies that my_safe_utoa behaves like sprintf(_, "%llu", _)
86
TEST(PrintUtilities, Utoa)
89
ulonglong intarr[]= { 0, 1, 8, 12, 1234, 88888, ULONG_MAX, ULONGLONG_MAX };
91
for (int ix= 0; ix < array_size(intarr); ++ix)
94
sprintf(sprintbuff, "%llu", intarr[ix]);
95
my_res= my_safe_utoa(10, intarr[ix], &buff[sizeof(buff)-1]);
96
EXPECT_STREQ(sprintbuff, my_res);
98
if (intarr[ix] <= ULONG_MAX)
100
sprintf(sprintbuff, "%lu", (ulong) intarr[ix]);
101
my_res= my_safe_utoa(10, (ulong) intarr[ix], &buff[sizeof(buff)-1]);
102
EXPECT_STREQ(sprintbuff, my_res);
108
// Verifies that my_safe_itoa behaves like sprintf(_, "%lld", _)
109
TEST(PrintUtilities, Itoa)
113
longlong intarr[]= { 0, 1, 8, 12, 1234, 88888, LONG_MAX, LONGLONG_MAX };
115
for (int ix= 0; ix < array_size(intarr); ++ix)
118
sprintf(sprintbuff, "%lld", intarr[ix]);
119
my_res= my_safe_itoa(10, intarr[ix], &buff[sizeof(buff)-1]);
120
EXPECT_STREQ(sprintbuff, my_res);
122
ll2str(intarr[ix], buff, 10, 0);
123
EXPECT_STREQ(sprintbuff, buff);
125
sprintf(sprintbuff, "%lld", -intarr[ix]);
126
my_res= my_safe_itoa(10, -intarr[ix], &buff[sizeof(buff)-1]);
127
EXPECT_STREQ(sprintbuff, my_res);
129
// This one fails ....
130
// ll2str(-intarr[ix], buff, 10, 0);
131
// EXPECT_STREQ(sprintbuff, buff)
132
// << "failed for " << -intarr[ix];
134
sprintf(sprintbuff, "%llx", intarr[ix]);
135
my_res= my_safe_itoa(16, intarr[ix], &buff[sizeof(buff)-1]);
136
EXPECT_STREQ(sprintbuff, my_res);
138
ll2str(intarr[ix], buff, 16, 0);
139
EXPECT_STREQ(sprintbuff, buff);
141
sprintf(sprintbuff, "%llx", -intarr[ix]);
142
my_res= my_safe_itoa(16, -intarr[ix], &buff[sizeof(buff)-1]);
143
EXPECT_STREQ(sprintbuff, my_res)
144
<< "failed for " << -intarr[ix];
146
ll2str(-intarr[ix], buff, 16, 0);
147
EXPECT_STREQ(sprintbuff, buff);
152
// Various tests for my_safe_snprintf.
153
TEST(PrintUtilities, Printf)
156
char sprintfbuff[512];
157
const char *null_str= NULL;
159
my_safe_snprintf(buff, sizeof(buff), "hello");
160
EXPECT_STREQ("hello", buff);
162
my_safe_snprintf(buff, sizeof(buff), "hello %s hello", "hello");
163
EXPECT_STREQ("hello hello hello", buff);
164
my_safe_snprintf(buff, sizeof(buff), "hello %s hello", null_str);
165
EXPECT_STREQ("hello (null) hello", buff);
167
my_safe_snprintf(buff, sizeof(buff), "hello %d hello", 42);
168
EXPECT_STREQ("hello 42 hello", buff);
169
my_safe_snprintf(buff, sizeof(buff), "hello %i hello", 42);
170
EXPECT_STREQ("hello 42 hello", buff);
171
my_safe_snprintf(buff, sizeof(buff), "hello %u hello", (unsigned) 42);
172
EXPECT_STREQ("hello 42 hello", buff);
174
my_safe_snprintf(buff, sizeof(buff), "hello %llu hello", ULONGLONG_MAX);
175
sprintf(sprintfbuff, "hello %llu hello", ULONGLONG_MAX);
176
EXPECT_STREQ(sprintfbuff, buff);
178
my_safe_snprintf(buff, sizeof(buff), "hello %x hello", 42);
179
EXPECT_STREQ("hello 2a hello", buff);
181
my_safe_snprintf(buff, sizeof(buff), "hello %x hello", -42);
182
sprintf(sprintfbuff, "hello %x hello", -42);
183
EXPECT_STREQ("hello ffffffd6 hello", buff);
184
EXPECT_STREQ(sprintfbuff, buff);
186
my_safe_snprintf(buff, sizeof(buff), "hello %llx hello", (longlong) -42);
187
sprintf(sprintfbuff, "hello %llx hello", (longlong) -42);
188
EXPECT_STREQ("hello ffffffffffffffd6 hello", buff);
189
EXPECT_STREQ(sprintfbuff, buff);
192
my_safe_snprintf(buff, sizeof(buff), "hello 0x%p hello", p);
193
my_snprintf(sprintfbuff, sizeof(sprintfbuff), "hello %p hello", p);
194
EXPECT_STREQ(sprintfbuff, buff);
198
// After the fix for Bug#14689561, this is no longer a death test.
199
TEST(HashFiloTest, TestHashFiloZeroSize)
202
t_cache= new hash_filo(5, 0, 0,
203
(my_hash_get_key) NULL,
204
(my_hash_free_key) NULL,
208
hash_filo_element entry;
209
// After resize (to zero) it tries to dereference last_link which is NULL.
210
t_cache->add(&entry);