aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer_test.go
blob: 629475421bc27cade676a200f96bbacf217f1de6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package p2p

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.Value()
            if err != nil {
                t.Errorf("data decoding error: %v", err)
            }
            expdata := []interface{}{[]byte{0x01}, []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)
    }
}