Debian Codegraph @

unittest/gunit/win_tests-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 <gtest/gtest.h>
25
26 #include "test_utils.h"
27
28 #include "my_io.h"
29 #include "my_thread.h"
30 #include "sql/log.h"
31 #include "sql/named_pipe.h"
32 #include "sql/sql_class.h"
33
34 #include <sddl.h>
35
36 namespace win_unittest {
37 using my_testing::Mock_error_handler;
38 using my_testing::Server_initializer;
39
40 extern "C" void mock_error_handler_hook(uint err, const char *str, myf MyFlags);
41
42 /**
43 An alternative error_handler for non-server unit tests since it does
44 not rely on THD. It sets the global error handler function.
45 Note that a Mock_global_error_handler instance is used in
46 TEST_F(NamedPipeTest, CreatePipeTwice) specifically to verify that a
47 server range error code is passed to my_printf_error during the execution of
48 create_server_named_pipe.
49 The use of my_printf_error with a server range error code would cause
50 an assertion failure if the "usual" combination of Mock_error_handler and
51 my_message_sql were used to verify this expected error code.
52 As and when the logger is refactored to simplify unit testing of expected
53 error codes, the my_printf_error call should be replaced with LogErr or
54 log_message and this use of Mock_global_error_handler replaced with the
55 appropriate (new) logger test.
56 */
57
58 class Mock_global_error_handler {
59 public:
60 explicit Mock_global_error_handler(uint expected_error)
61 : m_expected_error(expected_error), m_handle_called(0) {
62 current = this;
63 m_old_error_handler_hook = error_handler_hook;
64 error_handler_hook = mock_error_handler_hook;
65 }
66
67 virtual ~Mock_global_error_handler() {
68 if (m_expected_error == 0) {
69 EXPECT_EQ(0, m_handle_called);
70 } else {
71 EXPECT_GT(m_handle_called, 0);
72 }
73 error_handler_hook = m_old_error_handler_hook;
74 current = NULL;
75 }
76
77 void error_handler(uint err) {
78 EXPECT_EQ(m_expected_error, err);
79 ++m_handle_called;
80 }
81
82 int handle_called() const { return m_handle_called; }
83
84 static Mock_global_error_handler *current;
85
86 private:
87 uint m_expected_error;
88 int m_handle_called;
89
90 void (*m_old_error_handler_hook)(uint, const char *, myf);
91 };
92 Mock_global_error_handler *Mock_global_error_handler::current = NULL;
93
94 /*
95 Error handler function.
96 */
97 extern "C" void mock_error_handler_hook(uint err, const char *, myf) {
98 if (Mock_global_error_handler::current)
99 Mock_global_error_handler::current->error_handler(err);
100 }
101
102 class NamedPipeTest : public ::testing::Test {
103 protected:
104 static void SetUpTestCase() {
105 m_old_error_handler_hook = error_handler_hook;
106 // Make sure my_error() ends up calling my_message_sql so that
107 // Mock_error_handler is actually triggered.
108 error_handler_hook = my_message_sql;
109 }
110
111 static void TearDownTestCase() {
112 error_handler_hook = m_old_error_handler_hook;
113 }
114
115 void SetUp() override {
116 m_initializer.SetUp();
117
118 char pipe_rand_name[256];
119
120 m_pipe_handle = INVALID_HANDLE_VALUE;
121
122 /*
123 Generate a Unique Pipe Name incase multiple instances of the test is run.
124 */
125 sprintf_s(pipe_rand_name, sizeof(pipe_rand_name), "Pipe-%x",
126 GetTickCount());
127
128 const ::testing::TestInfo *const test_info =
129 ::testing::UnitTest::GetInstance()->current_test_info();
130
131 m_name.append(pipe_rand_name);
132 m_name.append("gunit");
133 m_name.append(test_info->name());
134 }
135
136 void TearDown() override {
137 if (m_pipe_handle != INVALID_HANDLE_VALUE) {
138 EXPECT_TRUE(CloseHandle(m_pipe_handle));
139 }
140 m_initializer.TearDown();
141 }
142
143 SECURITY_ATTRIBUTES *mp_sec_attr;
144 char m_pipe_name[256];
145 HANDLE m_pipe_handle;
146 std::string m_name;
147 Server_initializer m_initializer;
148
149 static ErrorHandlerFunctionPointer m_old_error_handler_hook;
150 };
151 ErrorHandlerFunctionPointer NamedPipeTest::m_old_error_handler_hook;
152
153 // Basic test: create a named pipe.
154 TEST_F(NamedPipeTest, CreatePipe) {
155 char exp_pipe_name[256];
156
157 m_pipe_handle = create_server_named_pipe(&mp_sec_attr, 1024, m_name.c_str(),
158 m_pipe_name, sizeof(m_pipe_name));
159
160 strxnmov(exp_pipe_name, sizeof(exp_pipe_name) - 1, "\\\\.\\pipe\\",
161 m_name.c_str(), NullS);
162
163 EXPECT_STREQ(m_pipe_name, exp_pipe_name);
164 EXPECT_NE(INVALID_HANDLE_VALUE, m_pipe_handle);
165 }
166
167 // Verify that we fail if we try to create the same named pipe twice.
168 TEST_F(NamedPipeTest, CreatePipeTwice) {
169 m_pipe_handle = create_server_named_pipe(&mp_sec_attr, 1024, m_name.c_str(),
170 m_pipe_name, sizeof(m_pipe_name));
171 EXPECT_NE(INVALID_HANDLE_VALUE, m_pipe_handle);
172 // Use Mock_global_error_handler rather than Mock_error_handler to verify
173 // server error codes (routing server error codes through my_message_sql
174 // via the error_handler_hook with Mock_error_handler would fail an assertion)
175 Mock_global_error_handler error_handler(ER_NPIPE_PIPE_ALREADY_IN_USE);
176 HANDLE handle = create_server_named_pipe(&mp_sec_attr, 1024, m_name.c_str(),
177 m_pipe_name, sizeof(m_pipe_name));
178 EXPECT_EQ(INVALID_HANDLE_VALUE, handle);
179 }
180
181 // Verify that a warning is written to the error log when using
182 // "*everyone* as the full access group name.
183 TEST_F(NamedPipeTest, CreatePipeForEveryone) {
184 Mock_error_handler error_handler(m_initializer.thd(),
185 WARN_NAMED_PIPE_ACCESS_EVERYONE);
186 m_pipe_handle =
187 create_server_named_pipe(&mp_sec_attr, 1024, m_name.c_str(), m_pipe_name,
188 sizeof(m_pipe_name), "*everyone*");
189 EXPECT_NE(INVALID_HANDLE_VALUE, m_pipe_handle);
190 }
191
192 // Verify that a warning is written to the error log when using
193 // the group name corresponding to the built in Windows group
194 // with SID S-1-1-0 (i.e. "everyone" on English systems)
195 TEST_F(NamedPipeTest, CreatePipeForEveryoneSid) {
196 PSID everyone_SID;
197 EXPECT_TRUE(ConvertStringSidToSid("S-1-1-0", &everyone_SID));
198 const DWORD max_name_len = 256;
199 char everyone_name[max_name_len];
200 DWORD everyone_name_size = max_name_len;
201 char domain_name[max_name_len];
202 DWORD domain_name_size = max_name_len;
203 SID_NAME_USE name_use;
204
205 EXPECT_TRUE(LookupAccountSid(NULL, everyone_SID, everyone_name,
206 &everyone_name_size, domain_name,
207 &domain_name_size, &name_use));
208 // The "S-1-1-0" SID is well known, so we expect the domain_name to empty and
209 // the name_use to be SidTypeWellKnownGroup
210 EXPECT_EQ(domain_name_size, 0);
211 EXPECT_EQ(name_use, SidTypeWellKnownGroup);
212
213 Mock_error_handler error_handler(m_initializer.thd(),
214 WARN_NAMED_PIPE_ACCESS_EVERYONE);
215 m_pipe_handle =
216 create_server_named_pipe(&mp_sec_attr, 1024, m_name.c_str(), m_pipe_name,
217 sizeof(m_pipe_name), everyone_name);
218 EXPECT_NE(INVALID_HANDLE_VALUE, m_pipe_handle);
219 }
220
221 } // namespace win_unittest