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

Make device display name check optional for federation/v1/user/keys/query #1311

Merged
merged 3 commits into from
Oct 28, 2022

Conversation

anoadragon453
Copy link
Member

@anoadragon453 anoadragon453 commented Oct 27, 2022

Some homeservers (Synapse) have started omitting device's display name over federation by default. The spec doesn't mandate that this field be available, so Sytest shouldn't either.

This PR modifies the check so that it is optional - only if the field is present do we check the value.

Comment on lines 31 to 35
# Device display names are optional for POST /user/keys/query responses.
# If one exists, ensure it's the one we expected.
my $device_display_name = $alice_device_keys->{"unsigned"}->{"device_display_name"};
(!defined $device_display_name) or ($device_display_name == "test display name") or
croak "Unexpected device_display_name: $device_display_name";
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I have no idea if this is idomatic perl (from #1307).

IIRC I manually inlined assert_eq to get the a == b or croak "wrong" form, and then shoved an extra defined check at the start.

Copy link
Member

Choose a reason for hiding this comment

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

I'd say in this situation it would be more idiomatic to use || between the conditions:

         (!defined $device_display_name || $device_display_name eq "test display name") or
            croak "Unexpected device_display_name: $device_display_name";

Note also the use of eq instead of ==: the latter is for numeric comparison:

$ perl -e '"a" == "b" and print "true\n";'
true

A simpler approach here would be to do something like:

         my $device_display_name = $alice_device_keys->{"unsigned"}->{"device_display_name"} // "";
         assert_eq "test display name", $device_display_name, "device display name";

(// is like an || operator, but whereas || returns its right-hand operand if the left-hand one is falsey, // only returns its right-hand operand if the left-hand operand is undef. See https://perldoc.perl.org/perlop#Logical-Defined-Or)

Copy link
Member

Choose a reason for hiding this comment

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

Note also the use of eq instead of ==: the latter is for numeric comparison:

(and yes, this is a massive footgun)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, yes, assert_eq does indeed use eq.

sub assert_eq
{
my ( $got, $want, $name ) = @_;
defined $got && defined $want && $got eq $want or
croak "Got ${\ pp $got }, expected ${\ pp $want } for $name";
}

Presumably this is bogus with == then. Can you fix that up here Andrew---and the test from #1307?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, done in ad8ad25.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually wonder if my original code would be easier to read for the average programmer?

         if (exists( $alice_device_keys->{"unsigned"}->{"device_display_name"} )) {
            assert_eq(
               $alice_device_keys->{"unsigned"}->{"device_display_name"},
               "test display name",
            );
         }

but either way I'm happy as long as it works.

Copy link
Contributor

@DMRobertson DMRobertson left a comment

Choose a reason for hiding this comment

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

== -> eq (aka fix my mistakes pls)

@DMRobertson
Copy link
Contributor

== -> eq (aka fix my mistakes pls)

(Or better: use Rich's formulation)

(Or better better: invent a time machine and prevent perl from being created)

@anoadragon453 anoadragon453 merged commit 8b409e0 into develop Oct 28, 2022
@anoadragon453 anoadragon453 deleted the anoa/device_names_are_not_assumed branch October 28, 2022 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants