Debian Codegraph @

unittest/gunit/my_error-t.cc

raw symbols SCIP
1 /* Copyright (c) 2012, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
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, version 2.0, 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 #include <errno.h>
25 #include <gtest/gtest.h>
26 #include <string.h>
27
28 #include "m_string.h" // native_strcasecmp
29 #include "my_base.h" // HA_ERR_KEY_NOT_FOUND
30 #include "my_config.h"
31 #include "my_sys.h" // my_strerror()
32
33 namespace my_error_unittest {
34
35 TEST(MyErrorTest, MyStrErrorSystem) {
36 char buf[512];
37 const char *msg;
38
39 msg = my_strerror(buf, sizeof(buf) - 1, 9999);
40 EXPECT_TRUE(!native_strcasecmp("unknown error", msg) ||
41 !native_strcasecmp("unknown error: 9999", msg) ||
42 !native_strcasecmp("unknown error 9999", msg))
43 << "msg<" << msg << ">";
44
45 // try a proper error number
46 msg = my_strerror(buf, sizeof(buf) - 1, EPERM);
47 const char *os_msg = strerror(EPERM);
48 EXPECT_STREQ(os_msg, msg) << "msg<" << msg << ">";
49 }
50
51 TEST(MyErrorTest, MyStrErrorHandlerPlugin) {
52 char buf[512];
53 const char *msg;
54
55 // try a HA error number
56 msg = my_strerror(buf, sizeof(buf) - 1, HA_ERR_KEY_NOT_FOUND);
57 EXPECT_STREQ("Didn't find key on read or update", msg);
58 }
59
60 TEST(MyErrorTest, MyGetErrMsgUninitialized) {
61 const char *msg;
62
63 msg = my_get_err_msg(HA_ERR_KEY_NOT_FOUND);
64 EXPECT_TRUE(msg == nullptr);
65 }
66
67 const char *faux_errmsgs[] = {"alpha", "beta", nullptr, "delta"};
68
69 static const int faux_error_first = 8000;
70 static const int faux_error_last = 8003;
71
72 const char *get_faux_errmsg(int nr) {
73 return faux_errmsgs[nr - faux_error_first];
74 }
75
76 TEST(MyErrorTest, MyGetErrMsgInitialized) {
77 const char *msg;
78
79 EXPECT_EQ(
80 0, my_error_register(get_faux_errmsg, faux_error_first, faux_error_last));
81
82 // flag error when trying to register overlapping area
83 EXPECT_NE(0, my_error_register(get_faux_errmsg, faux_error_first + 2,
84 faux_error_last + 2));
85
86 msg = my_get_err_msg(faux_error_first);
87 EXPECT_STREQ("alpha", msg);
88
89 msg = my_get_err_msg(faux_error_first + 1);
90 EXPECT_STREQ("beta", msg);
91
92 // within range. gives NULL here. higher level function will
93 // substitute a default string before printing.
94 msg = my_get_err_msg(faux_error_first + 2);
95 EXPECT_TRUE(msg == nullptr);
96
97 // out of range
98 msg = my_get_err_msg(faux_error_first - 1);
99 EXPECT_TRUE(msg == nullptr);
100
101 msg = my_get_err_msg(faux_error_last);
102 EXPECT_STREQ("delta", msg);
103
104 // out of range
105 msg = my_get_err_msg(faux_error_last + 1);
106 EXPECT_TRUE(msg == nullptr);
107
108 EXPECT_FALSE(my_error_unregister(faux_error_first, faux_error_last));
109
110 // flag error when trying to unregister twice
111 EXPECT_TRUE(my_error_unregister(faux_error_first, faux_error_last));
112 }
113
114 } // namespace my_error_unittest