MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_error-t.cc
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
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.
11 
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 */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "my_sys.h" // my_strerror()
21 #include "my_base.h" // HA_ERR_KEY_NOT_FOUND
22 #include <string.h>
23 
24 namespace my_error_unittest {
25 
26 TEST(MyErrorTest, MyStrErrorSystem)
27 {
28  char buf[512];
29  const char *msg;
30 
31  msg= my_strerror(buf, sizeof(buf) - 1, 9999);
32  EXPECT_TRUE(!strcasecmp("unknown error", msg) ||
33  !strcasecmp("unknown error: 9999", msg) ||
34  !strcasecmp("unknown error 9999", msg))
35  << "msg<" << msg << ">";
36 
37  // try a proper error number
38  msg= my_strerror(buf, sizeof(buf) - 1, EPERM);
39  const char *os_msg= strerror(EPERM);
40  EXPECT_STREQ(os_msg, msg)
41  << "msg<" << msg << ">";
42 }
43 
44 TEST(MyErrorTest, MyStrErrorHandlerPlugin)
45 {
46  char buf[512];
47  const char *msg;
48 
49  // try a HA error number
50  msg= my_strerror(buf, sizeof(buf) - 1, HA_ERR_KEY_NOT_FOUND);
51  EXPECT_STREQ("Didn't find key on read or update", msg);
52 }
53 
54 TEST(MyErrorTest, MyGetErrMsgUnitialized)
55 {
56  const char *msg;
57 
58  msg= my_get_err_msg(HA_ERR_KEY_NOT_FOUND);
59  EXPECT_TRUE(msg == NULL);
60 }
61 
62 const char *faux_errmsgs[]= { "alpha", "beta", NULL, "delta" };
63 
64 const char** get_faux_errmsgs()
65 {
66  return faux_errmsgs;
67 }
68 
69 static const int faux_error_first= 8000;
70 static const int faux_error_last= 8003;
71 
72 TEST(MyErrorTest, MyGetErrMsgInitialized)
73 {
74  const char *msg;
75 
76  EXPECT_EQ(0, my_error_register(get_faux_errmsgs,
77  faux_error_first,
78  faux_error_last));
79 
80  // flag error when trying to register overlapping area
81  EXPECT_NE(0, my_error_register(get_faux_errmsgs,
82  faux_error_first + 2,
83  faux_error_last + 2));
84 
85  msg= my_get_err_msg(faux_error_first);
86  EXPECT_STREQ("alpha", msg);
87 
88  msg= my_get_err_msg(faux_error_first + 1);
89  EXPECT_STREQ("beta", msg);
90 
91  // within range. gives NULL here. higher level function will
92  // substitute a default string before printing.
93  msg= my_get_err_msg(faux_error_first + 2);
94  EXPECT_TRUE(msg == NULL);
95 
96  // out of range
97  msg= my_get_err_msg(faux_error_first - 1);
98  EXPECT_TRUE(msg == NULL);
99 
100  msg= my_get_err_msg(faux_error_last);
101  EXPECT_STREQ("delta", msg);
102 
103  // out of range
104  msg= my_get_err_msg(faux_error_last + 1);
105  EXPECT_TRUE(msg == NULL);
106 
107  EXPECT_TRUE(my_error_unregister(faux_error_first, faux_error_last) != NULL);
108 
109  // flag error when trying to unregister twice
110  EXPECT_TRUE(my_error_unregister(faux_error_first, faux_error_last) == NULL);
111 }
112 
113 }