2019-08-07 20:44:33 +03:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-08-07 20:56:21 +03:00
|
|
|
"sync"
|
2019-08-07 20:44:33 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testAccept will accept connections on the transport, create a new link and tunnel on top
|
2019-08-07 20:56:21 +03:00
|
|
|
func testAccept(t *testing.T, tun Tunnel, wg *sync.WaitGroup) {
|
2019-08-07 20:44:33 +03:00
|
|
|
// listen on some virtual address
|
|
|
|
tl, err := tun.Listen("test-tunnel")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept a connection
|
|
|
|
c, err := tl.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a message
|
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
|
|
|
if err := c.Recv(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-07 20:56:21 +03:00
|
|
|
wg.Done()
|
2019-08-07 20:44:33 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSend will create a new link to an address and then a tunnel on top
|
|
|
|
func testSend(t *testing.T, tun Tunnel) {
|
|
|
|
// dial a new session
|
|
|
|
c, err := tun.Dial("test-tunnel")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-07 20:56:21 +03:00
|
|
|
defer c.Close()
|
2019-08-07 20:44:33 +03:00
|
|
|
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"test": "header",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTunnel(t *testing.T) {
|
|
|
|
// create a new listener
|
|
|
|
tun := NewTunnel(Nodes(":9096"))
|
|
|
|
err := tun.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-07 20:56:21 +03:00
|
|
|
defer tun.Close()
|
2019-08-07 20:44:33 +03:00
|
|
|
|
2019-08-07 20:56:21 +03:00
|
|
|
var wg sync.WaitGroup
|
2019-08-07 20:44:33 +03:00
|
|
|
|
|
|
|
// start accepting connections
|
2019-08-07 20:56:21 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go testAccept(t, tun, &wg)
|
2019-08-07 20:44:33 +03:00
|
|
|
|
|
|
|
// send a message
|
|
|
|
testSend(t, tun)
|
|
|
|
|
|
|
|
// wait until message is received
|
2019-08-07 20:56:21 +03:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwoTunnel(t *testing.T) {
|
|
|
|
// create a new tunnel client
|
|
|
|
tunA := NewTunnel(
|
|
|
|
Address(":9096"),
|
|
|
|
Nodes(":9097"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create a new tunnel server
|
|
|
|
tunB := NewTunnel(
|
|
|
|
Address(":9097"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// start tunB
|
|
|
|
err := tunB.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunB.Close()
|
|
|
|
|
|
|
|
// start tunA
|
|
|
|
err = tunA.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunA.Close()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// start accepting connections
|
|
|
|
wg.Add(1)
|
|
|
|
go testAccept(t, tunA, &wg)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go testAccept(t, tunB, &wg)
|
|
|
|
|
|
|
|
// send a message
|
|
|
|
testSend(t, tunA)
|
|
|
|
|
|
|
|
// send a message
|
|
|
|
testSend(t, tunB)
|
|
|
|
|
|
|
|
// wait until done
|
|
|
|
wg.Wait()
|
2019-08-07 20:44:33 +03:00
|
|
|
}
|