Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Room version 6 tests #869

Merged
merged 36 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
79ac786
Add a test-case for invalid JSON (integers out of range).
clokep Apr 27, 2020
695dfcb
Specify the room version.
clokep Apr 28, 2020
d80cb4b
Add tests for floats.
clokep Apr 29, 2020
65855e0
Switch to room version 6 instead of experimental version.
clokep May 14, 2020
d10ad89
Add version 6.
clokep May 14, 2020
dbc682a
Add a test for asserting the notifications power levels of room ver 6.
clokep May 15, 2020
e75245c
POST not PUT
clokep May 18, 2020
972e60b
Properly send infinity / NaN from Perl.
clokep May 18, 2020
727e23f
Fix typo.
clokep May 18, 2020
18e664a
Merge remote-tracking branch 'origin/develop' into clokep/room-ver-6
clokep May 18, 2020
82ae5cd
Clarify that invalid values aren't even JSON.
clokep May 19, 2020
459d9e0
Remove erronously added import.
clokep May 19, 2020
4e65103
Send join tests.
clokep May 21, 2020
d8eaaf8
Add a test for send invite.
clokep May 22, 2020
ee7c457
Reject a v6 room invite successfully.
clokep May 22, 2020
3df4a73
Test send_leave with bad JSON.
clokep May 26, 2020
5054240
Add a test for bad data in the return to make_invite.
clokep May 27, 2020
82907bc
Abstract the code that checks for a bad JSON return code.
clokep May 27, 2020
5bab21c
Add tests for /send API with bad JSON.
clokep May 27, 2020
c4aa0ee
Partially roll-back the JSON checks.
clokep May 27, 2020
9194474
Ensure a bad event doesn't come do backfill.
clokep May 28, 2020
d4340b6
Add a passing test for get_missing_events.
clokep May 29, 2020
2e6e10d
Modify the test to have bad JSON.
clokep Jun 1, 2020
11f68ec
Remove unused variables.
clokep Jun 1, 2020
ed08c3b
Merge branch 'develop' into clokep/room-ver-6
clokep Jun 1, 2020
6663e5d
Move helper for checking for a bad JSON value.
clokep Jun 1, 2020
fa8ce9c
Remove with_events option.
clokep Jun 1, 2020
0dd8fa3
Clarify the get_missing_events test.
clokep Jun 1, 2020
ee89a40
Unique room alias.
clokep Jun 1, 2020
a17a675
Consistently use Future->done.
clokep Jun 1, 2020
4e582a8
Remove duplicate checks.
clokep Jun 1, 2020
9ab4878
Add comments.
clokep Jun 2, 2020
f3d5574
Use federated_rooms_fixture.
clokep Jun 2, 2020
0aebc56
Use matrix_join_room.
clokep Jun 2, 2020
1238be2
Switch to matrix_join_room_synced.
clokep Jun 2, 2020
a49a240
Fix typos
clokep Jun 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/SyTest/Federation/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use SyTest::Assertions qw( :all );
use URI::Escape qw( uri_escape );

use constant SUPPORTED_ROOM_VERSIONS => [qw(
1 2 3 4 5
org.matrix.msc2260
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This room version was previously removed from Synapse, it also seems a bit weird to bit unstable room versions in here.

1 2 3 4 5 6
)];

sub configure
Expand Down
5 changes: 4 additions & 1 deletion lib/SyTest/Federation/Datastore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,13 @@ sub get_backfill_events
my $event = eval { $self->get_event( $id ) }
or next;

my $room = $self->get_room( $event->{room_id} ) or
croak "Unknown room $event->{room_id}";

push @events, $event;

push @event_ids, grep { !$exclude{$_} }
map { $_->[0] } @{ $event->{prev_events} };
@{ $room->event_ids_from_refs( $event->{prev_events} ) };

# Don't include this event if we encounter it again
$exclude{$id} = 1;
Expand Down
27 changes: 25 additions & 2 deletions tests/30rooms/08levels.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

sub lockeddown_room_fixture
{
my ( %options ) = @_;

fixture(
requires => [ $creator_fixture, $user_fixture,
qw( can_change_power_levels ) ],

setup => sub {
my ( $creator, $test_user ) = @_;

matrix_create_and_join_room( [ $creator, $test_user ] )
matrix_create_and_join_room( [ $creator, $test_user ], %options )
->then( sub {
my ( $room_id ) = @_;

Expand Down Expand Up @@ -154,6 +156,27 @@ sub test_powerlevel

$levels->{$levelname} = 10000000;
})->main::expect_http_403
})->SyTest::pass_on_done( "Fails at setting 75" );
})->SyTest::pass_on_done( "Fails at setting 10000000" );
};
}

multi_test "Users cannot set notifications powerlevel higher than their own",
requires => [ $creator_fixture, $user_fixture, lockeddown_room_fixture( room_version => "6" ),
qw( can_change_power_levels )],

