-
Notifications
You must be signed in to change notification settings - Fork 20.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2p: add test for base protocol disconnect
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package p2p | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestBaseProtocolDisconnect(t *testing.T) { | ||
peer := NewPeer(NewSimpleClientIdentity("p1", "", "", "foo"), nil) | ||
peer.ourID = NewSimpleClientIdentity("p2", "", "", "bar") | ||
peer.pubkeyHook = func(*peerAddr) error { return nil } | ||
|
||
rw1, rw2 := MsgPipe() | ||
done := make(chan struct{}) | ||
go func() { | ||
if err := expectMsg(rw2, handshakeMsg); err != nil { | ||
t.Error(err) | ||
} | ||
err := rw2.EncodeMsg(handshakeMsg, | ||
baseProtocolVersion, | ||
"", | ||
[]interface{}{}, | ||
0, | ||
make([]byte, 64), | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if err := expectMsg(rw2, getPeersMsg); err != nil { | ||
t.Error(err) | ||
} | ||
if err := rw2.EncodeMsg(discMsg, DiscQuitting); err != nil { | ||
t.Error(err) | ||
} | ||
close(done) | ||
}() | ||
|
||
if err := runBaseProtocol(peer, rw1); err == nil { | ||
t.Errorf("base protocol returned without error") | ||
} else if reason, ok := err.(discRequestedError); !ok || reason != DiscQuitting { | ||
t.Errorf("base protocol returned wrong error: %v", err) | ||
} | ||
<-done | ||
} | ||
|
||
func expectMsg(r MsgReader, code uint64) error { | ||
msg, err := r.ReadMsg() | ||
if err != nil { | ||
return err | ||
} | ||
if err := msg.Discard(); err != nil { | ||
return err | ||
} | ||
if msg.Code != code { | ||
return fmt.Errorf("wrong message code: got %d, expected %d", msg.Code, code) | ||
} | ||
return nil | ||
} |