2019-08-07 19:44:33 +02:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-08-07 19:56:21 +02:00
|
|
|
"sync"
|
2019-08-07 19:44:33 +02:00
|
|
|
"testing"
|
2019-08-08 01:19:16 +02:00
|
|
|
"time"
|
2019-08-07 19:44:33 +02:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testAccept will accept connections on the transport, create a new link and tunnel on top
|
2019-08-07 19:56:21 +02:00
|
|
|
func testAccept(t *testing.T, tun Tunnel, wg *sync.WaitGroup) {
|
2019-08-07 19:44:33 +02: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 {
|
2019-08-14 15:38:17 +02:00
|
|
|
// accept the message
|
2019-08-07 19:44:33 +02:00
|
|
|
m := new(transport.Message)
|
|
|
|
if err := c.Recv(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-14 15:38:17 +02:00
|
|
|
|
|
|
|
if v := m.Header["test"]; v != "send" {
|
|
|
|
t.Fatalf("Accept side expected test:send header. Received: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now respond
|
|
|
|
m.Header["test"] = "accept"
|
|
|
|
if err := c.Send(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:56:21 +02:00
|
|
|
wg.Done()
|
2019-08-07 19:44:33 +02: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 19:56:21 +02:00
|
|
|
defer c.Close()
|
2019-08-07 19:44:33 +02:00
|
|
|
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
2019-08-14 15:38:17 +02:00
|
|
|
"test": "send",
|
2019-08-07 19:44:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-14 15:38:17 +02:00
|
|
|
// send the message
|
2019-08-07 19:44:33 +02:00
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-14 15:38:17 +02:00
|
|
|
|
|
|
|
// now wait for the response
|
|
|
|
mr := new(transport.Message)
|
|
|
|
if err := c.Recv(mr); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := mr.Header["test"]; v != "accept" {
|
|
|
|
t.Fatalf("Message not received from accepted side. Received: %s", v)
|
|
|
|
}
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTunnel(t *testing.T) {
|
2019-08-07 19:56:21 +02:00
|
|
|
// create a new tunnel client
|
|
|
|
tunA := NewTunnel(
|
2019-08-07 23:02:58 +02:00
|
|
|
Address("127.0.0.1:9096"),
|
|
|
|
Nodes("127.0.0.1:9097"),
|
2019-08-07 19:56:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// create a new tunnel server
|
|
|
|
tunB := NewTunnel(
|
2019-08-07 23:02:58 +02:00
|
|
|
Address("127.0.0.1:9097"),
|
2019-08-07 19:56:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// start tunB
|
|
|
|
err := tunB.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunB.Close()
|
|
|
|
|
2019-08-08 01:19:16 +02:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2019-08-07 19:56:21 +02:00
|
|
|
// start tunA
|
|
|
|
err = tunA.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunA.Close()
|
|
|
|
|
2019-08-08 01:19:16 +02:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2019-08-07 19:56:21 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// start accepting connections
|
2019-08-08 01:19:16 +02:00
|
|
|
// on tunnel A
|
2019-08-07 19:56:21 +02:00
|
|
|
wg.Add(1)
|
2019-08-08 01:19:16 +02:00
|
|
|
go testAccept(t, tunA, &wg)
|
2019-08-07 19:56:21 +02:00
|
|
|
|
2019-08-08 01:19:16 +02:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// dial and send via B
|
|
|
|
testSend(t, tunB)
|
2019-08-07 19:56:21 +02:00
|
|
|
|
|
|
|
// wait until done
|
|
|
|
wg.Wait()
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
2019-08-14 02:23:03 +02:00
|
|
|
|
2019-08-14 14:00:10 +02:00
|
|
|
func TestLoopbackTunnel(t *testing.T) {
|
|
|
|
// create a new tunnel client
|
|
|
|
tun := NewTunnel(
|
|
|
|
Address("127.0.0.1:9096"),
|
|
|
|
Nodes("127.0.0.1:9096"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// start tunB
|
|
|
|
err := tun.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tun.Close()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// start accepting connections
|
|
|
|
// on tunnel A
|
|
|
|
wg.Add(1)
|
|
|
|
go testAccept(t, tun, &wg)
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
|
|
|
// dial and send via B
|
|
|
|
testSend(t, tun)
|
|
|
|
|
|
|
|
// wait until done
|
|
|
|
wg.Wait()
|
|
|
|
}
|