Skip to content

Commit

Permalink
Update tests to wrap net.Conn
Browse files Browse the repository at this point in the history
Updates tests that use net.Conn to wrap in net.PacketConn so that new
Client / Server constructors may be used.

Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
  • Loading branch information
hasheddan committed Aug 9, 2023
1 parent f960a37 commit ee04141
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/pion/dtls/v2/internal/util"
"github.com/pion/dtls/v2/pkg/crypto/selfsign"
"github.com/pion/logging"
"github.com/pion/transport/v2/dpipe"
Expand All @@ -30,7 +31,7 @@ func TestSimpleReadWrite(t *testing.T) {
gotHello := make(chan struct{})

go func() {
server, sErr := testServer(ctx, cb, &Config{
server, sErr := testServer(ctx, util.FromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
LoggerFactory: logging.NewDefaultLoggerFactory(),
}, false)
Expand All @@ -48,7 +49,7 @@ func TestSimpleReadWrite(t *testing.T) {
}
}()

client, err := testClient(ctx, ca, &Config{
client, err := testClient(ctx, util.FromConn(ca), ca.RemoteAddr(), &Config{
LoggerFactory: logging.NewDefaultLoggerFactory(),
InsecureSkipVerify: true,
}, false)
Expand Down Expand Up @@ -78,7 +79,7 @@ func benchmarkConn(b *testing.B, n int64) {
certificate, err := selfsign.GenerateSelfSigned()
server := make(chan *Conn)
go func() {
s, sErr := testServer(ctx, cb, &Config{
s, sErr := testServer(ctx, util.FromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
}, false)
if err != nil {
Expand All @@ -94,7 +95,7 @@ func benchmarkConn(b *testing.B, n int64) {
b.ReportAllocs()
b.SetBytes(int64(len(hw)))
go func() {
client, cErr := testClient(ctx, ca, &Config{InsecureSkipVerify: true}, false)
client, cErr := testClient(ctx, util.FromConn(ca), ca.RemoteAddr(), &Config{InsecureSkipVerify: true}, false)
if cErr != nil {
b.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cipher_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type CipherSuite interface {
Init(masterSecret, clientRandom, serverRandom []byte, isClient bool) error
IsInitialized() bool
Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error)
Decrypt(in []byte) ([]byte, error)
Decrypt(h recordlayer.Header, in []byte) ([]byte, error)
}

// CipherSuiteName provides the same functionality as tls.CipherSuiteName
Expand Down
5 changes: 3 additions & 2 deletions cipher_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/pion/dtls/v2/internal/ciphersuite"
"github.com/pion/dtls/v2/internal/util"
"github.com/pion/transport/v2/dpipe"
"github.com/pion/transport/v2/test"
)
Expand Down Expand Up @@ -70,14 +71,14 @@ func TestCustomCipherSuite(t *testing.T) {
c := make(chan result)

go func() {
client, err := testClient(ctx, ca, &Config{
client, err := testClient(ctx, util.FromConn(ca), ca.RemoteAddr(), &Config{
CipherSuites: []CipherSuiteID{},
CustomCipherSuites: cipherFactory,
}, true)
c <- result{client, err}
}()

server, err := testServer(ctx, cb, &Config{
server, err := testServer(ctx, util.FromConn(cb), cb.RemoteAddr(), &Config{
CipherSuites: []CipherSuiteID{},
CustomCipherSuites: cipherFactory,
}, true)
Expand Down
4 changes: 2 additions & 2 deletions resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

// Resume imports an already established dtls connection using a specific dtls state
func Resume(state *State, conn net.Conn, config *Config) (*Conn, error) {
func Resume(state *State, conn net.PacketConn, rAddr net.Addr, config *Config) (*Conn, error) {
if err := state.initCipherSuite(); err != nil {
return nil, err
}
c, err := createConn(context.Background(), conn, config, state.isClient, state)
c, err := createConn(context.Background(), conn, rAddr, config, state.isClient, state)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions resume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/pion/dtls/v2/internal/util"
"github.com/pion/dtls/v2/pkg/crypto/selfsign"
"github.com/pion/transport/v2/test"
)
Expand All @@ -32,7 +33,7 @@ func fatal(t *testing.T, errChan chan error, err error) {
t.Fatal(err)
}

func DoTestResume(t *testing.T, newLocal, newRemote func(net.Conn, *Config) (*Conn, error)) {
func DoTestResume(t *testing.T, newLocal, newRemote func(net.PacketConn, net.Addr, *Config) (*Conn, error)) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
Expand Down Expand Up @@ -67,7 +68,7 @@ func DoTestResume(t *testing.T, newLocal, newRemote func(net.Conn, *Config) (*Co
go func() {
var remote *Conn
var errR error
remote, errR = newRemote(remoteConn, config)
remote, errR = newRemote(util.FromConn(remoteConn), remoteConn.RemoteAddr(), config)
if errR != nil {
errChan <- errR
}
Expand All @@ -89,7 +90,7 @@ func DoTestResume(t *testing.T, newLocal, newRemote func(net.Conn, *Config) (*Co
}()

var local *Conn
local, err = newLocal(localConn1, config)
local, err = newLocal(util.FromConn(localConn1), localConn1.RemoteAddr(), config)
if err != nil {
fatal(t, errChan, err)
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func DoTestResume(t *testing.T, newLocal, newRemote func(net.Conn, *Config) (*Co

// Resume dtls connection
var resumed net.Conn
resumed, err = Resume(deserialized, localConn2, config)
resumed, err = Resume(deserialized, util.FromConn(localConn2), localConn2.RemoteAddr(), config)
if err != nil {
fatal(t, errChan, err)
}
Expand Down

0 comments on commit ee04141

Please sign in to comment.