# 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.
#
# See also BUG#32407.


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

disable_warnings;
DROP TABLE IF EXISTS t1;
enable_warnings;

# Test to show BUG#32407.  This reads a binlog created with the
# mysql-5.1-telco-6.1 tree, specifically at the tag
# mysql-5.1.15-ndb-6.1.23, and applies it to the database.  The test
# should fail before BUG#32407 was fixed and succeed afterwards.
--echo ==== Test BUG#32407 ====

# The binlog contains row events equivalent to:
# CREATE TABLE t1 (a int) engine = myisam
# INSERT INTO t1 VALUES (1), (1)
exec $MYSQL_BINLOG suite/binlog/std_data/bug32407.001 | $MYSQL;
# The above line should succeed and t1 should contain two ones
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;


# Test that mysqlbinlog stops with an error message when the
# --base64-output=never flag is used on a binlog with base64 events.
--echo ==== Test --base64-output=never on a binlog with row events ====

# mysqlbinlog should fail
--replace_regex /#[0-9][0-9][0-9][0-9][0-9][0-9] .*/<#>/   /SET \@\@session.pseudo_thread_id.*/<#>/
error 1;
exec $MYSQL_BINLOG --base64-output=never suite/binlog/std_data/bug32407.001;
# the above line should output the query log event and then stop


# 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_code: 1105");
call mtr.add_suppression("Slave SQL.*Table definition on master and slave does not match: Column 1 size mismatch.* Error_code: 1535");

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