@@ -21,34 +21,44 @@ import (
21
21
// these are mock service implementations for testing
22
22
func makeAutoNATServicePrivate (ctx context.Context , t * testing.T ) host.Host {
23
23
h := bhost .NewBlankHost (swarmt .GenSwarm (t ))
24
- h .SetStreamHandler (AutoNATProto , sayAutoNATPrivate )
24
+ h .SetStreamHandler (AutoNATProto , sayPrivateStreamHandler ( t ) )
25
25
return h
26
26
}
27
27
28
- func makeAutoNATServicePublic (ctx context.Context , t * testing.T ) host.Host {
29
- h := bhost .NewBlankHost (swarmt .GenSwarm (t ))
30
- h .SetStreamHandler (AutoNATProto , sayAutoNATPublic )
31
- return h
32
- }
33
-
34
- func sayAutoNATPrivate (s network.Stream ) {
35
- defer s .Close ()
36
- w := protoio .NewDelimitedWriter (s )
37
- res := pb.Message {
38
- Type : pb .Message_DIAL_RESPONSE .Enum (),
39
- DialResponse : newDialResponseError (pb .Message_E_DIAL_ERROR , "no dialable addresses" ),
28
+ func sayPrivateStreamHandler (t * testing.T ) network.StreamHandler {
29
+ return func (s network.Stream ) {
30
+ defer s .Close ()
31
+ r := protoio .NewDelimitedReader (s , network .MessageSizeMax )
32
+ if err := r .ReadMsg (& pb.Message {}); err != nil {
33
+ t .Error (err )
34
+ return
35
+ }
36
+ w := protoio .NewDelimitedWriter (s )
37
+ res := pb.Message {
38
+ Type : pb .Message_DIAL_RESPONSE .Enum (),
39
+ DialResponse : newDialResponseError (pb .Message_E_DIAL_ERROR , "no dialable addresses" ),
40
+ }
41
+ w .WriteMsg (& res )
40
42
}
41
- w .WriteMsg (& res )
42
43
}
43
44
44
- func sayAutoNATPublic (s network.Stream ) {
45
- defer s .Close ()
46
- w := protoio .NewDelimitedWriter (s )
47
- res := pb.Message {
48
- Type : pb .Message_DIAL_RESPONSE .Enum (),
49
- DialResponse : newDialResponseOK (s .Conn ().RemoteMultiaddr ()),
50
- }
51
- w .WriteMsg (& res )
45
+ func makeAutoNATServicePublic (ctx context.Context , t * testing.T ) host.Host {
46
+ h := bhost .NewBlankHost (swarmt .GenSwarm (t ))
47
+ h .SetStreamHandler (AutoNATProto , func (s network.Stream ) {
48
+ defer s .Close ()
49
+ r := protoio .NewDelimitedReader (s , network .MessageSizeMax )
50
+ if err := r .ReadMsg (& pb.Message {}); err != nil {
51
+ t .Error (err )
52
+ return
53
+ }
54
+ w := protoio .NewDelimitedWriter (s )
55
+ res := pb.Message {
56
+ Type : pb .Message_DIAL_RESPONSE .Enum (),
57
+ DialResponse : newDialResponseOK (s .Conn ().RemoteMultiaddr ()),
58
+ }
59
+ w .WriteMsg (& res )
60
+ })
61
+ return h
52
62
}
53
63
54
64
func makeAutoNAT (ctx context.Context , t * testing.T , ash host.Host ) (host.Host , AutoNAT ) {
@@ -184,7 +194,7 @@ func TestAutoNATPublictoPrivate(t *testing.T) {
184
194
185
195
expectEvent (t , s , network .ReachabilityPublic )
186
196
187
- hs .SetStreamHandler (AutoNATProto , sayAutoNATPrivate )
197
+ hs .SetStreamHandler (AutoNATProto , sayPrivateStreamHandler ( t ) )
188
198
hps := makeAutoNATServicePrivate (ctx , t )
189
199
connect (t , hps , hc )
190
200
identifyAsServer (hps , hc )
@@ -252,7 +262,7 @@ func TestAutoNATObservationRecording(t *testing.T) {
252
262
case <- s .Out ():
253
263
t .Fatal ("not expecting a public reachability event" )
254
264
default :
255
- //expected
265
+ // expected
256
266
}
257
267
258
268
addr , _ := ma .NewMultiaddr ("/ip4/127.0.0.1/udp/1234" )
0 commit comments