-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandshake_test.go
67 lines (59 loc) · 1.62 KB
/
handshake_test.go
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
package p2p
import (
"math/rand"
"testing"
)
func testRandomHandshake() *Handshake {
hs := new(Handshake)
hs.Version = uint16(rand.Uint32())
hs.Type = ParcelType(rand.Uint32())
hs.NodeID = rand.Uint32()
hs.Network = NetworkID(rand.Uint32())
hs.Loopback = rand.Uint64()
hs.ListenPort = testRandomPort()
if hs.Type == TypeRejectAlternative {
alts := 1 + rand.Intn(16)
hs.Alternatives = testRandomEndpointList(alts)
}
return hs
}
func TestHandshake_Valid(t *testing.T) {
conf := DefaultP2PConfiguration()
var handshakes []*Handshake
for i := 0; i < 7; i++ {
hs := newHandshake(&conf, 0)
hs.NodeID++
handshakes = append(handshakes, hs)
}
// 0 is default payload
handshakes[1].Version = 2 // incompatible version
handshakes[2].Network = 0xf00
handshakes[3].ListenPort = "foo"
handshakes[4].ListenPort = ""
handshakes[5].ListenPort = "0"
handshakes[6].ListenPort = "900000"
type args struct {
conf *Configuration
}
tests := []struct {
name string
h *Handshake
args args
wantErr bool
}{
{"default (valid)", handshakes[0], args{&conf}, false},
{"wrong version", handshakes[1], args{&conf}, true},
{"wrong network", handshakes[2], args{&conf}, true},
{"unparseable port", handshakes[3], args{&conf}, true},
{"empty port", handshakes[4], args{&conf}, true},
{"zero port", handshakes[5], args{&conf}, true},
{"too high port", handshakes[6], args{&conf}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.h.Valid(tt.args.conf); (err != nil) != tt.wantErr {
t.Errorf("Handshake.Valid() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}