1 /* Copyright (c) 2011, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 
24 #ifndef TEST_UTILS_INCLUDED
25 #define TEST_UTILS_INCLUDED
26 
27 #include "sql_error.h"
28 #include "sql_class.h"
29 #include "set_var.h"
30 
31 extern thread_local_key_t THR_MALLOC;
32 extern thread_local_key_t THR_THD;
33 extern bool THR_THD_initialized;
34 extern bool THR_MALLOC_initialized;
35 extern mysql_mutex_t LOCK_open;
36 extern uint    opt_debug_sync_timeout;
37 extern "C" void sql_alloc_error_handler(void);
38 
39 // A simple helper function to determine array size.
40 template <class T, int size>
array_size(const T (&)[size])41 int array_size(const T (&)[size])
42 {
43   return size;
44 }
45 
46 namespace my_testing {
47 
native_compare(size_t * length,unsigned char ** a,unsigned char ** b)48 inline int native_compare(size_t *length, unsigned char **a, unsigned char **b)
49 {
50   return memcmp(*a, *b, *length);
51 }
52 
get_ptr_compare(size_t size MY_ATTRIBUTE ((unused)))53 inline qsort2_cmp get_ptr_compare(size_t size MY_ATTRIBUTE((unused)))
54 {
55   return (qsort2_cmp) native_compare;
56 }
57 
58 void setup_server_for_unit_tests();
59 void teardown_server_for_unit_tests();
60 int chars_2_decimal(const char *chars, my_decimal *to);
61 
62 /*
63   A class which wraps the necessary setup/teardown logic for
64   unit tests which depend on a working THD environment.
65  */
66 class Server_initializer
67 {
68 public:
Server_initializer()69   Server_initializer() : m_thd(NULL) {}
70 
71   // Invoke these from corresponding functions in test fixture classes.
72   void SetUp();
73   void TearDown();
74 
75   // Sets expected error for error_handler_hook.
76   static void set_expected_error(uint val);
77 
thd()78   THD *thd() const { return m_thd; }
79 private:
80   THD *m_thd;
81 };
82 
83 /**
84    A mock error handler which registers itself with the THD in the CTOR,
85    and unregisters in the DTOR. The function handle_condition() will
86    verify that it is called with the expected error number.
87    The DTOR will verify that handle_condition() has actually been called.
88 */
89 class Mock_error_handler : public Internal_error_handler
90 {
91 public:
92   Mock_error_handler(THD *thd, uint expected_error);
93   virtual ~Mock_error_handler();
94 
95   virtual bool handle_condition(THD *thd,
96                                 uint sql_errno,
97                                 const char* sqlstate,
98                                 Sql_condition::enum_severity_level *level,
99                                 const char* msg);
100 
handle_called()101   int handle_called() const { return m_handle_called; }
102 private:
103   THD *m_thd;
104   uint m_expected_error;
105   int  m_handle_called;
106 };
107 
108 
109 }  // namespace my_testing
110 
111 #endif  // TEST_UTILS_INCLUDED
112