aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer_test.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-06-26 20:48:50 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-06-26 20:48:50 +0800
commitd84638bd31878f772c6d1de3b491160319ddfc4b (patch)
treea89ab7e943253f69fb8323da3940011cb36d57d9 /p2p/peer_test.go
parentb0a5be4495962c291a25cbea793e43bad0781510 (diff)
downloadgo-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.gz
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.bz2
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.lz
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.xz
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.zst
go-tangerine-d84638bd31878f772c6d1de3b491160319ddfc4b.zip
p2p: support protocol version negotiation
Diffstat (limited to 'p2p/peer_test.go')
-rw-r--r--p2p/peer_test.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/p2p/peer_test.go b/p2p/peer_test.go
index 575d0ff79..95167f4a1 100644
--- a/p2p/peer_test.go
+++ b/p2p/peer_test.go
@@ -196,3 +196,98 @@ func TestNewPeer(t *testing.T) {
p.Disconnect(DiscAlreadyConnected) // Should not hang
}
+
+func TestMatchProtocols(t *testing.T) {
+ tests := []struct {
+ Local []Cap
+ Remote []Protocol
+ Match map[string]protoRW
+ }{
+ {
+ // No remote protocols
+ Local: []Cap{{Name: "a"}},
+ },
+ {
+ // No local capabilities
+ Remote: []Protocol{{Name: "a"}},
+ },
+ {
+ // No mutual protocols
+ Local: []Cap{{Name: "a"}},
+ Remote: []Protocol{{Name: "b"}},
+ },
+ {
+ // Some matches, some differences
+ Local: []Cap{{Name: "local"}, {Name: "match1"}, {Name: "match2"}},
+ Remote: []Protocol{{Name: "match1"}, {Name: "match2"}, {Name: "remote"}},
+ Match: map[string]protoRW{"match1": {Protocol: Protocol{Name: "match1"}}, "match2": {Protocol: Protocol{Name: "match2"}}},
+ },
+ {
+ // Various alphabetical ordering
+ Local: []Cap{{Name: "aa"}, {Name: "ab"}, {Name: "bb"}, {Name: "ba"}},
+ Remote: []Protocol{{Name: "ba"}, {Name: "bb"}, {Name: "ab"}, {Name: "aa"}},
+ Match: map[string]protoRW{"aa": {Protocol: Protocol{Name: "aa"}}, "ab": {Protocol: Protocol{Name: "ab"}}, "ba": {Protocol: Protocol{Name: "ba"}}, "bb": {Protocol: Protocol{Name: "bb"}}},
+ },
+ {
+ // No mutual versions
+ Local: []Cap{{Version: 1}},
+ Remote: []Protocol{{Version: 2}},
+ },
+ {
+ // Multiple versions, single common
+ Local: []Cap{{Version: 1}, {Version: 2}},
+ Remote: []Protocol{{Version: 2}, {Version: 3}},
+ Match: map[string]protoRW{"": {Protocol: Protocol{Version: 2}}},
+ },
+ {
+ // Multiple versions, multiple common
+ Local: []Cap{{Version: 1}, {Version: 2}, {Version: 3}, {Version: 4}},
+ Remote: []Protocol{{Version: 2}, {Version: 3}},
+ Match: map[string]protoRW{"": {Protocol: Protocol{Version: 3}}},
+ },
+ {
+ // Various version orderings
+ Local: []Cap{{Version: 4}, {Version: 1}, {Version: 3}, {Version: 2}},
+ Remote: []Protocol{{Version: 2}, {Version: 3}, {Version: 1}},
+ Match: map[string]protoRW{"": {Protocol: Protocol{Version: 3}}},
+ },
+ {
+ // Versions overriding sub-protocol lengths
+ Local: []Cap{{Version: 1}, {Version: 2}, {Version: 3}, {Name: "a"}},
+ Remote: []Protocol{{Version: 1, Length: 1}, {Version: 2, Length: 2}, {Version: 3, Length: 3}, {Name: "a"}},
+ Match: map[string]protoRW{"": {Protocol: Protocol{Version: 3}}, "a": {Protocol: Protocol{Name: "a"}, offset: 3}},
+ },
+ }
+
+ for i, tt := range tests {
+ result := matchProtocols(tt.Remote, tt.Local, nil)
+ if len(result) != len(tt.Match) {
+ t.Errorf("test %d: negotiation mismatch: have %v, want %v", i, len(result), len(tt.Match))
+ continue
+ }
+ // Make sure all negotiated protocols are needed and correct
+ for name, proto := range result {
+ match, ok := tt.Match[name]
+ if !ok {
+ t.Errorf("test %d, proto '%s': negotiated but shouldn't have", i, name)
+ continue
+ }
+ if proto.Name != match.Name {
+ t.Errorf("test %d, proto '%s': name mismatch: have %v, want %v", i, name, proto.Name, match.Name)
+ }
+ if proto.Version != match.Version {
+ t.Errorf("test %d, proto '%s': version mismatch: have %v, want %v", i, name, proto.Version, match.Version)
+ }
+ if proto.offset-baseProtocolLength != match.offset {
+ t.Errorf("test %d, proto '%s': offset mismatch: have %v, want %v", i, name, proto.offset-baseProtocolLength, match.offset)
+ }
+ }
+ // Make sure no protocols missed negotiation
+ for name, _ := range tt.Match {
+ if _, ok := result[name]; !ok {
+ t.Errorf("test %d, proto '%s': not negotiated, should have", i, name)
+ continue
+ }
+ }
+ }
+}