Skip to content

Commit

Permalink
chore: refactor test using signaling
Browse files Browse the repository at this point in the history
  • Loading branch information
certaintls committed Jun 21, 2022
1 parent 6edcfff commit 13e0c07
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/core/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/ioutil"
"net"
"testing"
"time"

"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/congestion"
Expand Down Expand Up @@ -49,17 +48,18 @@ const (
func TestE2E(t *testing.T) {
// Server and Client share the same obfuscator
obfuscator := obfs.NewXPlusObfuscator([]byte(obfs_str))
signal := make(chan struct{})

go runServer(obfuscator)
go runServer(obfuscator, signal)

err := runClient(obfuscator)
err := runClient(obfuscator, signal)
if err != nil {
t.Fail()
}
}

// Simulate a server
func runServer(obfuscator *obfs.XPlusObfuscator) error {
func runServer(obfuscator *obfs.XPlusObfuscator, signal chan struct{}) error {
// Load TLS server config
cer, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
Expand Down Expand Up @@ -124,7 +124,9 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
fmt.Println("Failed to initialize server")
}

serverBuffer := make([]byte, len(test_request))
fmt.Println("Server up and running")
signal <- struct{}{}

serverConn, err := l.Accept()
defer serverConn.Close()
Expand All @@ -133,8 +135,6 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
return err
}

time.Sleep(time.Second * 2)
serverBuffer := make([]byte, len(test_request))
fmt.Println("Server starts reading from connection")
_, err = serverConn.Read(serverBuffer)

Expand All @@ -145,6 +145,7 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
s := string(serverBuffer)
if s == test_request {
fmt.Println("Server received the expected data from the client")
signal <- struct{}{}
_, err = serverConn.Write([]byte(test_response))
fmt.Println("Server sent the response to the client")
return err
Expand All @@ -154,7 +155,7 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
}

// Simulate a client
func runClient(obfuscator *obfs.XPlusObfuscator) error {
func runClient(obfuscator *obfs.XPlusObfuscator, signal chan struct{}) error {
// Load TLS client config
var clientTlsConfig = &tls.Config{
InsecureSkipVerify: false,
Expand Down Expand Up @@ -188,6 +189,7 @@ func runClient(obfuscator *obfs.XPlusObfuscator) error {
EnableDatagrams: true,
}

<-signal
client, err := NewClient(server_addr, protocol, []byte(auth_str), clientTlsConfig, quicConfig,
transport.DefaultClientTransport, client_up_mbps, client_down_mbps,
congestionFactory, obfuscator)
Expand All @@ -207,14 +209,13 @@ func runClient(obfuscator *obfs.XPlusObfuscator) error {
}

// write data from clientConn for server to read
time.Sleep(time.Second * 2)
_, err = clientConn.Write([]byte(test_request))
if err != nil {
return err
}
fmt.Println("Client sent the data to the server")

time.Sleep(time.Second * 5)
<-signal
clientBuffer := make([]byte, len(test_response))
fmt.Println("Client starts reading from connection")
_, err = clientConn.Read(clientBuffer)
Expand Down

0 comments on commit 13e0c07

Please sign in to comment.