-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement E2E test for Client/Server
Relates to #11
- Loading branch information
Showing
4 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package pion | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"runtime" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pions/dtls/pkg/dtls" | ||
) | ||
|
||
const testMessage = "Hello World" | ||
const serverPort = 5555 | ||
const testTimeLimit = 5 * time.Second | ||
const messageRetry = 200 * time.Millisecond | ||
const expectedGoRoutineCount = 3 | ||
|
||
// Counter to make sure both sides got a message | ||
var messageRecvCount uint64 | ||
|
||
func simpleReadWrite(errChan chan error, outChan chan string, conn io.ReadWriteCloser) { | ||
go func() { | ||
buffer := make([]byte, 8192) | ||
n, err := conn.Read(buffer) | ||
if err != nil { | ||
errChan <- err | ||
return | ||
} | ||
|
||
outChan <- string(buffer[:n]) | ||
atomic.AddUint64(&messageRecvCount, 1) | ||
}() | ||
|
||
for { | ||
if atomic.LoadUint64(&messageRecvCount) == 2 { | ||
break | ||
} else if _, err := conn.Write([]byte(testMessage)); err != nil { | ||
errChan <- err | ||
break | ||
} | ||
|
||
time.Sleep(messageRetry) | ||
} | ||
|
||
if err := conn.Close(); err != nil { | ||
errChan <- err | ||
} | ||
} | ||
|
||
/* | ||
Simple DTLS Client/Server can communicate | ||
- Assert that you can send messages both ways | ||
- Assert that Close() on both ends work | ||
- Assert that no Goroutines are leaked | ||
*/ | ||
func TestPionE2ESimple(t *testing.T) { | ||
errChan := make(chan error) | ||
clientChan := make(chan string) | ||
serverChan := make(chan string) | ||
|
||
cert, key, err := createSelfSigned() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// DTLS Client | ||
go func() { | ||
conn, err := dtls.Dial("udp", | ||
&net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: serverPort}, | ||
&dtls.Config{Certificate: cert, PrivateKey: key}, | ||
) | ||
if err != nil { | ||
errChan <- err | ||
return | ||
} | ||
simpleReadWrite(errChan, clientChan, conn) | ||
}() | ||
|
||
// DTLS Server | ||
go func() { | ||
listener, err := dtls.Listen("udp", | ||
&net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: serverPort}, | ||
&dtls.Config{Certificate: cert, PrivateKey: key}, | ||
) | ||
if err != nil { | ||
errChan <- err | ||
return | ||
} | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
errChan <- err | ||
return | ||
} | ||
|
||
simpleReadWrite(errChan, serverChan, conn) | ||
}() | ||
|
||
func() { | ||
seenClient, seenServer := false, false | ||
for { | ||
select { | ||
case err := <-errChan: | ||
fmt.Println(err) | ||
case <-time.After(testTimeLimit): | ||
t.Fatalf("Test timeout, seenClient %t seenServer %t", seenClient, seenServer) | ||
case clientMsg := <-clientChan: | ||
if clientMsg != testMessage { | ||
t.Fatalf("clientMsg does not equal test message: %s %s", clientMsg, testMessage) | ||
} | ||
|
||
seenClient = true | ||
if seenClient && seenServer { | ||
return | ||
} | ||
case serverMsg := <-serverChan: | ||
if serverMsg != testMessage { | ||
t.Fatalf("serverMsg does not equal test message: %s %s", serverMsg, testMessage) | ||
} | ||
|
||
seenServer = true | ||
if seenClient && seenServer { | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
|
||
// We have seen Client+Server communication! Now count goroutines to make sure we haven't leaked | ||
time.Sleep(time.Second * 3) // TODO this could be racey | ||
goRoutineCount := runtime.NumGoroutine() | ||
if goRoutineCount != expectedGoRoutineCount { | ||
t.Fatalf("goRoutineCount != expectedGoRoutineCount, possible leak: %d %d", goRoutineCount, expectedGoRoutineCount) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package pion | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/hex" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
func createSelfSigned() (*x509.Certificate, crypto.PrivateKey, error) { | ||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
origin := make([]byte, 16) | ||
|
||
// Max random value, a 130-bits integer, i.e 2^130 - 1 | ||
maxBigInt := new(big.Int) | ||
/* #nosec */ | ||
maxBigInt.Exp(big.NewInt(2), big.NewInt(130), nil).Sub(maxBigInt, big.NewInt(1)) | ||
serialNumber, err := rand.Int(rand.Reader, maxBigInt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
template := x509.Certificate{ | ||
ExtKeyUsage: []x509.ExtKeyUsage{ | ||
x509.ExtKeyUsageClientAuth, | ||
x509.ExtKeyUsageServerAuth, | ||
}, | ||
BasicConstraintsValid: true, | ||
NotBefore: time.Now(), | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
NotAfter: time.Now().AddDate(0, 1, 0), | ||
SerialNumber: serialNumber, | ||
Version: 2, | ||
Subject: pkix.Name{CommonName: hex.EncodeToString(origin)}, | ||
IsCA: true, | ||
} | ||
|
||
raw, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
cert, err := x509.ParseCertificate(raw) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return cert, priv, nil | ||
} |