Debian Codegraph @

unittest/gunit/get_diagnostics-t.cc

raw symbols SCIP
1 /* Copyright (c) 2011, 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 #include <stddef.h>
26 #include <sys/types.h>
27
28 #include "lex_string.h"
29 #include "my_dbug.h"
30 #include "my_inttypes.h"
31 #include "sql/item.h"
32 #include "sql/item_func.h"
33 #include "sql/sql_class.h"
34 #include "sql/sql_get_diagnostics.h"
35 #include "unittest/gunit/test_utils.h"
36
37 namespace get_diagnostics_unittest {
38
39 using my_testing::Mock_error_handler;
40 using my_testing::Server_initializer;
41
42 class GetDiagnosticsTest : public ::testing::Test {
43 protected:
44 void SetUp() override { initializer.SetUp(); }
45 void TearDown() override { initializer.TearDown(); }
46
47 THD *thd() { return initializer.thd(); }
48
49 Server_initializer initializer;
50 };
51
52 class FailHelper {
53 public:
54 void fail(const char *message) { FAIL() << message; }
55 };
56
57 LEX_CSTRING var_name1 = {STRING_WITH_LEN("var1")};
58 LEX_CSTRING var_name2 = {STRING_WITH_LEN("var2")};
59
60 class MockDiagInfoItem : public Diagnostics_information_item {
61 public:
62 MockDiagInfoItem(Item *target, int value)
63 : Diagnostics_information_item(target), m_value(value) {}
64
65 Item *get_value(THD *thd, const Diagnostics_area *) {
66 return new (thd->mem_root) Item_int(m_value);
67 }
68
69 private:
70 int m_value;
71 };
72
73 class MockDiagInfo : public Diagnostics_information, private FailHelper {
74 public:
75 MockDiagInfo(List<MockDiagInfoItem> *items) : m_items(items) {}
76
77 protected:
78 bool aggregate(THD *thd, const Diagnostics_area *da) override {
79 bool rv = false;
80 MockDiagInfoItem *diag_info_item;
81 List_iterator<MockDiagInfoItem> it(*m_items);
82
83 while ((diag_info_item = it++)) {
84 if ((rv = evaluate(thd, diag_info_item, da))) break;
85 }
86
87 return rv;
88 }
89
90 ~MockDiagInfo() override { fail("MockDiagInfo destructor invoked."); }
91
92 private:
93 List<MockDiagInfoItem> *m_items;
94 };
95
96 // GET [CURRENT] DIAGNOSTICS @var1 = 1, @var2 = 2;
97 TEST_F(GetDiagnosticsTest, Cmd) {
98 Item *var;
99 Sql_cmd *cmd;
100 MockDiagInfo *info;
101 MockDiagInfoItem *diag_info_item;
102 List<MockDiagInfoItem> items;
103 MEM_ROOT *mem_root = thd()->mem_root;
104
105 // set var1 item
106 var = new (mem_root) Item_func_get_user_var(var_name1);
107 diag_info_item = new (mem_root) MockDiagInfoItem(var, 1);
108 EXPECT_FALSE(items.push_back(diag_info_item));
109
110 // set var2 item
111 var = new (mem_root) Item_func_get_user_var(var_name2);
112 diag_info_item = new (mem_root) MockDiagInfoItem(var, 2);
113 EXPECT_FALSE(items.push_back(diag_info_item));
114
115 // Information list and command
116 info = new (mem_root) MockDiagInfo(&items);
117 info->set_which_da(Diagnostics_information::CURRENT_AREA);
118 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
119
120 EXPECT_FALSE(cmd->execute(thd()));
121 EXPECT_TRUE(thd()->get_stmt_da()->is_ok());
122
123 // check var1 value
124 var = new (mem_root) Item_func_get_user_var(var_name1);
125 EXPECT_FALSE(var->fix_fields(thd(), &var));
126 EXPECT_EQ(1, var->val_int());
127
128 // check var2 value
129 var = new (mem_root) Item_func_get_user_var(var_name2);
130 EXPECT_FALSE(var->fix_fields(thd(), &var));
131 EXPECT_EQ(2, var->val_int());
132 }
133
134 // Verifies death with a assert if target item is not settable.
135 // Google Test recommends DeathTest suffix for classes used in death tests.
136 typedef GetDiagnosticsTest GetDiagnosticsTestDeathTest;
137
138 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
139 TEST_F(GetDiagnosticsTestDeathTest, DieWhenUnsettableItem) {
140 Item *var;
141 Sql_cmd *cmd;
142 MockDiagInfo *info;
143 MockDiagInfoItem *diag_info_item;
144 List<MockDiagInfoItem> items;
145 MEM_ROOT *mem_root = thd()->mem_root;
146
147 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
148
149 // Unsettable item
150 var = new (mem_root) Item_int(1);
151 diag_info_item = new (mem_root) MockDiagInfoItem(var, 1);
152 EXPECT_FALSE(items.push_back(diag_info_item));
153
154 // Information list and command
155 info = new (mem_root) MockDiagInfo(&items);
156 info->set_which_da(Diagnostics_information::CURRENT_AREA);
157 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
158
159 EXPECT_DEATH(cmd->execute(thd()), ".*Assertion.*srp.*");
160 }
161 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
162
163 class MockDiagInfoError : public Diagnostics_information {
164 public:
165 MockDiagInfoError(bool fatal_error) : m_fatal_error(fatal_error) {}
166
167 protected:
168 bool aggregate(THD *thd, const Diagnostics_area *) override {
169 myf flag = m_fatal_error ? MYF(ME_FATALERROR) : MYF(0);
170 my_message_sql(ER_UNKNOWN_ERROR, "Unknown error", flag);
171 return thd->is_error();
172 }
173
174 private:
175 bool m_fatal_error;
176 };
177
178 // GET DIAGNOSTICS itself causes an error.
179 TEST_F(GetDiagnosticsTest, Error) {
180 Sql_cmd *cmd;
181 MockDiagInfoError *info;
182 MEM_ROOT *mem_root = thd()->mem_root;
183
184 // Pre-existing warning
185 push_warning_printf(thd(), Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED,
186 "Data truncated");
187
188 // Simulate GET DIAGNOSTICS as a new command separated
189 // from the one that generated the warning
190 thd()->reset_for_next_command();
191
192 // Error bound "information" and command
193 info = new (mem_root) MockDiagInfoError(false);
194 info->set_which_da(Diagnostics_information::CURRENT_AREA);
195 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
196
197 initializer.set_expected_error(ER_UNKNOWN_ERROR);
198
199 // Should succeed, not a fatal error
200 EXPECT_FALSE(cmd->execute(thd()));
201 EXPECT_TRUE(thd()->get_stmt_da()->is_ok());
202
203 // New condition for the error
204 EXPECT_EQ(1U, thd()->get_stmt_da()->last_statement_cond_count());
205
206 // Counted as a error
207 EXPECT_EQ(1U, thd()->get_stmt_da()->error_count(thd()));
208
209 // Error is appended
210 EXPECT_EQ(2U, thd()->get_stmt_da()->warn_count(thd()));
211 }
212
213 // GET DIAGNOSTICS itself causes a fatal error.
214 TEST_F(GetDiagnosticsTest, FatalError) {
215 Sql_cmd *cmd;
216 MockDiagInfoError *info;
217 MEM_ROOT *mem_root = thd()->mem_root;
218
219 // Pre-existing warning
220 push_warning_printf(thd(), Sql_condition::SL_WARNING, WARN_DATA_TRUNCATED,
221 "Data truncated");
222
223 // Simulate GET DIAGNOSTICS as a new command separated
224 // from the one that generated the warning
225 thd()->reset_for_next_command();
226
227 // Error bound "information" and command
228 info = new (mem_root) MockDiagInfoError(true);
229 info->set_which_da(Diagnostics_information::CURRENT_AREA);
230 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
231
232 initializer.set_expected_error(ER_UNKNOWN_ERROR);
233
234 // Should not succeed due to a fatal error
235 EXPECT_TRUE(cmd->execute(thd()));
236 EXPECT_TRUE(thd()->get_stmt_da()->is_error());
237
238 // No new condition for the error
239 EXPECT_EQ(0U, thd()->get_stmt_da()->error_count(thd()));
240
241 // Fatal error is set, not appended
242 EXPECT_EQ(1U, thd()->get_stmt_da()->warn_count(thd()));
243 }
244
245 // GET [CURRENT] DIAGNOSTICS @var1 = NUMBER, @var2 = ROW_COUNT;
246 TEST_F(GetDiagnosticsTest, StatementInformation) {
247 Item *var;
248 Sql_cmd *cmd;
249 Statement_information *info;
250 Statement_information_item *diag_info_item;
251 List<Statement_information_item> items;
252 MEM_ROOT *mem_root = thd()->mem_root;
253
254 // NUMBER = 1 warning
255 thd()->raise_warning(ER_UNKNOWN_ERROR);
256 // ROW_COUNT = 5
257 thd()->set_row_count_func(5U);
258
259 // var1 will receive the value of NUMBER
260 var = new (mem_root) Item_func_get_user_var(var_name1);
261 diag_info_item = new (mem_root)
262 Statement_information_item(Statement_information_item::NUMBER, var);
263 EXPECT_FALSE(items.push_back(diag_info_item));
264
265 // var2 will receive the value of ROW_COUNT
266 var = new (mem_root) Item_func_get_user_var(var_name2);
267 diag_info_item = new (mem_root)
268 Statement_information_item(Statement_information_item::ROW_COUNT, var);
269 EXPECT_FALSE(items.push_back(diag_info_item));
270
271 // Information list and command
272 info = new (mem_root) Statement_information(&items);
273 info->set_which_da(Diagnostics_information::CURRENT_AREA);
274 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
275
276 EXPECT_FALSE(cmd->execute(thd()));
277 EXPECT_TRUE(thd()->get_stmt_da()->is_ok());
278
279 // check var1 value
280 var = new (mem_root) Item_func_get_user_var(var_name1);
281 EXPECT_FALSE(var->fix_fields(thd(), &var));
282 EXPECT_EQ(1U, var->val_uint());
283
284 // check var2 value
285 var = new (mem_root) Item_func_get_user_var(var_name2);
286 EXPECT_FALSE(var->fix_fields(thd(), &var));
287 EXPECT_EQ(5U, var->val_int());
288 }
289
290 // GET DIAGNOSTICS CONDITION 1 @var1 = MYSQL_ERRNO, @var2 = MESSAGE_TEXT;
291 TEST_F(GetDiagnosticsTest, ConditionInformation) {
292 Item *var;
293 String str;
294 Sql_cmd *cmd;
295 Condition_information *info;
296 Condition_information_item *diag_info_item;
297 List<Condition_information_item> items;
298 MEM_ROOT *mem_root = thd()->mem_root;
299
300 // Pre-existing error
301 my_message_sql(ER_UNKNOWN_ERROR, "Unknown error", MYF(0));
302
303 // Simulate GET DIAGNOSTICS as a new command separated
304 // from the one that generated the error
305 thd()->reset_for_next_command();
306
307 // var1 will receive the value of MYSQL_ERRNO
308 var = new (mem_root) Item_func_get_user_var(var_name1);
309 diag_info_item = new (mem_root)
310 Condition_information_item(Condition_information_item::MYSQL_ERRNO, var);
311 EXPECT_FALSE(items.push_back(diag_info_item));
312
313 // var2 will receive the value of MESSAGE_TEXT
314 var = new (mem_root) Item_func_get_user_var(var_name2);
315 diag_info_item = new (mem_root)
316 Condition_information_item(Condition_information_item::MESSAGE_TEXT, var);
317 EXPECT_FALSE(items.push_back(diag_info_item));
318
319 // condition number (1)
320 var = new (mem_root) Item_uint(1);
321
322 // Information list and command
323 info = new (mem_root) Condition_information(var, &items);
324 info->set_which_da(Diagnostics_information::CURRENT_AREA);
325 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
326
327 EXPECT_FALSE(cmd->execute(thd()));
328 EXPECT_TRUE(thd()->get_stmt_da()->is_ok());
329
330 // check var1 value
331 var = new (mem_root) Item_func_get_user_var(var_name1);
332 EXPECT_FALSE(var->fix_fields(thd(), &var));
333 EXPECT_EQ(ulonglong(ER_UNKNOWN_ERROR), var->val_uint());
334
335 // check var2 value
336 var = new (mem_root) Item_func_get_user_var(var_name2);
337 EXPECT_FALSE(var->fix_fields(thd(), &var));
338 EXPECT_EQ(&str, var->val_str(&str));
339 EXPECT_STREQ("Unknown error", str.c_ptr_safe());
340 }
341
342 static Item *get_cond_info_item(THD *thd, uint number,
343 Condition_information_item::Name name) {
344 Item *var;
345 Sql_cmd *cmd;
346 Condition_information *info;
347 Condition_information_item *diag_info_item;
348 List<Condition_information_item> items;
349 MEM_ROOT *mem_root = thd->mem_root;
350 LEX_CSTRING var_name = {STRING_WITH_LEN("get_cond_info_item")};
351
352 // Simulate GET DIAGNOSTICS as a new command
353 thd->reset_for_next_command();
354
355 // var1 will receive the value of MYSQL_ERRNO
356 var = new (mem_root) Item_func_get_user_var(var_name);
357 diag_info_item = new (mem_root) Condition_information_item(name, var);
358 EXPECT_FALSE(items.push_back(diag_info_item));
359
360 // condition number
361 var = new (mem_root) Item_uint(number);
362
363 // Information list and command
364 info = new (mem_root) Condition_information(var, &items);
365 info->set_which_da(Diagnostics_information::CURRENT_AREA);
366 cmd = new (mem_root) Sql_cmd_get_diagnostics(info);
367
368 EXPECT_FALSE(cmd->execute(thd));
369 EXPECT_TRUE(thd->get_stmt_da()->is_ok());
370
371 // make a user var item
372 var = new (mem_root) Item_func_get_user_var(var_name);
373 EXPECT_FALSE(var->fix_fields(thd, &var));
374
375 return var;
376 }
377
378 // GET DIAGNOSTICS CONDITION 1 @var = CLASS_ORIGIN;
379 // GET DIAGNOSTICS CONDITION 1 @var = SUBCLASS_ORIGIN;
380 TEST_F(GetDiagnosticsTest, ConditionInformationClassOrigin) {
381 Item *var;
382 String str;
383
384 // "MySQL" origin
385 push_warning_printf(thd(), Sql_condition::SL_WARNING, ER_XAER_NOTA,
386 "Unknown XID");
387
388 // "ISO 9075" origin
389 push_warning_printf(thd(), Sql_condition::SL_WARNING, ER_UNKNOWN_ERROR,
390 "Unknown error");
391
392 // Condition 1 CLASS_ORIGIN
393 var = get_cond_info_item(thd(), 1, Condition_information_item::CLASS_ORIGIN);
394 EXPECT_EQ(&str, var->val_str(&str));
395 EXPECT_STREQ("MySQL", str.c_ptr_safe());
396
397 // Condition 1 SUBCLASS_ORIGIN
398 var =
399 get_cond_info_item(thd(), 1, Condition_information_item::SUBCLASS_ORIGIN);
400 EXPECT_EQ(&str, var->val_str(&str));
401 EXPECT_STREQ("MySQL", str.c_ptr_safe());
402
403 // Condition 2 CLASS_ORIGIN
404 var = get_cond_info_item(thd(), 2, Condition_information_item::CLASS_ORIGIN);
405 EXPECT_EQ(&str, var->val_str(&str));
406 EXPECT_STREQ("ISO 9075", str.c_ptr_safe());
407
408 // Condition 2 CLASS_ORIGIN
409 var =
410 get_cond_info_item(thd(), 2, Condition_information_item::SUBCLASS_ORIGIN);
411 EXPECT_EQ(&str, var->val_str(&str));
412 EXPECT_STREQ("ISO 9075", str.c_ptr_safe());
413 }
414
415 // Push + pop diagnostics area
416 TEST_F(GetDiagnosticsTest, PushPopDiagnosticsArea) {
417 Diagnostics_area da1(false);
418 Diagnostics_area da2(false);
419 Diagnostics_area *org_da = thd()->get_stmt_da();
420
421 thd()->push_diagnostics_area(&da1);
422 EXPECT_EQ(&da1, thd()->get_stmt_da());
423 EXPECT_EQ(org_da, thd()->get_stacked_da());
424
425 thd()->push_diagnostics_area(&da2);
426 EXPECT_EQ(&da2, thd()->get_stmt_da());
427 EXPECT_EQ(&da1, thd()->get_stacked_da());
428
429 thd()->pop_diagnostics_area();
430 EXPECT_EQ(&da1, thd()->get_stmt_da());
431 EXPECT_EQ(org_da, thd()->get_stacked_da());
432
433 thd()->pop_diagnostics_area();
434 EXPECT_EQ(org_da, thd()->get_stmt_da());
435 EXPECT_TRUE(thd()->get_stacked_da() == nullptr);
436 }
437
438 // Pop when there is just one diagnostics area = assert
439 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
440 TEST_F(GetDiagnosticsTestDeathTest, DiePopDiagnosticsArea) {
441 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
442
443 EXPECT_DEATH(thd()->pop_diagnostics_area(), ".*Assertion.*m_stacked_da*");
444 }
445 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
446
447 // Pushed diagnostics area should initially contain copy of conditions
448 TEST_F(GetDiagnosticsTest, PushDiagnosticsArea) {
449 Diagnostics_area da(false);
450 Diagnostics_area *org_da = thd()->get_stmt_da();
451
452 Item *var;
453 String str;
454
455 // "MySQL" origin
456 push_warning_printf(thd(), Sql_condition::SL_WARNING, ER_XAER_NOTA,
457 "Unknown XID");
458
459 // "ISO 9075" origin
460 push_warning_printf(thd(), Sql_condition::SL_WARNING, ER_UNKNOWN_ERROR,
461 "Unknown error");
462
463 // Push new diagnostics area, clear old
464 thd()->push_diagnostics_area(&da);
465 org_da->reset_condition_info(thd());
466 EXPECT_TRUE(org_da->cond_count() == 0);
467 EXPECT_FALSE(da.cond_count() == 0);
468 EXPECT_FALSE(thd()->get_stmt_da()->cond_count() == 0);
469
470 // Condition 1 CLASS_ORIGIN
471 var = get_cond_info_item(thd(), 1, Condition_information_item::CLASS_ORIGIN);
472 EXPECT_EQ(&str, var->val_str(&str));
473 EXPECT_STREQ("MySQL", str.c_ptr_safe());
474
475 // Condition 1 SUBCLASS_ORIGIN
476 var =
477 get_cond_info_item(thd(), 1, Condition_information_item::SUBCLASS_ORIGIN);
478 EXPECT_EQ(&str, var->val_str(&str));
479 EXPECT_STREQ("MySQL", str.c_ptr_safe());
480
481 // Condition 2 CLASS_ORIGIN
482 var = get_cond_info_item(thd(), 2, Condition_information_item::CLASS_ORIGIN);
483 EXPECT_EQ(&str, var->val_str(&str));
484 EXPECT_STREQ("ISO 9075", str.c_ptr_safe());
485
486 // Condition 2 CLASS_ORIGIN
487 var =
488 get_cond_info_item(thd(), 2, Condition_information_item::SUBCLASS_ORIGIN);
489 EXPECT_EQ(&str, var->val_str(&str));
490 EXPECT_STREQ("ISO 9075", str.c_ptr_safe());
491
492 thd()->pop_diagnostics_area();
493 EXPECT_TRUE(thd()->get_stacked_da() == nullptr);
494 EXPECT_TRUE(thd()->get_stmt_da()->cond_count() == 0);
495 }
496
497 } // namespace get_diagnostics_unittest