1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
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
23  * 02110-1301  USA
24  */
25 
26 
27 #include "ngs/interface/client_interface.h"
28 #include "ngs/interface/server_interface.h"
29 #include "ngs_common/connection_vio.h"
30 #include "ngs/scheduler.h"
31 #include "xpl_session.h"
32 #include "sql_data_context.h"
33 
34 
35 namespace ngs
36 {
37 namespace test
38 {
39 
40 class Mock_connection : public ngs::Connection_vio
41 {
42 public:
Mock_connection()43   Mock_connection()
44   : ngs::Connection_vio(m_context, NULL)
45   {
46   }
47 
48   MOCK_METHOD0(connection_type, Connection_type ());
49   MOCK_METHOD0(options, ngs::IOptions_session_ptr());
50 private:
51   Ssl_context m_context;
52 };
53 
54 
55 class Mock_scheduler_dynamic : public Scheduler_dynamic
56 {
57 public:
Mock_scheduler_dynamic()58   Mock_scheduler_dynamic()
59   : Scheduler_dynamic("")
60   {
61   }
62 
63   MOCK_METHOD0(launch, void());
64   MOCK_METHOD0(stop, void());
65   MOCK_METHOD0(thread_init, bool ());
66   MOCK_METHOD0(thread_end, void ());
67   MOCK_METHOD1(set_num_workers, unsigned int(unsigned int n));
68 };
69 
70 
71 class Mock_server : public ngs::Server_interface
72 {
73 public:
74 
75 
get_auth_handler(const std::string & p1,Session_interface * p2)76   Authentication_handler_ptr get_auth_handler(const std::string &p1, Session_interface *p2)
77   {
78     return Authentication_handler_ptr(get_auth_handler2(p1, p2));
79   }
80 
81   MOCK_METHOD2(get_auth_handler2, Authentication_handler_ptr::element_type *(const std::string &, Session_interface *));
82   MOCK_CONST_METHOD0(get_config, ngs::shared_ptr<Protocol_config> ());
83   MOCK_METHOD0(is_running, bool ());
84   MOCK_CONST_METHOD0(get_worker_scheduler, ngs::shared_ptr<Scheduler_dynamic> ());
85   MOCK_CONST_METHOD0(ssl_context, Ssl_context *());
86   MOCK_METHOD1(on_client_closed, void (const Client_interface &));
87   MOCK_METHOD3(create_session, ngs::shared_ptr<Session_interface> (Client_interface &, Protocol_encoder &, int));
88   MOCK_METHOD0(get_client_exit_mutex, Mutex &());
89   MOCK_METHOD0(restart_client_supervision_timer, void ());
90 
91   // Workaround for GMOCK undefined behaviour with ResultHolder
92   MOCK_METHOD2(get_authentication_mechanisms_void, bool (std::vector<std::string> &auth_mech, Client_interface &client));
get_authentication_mechanisms(std::vector<std::string> & auth_mech,Client_interface & client)93   void get_authentication_mechanisms(std::vector<std::string> &auth_mech, Client_interface &client)
94   {
95     get_authentication_mechanisms_void(auth_mech, client);
96   }
97 };
98 
99 }  // namespace test
100 }  // namespace ngs
101 
102 
103 namespace xpl
104 {
105 namespace test
106 {
107 
108 
109 class Mock_client : public ngs::Client_interface
110 {
111 public:
112   MOCK_METHOD0(get_session_exit_mutex, ngs::Mutex &());
113 
114   MOCK_CONST_METHOD0(client_id, const char *());
115 
116   MOCK_CONST_METHOD0(client_address, const char *());
117   MOCK_CONST_METHOD0(client_hostname, const char *());
118   MOCK_METHOD0(connection, ngs::Connection_vio  &());
119   MOCK_CONST_METHOD0(server, ngs::Server_interface &());
120 
121   MOCK_CONST_METHOD0(client_id_num, Client_id ());
122   MOCK_CONST_METHOD0(client_port, int ());
123 
124   MOCK_CONST_METHOD0(get_accept_time, ngs::chrono::time_point ());
125   MOCK_CONST_METHOD0(get_state, Client_state ());
126 
127   MOCK_METHOD0(session, ngs::shared_ptr<ngs::Session_interface> ());
128   MOCK_METHOD0(supports_expired_passwords, bool ());
129 public:
130   MOCK_METHOD1(on_session_reset_void, bool (ngs::Session_interface &));
131   MOCK_METHOD1(on_session_close_void, bool (ngs::Session_interface &));
132   MOCK_METHOD1(on_session_auth_success_void, bool (ngs::Session_interface &));
133 
134   MOCK_METHOD0(disconnect_and_trigger_close_void, bool ());
135   MOCK_METHOD0(activate_tls_void, bool ());
136   MOCK_METHOD0(on_auth_timeout_void, bool ());
137   MOCK_METHOD0(on_server_shutdown_void, bool ());
138   MOCK_METHOD1(run_void, bool (bool));
139   MOCK_METHOD0(reset_accept_time_void, bool ());
140 
on_session_reset(ngs::Session_interface & arg)141   void on_session_reset(ngs::Session_interface &arg)
142   {
143     on_session_reset_void(arg);
144   }
145 
on_session_close(ngs::Session_interface & arg)146   void on_session_close(ngs::Session_interface &arg)
147   {
148     on_session_close_void(arg);
149   }
150 
on_session_auth_success(ngs::Session_interface & arg)151   void on_session_auth_success(ngs::Session_interface &arg)
152   {
153     on_session_auth_success_void(arg);
154   }
155 
disconnect_and_trigger_close()156   void disconnect_and_trigger_close()
157   {
158     disconnect_and_trigger_close_void();
159   }
160 
activate_tls()161   void activate_tls()
162   {
163     activate_tls_void();
164   }
165 
on_auth_timeout()166   void on_auth_timeout()
167   {
168     on_auth_timeout_void();
169   }
170 
on_server_shutdown()171   void on_server_shutdown()
172   {
173     on_server_shutdown_void();
174   }
175 
run(bool arg)176   void run(bool arg)
177   {
178     run_void(arg);
179   }
180 
reset_accept_time()181   void reset_accept_time()
182   {
183     reset_accept_time_void();
184   }
185 };
186 
187 class Mock_session : public xpl::Session
188 {
189 public:
Mock_session(ngs::Client_interface * client)190   Mock_session(ngs::Client_interface* client)
191   : xpl::Session(*client, NULL, 0)
192   {
193   }
194 
195   MOCK_METHOD0(data_context, xpl::Sql_data_context&());
196 };
197 
198 
199 class Mock_sql_data_context : public xpl::Sql_data_context
200 {
201 public:
202   Mock_sql_data_context(ngs::Protocol_encoder *p=0)
Sql_data_context(p)203   : xpl::Sql_data_context(p)
204   {
205   }
206 
207   MOCK_METHOD8(authenticate, ngs::Error_code (const char *, const char *, const char *, const char *, On_user_password_hash , bool, ngs::IOptions_session_ptr &, const ngs::Connection_type));
208   MOCK_METHOD5(execute_sql_and_collect_results, ngs::Error_code (
209       const char *,
210       std::size_t,
211       std::vector<Command_delegate::Field_type> &,
212       Buffering_command_delegate::Resultset &,
213       Result_info &));
214 };
215 
216 
217 } // namespace test
218 }  // namespace xpl
219