1 /*
2 Copyright (c) 2019, 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 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 #include "mock_session.h"
26
27 #include "classic_mock_session.h"
28 #include "x_mock_session.h"
29
30 #include <thread>
31
32 #ifndef _WIN32
33 #include <fcntl.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <signal.h>
38 #include <sys/select.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43 #else
44 #include <windows.h>
45 #include <winsock2.h>
46 #include <ws2tcpip.h>
47 #endif
48
49 #include "mysql/harness/logging/logging.h"
50 IMPORT_LOG_FUNCTIONS()
51
52 namespace server_mock {
53
debug_trace_result(const ResultsetResponse * resultset)54 static void debug_trace_result(const ResultsetResponse *resultset) {
55 std::cout << "QUERY RESULT:\n";
56 for (size_t i = 0; i < resultset->rows.size(); ++i) {
57 for (const auto &cell : resultset->rows[i])
58 std::cout << " | " << (cell.first ? cell.second : "NULL");
59 std::cout << " |\n";
60 }
61 std::cout << "\n\n\n" << std::flush;
62 }
63
MySQLServerMockSession(socket_t client_sock,std::unique_ptr<StatementReaderBase> statement_processor,bool debug_mode)64 MySQLServerMockSession::MySQLServerMockSession(
65 socket_t client_sock,
66 std::unique_ptr<StatementReaderBase> statement_processor, bool debug_mode)
67 : client_socket_{client_sock},
68 json_reader_{std::move(statement_processor)},
69 debug_mode_{debug_mode} {
70 // if it doesn't work, no problem.
71 int one = 1;
72 setsockopt(client_socket_, IPPROTO_TCP, TCP_NODELAY,
73 reinterpret_cast<const char *>(&one), sizeof(one));
74
75 non_blocking(client_socket_, false);
76 }
77
~MySQLServerMockSession()78 MySQLServerMockSession::~MySQLServerMockSession() {}
79
run()80 void MySQLServerMockSession::run() {
81 try {
82 bool res = process_handshake();
83 if (!res) {
84 std::cout << "Error processing handshake with client: " << client_socket_
85 << std::endl;
86 }
87
88 res = process_statements();
89 if (!res) {
90 std::cout << "Error processing statements with client: " << client_socket_
91 << std::endl;
92 }
93 } catch (const std::exception &e) {
94 log_warning("Exception caught in connection loop: %s", e.what());
95 }
96 }
97
handle_statement(const StatementResponse & statement)98 void MySQLServerMockSession::handle_statement(
99 const StatementResponse &statement) {
100 using ResponseType = StatementResponse::ResponseType;
101
102 switch (statement.response_type) {
103 case ResponseType::OK: {
104 if (debug_mode_) std::cout << std::endl; // visual separator
105 OkResponse *response =
106 dynamic_cast<OkResponse *>(statement.response.get());
107
108 harness_assert(response);
109 std::this_thread::sleep_for(statement.exec_time);
110 send_ok(0, response->last_insert_id, 0, response->warning_count);
111 } break;
112 case ResponseType::RESULT: {
113 ResultsetResponse *response =
114 dynamic_cast<ResultsetResponse *>(statement.response.get());
115 harness_assert(response);
116 if (debug_mode_) {
117 debug_trace_result(response);
118 }
119
120 send_resultset(*response, statement.exec_time);
121 } break;
122 case ResponseType::ERROR: {
123 if (debug_mode_) std::cout << std::endl; // visual separator
124 ErrorResponse *response =
125 dynamic_cast<ErrorResponse *>(statement.response.get());
126 harness_assert(response);
127 send_error(response->code, response->msg);
128 } break;
129 default:;
130 throw std::runtime_error("Unsupported command in handle_statement(): " +
131 std::to_string((int)statement.response_type));
132 }
133 }
134
135 /*static*/ std::unique_ptr<MySQLServerMockSession>
create_session(const std::string & protocol,socket_t client_socket,std::unique_ptr<StatementReaderBase> statement_processor,bool debug_mode)136 MySQLServerMockSession::create_session(
137 const std::string &protocol, socket_t client_socket,
138 std::unique_ptr<StatementReaderBase> statement_processor, bool debug_mode) {
139 std::unique_ptr<MySQLServerMockSession> result;
140 if (protocol == "classic") {
141 result.reset(new MySQLServerMockSessionClassic(
142 client_socket, std::move(statement_processor), debug_mode));
143 } else if (protocol == "x") {
144 result.reset(new MySQLServerMockSessionX(
145 client_socket, std::move(statement_processor), debug_mode));
146 } else {
147 throw std::runtime_error("Unknown protocol: '" + protocol + "'");
148 }
149
150 return result;
151 }
152
153 } // namespace server_mock
154