1 /*
2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
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 02110-1301  USA
23  */
24 
25 #ifndef UNITTEST_GUNIT_XPLUGIN_XPL_MOCK_NGS_GENERAL_H_
26 #define UNITTEST_GUNIT_XPLUGIN_XPL_MOCK_NGS_GENERAL_H_
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include "plugin/x/src/interface/listener_factory.h"
33 #include "plugin/x/src/interface/operations_factory.h"
34 #include "plugin/x/src/interface/socket.h"
35 #include "plugin/x/src/interface/socket_events.h"
36 #include "plugin/x/src/interface/ssl_context_options.h"
37 #include "plugin/x/src/ssl_session_options.h"
38 
39 namespace xpl {
40 namespace test {
41 
42 class Mock_options_session : public iface::Ssl_session_options {
43  public:
44   MOCK_CONST_METHOD0(active_tls, bool());
45 
46   MOCK_CONST_METHOD0(ssl_cipher, std::string());
47   MOCK_CONST_METHOD0(ssl_version, std::string());
48   MOCK_CONST_METHOD0(ssl_cipher_list, std::vector<std::string>());
49 
50   MOCK_CONST_METHOD0(ssl_verify_depth, int64_t());
51   MOCK_CONST_METHOD0(ssl_verify_mode, int64_t());
52   MOCK_CONST_METHOD0(ssl_sessions_reused, int64_t());
53 
54   MOCK_CONST_METHOD0(ssl_get_verify_result_and_cert, int64_t());
55   MOCK_CONST_METHOD0(ssl_get_peer_certificate_issuer, std::string());
56   MOCK_CONST_METHOD0(ssl_get_peer_certificate_subject, std::string());
57 };
58 
59 class Mock_options_context : public iface::Ssl_context_options {
60  public:
61   MOCK_METHOD0(ssl_ctx_verify_depth, int64_t());
62   MOCK_METHOD0(ssl_ctx_verify_mode, int64_t());
63 
64   MOCK_METHOD0(ssl_server_not_after, std::string());
65   MOCK_METHOD0(ssl_server_not_before, std::string());
66 
67   MOCK_METHOD0(ssl_sess_accept_good, int64_t());
68   MOCK_METHOD0(ssl_sess_accept, int64_t());
69   MOCK_METHOD0(ssl_accept_renegotiates, int64_t());
70 
71   MOCK_METHOD0(ssl_session_cache_mode, std::string());
72 
73   MOCK_METHOD0(ssl_session_cache_hits, int64_t());
74   MOCK_METHOD0(ssl_session_cache_misses, int64_t());
75   MOCK_METHOD0(ssl_session_cache_overflows, int64_t());
76   MOCK_METHOD0(ssl_session_cache_size, int64_t());
77   MOCK_METHOD0(ssl_session_cache_timeouts, int64_t());
78   MOCK_METHOD0(ssl_used_session_cache_entries, int64_t());
79 };
80 
81 class Mock_socket : public iface::Socket {
82  public:
83   MOCK_METHOD2(bind, int(const struct sockaddr *, socklen_t));
84   MOCK_METHOD3(accept,
85                MYSQL_SOCKET(PSI_socket_key, struct sockaddr *, socklen_t *));
86   MOCK_METHOD1(listen, int(int));
87 
88   MOCK_METHOD0(close, void());
89 
90   MOCK_METHOD0(get_socket_mysql, MYSQL_SOCKET());
91   MOCK_METHOD0(get_socket_fd, my_socket());
92 
93   MOCK_METHOD4(set_socket_opt, int(int, int, const SOCKBUF_T *, socklen_t));
94   MOCK_METHOD0(set_socket_thread_owner, void());
95 };
96 
97 class Mock_system : public iface::System {
98  public:
99   MOCK_METHOD1(unlink, int32_t(const char *));
100   MOCK_METHOD2(kill, int32_t(int32_t, int32_t));
101 
102   MOCK_METHOD0(get_ppid, int32_t());
103   MOCK_METHOD0(get_pid, int32_t());
104   MOCK_METHOD0(get_errno, int32_t());
105 
106   MOCK_METHOD0(get_socket_errno, int32_t());
107   MOCK_METHOD1(set_socket_errno, void(const int32_t));
108   MOCK_METHOD2(get_socket_error_and_message, void(int32_t *, std::string *));
109 
110   MOCK_METHOD1(freeaddrinfo, void(addrinfo *));
111   MOCK_METHOD4(getaddrinfo, int32_t(const char *, const char *,
112                                     const addrinfo *, addrinfo **));
113 
114   MOCK_METHOD1(sleep, void(uint32_t));
115 };
116 
117 class Mock_file : public iface::File {
118  public:
119   MOCK_METHOD0(is_valid, bool());
120 
121   MOCK_METHOD0(close, int());
122   MOCK_METHOD2(read, int(void *, int));
123   MOCK_METHOD2(write, int(void *, int));
124   MOCK_METHOD0(fsync, int());
125 };
126 
127 class Mock_factory : public iface::Operations_factory {
128  public:
129   MOCK_METHOD4(create_socket,
130                std::shared_ptr<iface::Socket>(PSI_socket_key, int, int, int));
131   MOCK_METHOD1(create_socket, std::shared_ptr<iface::Socket>(MYSQL_SOCKET));
132 
133   MOCK_METHOD3(open_file, std::shared_ptr<iface::File>(const char *, int, int));
134 
135   MOCK_METHOD0(create_system_interface, std::shared_ptr<iface::System>());
136 };
137 
138 class Mock_socket_events : public iface::Socket_events {
139  public:
140   MOCK_METHOD2(listen, bool(std::shared_ptr<iface::Socket>,
141                             std::function<void(iface::Connection_acceptor &)>));
142   MOCK_METHOD2(add_timer, void(const std::size_t, std::function<bool()>));
143   MOCK_METHOD0(loop, void());
144   MOCK_METHOD0(break_loop, void());
145 };
146 
147 class Mock_listener_factory_interface : public iface::Listener_factory {
148  public:
149   MOCK_CONST_METHOD3(create_unix_socket_listener_ptr,
150                      iface::Listener *(const std::string &unix_socket_path,
151                                        const iface::Socket_events &event,
152                                        const uint32_t backlog));
153 
154   MOCK_CONST_METHOD6(create_tcp_socket_listener_ptr,
155                      iface::Listener *(const std::string &bind_address,
156                                        const std::string &network_namespace,
157                                        const unsigned short port,
158                                        const uint32_t port_open_timeout,
159                                        const iface::Socket_events &event,
160                                        const uint32_t backlog));
161 
create_unix_socket_listener(const std::string & unix_socket_path,iface::Socket_events * event,const uint32_t backlog)162   std::unique_ptr<iface::Listener> create_unix_socket_listener(
163       const std::string &unix_socket_path, iface::Socket_events *event,
164       const uint32_t backlog) const override {
165     return std::unique_ptr<iface::Listener>{
166         create_unix_socket_listener_ptr(unix_socket_path, *event, backlog)};
167   }
168 
create_tcp_socket_listener(const std::string & bind_address,const std::string & network_namespace,const uint16_t port,const uint32_t port_open_timeout,iface::Socket_events * event,const uint32_t backlog)169   std::unique_ptr<iface::Listener> create_tcp_socket_listener(
170       const std::string &bind_address, const std::string &network_namespace,
171       const uint16_t port, const uint32_t port_open_timeout,
172       iface::Socket_events *event, const uint32_t backlog) const override {
173     return std::unique_ptr<iface::Listener>{
174         create_tcp_socket_listener_ptr(bind_address, network_namespace, port,
175                                        port_open_timeout, *event, backlog)};
176   }
177 };
178 
179 }  // namespace test
180 }  // namespace xpl
181 
182 #endif  // UNITTEST_GUNIT_XPLUGIN_XPL_MOCK_NGS_GENERAL_H_
183