aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-11-22 04:48:49 +0800
committerFelix Lange <fjl@twurst.com>2014-11-22 04:52:45 +0800
commit59b63caf5e4de64ceb7dcdf01551a080f53b1672 (patch)
treea4e79590284c5afe4d6927b422a5092b074e7938 /p2p/peer_test.go
parente4a601c6444afdc11ce0cb80d7fd83116de2c8b9 (diff)
downloaddexon-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.gz
dexon-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.zst
dexon-59b63caf5e4de64ceb7dcdf01551a080f53b1672.zip
p2p: API cleanup and PoC 7 compatibility
Whoa, one more big commit. I didn't manage to untangle the changes while working towards compatibility.
Diffstat (limited to 'p2p/peer_test.go')
-rw-r--r--p2p/peer_test.go308
1 files changed, 220 insertions, 88 deletions
diff --git a/p2p/peer_test.go b/p2p/peer_test.go
index da62cc380..1afa0ab17 100644
--- a/p2p/peer_test.go
+++ b/p2p/peer_test.go
@@ -1,90 +1,222 @@
package p2p
-// "net"
-
-// func TestPeer(t *testing.T) {
-// handlers := make(Handlers)
-// testProtocol := &TestProtocol{recv: make(chan testMsg)}
-// handlers["aaa"] = func(p *Peer) Protocol { return testProtocol }
-// handlers["ccc"] = func(p *Peer) Protocol { return testProtocol }
-// addr := &TestAddr{"test:30"}
-// conn := NewTestNetworkConnection(addr)
-// _, server := SetupTestServer(handlers)
-// server.Handshake()
-// peer := NewPeer(conn, addr, true, server)
-// // peer.Messenger().AddProtocols([]string{"aaa", "ccc"})
-// peer.Start()
-// defer peer.Stop()
-// time.Sleep(2 * time.Millisecond)
-// if len(conn.Out) != 1 {
-// t.Errorf("handshake not sent")
-// } else {
-// out := conn.Out[0]
-// packet := Packet(0, HandshakeMsg, P2PVersion, []byte(peer.server.identity.String()), []interface{}{peer.server.protocols}, peer.server.port, peer.server.identity.Pubkey()[1:])
-// if bytes.Compare(out, packet) != 0 {
-// t.Errorf("incorrect handshake packet %v != %v", out, packet)
-// }
-// }
-
-// packet := Packet(0, HandshakeMsg, P2PVersion, []byte("peer"), []interface{}{"bbb", "aaa", "ccc"}, 30, []byte("0000000000000000000000000000000000000000000000000000000000000000"))
-// conn.In(0, packet)
-// time.Sleep(10 * time.Millisecond)
-
-// pro, _ := peer.Messenger().protocols[0].(*BaseProtocol)
-// if pro.state != handshakeReceived {
-// t.Errorf("handshake not received")
-// }
-// if peer.Port != 30 {
-// t.Errorf("port incorrectly set")
-// }
-// if peer.Id != "peer" {
-// t.Errorf("id incorrectly set")
-// }
-// if string(peer.Pubkey) != "0000000000000000000000000000000000000000000000000000000000000000" {
-// t.Errorf("pubkey incorrectly set")
-// }
-// fmt.Println(peer.Caps)
-// if len(peer.Caps) != 3 || peer.Caps[0] != "aaa" || peer.Caps[1] != "bbb" || peer.Caps[2] != "ccc" {
-// t.Errorf("protocols incorrectly set")
-// }
-
-// msg := NewMsg(3)
-// err := peer.Write("aaa", msg)
-// if err != nil {
-// t.Errorf("expect no error for known protocol: %v", err)
-// } else {
-// time.Sleep(1 * time.Millisecond)
-// if len(conn.Out) != 2 {
-// t.Errorf("msg not written")
-// } else {
-// out := conn.Out[1]
-// packet := Packet(16, 3)
-// if bytes.Compare(out, packet) != 0 {
-// t.Errorf("incorrect packet %v != %v", out, packet)
-// }
-// }
-// }
-
-// msg = NewMsg(2)
-// err = peer.Write("ccc", msg)
-// if err != nil {
-// t.Errorf("expect no error for known protocol: %v", err)
-// } else {
-// time.Sleep(1 * time.Millisecond)
-// if len(conn.Out) != 3 {
-// t.Errorf("msg not written")
-// } else {
-// out := conn.Out[2]
-// packet := Packet(21, 2)
-// if bytes.Compare(out, packet) != 0 {
-// t.Errorf("incorrect packet %v != %v", out, packet)
-// }
-// }
-// }
-
-// err = peer.Write("bbb", msg)
-// time.Sleep(1 * time.Millisecond)
-// if err == nil {
-// t.Errorf("expect error for unknown protocol")
-// }
-// }
+import (
+ "bufio"
+ "net"
+ "reflect"
+ "testing"
+ "time"
+)
+
+var discard = Protocol{
+ Name: "discard",
+ Length: 1,
+ Run: func(p *Peer, rw MsgReadWriter) error {
+ for {
+ msg, err := rw.ReadMsg()
+ if err != nil {
+ return err
+ }
+ if err = msg.Discard(); err != nil {
+ return err
+ }
+ }
+ },
+}
+
+func testPeer(protos []Protocol) (net.Conn, *Peer, <-chan error) {
+ conn1, conn2 := net.Pipe()
+ id := NewSimpleClientIdentity("test", "0", "0", "public key")
+ peer := newPeer(conn1, protos, nil)
+ peer.ourID = id
+ peer.pubkeyHook = func(*peerAddr) error { return nil }
+ errc := make(chan error, 1)
+ go func() {
+ _, err := peer.loop()
+ errc <- err
+ }()
+ return conn2, peer, errc
+}
+
+func TestPeerProtoReadMsg(t *testing.T) {
+ defer testlog(t).detach()
+
+ done := make(chan struct{})
+ proto := Protocol{
+ Name: "a",
+ Length: 5,
+ Run: func(peer *Peer, rw MsgReadWriter) error {
+ msg, err := rw.ReadMsg()
+ if err != nil {
+ t.Errorf("read error: %v", err)
+ }
+ if msg.Code != 2 {
+ t.Errorf("incorrect msg code %d relayed to protocol", msg.Code)
+ }
+ data, err := msg.Data()
+ if err != nil {
+ t.Errorf("data decoding error: %v", err)
+ }
+ expdata := []interface{}{1, []byte{0x30, 0x30, 0x30}}
+ if !reflect.DeepEqual(data.Slice(), expdata) {
+ t.Errorf("incorrect msg data %#v", data.Slice())
+ }
+ close(done)
+ return nil
+ },
+ }
+
+ net, peer, errc := testPeer([]Protocol{proto})
+ defer net.Close()
+ peer.startSubprotocols([]Cap{proto.cap()})
+
+ writeMsg(net, NewMsg(18, 1, "000"))
+ select {
+ case <-done:
+ case err := <-errc:
+ t.Errorf("peer returned: %v", err)
+ case <-time.After(2 * time.Second):
+ t.Errorf("receive timeout")
+ }
+}
+
+func TestPeerProtoReadLargeMsg(t *testing.T) {
+ defer testlog(t).detach()
+
+ msgsize := uint32(10 * 1024 * 1024)
+ done := make(chan struct{})
+ proto := Protocol{
+ Name: "a",
+ Length: 5,
+ Run: func(peer *Peer, rw MsgReadWriter) error {
+ msg, err := rw.ReadMsg()
+ if err != nil {
+ t.Errorf("read error: %v", err)
+ }
+ if msg.Size != msgsize+4 {
+ t.Errorf("incorrect msg.Size, got %d, expected %d", msg.Size, msgsize)
+ }
+ msg.Discard()
+ close(done)
+ return nil
+ },
+ }
+
+ net, peer, errc := testPeer([]Protocol{proto})
+ defer net.Close()
+ peer.startSubprotocols([]Cap{proto.cap()})
+
+ writeMsg(net, NewMsg(18, make([]byte, msgsize)))
+ select {
+ case <-done:
+ case err := <-errc:
+ t.Errorf("peer returned: %v", err)
+ case <-time.After(2 * time.Second):
+ t.Errorf("receive timeout")
+ }
+}
+
+func TestPeerProtoEncodeMsg(t *testing.T) {
+ defer testlog(t).detach()
+
+ proto := Protocol{
+ Name: "a",
+ Length: 2,
+ Run: func(peer *Peer, rw MsgReadWriter) error {
+ if err := rw.EncodeMsg(2); err == nil {
+ t.Error("expected error for out-of-range msg code, got nil")
+ }
+ if err := rw.EncodeMsg(1); err != nil {
+ t.Errorf("write error: %v", err)
+ }
+ return nil
+ },
+ }
+ net, peer, _ := testPeer([]Protocol{proto})
+ defer net.Close()
+ peer.startSubprotocols([]Cap{proto.cap()})
+
+ bufr := bufio.NewReader(net)
+ msg, err := readMsg(bufr)
+ if err != nil {
+ t.Errorf("read error: %v", err)
+ }
+ if msg.Code != 17 {
+ t.Errorf("incorrect message code: got %d, expected %d", msg.Code, 17)
+ }
+}
+
+func TestPeerWrite(t *testing.T) {
+ defer testlog(t).detach()
+
+ net, peer, peerErr := testPeer([]Protocol{discard})
+ defer net.Close()
+ peer.startSubprotocols([]Cap{discard.cap()})
+
+ // test write errors
+ if err := peer.writeProtoMsg("b", NewMsg(3)); err == nil {
+ t.Errorf("expected error for unknown protocol, got nil")
+ }
+ if err := peer.writeProtoMsg("discard", NewMsg(8)); err == nil {
+ t.Errorf("expected error for out-of-range msg code, got nil")
+ } else if perr, ok := err.(*peerError); !ok || perr.Code != errInvalidMsgCode {
+ t.Errorf("wrong error for out-of-range msg code, got %#v", err)
+ }
+
+ // setup for reading the message on the other end
+ read := make(chan struct{})
+ go func() {
+ bufr := bufio.NewReader(net)
+ msg, err := readMsg(bufr)
+ if err != nil {
+ t.Errorf("read error: %v", err)
+ } else if msg.Code != 16 {
+ t.Errorf("wrong code, got %d, expected %d", msg.Code, 16)
+ }
+ msg.Discard()
+ close(read)
+ }()
+
+ // test succcessful write
+ if err := peer.writeProtoMsg("discard", NewMsg(0)); err != nil {
+ t.Errorf("expect no error for known protocol: %v", err)
+ }
+ select {
+ case <-read:
+ case err := <-peerErr:
+ t.Fatalf("peer stopped: %v", err)
+ }
+}
+
+func TestPeerActivity(t *testing.T) {
+ // shorten inactivityTimeout while this test is running
+ oldT := inactivityTimeout
+ defer func() { inactivityTimeout = oldT }()
+ inactivityTimeout = 20 * time.Millisecond
+
+ net, peer, peerErr := testPeer([]Protocol{discard})
+ defer net.Close()
+ peer.startSubprotocols([]Cap{discard.cap()})
+
+ sub := peer.activity.Subscribe(time.Time{})
+ defer sub.Unsubscribe()
+
+ for i := 0; i < 6; i++ {
+ writeMsg(net, NewMsg(16))
+ select {
+ case <-sub.Chan():
+ case <-time.After(inactivityTimeout / 2):
+ t.Fatal("no event within ", inactivityTimeout/2)
+ case err := <-peerErr:
+ t.Fatal("peer error", err)
+ }
+ }
+
+ select {
+ case <-time.After(inactivityTimeout * 2):
+ case <-sub.Chan():
+ t.Fatal("got activity event while connection was inactive")
+ case err := <-peerErr:
+ t.Fatal("peer error", err)
+ }
+}
mmitgraph'>* Update x11-drivers/xf86-video-nv to 2.1.8.flz2008-03-312-4/+4 * - Mark BROKEN on 5.Xpav2008-03-151-1/+7 * Add patch to fix possibly insecure mouse behavior when switching VT.flz2008-03-132-0/+39 * - Make the IGNORE message even more explicit when there areflz2008-03-131-1/+1 * Add patch to fix mouse pointer coordinates.flz2008-03-132-0/+29 * Remove support for XFree8-4.flz2008-03-082-24/+0 * Fix pkg-plist.flz2008-03-074-4/+6 * Update x11-drivers/xf86-video-ati to 6.8.0.flz2008-03-064-8/+8 * Remove quirk for openchrome as it now installs openchrome_drv.so insteadflz2008-03-061-1/+1 * Oops, bump PORTREVISION to remake on amd64.nork2008-02-201-1/+1 * o Fix invalid synaptics issue with x11-drivers/xf86-input-voidnork2008-02-202-5/+10 * Update xf86-video-ati to 6.7.197.flz2008-02-074-8/+8 * Update xf86-video-nv to 2.1.7.flz2008-02-072-4/+4 * Add glproto to USE_XORG.flz2008-01-191-1/+1 * - Update port to 0.2.901.flz2008-01-116-62/+29 * Rollback to xf86-video-mga 1.4.7 (and patches from master as of today).flz2008-01-023-4/+156 * Rollback to xf86-video-intel 2.1.1 before anyone notices.flz2008-01-024-8/+8 * Update x11-drivers/xf86-video-radeonhd to 1.1.0.flz2008-01-024-8/+8 * Update x11-drivers/xf86-video-intel to 2.2.0.flz2008-01-024-8/+8 * Turn off via video driver for sparc64. It doesn't make sense, and currentlylinimon2007-12-251-1/+3 * Update x11-drivers/xf86-video-ati to 6.7.196.flz2007-12-134-8/+8 * Update x11-drivers/xf86-video-i810 to 1.7.4.flz2007-12-122-5/+4 * - Make intel driver default rather than i810.flz2007-12-121-3/+5 * Update to 1.0.0 which adds, among other things, working MacBook Pro support.marcus2007-12-034-12/+12 * Unbreak pthread-related issues on 5.xade2007-11-272-7/+7 * Mark as broken on 5.x: fails to find pthread.h.linimon2007-11-251-1/+1 * Mark as BROKEN on 5.x: fails to compile.linimon2007-11-221-1/+7 * Update to 2.1.6.delphij2007-10-262-4/+4 * Re-add patch-at_scancode.c and patch-bsd_kbd.c, which prior tomarius2007-10-253-0/+65 * This package contains the X.Org xf86-video-radeonhd driver.miwi2007-10-239-0/+59 * Update xf86-input-mouse to 1.2.3.flz2007-10-152-4/+4 * Update xf86-video-ati to 6.7.195.flz2007-10-104-8/+8 * - Don't add sunffb video driver to VIDEO_OFF on !sparc64 as itflz2007-10-101-9/+23 * Update xf86-video-mga to 1.9.100.flz2007-10-102-4/+4 * Update xf86-video-nv to 2.1.5.flz2007-10-102-5/+4 * Update xf86-video-ati to 6.7.194.flz2007-10-104-8/+8 * Update xf86-input-vmmouse to 12.4.3.flz2007-10-102-5/+4 * Update xf86-video-vmware to 10.15.2.flz2007-10-102-4/+4 * Add xf86-video-openchrome 0.2.6, an alternative driver for VIA chipsets.flz2007-10-106-0/+81 * Fix CATEGORIES after repocopy from x11-serverserwin2007-10-071-1/+1 * Change to new x11-drivers category.nork2007-10-061-0/+1 * Update distinfo. Previous checksum was for non-official vmmouse release.flz2007-09-212-3/+4 * Update x11-drivers/xf86-video-driver to 6.7.193.flz2007-09-204-8/+8 * Bump PORTREVISION after xorg-server ABI change.flz2007-09-1528-6/+28 * - Update X.org ports to 7.3.flz2007-09-1451-111/+184 * Take over maintainership.flz2007-09-141-1/+1 * [maintainer update] x11-drivers/xf86-input-vmmouse: update to 12.4.2edwin2007-09-122-4/+4 * - Update to 12.4.1miwi2007-09-073-5/+4 * Add intel driver to VIDEO_OFF and check for conflict with i810.flz2007-08-291-2/+6 * Style fix (xextproto listed twice).flz2007-08-291-1/+1 * Add sunffb to VIDEO_ON on sparc64.flz2007-08-291-2/+11 * - Don't depend on xlibs.flz2007-08-291-63/+53 * Update x11-drivers/xf86-video-nv to 2.1.3.flz2007-08-242-4/+4 * Update x11-drivers/xf86-video-nv to 2.1.2.flz2007-07-193-6/+4 * xf86-video-intel: update to 2.1.0anholt2007-07-066-8/+12 * This package contains the X.Org xf86-input-vmmouse driver.miwi2007-06-045-0/+29 * Add conflicts with xf86-video-intel.lesi2007-06-011-0/+2 * Add xf86-video-intel driver that obsoletes xf86-video-i810 one. It willlesi2007-06-019-0/+95 * - Welcome X.org 7.2 \o/.flz2007-05-20245-12/+2109 * Update to 0.14.6.nork2006-08-133-41/+50 * Add a description of CorePointer, and Cosmetic change.nork2006-03-191-2/+7 * Oops, fix typo.nork2006-03-191-1/+1 * Fix up some parameters and messages.nork2006-03-191-10/+23 * o Refrect my environment values.nork2006-03-193-7/+13 * o Rename .o to .so. [1]nork2006-03-193-10/+16