From e345b9d825a6e1e853f9faedd1b6457eca295913 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Fri, 17 Aug 2018 16:37:32 -0400 Subject: [PATCH 1/9] initial tests for key backup --- tests/41end-to-end-keys/07-backup.pl | 271 +++++++++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 tests/41end-to-end-keys/07-backup.pl diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl new file mode 100644 index 000000000..9bf465323 --- /dev/null +++ b/tests/41end-to-end-keys/07-backup.pl @@ -0,0 +1,271 @@ +my $fixture = local_user_fixture(); + +my $current_version; # FIXME: is there a better way of passing the backup version between tests? + +test "Can create backup version", + requires => [ $fixture ], + + proves => [qw( can_create_backup_version )], + + do => sub { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "POST", + uri => "/unstable/room_keys/version", + content => { + algorithm => "m.megolm_backup.v1", + auth_data => "anopaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "version" ); + + $current_version = $content->{version}; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/version", + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "algorithm" ); + + assert_json_keys( $content, "auth_data" ); + + $content->{algorithm} eq "m.megolm_backup.v1" or + die "Expected algorithm to match submitted data"; + + $content->{auth_data} eq "anopaquestring" or + die "Expected auth_data to match submitted data"; + + # FIXME: check that version matches the version returned above + + Future->done(1); + }); + }; + +test "Can backup keys", + requires => [ $fixture, qw( can_create_backup_version ) ], + + proves => [qw( can_backup_e2e_keys )], + + do => sub { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + }, + content => { + first_message_index => 3, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anopaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + } + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "first_message_index" ); + + assert_json_keys( $content, "forwarded_count" ); + + assert_json_keys( $content, "is_verified" ); + + assert_json_keys( $content, "session_data" ); + + $content->{first_message_index} == 3 or + die "Expected first message index to match submitted data"; + + $content->{forwarded_count} == 0 or + die "Expected forwarded count to match submitted data"; + + $content->{is_verified} == JSON::false or + die "Expected is_verified to match submitted data"; + + $content->{session_data} eq "anopaquestring" or + die "Expected session data to match submitted data"; + + Future->done(1); + }); + }; + +test "Can update keys with better versions", + requires => [ $fixture, qw( can_backup_e2e_keys ) ], + + proves => [qw( can_update_e2e_keys )], + + do => sub { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + }, + content => { + first_message_index => 1, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anotheropaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + } + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "first_message_index" ); + + assert_json_keys( $content, "forwarded_count" ); + + assert_json_keys( $content, "is_verified" ); + + assert_json_keys( $content, "session_data" ); + + $content->{first_message_index} == 1 or + die "Expected first message index to match submitted data"; + + $content->{forwarded_count} == 0 or + die "Expected forwarded count to match submitted data"; + + $content->{is_verified} == JSON::false or + die "Expected is_verified to match submitted data"; + + $content->{session_data} eq "anotheropaquestring" or + die "Expected session data to match submitted data"; + + Future->done(1); + }); + }; + +test "Will not update keys with worse versions", + requires => [ $fixture, qw( can_update_e2e_keys ) ], + + proves => [qw( wont_update_e2e_keys )], + + do => sub { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + }, + content => { + first_message_index => 5, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "yetanotheropaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $current_version, + } + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "first_message_index" ); + + assert_json_keys( $content, "forwarded_count" ); + + assert_json_keys( $content, "is_verified" ); + + assert_json_keys( $content, "session_data" ); + + # The data should not be overwritten, so should be the same as what + # was set by the previous test. + $content->{first_message_index} == 1 or + die "Expected first message index to match submitted data"; + + $content->{forwarded_count} == 0 or + die "Expected forwarded count to match submitted data"; + + $content->{is_verified} == JSON::false or + die "Expected is_verified to match submitted data"; + + $content->{session_data} eq "anotheropaquestring" or + die "Expected session data to match submitted data"; + + Future->done(1); + }); + }; + +test "Will not back up to an old backup version", + requires => [ $fixture, qw( can_create_backup_version ) ], + + proves => [qw( wont_backup_to_old_version )], + + do => sub { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "POST", + uri => "/unstable/room_keys/version", + content => { + algorithm => "m.megolm_backup.v1", + auth_data => "anopaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "version" ); + + my $old_version = $current_version; + + $current_version = $content->{version}; + + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $old_version, + }, + content => { + first_message_index => 3, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anopaquestring", + } + ); + })->main::expect_http_4xx + ->then_done(1); + }; From 888645969ce9b4feb60143ef88e2f589a318cb2d Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 5 Oct 2018 14:33:57 +0100 Subject: [PATCH 2/9] Add test asserting backups can be deleted --- tests/41end-to-end-keys/07-backup.pl | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 9bf465323..363e54096 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -269,3 +269,49 @@ })->main::expect_http_4xx ->then_done(1); }; + +test "Can delete backup", + requires => [ $fixture, qw( can_create_backup_version ) ], + + do => sub { + my ( $user ) = @_; + + log_if_fail "Deleting version: ", $current_version; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/version", + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "DELETE", + uri => "/unstable/room_keys/version/$current_version", + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/version", + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "DELETE", + uri => "/unstable/room_keys/version/$content->{version}", + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/version", + ); + })->main::expect_http_404; + }; From bd4989fd49189da5f92df1cfa98004faa5a7fe17 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 5 Oct 2018 15:10:26 +0100 Subject: [PATCH 3/9] Add regression test for backup version re-use Make sure keys from old backups don't appear in newly created ones https://github.com/vector-im/riot-web/issues/7448 --- tests/41end-to-end-keys/07-backup.pl | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 363e54096..3791d3c7b 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -315,3 +315,73 @@ ); })->main::expect_http_404; }; + +test "Deleted & recreated backups are empty", + requires => [ $fixture, qw( can_create_backup_version ) ], + + do => sub { + my ( $user ) = @_; + + my $version; + + do_request_json_for( $user, + method => "POST", + uri => "/unstable/room_keys/version", + content => { + algorithm => "m.megolm_backup.v1", + auth_data => "anevenmoreopaquestring", + } + )->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + assert_json_keys( $content, "version" ); + + $version = $content->{version}; + + log_if_fail "Created version $version"; + + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/!abcd/1234", + params => { + version => $version, + }, + content => { + first_message_index => 3, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "areallyopaquestring", + } + ); + })->then( sub { + log_if_fail "Deleting version $version"; + do_request_json_for( $user, + method => "DELETE", + uri => "/unstable/room_keys/version/$version", + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + do_request_json_for( $user, + method => "POST", + uri => "/unstable/room_keys/version", + content => { + algorithm => "m.megolm_backup.v1", + auth_data => "omgyouwouldntbelievehowopaquethisstringis", + } + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Created version $content->{version}"; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/keys", + params => { + version => $content->{version}, + } + ); + })->main::expect_http_404; + }; From d29dee30f4038b71f90c6b71d08f0ae26da39cf8 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 9 Oct 2018 19:43:15 +0100 Subject: [PATCH 4/9] retest please From aeaf2c4ee324cae1b1be85b554a625b449f9943f Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 12 Oct 2018 13:08:04 +0100 Subject: [PATCH 5/9] amalgamate json key assertions --- tests/41end-to-end-keys/07-backup.pl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 3791d3c7b..7bc4da51b 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -84,13 +84,7 @@ my ( $content ) = @_; log_if_fail "Content", $content; - assert_json_keys( $content, "first_message_index" ); - - assert_json_keys( $content, "forwarded_count" ); - - assert_json_keys( $content, "is_verified" ); - - assert_json_keys( $content, "session_data" ); + assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); $content->{first_message_index} == 3 or die "Expected first message index to match submitted data"; From bd291b4db06a11281dfd7a11436fd74e9cbd359c Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 12 Oct 2018 13:09:52 +0100 Subject: [PATCH 6/9] more amalgamation --- tests/41end-to-end-keys/07-backup.pl | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 7bc4da51b..7dc155565 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -137,13 +137,7 @@ my ( $content ) = @_; log_if_fail "Content", $content; - assert_json_keys( $content, "first_message_index" ); - - assert_json_keys( $content, "forwarded_count" ); - - assert_json_keys( $content, "is_verified" ); - - assert_json_keys( $content, "session_data" ); + assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); $content->{first_message_index} == 1 or die "Expected first message index to match submitted data"; @@ -196,13 +190,7 @@ my ( $content ) = @_; log_if_fail "Content", $content; - assert_json_keys( $content, "first_message_index" ); - - assert_json_keys( $content, "forwarded_count" ); - - assert_json_keys( $content, "is_verified" ); - - assert_json_keys( $content, "session_data" ); + assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); # The data should not be overwritten, so should be the same as what # was set by the previous test. From e1dbfc1d5aa8fcefd455b1b7c25f217d0d720160 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 12 Oct 2018 13:15:20 +0100 Subject: [PATCH 7/9] fix indentation --- tests/41end-to-end-keys/07-backup.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 7dc155565..558409231 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -247,7 +247,7 @@ is_verified => JSON::false, session_data => "anopaquestring", } - ); + ); })->main::expect_http_4xx ->then_done(1); }; From cdc527f583d84a849cd57baa90b519cffacd2cf3 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 12 Oct 2018 14:31:05 +0100 Subject: [PATCH 8/9] subroutines & non-interdependent tests Factor the common HTTP hits out into matrix_* subs and make the tests independent from one another. --- tests/41end-to-end-keys/07-backup.pl | 354 +++++++++++++++------------ 1 file changed, 200 insertions(+), 154 deletions(-) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 558409231..09f38e78f 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -10,25 +10,16 @@ do => sub { my ( $user ) = @_; - do_request_json_for( $user, - method => "POST", - uri => "/unstable/room_keys/version", - content => { - algorithm => "m.megolm_backup.v1", - auth_data => "anopaquestring", - } - )->then( sub { + my $version; + + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; assert_json_keys( $content, "version" ); + $version = $content->{version}; - $current_version = $content->{version}; - - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/version", - ); + matrix_get_key_backup_info( $user ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; @@ -43,7 +34,7 @@ $content->{auth_data} eq "anopaquestring" or die "Expected auth_data to match submitted data"; - # FIXME: check that version matches the version returned above + assert_eq( $content->{version}, $version ); Future->done(1); }); @@ -56,34 +47,29 @@ do => sub { my ( $user ) = @_; + my $version; - do_request_json_for( $user, - method => "PUT", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - }, - content => { - first_message_index => 3, - forwarded_count => 0, - is_verified => JSON::false, - session_data => "anopaquestring", - } - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - } + $version = $content->{version}; + + matrix_backup_keys( $user, '!abcd', '1234', $version, { + first_message_index => 3, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anopaquestring", + }, ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; + matrix_get_backup_key( $user, '!abcd', '1234', $version ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); $content->{first_message_index} == 3 or @@ -109,30 +95,33 @@ do => sub { my ( $user ) = @_; + my $version; - do_request_json_for( $user, - method => "PUT", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - }, - content => { - first_message_index => 1, - forwarded_count => 0, - is_verified => JSON::false, - session_data => "anotheropaquestring", - } - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - } + $version = $content->{version}; + + matrix_backup_keys( $user, '!abcd', '1234', $version, { + first_message_index => 2, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anotheropaquestring", + }, ); + })->then( sub { + matrix_backup_keys( $user, '!abcd', '1234', $version, { + first_message_index => 1, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anotheropaquestring", + }, + ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + + matrix_get_backup_key( $user, '!abcd', '1234', $version ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; @@ -162,39 +151,42 @@ do => sub { my ( $user ) = @_; + my $version; - do_request_json_for( $user, - method => "PUT", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - }, - content => { - first_message_index => 5, - forwarded_count => 0, - is_verified => JSON::false, - session_data => "yetanotheropaquestring", - } - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $current_version, - } + $version = $content->{version}; + + matrix_backup_keys( $user, '!abcd', '1234', $version, { + first_message_index => 2, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anotheropaquestring", + }, + ); + })->then( sub { + matrix_backup_keys( $user, '!abcd', '1234', $version, { + first_message_index => 3, + forwarded_count => 0, + is_verified => JSON::false, + session_data => "anotheropaquestring", + }, ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; + matrix_get_backup_key( $user, '!abcd', '1234', $version ); + })->then( sub { + my ( $content ) = @_; + log_if_fail "Content", $content; + assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); # The data should not be overwritten, so should be the same as what # was set by the previous test. - $content->{first_message_index} == 1 or + $content->{first_message_index} == 2 or die "Expected first message index to match submitted data"; $content->{forwarded_count} == 0 or @@ -217,36 +209,21 @@ do => sub { my ( $user ) = @_; + my $old_version; - do_request_json_for( $user, - method => "POST", - uri => "/unstable/room_keys/version", - content => { - algorithm => "m.megolm_backup.v1", - auth_data => "anopaquestring", - } - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; - - assert_json_keys( $content, "version" ); - - my $old_version = $current_version; - $current_version = $content->{version}; + $old_version = $content->{version}; - do_request_json_for( $user, - method => "PUT", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $old_version, - }, - content => { + matrix_create_key_backup( $user ); + })->then( sub { + matrix_backup_keys( $user, '!abcd', '1234', $old_version, { first_message_index => 3, forwarded_count => 0, is_verified => JSON::false, - session_data => "anopaquestring", - } + session_data => "anotheropaquestring", + }, ); })->main::expect_http_4xx ->then_done(1); @@ -257,44 +234,30 @@ do => sub { my ( $user ) = @_; + my $first_version; + my $second_version; - log_if_fail "Deleting version: ", $current_version; - - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/version", - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; + $first_version = $content->{version}; - do_request_json_for( $user, - method => "DELETE", - uri => "/unstable/room_keys/version/$current_version", - ); + matrix_create_key_backup( $user ); })->then( sub { my ( $content ) = @_; - log_if_fail "Content", $content; + $second_version = $content->{version}; - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/version", - ); + matrix_delete_key_backup( $user, $second_version ); })->then( sub { - my ( $content ) = @_; - log_if_fail "Content", $content; - - do_request_json_for( $user, - method => "DELETE", - uri => "/unstable/room_keys/version/$content->{version}", - ); + matrix_get_key_backup_info( $user ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; - do_request_json_for( $user, - method => "GET", - uri => "/unstable/room_keys/version", - ); + my $new_version = $content->{version}; + + assert_eq( $new_version, $first_version ); + + matrix_get_key_backup_info ( $user, $second_version ); })->main::expect_http_404; }; @@ -306,14 +269,7 @@ my $version; - do_request_json_for( $user, - method => "POST", - uri => "/unstable/room_keys/version", - content => { - algorithm => "m.megolm_backup.v1", - auth_data => "anevenmoreopaquestring", - } - )->then( sub { + matrix_create_key_backup( $user )->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; @@ -323,41 +279,27 @@ log_if_fail "Created version $version"; - do_request_json_for( $user, - method => "PUT", - uri => "/unstable/room_keys/keys/!abcd/1234", - params => { - version => $version, - }, - content => { + matrix_backup_keys( $user, '!abcd', '1234', $version, { first_message_index => 3, forwarded_count => 0, is_verified => JSON::false, session_data => "areallyopaquestring", - } + }, ); })->then( sub { log_if_fail "Deleting version $version"; - do_request_json_for( $user, - method => "DELETE", - uri => "/unstable/room_keys/version/$version", - ); + + matrix_delete_key_backup( $user, $version ); })->then( sub { my ( $content ) = @_; log_if_fail "Content", $content; - do_request_json_for( $user, - method => "POST", - uri => "/unstable/room_keys/version", - content => { - algorithm => "m.megolm_backup.v1", - auth_data => "omgyouwouldntbelievehowopaquethisstringis", - } - ); + matrix_create_key_backup( $user ); })->then( sub { my ( $content ) = @_; log_if_fail "Created version $content->{version}"; + # get all keys do_request_json_for( $user, method => "GET", uri => "/unstable/room_keys/keys", @@ -367,3 +309,107 @@ ); })->main::expect_http_404; }; + + +=head2 matrix_create_key_backup + + matrix_create_key_backup( $user ) + +Create a new key backup version + +=cut + +sub matrix_create_key_backup { + my ( $user ) = @_; + + do_request_json_for( $user, + method => "POST", + uri => "/unstable/room_keys/version", + content => { + algorithm => "m.megolm_backup.v1", + auth_data => "anopaquestring", + } + ) +} + +=head2 matrix_delete_key_backup + + matrix_delete_key_backup( $user, $version ) + +Delete a key backup version + +=cut + +sub matrix_delete_key_backup { + my ( $user, $version ) = @_; + + do_request_json_for( $user, + method => "DELETE", + uri => "/unstable/room_keys/version/$version", + ); +} + + +=head2 matrix_get_key_backup_info + + matrix_get_key_backup_info( $user, $version ) + +Fetch the metadata about a key backup version, or the latest +version if version is omitted + +=cut + +sub matrix_get_key_backup_info { + my ( $user, $version ) = @_; + + my $url = "/unstable/room_keys/version"; + + if (defined($version)) { + $url .= "/$version"; + } + + do_request_json_for( $user, + method => "GET", + uri => $url, + ) +} + +=head2 matrix_backup_keys + + matrix_backup_keys( $user, $room_id, $session_id, $version, $content ) + +Send keys to a given key backup version + +=cut + +sub matrix_backup_keys { + my ( $user, $room_id, $session_id, $version, $content ) = @_; + do_request_json_for( $user, + method => "PUT", + uri => "/unstable/room_keys/keys/$room_id/$session_id", + params => { + version => $version, + }, + content => $content, + ) +} + +=head2 matrix_get_backup_key + + matrix_get_backup_key( $user, $room_id, $session_id, $version ) + +Send keys to a given key backup version + +=cut + +sub matrix_get_backup_key { + my ( $user, $room_id, $session_id, $version ) = @_; + + do_request_json_for( $user, + method => "GET", + uri => "/unstable/room_keys/keys/$room_id/$session_id", + params => { + version => $version, + }, + ); +} From 2f1ccc52d49c8610bd53bebcef699d58603e0081 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 12 Oct 2018 14:39:43 +0100 Subject: [PATCH 9/9] Use assert_eq rather than manually testing --- tests/41end-to-end-keys/07-backup.pl | 36 ++++++++++------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/tests/41end-to-end-keys/07-backup.pl b/tests/41end-to-end-keys/07-backup.pl index 09f38e78f..ce6021d1c 100644 --- a/tests/41end-to-end-keys/07-backup.pl +++ b/tests/41end-to-end-keys/07-backup.pl @@ -72,17 +72,13 @@ assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); - $content->{first_message_index} == 3 or - die "Expected first message index to match submitted data"; + assert_eq( $content->{first_message_index}, 3, "Expected first message index to match submitted data" ); - $content->{forwarded_count} == 0 or - die "Expected forwarded count to match submitted data"; + assert_eq( $content->{forwarded_count}, 0, "Expected forwarded count to match submitted data" ); - $content->{is_verified} == JSON::false or - die "Expected is_verified to match submitted data"; + assert_eq( $content->{is_verified}, JSON::false, "Expected is_verified to match submitted data" ); - $content->{session_data} eq "anopaquestring" or - die "Expected session data to match submitted data"; + assert_eq( $content->{session_data}, "anopaquestring", "Expected session data to match submitted data" ); Future->done(1); }); @@ -128,17 +124,13 @@ assert_json_keys( $content, qw( first_message_index forwarded_count is_verified session_data ) ); - $content->{first_message_index} == 1 or - die "Expected first message index to match submitted data"; + assert_eq( $content->{first_message_index}, 1, "Expected first message index to match submitted data" ); - $content->{forwarded_count} == 0 or - die "Expected forwarded count to match submitted data"; + assert_eq( $content->{forwarded_count}, 0, "Expected forwarded count to match submitted data" ); - $content->{is_verified} == JSON::false or - die "Expected is_verified to match submitted data"; + assert_eq( $content->{is_verified}, JSON::false, "Expected is_verified to match submitted data" ); - $content->{session_data} eq "anotheropaquestring" or - die "Expected session data to match submitted data"; + assert_eq( $content->{session_data}, "anotheropaquestring", "Expected session data to match submitted data" ); Future->done(1); }); @@ -186,17 +178,13 @@ # The data should not be overwritten, so should be the same as what # was set by the previous test. - $content->{first_message_index} == 2 or - die "Expected first message index to match submitted data"; + assert_eq( $content->{first_message_index}, 2, "Expected first message index to match submitted data" ); - $content->{forwarded_count} == 0 or - die "Expected forwarded count to match submitted data"; + assert_eq( $content->{forwarded_count}, 0, "Expected forwarded count to match submitted data" ); - $content->{is_verified} == JSON::false or - die "Expected is_verified to match submitted data"; + assert_eq( $content->{is_verified}, JSON::false, "Expected is_verified to match submitted data" ); - $content->{session_data} eq "anotheropaquestring" or - die "Expected session data to match submitted data"; + assert_eq( $content->{session_data}, "anotheropaquestring", "Expected session data to match submitted data" ); Future->done(1); });