~jan-kneschke/mysql-proxy/packet-tracking-assertions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--[[ $%BEGINLICENSE%$
 Copyright (C) 2008 MySQL AB, 2008 Sun Microsystems, Inc

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 $%ENDLICENSE%$ --]]
local proto = require("mysql.proto")

---
-- Bug#35669 is about failing lua scripts should case a error-msg on the MySQL protocol
--
-- we have to jump through some hoops to make this testable with mysqltest as it
-- expects that the initial, default connection always succeeds
--
-- To get this working we use 2 proxy scripts and chain them:
-- 1) ACK the initial connection, forward the 2nd connection to the faulty script
-- 2) loading a faulty script and generate the error-msg
--

function connect_server()
	if not proxy.global.is_faulty then
		-- only ACK mysqltest's default connection
		proxy.response = {
			type = proxy.MYSQLD_PACKET_RAW,
			packets = {
				proto.to_challenge_packet({})
			}
		}
	
		-- the next connection is the faulty connection
		proxy.global.is_faulty = true
		is_initial_connection = true
		return proxy.PROXY_SEND_RESULT
	end
end

---
-- provide a mock function for all commands that mysqltest sends 
-- on the default connection
--
function read_query(packet)
	-- pass on everything that is not on the initial connection
	if not is_initial_connection then return end

	if packet:byte() ~= proxy.COM_QUERY then
		proxy.response = {
			type = proxy.MYSQLD_PACKET_OK
		}
		return proxy.PROXY_SEND_RESULT
	end

	proxy.response = {
		type = proxy.MYSQLD_PACKET_ERR,
		errmsg = "(bug_35669-mock) >" .. packet:sub(2) .. "<"
	}
	return proxy.PROXY_SEND_RESULT
end