# This test case verifies that the mysqlbinlog --base64-output=X flags
# work as expected, and that BINLOG statements with row events fail if
# they are not preceded by BINLOG statements with Format description
# events.

# BINLOG statement does not work in embedded mode.
source include/not_embedded.inc;

call mtr.add_suppression("BINLOG_BASE64_EVENT: According to the master's version");
call mtr.add_suppression("BINLOG_BASE64_EVENT: Column 1 of table 'test.char128_utf8' cannot be converted");

CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (1), (1);
select * from t1;

# Test that a BINLOG statement encoding a row event fails unless a
# Format_description_event as been supplied with an earlier BINLOG
# statement.
--echo ==== Test BINLOG statement w/o FD event ====

# This is a binlog statement consisting of one Table_map_log_event and
# one Write_rows_log_event.  Together, they correspond to the
# following query:
# INSERT INTO TABLE test.t1 VALUES (2)

error ER_NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BINLOG_STATEMENT;
BINLOG '
SVtYRxMBAAAAKQAAADQBAAAAABAAAAAAAAAABHRlc3QAAnQxAAEDAAE=
SVtYRxcBAAAAIgAAAFYBAAAQABAAAAAAAAEAAf/+AgAAAA==
';
# The above line should fail and 2 should not be in the table
select * from t1;


# Test that it works to read a Format_description_log_event with a
# BINLOG statement, followed by a row-event in base64 from the same
# version.
--echo ==== Test BINLOG statement with FD event ====

# This is a binlog statement containing a Format_description_log_event
# from the same version as the Table_map and Write_rows_log_event.
BINLOG '
ODdYRw8BAAAAZgAAAGoAAAABAAQANS4xLjIzLXJjLWRlYnVnLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA4N1hHEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
';

# This is a Table_map_log_event+Write_rows_log_event corresponding to:
# INSERT INTO TABLE test.t1 VALUES (3)
BINLOG '
TFtYRxMBAAAAKQAAAH8BAAAAABAAAAAAAAAABHRlc3QAAnQxAAEDAAE=
TFtYRxcBAAAAIgAAAKEBAAAQABAAAAAAAAEAAf/+AwAAAA==
';
# The above line should succeed and 3 should be in the table
select * from t1;

# The same as above with one-fragment BINLOG to prove
# equivalency with the fragmented BINLOG @frag_0, @frag_1.
DELETE FROM t1 WHERE a=3;
# This is a binlog statement containing a Format_description_log_event
# from the same version as the Table_map and Write_rows_log_event.
BINLOG '
ODdYRw8BAAAAZgAAAGoAAAABAAQANS4xLjIzLXJjLWRlYnVnLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA4N1hHEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
';

# This is a Table_map_log_event+Write_rows_log_event corresponding to:
# INSERT INTO TABLE test.t1 VALUES (3)
SET @binlog_fragment_0='
TFtYRxMBAAAAKQAAAH8BAAAAABAAAAAAAAAABHRlc3QAAnQxAAEDAAE=
TFtYRxcBAAAAIgAAAKEBAAAQABAAAAAAAAEAAf/+AwAAAA==
';
SET @binlog_fragment_1='';
BINLOG @binlog_fragment_0, @binlog_fragment_1;
# The above line should succeed and 3 should be in the table:
select * from t1;
# show "one-shot" feature of binlog_fragment variables
SELECT @binlog_fragment_0, @binlog_fragment_1 as 'NULL','NULL';

# Test that the following fails cleanly: "First, read a
# Format_description event which has N event types. Then, read an
# event of type M>N"
--echo ==== Test non-matching FD event and Row event ====

# This is the Format_description_log_event from
# bug32407.001, encoded in base64. It contains only the old
# row events (number of event types is 22)
BINLOG '
4CdYRw8BAAAAYgAAAGYAAAAAAAQANS4xLjE1LW5kYi02LjEuMjQtZGVidWctbG9nAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADgJ1hHEzgNAAgAEgAEBAQEEgAATwAEGggICAg=
';

# The following is a Write_rows_log_event with event type 23, i.e.,
# not supported by the Format_description_log_event above.  It
# corresponds to the following query:
# INSERT INTO t1 VALUES (5)
error 1149;
BINLOG '
Dl1YRxMBAAAAKQAAADQBAAAAABAAAAAAAAAABHRlc3QAAnQxAAEDAAE=
Dl1YRxcBAAAAIgAAAFYBAAAQABAAAAAAAAEAAf/+BQAAAA==
';
# the above line should fail and 5 should not be in the binlog.
select * from t1;

# Test that BUG#37426 is triggered.

CREATE TABLE char128_utf8 ( 
    i1 INT NOT NULL, 
    c CHAR(128) CHARACTER SET utf8 NOT NULL, 
    i2 INT NOT NULL);
CREATE TABLE char63_utf8 (
      i1 INT NOT NULL,
      c CHAR(63) CHARACTER SET utf8 NOT NULL,
      i2 INT NOT NULL);

#
# This is the format description log event
#

BINLOG '
MuNkSA8BAAAAZgAAAGoAAAAAAAQANS4xLjI1LXJjLWRlYnVnLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAy42RIEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
';

# ... this event corresponding to
#
#    INSERT INTO char63_utf8 VALUES ( 1, "123", 1 )
#
# The binlog event below shall not trigger the bug check

BINLOG '
3u9kSBMBAAAANgAAAJYBAAAAABAAAAAAAAAABHRlc3QAC2NoYXI2M191dGY4AAMD/gMC/r0A
3u9kSBcBAAAAKgAAAMABAAAQABAAAAAAAAEAA//4AQAAAAMxMjMBAAAA
';
SELECT * FROM char63_utf8;

# ... and this is an event corresponding to
#
#    INSERT INTO char128_utf8 VALUES ( 1, "123", 1 )
#
# The binlog event below shall trigger the bug check and produce an error
#

error ER_UNKNOWN_ERROR;
BINLOG '
iONkSBMBAAAANwAAAJkBAAAAABAAAAAAAAAABHRlc3QADGNoYXIxMjhfdXRmOAADA/4DAv6AAA==
iONkSBcBAAAAKwAAAMQBAAAQABAAAAAAAAEAA//4AQAAAAMAMTIzAQAAAA==
';

drop table t1, char63_utf8, char128_utf8;

call mtr.add_suppression("Slave SQL.*master suffers from this bug: http:..bugs.mysql.com.bug.php.id=37426.* error.* 1105");
call mtr.add_suppression("Slave SQL.*Table definition on master and slave does not match: Column 1 size mismatch.* error.* 1535");
call mtr.add_suppression("Slave SQL.*Column 1 of table .test.char128_utf8. cannot be converted.* error.* 1677");

--echo #
--echo # Bug #54393: crash and/or valgrind errors in 
--echo # mysql_client_binlog_statement
--echo #
--error ER_SYNTAX_ERROR
BINLOG '';
--error ER_BASE64_DECODE_ERROR
BINLOG '123';
--error ER_BASE64_DECODE_ERROR
BINLOG '-2079193929';
--error ER_BASE64_DECODE_ERROR
BINLOG 'xç↓%~∙D╒ƒ╡';