-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
ltep_test.go
129 lines (119 loc) · 4.01 KB
/
ltep_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
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
package torrent_test
import (
"math/rand"
"strconv"
"testing"
"github.com/anacrolix/sync"
qt "github.com/frankban/quicktest"
. "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/internal/testutil"
pp "github.com/anacrolix/torrent/peer_protocol"
)
const (
testRepliesToOddsExtensionName = "pm_me_odds"
testRepliesToEvensExtensionName = "pm_me_evens"
)
func countHandler(
c *qt.C,
wg *sync.WaitGroup,
// Name of the endpoint that this handler is for, for logging.
handlerName string,
// Whether we expect evens or odds
expectedMod2 uint,
// Extension name of messages we expect to handle.
answerToName pp.ExtensionName,
// Extension name of messages we expect to send.
replyToName pp.ExtensionName,
// Signal done when this value is seen.
doneValue uint,
) func(event PeerConnReadExtensionMessageEvent) {
return func(event PeerConnReadExtensionMessageEvent) {
// Read handshake, don't look it up.
if event.ExtensionNumber == 0 {
return
}
name, builtin, err := event.PeerConn.LocalLtepProtocolMap.LookupId(event.ExtensionNumber)
c.Assert(err, qt.IsNil)
// Not a user protocol.
if builtin {
return
}
switch name {
case answerToName:
u64, err := strconv.ParseUint(string(event.Payload), 10, 0)
c.Assert(err, qt.IsNil)
i := uint(u64)
c.Logf("%v got %d", handlerName, i)
if i == doneValue {
wg.Done()
return
}
c.Assert(i%2, qt.Equals, expectedMod2)
go func() {
c.Assert(
event.PeerConn.WriteExtendedMessage(
replyToName,
[]byte(strconv.FormatUint(uint64(i+1), 10))),
qt.IsNil)
}()
default:
c.Fatalf("got unexpected extension name %q", name)
}
}
}
func TestUserLtep(t *testing.T) {
c := qt.New(t)
var wg sync.WaitGroup
makeCfg := func() *ClientConfig {
cfg := TestingConfig(t)
// Only want a single connection to between the clients.
cfg.DisableUTP = true
cfg.DisableIPv6 = true
return cfg
}
evensCfg := makeCfg()
evensCfg.Callbacks.ReadExtendedHandshake = func(pc *PeerConn, msg *pp.ExtendedHandshakeMessage) {
// The client lock is held while handling this event, so we have to do synchronous work in a
// separate goroutine.
go func() {
// Check sending an extended message for a protocol the peer doesn't support is an error.
c.Check(pc.WriteExtendedMessage("pm_me_floats", []byte("3.142")), qt.IsNotNil)
// Kick things off by sending a 1.
c.Check(pc.WriteExtendedMessage(testRepliesToOddsExtensionName, []byte("1")), qt.IsNil)
}()
}
evensCfg.Callbacks.PeerConnReadExtensionMessage = append(
evensCfg.Callbacks.PeerConnReadExtensionMessage,
countHandler(c, &wg, "evens", 0, testRepliesToEvensExtensionName, testRepliesToOddsExtensionName, 100))
evensCfg.Callbacks.PeerConnAdded = append(evensCfg.Callbacks.PeerConnAdded, func(conn *PeerConn) {
conn.LocalLtepProtocolMap.AddUserProtocol(testRepliesToEvensExtensionName)
c.Assert(conn.LocalLtepProtocolMap.Index[conn.LocalLtepProtocolMap.NumBuiltin:], qt.HasLen, 1)
})
oddsCfg := makeCfg()
oddsCfg.Callbacks.PeerConnAdded = append(oddsCfg.Callbacks.PeerConnAdded, func(conn *PeerConn) {
conn.LocalLtepProtocolMap.AddUserProtocol(testRepliesToOddsExtensionName)
c.Assert(conn.LocalLtepProtocolMap.Index[conn.LocalLtepProtocolMap.NumBuiltin:], qt.HasLen, 1)
})
oddsCfg.Callbacks.PeerConnReadExtensionMessage = append(
oddsCfg.Callbacks.PeerConnReadExtensionMessage,
countHandler(c, &wg, "odds", 1, testRepliesToOddsExtensionName, testRepliesToEvensExtensionName, 100))
cl1, err := NewClient(oddsCfg)
c.Assert(err, qt.IsNil)
defer cl1.Close()
cl2, err := NewClient(evensCfg)
c.Assert(err, qt.IsNil)
defer cl2.Close()
addOpts := AddTorrentOpts{}
rand.Read(addOpts.InfoHash[:])
t1, _ := cl1.AddTorrentOpt(addOpts)
t2, _ := cl2.AddTorrentOpt(addOpts)
defer testutil.ExportStatusWriter(cl1, "cl1", t)()
defer testutil.ExportStatusWriter(cl2, "cl2", t)()
// Expect one PeerConn to see the value.
wg.Add(1)
added := t1.AddClientPeer(cl2)
// Ensure some addresses for the other client were added.
c.Assert(added, qt.Not(qt.Equals), 0)
wg.Wait()
_ = t2
}