do => sub {
my ( $user, undef, $room_id ) = @_;

matrix_change_room_power_levels( $user, $room_id, sub {
my ( $levels ) = @_;

$levels->{notifications}{room} = 25;
})->SyTest::pass_on_done( "Succeeds at setting 25" )
->then( sub {
matrix_change_room_power_levels( $user, $room_id, sub {
my ( $levels ) = @_;

$levels->{notifications}{room} = 10000000;
})->main::expect_http_403
})->SyTest::pass_on_done( "Fails at setting 10000000" );
};
2 changes: 1 addition & 1 deletion tests/32room-versions.pl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use URI::Escape qw( uri_escape );

# We test that some basic functionality works across all room versions
foreach my $version ( qw ( 1 2 3 4 5 ) ) {
foreach my $version ( qw ( 1 2 3 4 5 6 ) ) {
multi_test "User can create and send/receive messages in a room with version $version",
requires => [ local_user_fixture() ],

Expand Down
91 changes: 91 additions & 0 deletions tests/50federation/30room-join.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,94 @@ sub assert_is_valid_pdu {
}),
)
};

push @EXPORT, qw( expect_bad_json );

sub expect_bad_json {
my ($f) = @_;

$f->main::expect_http_400
->then(sub {
# done
my ($response) = @_;
log_if_fail "Response", $response;
my $body = decode_json($response->content);
log_if_fail "Body", $body;

assert_eq($body->{errcode}, "M_BAD_JSON", 'responsecode');
Future->done(1);
});
clokep marked this conversation as resolved.
Show resolved Hide resolved
};

test "Inbound: room version 6 rejects invalid JSON",
clokep marked this conversation as resolved.
Show resolved Hide resolved
requires => [ $main::OUTBOUND_CLIENT, $main::INBOUND_SERVER,
local_user_and_room_fixtures( room_opts => { room_version => "6" } ),
federation_user_id_fixture() ],

do => sub {
my ( $outbound_client, $inbound_server, $creator, $room_id, $user_id ) = @_;
my $first_home_server = $creator->server_name;

my $local_server_name = $outbound_client->server_name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems unused?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used to set the origin below in the send_join request.

my $datastore = $inbound_server->datastore;

$outbound_client->do_request_json(
method => "GET",
hostname => $first_home_server,
uri => "/v1/make_join/$room_id/$user_id",
params => {
ver => ["6"],
},
)->then( sub {
my ( $body ) = @_;

log_if_fail "make_join body", $body;

assert_json_keys( $body, qw( event ));

my $protoevent = $body->{event};

assert_json_keys( $protoevent, qw(
auth_events content depth room_id sender state_key type
));

assert_json_nonempty_list( $protoevent->{auth_events} );

assert_json_nonempty_list( $protoevent->{prev_events} );

assert_json_number( $protoevent->{depth} );

$protoevent->{room_id} eq $room_id or
die "Expected 'room_id' to be $room_id";
$protoevent->{sender} eq $user_id or
die "Expected 'sender' to be $user_id";
$protoevent->{state_key} eq $user_id or
die "Expected 'state_key' to be $user_id";
$protoevent->{type} eq "m.room.member" or
die "Expected 'type' to be 'm.room.member'";

assert_json_keys( my $content = $protoevent->{content}, qw( membership ) );
$content->{membership} eq "join" or
die "Expected 'membership' to be 'join'";
clokep marked this conversation as resolved.
Show resolved Hide resolved

my %event = (
( map { $_ => $protoevent->{$_} } qw(
auth_events content depth prev_events room_id sender
state_key type ) ),

origin => $local_server_name,
origin_server_ts => $inbound_server->time_ms,
);
# Insert a "bad" value into the send join, in this case a float.
${event}{contents}{bad_val} = 1.1;

$datastore->sign_event( \%event );

$outbound_client->do_request_json(
method => "PUT",
hostname => $first_home_server,
uri => "/v2/send_join/$room_id/xxx",
content => \%event,
)
})->main::expect_bad_json;
};
138 changes: 129 additions & 9 deletions tests/50federation/33room-get-missing-events.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
my ( $outbound_client, $inbound_server, $creator, $room_id, $user_id ) = @_;
my $first_home_server = $creator->server_name;

my $local_server_name = $inbound_server->server_name;
my $datastore = $inbound_server->datastore;

my $missing_event;
my $missing_event_id;

