--source include/have_debug.inc
--source include/not_embedded.inc

--echo # Currently binlog is disabled. After initializing check_consecutive_binlog_key_versions we will restart the
--echo # server with binlog enabled. check_consecutive_binlog_key_versions will make sure that a new version of
--echo # percona_binlog key is used after each rotate_system_key followed by FLUSH LOGS calls.

--let $restart_parameters=restart:--log-bin=master-bin --debug=d,check_consecutive_binlog_key_versions;
--source include/restart_mysqld.inc

RESET MASTER;
--echo # master-bin.000001

SELECT 1;
SELECT 2;

--let $assert_text= Successful rotation of percona_binlog
--let $assert_cond= "[SELECT rotate_system_key\\(\\"percona_binlog\\"\\) = 1]" = 1
--source include/assert.inc

FLUSH LOGS;

--echo # master-bin.000002

SELECT 3;
SELECT 4;

--let $assert_text= Successful rotation of percona_binlog
--let $assert_cond= "[SELECT rotate_system_key\\(\\"percona_binlog\\"\\) = 1]" = 1
--source include/assert.inc

FLUSH LOGS;

--echo # master-bin.000003

SELECT 5;
SELECT 6;

--let $assert_text= Successful rotation of percona_binlog
--let $assert_cond= "[SELECT rotate_system_key\\(\\"percona_binlog\\"\\) = 1]" = 1
--source include/assert.inc

FLUSH LOGS;

--echo # master-bin.000004
SET GLOBAL debug="-d,check_consecutive_binlog_key_versions";

# First check that each binlog is encrypted by trying to decrypt it using mysqlbinlog
let datadir=`select @@datadir`;
let filename= master-bin.000002;
let local=$datadir/$filename;
--error 1
exec $MYSQL_BINLOG $local --hexdump > /dev/null;
let filename= master-bin.000003;
let local=$datadir/$filename;
--error 1
exec $MYSQL_BINLOG $local --hexdump > /dev/null;

# Check that each generated binlog can be read by the server by using mysqlbinlog  with --read-from-remote-server
# option
let filename= master-bin.000002;
let remote=--read-from-remote-server --protocol=tcp --host=127.0.0.1 --port=$MASTER_MYPORT -uroot $filename;
exec $MYSQL_BINLOG --hexdump $remote > /dev/null;

let filename= master-bin.000003;
let remote=--read-from-remote-server --protocol=tcp --host=127.0.0.1 --port=$MASTER_MYPORT -uroot $filename;
exec $MYSQL_BINLOG --hexdump $remote > /dev/null;

# Turning off binlog encryption to check if server can decrypt binlog with different percona_binlog keys versions
# while not encrypting binary log
--let $restart_parameters=restart:--log-bin=master-bin --encrypt-binlog=0
--source include/restart_mysqld.inc

--echo #master-bin.000005

SELECT 7;
SELECT 8;

FLUSH LOGS;

--echo #master-bin.000006

# Check that current master-bin is not encrypted by reading it with mysqlbinlog
let filename= master-bin.000005;
let local=$datadir/$filename;
exec $MYSQL_BINLOG $local --hexdump > /dev/null;

# Check that binlog encrypted with different percona_binlog key versions can be read by the server

let remote=--read-from-remote-server --protocol=tcp --host=127.0.0.1 --port=$MASTER_MYPORT -uroot $filename;
exec $MYSQL_BINLOG --hexdump $remote > /dev/null;

let filename= master-bin.000002;
let remote=--read-from-remote-server --protocol=tcp --host=127.0.0.1 --port=$MASTER_MYPORT -uroot $filename;
exec $MYSQL_BINLOG --hexdump $remote > /dev/null;

let filename= master-bin.000003;
let remote=--read-from-remote-server --protocol=tcp --host=127.0.0.1 --port=$MASTER_MYPORT -uroot $filename;
exec $MYSQL_BINLOG --hexdump $remote > /dev/null;
