Skip to content

Commit

Permalink
Add Equal method for CrossSigningKey
Browse files Browse the repository at this point in the history
  • Loading branch information
S7evinK committed Dec 21, 2024
1 parent e86ab16 commit feba51a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
45 changes: 44 additions & 1 deletion fclient/crosssigning.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package fclient

import (
"bytes"
"encoding/json"

Check failure on line 19 in fclient/crosssigning.go

View workflow job for this annotation

GitHub Actions / Linting

File is not `goimports`-ed (goimports)

"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -46,6 +46,49 @@ type CrossSigningKey struct {

func (s *CrossSigningKey) isCrossSigningBody() {} // implements CrossSigningBody

func (s *CrossSigningKey) Equal(other *CrossSigningKey) bool {
if s == nil || other == nil {
return false
}
if s.UserID != other.UserID {
return false
}
if len(s.Usage) != len(other.Usage) {
return false
}
for i := range s.Usage {
if s.Usage[i] != other.Usage[i] {
return false
}
}
if len(s.Keys) != len(other.Keys) {
return false
}
for k, v := range s.Keys {
if !bytes.Equal(other.Keys[k], v) {
return false
}
}
if len(s.Signatures) != len(other.Signatures) {
return false
}
for k, v := range s.Signatures {
otherV, ok := other.Signatures[k]
if !ok {
return false
}
if len(v) != len(otherV) {
return false
}
for k2, v2 := range v {
if !bytes.Equal(otherV[k2], v2) {
return false
}
}
}
return true
}

type CrossSigningBody interface {
isCrossSigningBody()
}
Expand Down
95 changes: 95 additions & 0 deletions fclient/crosssigning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package fclient

import (

Check failure on line 3 in fclient/crosssigning_test.go

View workflow job for this annotation

GitHub Actions / Linting

File is not `goimports`-ed (goimports)
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
"testing"
)

func TestCrossSigningKeyEqual(t *testing.T) {
tests := []struct {
name string
s *CrossSigningKey
other *CrossSigningKey
expect bool
}{
{
name: "NilReceiver_ReturnsFalse",
s: nil,
other: &CrossSigningKey{},
expect: false,
},
{
name: "NilOther_ReturnsFalse",
s: &CrossSigningKey{},
other: nil,
expect: false,
},
{
name: "DifferentUserID_ReturnsFalse",
s: &CrossSigningKey{UserID: "user1"},
other: &CrossSigningKey{UserID: "user2"},
expect: false,
},
{
name: "DifferentUsageLength_ReturnsFalse",
s: &CrossSigningKey{Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeMaster}},
other: &CrossSigningKey{Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeMaster, CrossSigningKeyPurposeSelfSigning}},
expect: false,
},
{
name: "DifferentUsageValues_ReturnsFalse",
s: &CrossSigningKey{Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeMaster}},
other: &CrossSigningKey{Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeSelfSigning}},
expect: false,
},
{
name: "DifferentKeysLength_ReturnsFalse",
s: &CrossSigningKey{Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {}}},
other: &CrossSigningKey{Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {}, "key2": {}}},
expect: false,
},
{
name: "DifferentKeysValues_ReturnsFalse",
s: &CrossSigningKey{Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {}}},
other: &CrossSigningKey{Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {1}}},
expect: false,
},
{
name: "DifferentSignaturesLength_ReturnsFalse",
s: &CrossSigningKey{Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {}}}},
other: &CrossSigningKey{Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {}}, "sig2": {"key2": {}}}},
expect: false,
},
{
name: "DifferentSignaturesValues_ReturnsFalse",
s: &CrossSigningKey{Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {}}}},
other: &CrossSigningKey{Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {1}}}},
expect: false,
},
{
name: "IdenticalKeys_ReturnsTrue",
s: &CrossSigningKey{
UserID: "user1",
Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeMaster},
Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {}},
Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {}}},
},
other: &CrossSigningKey{
UserID: "user1",
Usage: []CrossSigningKeyPurpose{CrossSigningKeyPurposeMaster},
Keys: map[gomatrixserverlib.KeyID]spec.Base64Bytes{"key1": {}},
Signatures: map[string]map[gomatrixserverlib.KeyID]spec.Base64Bytes{"sig1": {"key1": {}}},
},
expect: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.Equal(tt.other); got != tt.expect {
t.Errorf("Equal() = %v, want %v", got, tt.expect)
}
})
}
}

0 comments on commit feba51a

Please sign in to comment.