$outbound_client->join_room(
server_name => $first_home_server,
Expand All @@ -27,14 +26,15 @@
my $latest_event = $room->get_current_state_event( "m.room.member", $user_id );

# Generate but don't send an event
$missing_event = $room->create_and_insert_event(
my $missing_event = $room->create_and_insert_event(
type => "m.room.message",

sender => $user_id,
content => {
body => "Message 1",
},
);
$missing_event_id = $room->id_for_event( $missing_event );

# Generate another one and do send it so it will refer to the
# previous in its prev_events field
Expand All @@ -43,9 +43,7 @@

# This would be done by $room->create_and_insert_event anyway but lets be
# sure for this test
prev_events => [
[ $missing_event->{event_id}, $missing_event->{hashes} ],
],
prev_events => $room->make_event_refs( $missing_event ),

sender => $user_id,
content => {
Expand All @@ -65,13 +63,13 @@
assert_json_list( my $earliest = $body->{earliest_events} );
@$earliest == 1 or
die "Expected a single 'earliest_event' ID";
assert_eq( $earliest->[0], $latest_event->{event_id},
assert_eq( $earliest->[0], $room->id_for_event( $latest_event ),
'earliest_events[0]' );

assert_json_list( my $latest = $body->{latest_events} );
@$latest == 1 or
die "Expected a single 'latest_events' ID";
assert_eq( $latest->[0], $sent_event->{event_id},
assert_eq( $latest->[0], $room->id_for_event( $sent_event ),
'latest_events[0]' );

my @events = $datastore->get_backfill_events(
Expand Down Expand Up @@ -99,7 +97,7 @@
check => sub {
my ( $event ) = @_;
$event->{type} eq "m.room.message" &&
$event->{event_id} eq $missing_event->{event_id};
$event->{event_id} eq $missing_event_id;
},
);
});
Expand Down Expand Up @@ -413,3 +411,125 @@ sub sytest_user_and_room_fixture {
Future->done;
});
};

test "Outbound federation will ignore a missing event with bad JSON for room version 6",
requires => [ $main::OUTBOUND_CLIENT, $main::INBOUND_SERVER,
local_user_and_room_fixtures(
user_opts => { with_events => 1 },
clokep marked this conversation as resolved.
Show resolved Hide resolved
room_opts => { room_version => "6" },
),
federation_user_id_fixture() ],

do => sub {
my ( $outbound_client, $inbound_server, $creator, $room_id, $user_id ) = @_;
my $first_home_server = $creator->server_name;

my $datastore = $inbound_server->datastore;

$outbound_client->join_room(
server_name => $first_home_server,
room_id => $room_id,
user_id => $user_id,
version => "6",
)->then( sub {
clokep marked this conversation as resolved.
Show resolved Hide resolved
my ( $room ) = @_;

# TODO: We happen to know the latest event in the server should be my
# m.room.member state event, but that's a bit fragile
my $latest_event = $room->get_current_state_event( "m.room.member", $user_id );

log_if_fail "Latest event", $latest_event;

# Generate but don't send an event
my $missing_event = $room->create_and_insert_event(
type => "m.room.message",

sender => $user_id,
content => {
body => "Message 1",
bad_val => 1.1,
},
);

log_if_fail "Missing event", $missing_event;

# Generate another one and do send it so it will refer to the
# previous in its prev_events field
my $sent_event = $room->create_and_insert_event(
type => "m.room.message",

# This would be done by $room->create_and_insert_event anyway but lets be
# sure for this test
prev_events => $room->make_event_refs( $missing_event ),

sender => $user_id,
content => {
body => "Message 2",
},
);
my $sent_event_id = $room->id_for_event( $sent_event );

log_if_fail "Sent event", $sent_event;

Future->needs_all(
$inbound_server->await_request_get_missing_events( $room_id )
->then( sub {
my ( $req ) = @_;
my $body = $req->body_from_json;

log_if_fail "Body", $body;

assert_json_keys( $body, qw( earliest_events latest_events limit ));
# TODO: min_depth but I have no idea what it does

assert_json_list( my $earliest = $body->{earliest_events} );
@$earliest == 1 or
die "Expected a single 'earliest_event' ID";
assert_eq( $earliest->[0], $room->id_for_event( $latest_event ),
'earliest_events[0]' );

assert_json_list( my $latest = $body->{latest_events} );
@$latest == 1 or
die "Expected a single 'latest_events' ID";
assert_eq( $latest->[0], $sent_event_id,
'latest_events[0]' );

my @events = $datastore->get_backfill_events(
start_at => $latest,
stop_before => $earliest,
limit => $body->{limit},
);

log_if_fail "Backfilling", @events;

$req->respond_json( {
events => \@events,
} );

log_if_fail "Done here";
clokep marked this conversation as resolved.
Show resolved Hide resolved

Future->done(1);
clokep marked this conversation as resolved.
Show resolved Hide resolved
}),

# Can't use send_event here because that checks none were rejected.
$outbound_client->send_transaction(
destination => $first_home_server,
pdus => [ $sent_event ],
)->then( sub {
my ( $body ) = @_;

log_if_fail "Send response", $body;

assert_json_keys( $body, 'pdus' );
# 'pdus' is a map from event id to error details.
my $pdus = $body->{pdus};

# Sending the event fails since fetching the event results in
# invalid JSON.
assert_json_keys( $pdus, $sent_event_id );
clokep marked this conversation as resolved.
Show resolved Hide resolved

Future->done;
}),
);
});
};
Loading