11package smux
22
33import (
4+ "bytes"
45 crand "crypto/rand"
56 "encoding/binary"
67 "fmt"
@@ -16,7 +17,7 @@ import (
1617// setupServer starts new server listening on a random localhost port and
1718// returns address of the server, function to stop the server, new client
1819// connection to this server or an error.
19- func setupServer (tb testing.TB ) (addr string , stopfunc func (), client net.Conn , err error ) {
20+ func setupServer (tb testing.TB , metadata ... byte ) (addr string , stopfunc func (), client net.Conn , err error ) {
2021 ln , err := net .Listen ("tcp" , "localhost:0" )
2122 if err != nil {
2223 return "" , nil , nil , err
@@ -27,7 +28,7 @@ func setupServer(tb testing.TB) (addr string, stopfunc func(), client net.Conn,
2728 tb .Error (err )
2829 return
2930 }
30- go handleConnection (conn )
31+ go handleConnection (tb , conn , metadata ... )
3132 }()
3233 addr = ln .Addr ().String ()
3334 conn , err := net .Dial ("tcp" , addr )
@@ -38,10 +39,13 @@ func setupServer(tb testing.TB) (addr string, stopfunc func(), client net.Conn,
3839 return ln .Addr ().String (), func () { ln .Close () }, conn , nil
3940}
4041
41- func handleConnection (conn net.Conn ) {
42+ func handleConnection (tb testing. TB , conn net.Conn , metadata ... byte ) {
4243 session , _ := Server (conn , nil )
4344 for {
4445 if stream , err := session .AcceptStream (); err == nil {
46+ if ! bytes .Equal (metadata , stream .Metadata ()) {
47+ tb .Fatal ("metadata mimatch" )
48+ }
4549 go func (s io.ReadWriteCloser ) {
4650 buf := make ([]byte , 65536 )
4751 for {
@@ -58,6 +62,18 @@ func handleConnection(conn net.Conn) {
5862 }
5963}
6064
65+ func TestMetadata (t * testing.T ) {
66+ metadata := []byte ("hello, world" )
67+ _ , stop , cli , err := setupServer (t , metadata ... )
68+ if err != nil {
69+ t .Fatal (err )
70+ }
71+ defer stop ()
72+ session , _ := Client (cli , nil )
73+ session .OpenStream (metadata ... )
74+ session .Close ()
75+ }
76+
6177func TestEcho (t * testing.T ) {
6278 _ , stop , cli , err := setupServer (t )
6379 if err != nil {
0 commit comments