@ -14,67 +14,71 @@ import (
"tailscale.com/types/key"
"tailscale.com/types/key"
)
)
func TestSendRecv ( t * testing . T ) {
func newPrivateKey ( t * testing . T ) ( k key . Private ) {
const numClients = 3
if _ , err := crand . Read ( k [ : ] ) ; err != nil {
var serverPrivateKey key . Private
if _ , err := crand . Read ( serverPrivateKey [ : ] ) ; err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
return
}
func TestSendRecv ( t * testing . T ) {
serverPrivateKey := newPrivateKey ( t )
s := NewServer ( serverPrivateKey , t . Logf )
defer s . Close ( )
const numClients = 3
var clientPrivateKeys [ ] key . Private
var clientPrivateKeys [ ] key . Private
for i := 0 ; i < numClients ; i ++ {
var key key . Private
if _ , err := crand . Read ( key [ : ] ) ; err != nil {
t . Fatal ( err )
}
clientPrivateKeys = append ( clientPrivateKeys , key )
}
var clientKeys [ ] key . Public
var clientKeys [ ] key . Public
for _ , privKey := range clientPrivateKeys {
for i := 0 ; i < numClients ; i ++ {
clientKeys = append ( clientKeys , privKey . Public ( ) )
priv := newPrivateKey ( t )
clientPrivateKeys = append ( clientPrivateKeys , priv )
clientKeys = append ( clientKeys , priv . Public ( ) )
}
}
ln , err := net . Listen ( "tcp" , ":0" )
ln , err := net . Listen ( "tcp" , ":0" )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
defer ln . Close ( )
var clientConns [ ] net . Conn
var clientConns [ ] net . Conn
var clients [ ] * Client
var recvChs [ ] chan [ ] byte
errCh := make ( chan error , 3 )
for i := 0 ; i < numClients ; i ++ {
for i := 0 ; i < numClients ; i ++ {
conn , err := net . Dial ( "tcp" , ln . Addr ( ) . String ( ) )
t . Logf ( "Connecting client %d ..." , i )
cout , err := net . Dial ( "tcp" , ln . Addr ( ) . String ( ) )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
clientConns = append ( clientConns , conn )
defer cout . Close ( )
}
clientConns = append ( clientConns , cout )
s := NewServer ( serverPrivateKey , t . Logf )
defer s . Close ( )
cin , err := ln . Accept ( )
for i := 0 ; i < numClients ; i ++ {
netConn , err := ln . Accept ( )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
conn := bufio . NewReadWriter ( bufio . NewReader ( netConn ) , bufio . NewWriter ( netConn ) )
defer cin . Close ( )
go s . Accept ( netConn , conn )
go s . Accept ( cin , bufio . NewReadWriter ( bufio . NewReader ( cin ) , bufio . NewWriter ( cin ) ) )
}
var clients [ ] * Client
var recvChs [ ] chan [ ] byte
errCh := make ( chan error , 3 )
for i := 0 ; i < numClients ; i ++ {
key := clientPrivateKeys [ i ]
key := clientPrivateKeys [ i ]
netConn := clientConns [ i ]
brw := bufio . NewReadWriter ( bufio . NewReader ( cout ) , bufio . NewWriter ( cout ) )
conn := bufio . NewReadWriter ( bufio . NewReader ( netConn ) , bufio . NewWriter ( netConn ) )
c , err := NewClient ( key , cout , brw , t . Logf )
c , err := NewClient ( key , netConn , conn , t . Logf )
if err != nil {
if err != nil {
t . Fatalf ( "client %d: %v" , i , err )
t . Fatalf ( "client %d: %v" , i , err )
}
}
clients = append ( clients , c )
clients = append ( clients , c )
recvChs = append ( recvChs , make ( chan [ ] byte ) )
recvChs = append ( recvChs , make ( chan [ ] byte ) )
t . Logf ( "Connected client %d." , i )
}
t . Logf ( "Starting read loops" )
for i := 0 ; i < numClients ; i ++ {
go func ( i int ) {
go func ( i int ) {
for {
for {
b := make ( [ ] byte , 1 << 16 )
b := make ( [ ] byte , 1 << 16 )
n , err := c . Recv ( b )
n , err := c lients[ i ] . Recv ( b )
if err != nil {
if err != nil {
errCh <- err
errCh <- err
return
return
@ -120,4 +124,7 @@ func TestSendRecv(t *testing.T) {
recv ( 2 , string ( msg2 ) )
recv ( 2 , string ( msg2 ) )
recvNothing ( 0 )
recvNothing ( 0 )
recvNothing ( 1 )
recvNothing ( 1 )
t . Logf ( "passed" )
s . Close ( )
}
}