1
1
package smux
2
2
3
3
import (
4
+ "bytes"
4
5
crand "crypto/rand"
5
6
"encoding/binary"
6
7
"fmt"
@@ -25,7 +26,7 @@ func init() {
25
26
// setupServer starts new server listening on a random localhost port and
26
27
// returns address of the server, function to stop the server, new client
27
28
// connection to this server or an error.
28
- func setupServer (tb testing.TB ) (addr string , stopfunc func (), client net.Conn , err error ) {
29
+ func setupServer (tb testing.TB , metadata ... byte ) (addr string , stopfunc func (), client net.Conn , err error ) {
29
30
ln , err := net .Listen ("tcp" , "localhost:0" )
30
31
if err != nil {
31
32
return "" , nil , nil , err
@@ -35,7 +36,7 @@ func setupServer(tb testing.TB) (addr string, stopfunc func(), client net.Conn,
35
36
if err != nil {
36
37
return
37
38
}
38
- go handleConnection (conn )
39
+ go handleConnection (tb , conn , metadata ... )
39
40
}()
40
41
addr = ln .Addr ().String ()
41
42
conn , err := net .Dial ("tcp" , addr )
@@ -46,10 +47,13 @@ func setupServer(tb testing.TB) (addr string, stopfunc func(), client net.Conn,
46
47
return ln .Addr ().String (), func () { ln .Close () }, conn , nil
47
48
}
48
49
49
- func handleConnection (conn net.Conn ) {
50
+ func handleConnection (tb testing. TB , conn net.Conn , metadata ... byte ) {
50
51
session , _ := Server (conn , nil )
51
52
for {
52
53
if stream , err := session .AcceptStream (); err == nil {
54
+ if ! bytes .Equal (metadata , stream .Metadata ()) {
55
+ tb .Fatal ("metadata mismatch" )
56
+ }
53
57
go func (s io.ReadWriteCloser ) {
54
58
buf := make ([]byte , 65536 )
55
59
for {
@@ -66,6 +70,18 @@ func handleConnection(conn net.Conn) {
66
70
}
67
71
}
68
72
73
+ func TestMetadata (t * testing.T ) {
74
+ metadata := []byte ("hello, world" )
75
+ _ , stop , cli , err := setupServer (t , metadata ... )
76
+ if err != nil {
77
+ t .Fatal (err )
78
+ }
79
+ defer stop ()
80
+ session , _ := Client (cli , nil )
81
+ session .OpenStream (metadata ... )
82
+ session .Close ()
83
+ }
84
+
69
85
func TestEcho (t * testing.T ) {
70
86
_ , stop , cli , err := setupServer (t )
71
87
if err != nil {
0 commit comments