Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ linters-settings:
recommendations:
- errors
forbidigo:
analyze-types: true
forbid:
- ^fmt.Print(f|ln)?$
- ^log.(Panic|Fatal|Print)(f|ln)?$
- ^os.Exit$
- ^panic$
- ^print(ln)?$
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
pkg: ^testing$
msg: "use testify/assert instead"
varnamelen:
max-distance: 12
min-name-length: 2
Expand Down Expand Up @@ -123,13 +127,18 @@ linters:
- wsl # Whitespace Linter - Forces you to use empty lines!

issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-use-default: false
exclude-dirs-use-default: false
exclude-rules:
# Allow complex tests and examples, better to be self contained
- path: (examples|main\.go|_test\.go)
- path: (examples|main\.go)
linters:
- gocognit
- forbidigo
- path: _test\.go
linters:
- gocognit

# Allow forbidden identifiers in CLI commands
Expand Down
61 changes: 21 additions & 40 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pion/logging"
"github.com/pion/transport/v3/dpipe"
"github.com/pion/transport/v3/test"
"github.com/stretchr/testify/assert"
)

func TestSimpleReadWrite(t *testing.T) {
Expand All @@ -25,51 +26,38 @@ func TestSimpleReadWrite(t *testing.T) {

ca, cb := dpipe.Pipe()
certificate, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
gotHello := make(chan struct{})

go func() {
server, sErr := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
LoggerFactory: logging.NewDefaultLoggerFactory(),
}, false)
if sErr != nil {
t.Error(sErr)
assert.NoError(t, sErr)

return
}
buf := make([]byte, 1024)
if _, sErr = server.Read(buf); sErr != nil { //nolint:contextcheck
t.Error(sErr)
}
_, sErr = server.Read(buf) //nolint:contextcheck
assert.NoError(t, sErr)

gotHello <- struct{}{}
if sErr = server.Close(); sErr != nil { //nolint:contextcheck
t.Error(sErr)
}
assert.NoError(t, server.Close()) //nolint:contextcheck
}()

client, err := testClient(ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), &Config{
LoggerFactory: logging.NewDefaultLoggerFactory(),
InsecureSkipVerify: true,
}, false)
if err != nil {
t.Fatal(err)
}
if _, err = client.Write([]byte("hello")); err != nil {
t.Error(err)
}
assert.NoError(t, err)
_, err = client.Write([]byte("hello"))
assert.NoError(t, err)
select {
case <-gotHello:
// OK
case <-time.After(time.Second * 5):
t.Error("timeout")
}

if err = client.Close(); err != nil {
t.Error(err)
assert.Fail(t, "timeout")
}
assert.NoError(t, client.Close())
}

func benchmarkConn(b *testing.B, payloadSize int64) {
Expand All @@ -80,43 +68,36 @@ func benchmarkConn(b *testing.B, payloadSize int64) {

ca, cb := dpipe.Pipe()
certificate, err := selfsign.GenerateSelfSigned()
assert.NoError(b, err)
server := make(chan *Conn)

go func() {
s, sErr := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
}, false)
if err != nil {
b.Error(sErr)
assert.NoError(b, sErr)

return
}
server <- s
}()
if err != nil {
b.Fatal(err)
}

hw := make([]byte, payloadSize)
b.ReportAllocs()
b.SetBytes(int64(len(hw)))
go func() {
client, cErr := testClient(
ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), &Config{InsecureSkipVerify: true}, false,
)
if cErr != nil {
b.Error(err)
}
assert.NoError(b, cErr)
for {
if _, cErr = client.Write(hw); cErr != nil { //nolint:contextcheck
b.Error(err)
}
_, cErr = client.Write(hw) //nolint:contextcheck
assert.NoError(b, cErr)
}
}()
s := <-server
buf := make([]byte, 2048)
for i := 0; i < b.N; i++ {
if _, err = s.Read(buf); err != nil {
b.Error(err)
}
_, err = s.Read(buf)
assert.NoError(b, err)
}
})
}
Expand Down
23 changes: 6 additions & 17 deletions certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@ package dtls

import (
"crypto/tls"
"reflect"
"testing"

"github.com/pion/dtls/v3/pkg/crypto/selfsign"
"github.com/stretchr/testify/assert"
)

func TestGetCertificate(t *testing.T) {
certificateWildcard, err := selfsign.GenerateSelfSignedWithDNS("*.test.test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

certificateTest, err := selfsign.GenerateSelfSignedWithDNS("test.test", "www.test.test", "pop.test.test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

certificateRandom, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

testCases := []struct {
localCertificates []tls.Certificate
Expand Down Expand Up @@ -92,13 +86,8 @@ func TestGetCertificate(t *testing.T) {
localGetCertificate: test.getCertificate,
}
cert, err := cfg.getCertificate(&ClientHelloInfo{ServerName: test.serverName})
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(cert.Leaf, test.expectedCertificate.Leaf) {
t.Fatalf("Certificate does not match: expected(%v) actual(%v)", test.expectedCertificate.Leaf, cert.Leaf)
}
assert.NoError(t, err)
assert.Equal(t, test.expectedCertificate.Leaf, cert.Leaf, "Certificate Leaf should match expected")
})
}
}
37 changes: 9 additions & 28 deletions cipher_suite_go114_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,29 @@ package dtls

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInsecureCipherSuites(t *testing.T) {
r := InsecureCipherSuites()

if len(r) != 0 {
t.Fatalf("Expected no insecure ciphersuites, got %d", len(r))
}
assert.Empty(t, InsecureCipherSuites(), "Expected no insecure ciphersuites")
}

func TestCipherSuites(t *testing.T) {
ours := allCipherSuites()
theirs := CipherSuites()

if len(ours) != len(theirs) {
t.Fatalf("Expected %d CipherSuites, got %d", len(ours), len(theirs))
}
assert.Equal(t, len(ours), len(theirs))

for i, s := range ours {
i := i
s := s
t.Run(s.String(), func(t *testing.T) {
cipher := theirs[i]
if cipher.ID != uint16(s.ID()) {
t.Fatalf("Expected ID: 0x%04X, got 0x%04X", s.ID(), cipher.ID)
}

if cipher.Name != s.String() {
t.Fatalf("Expected Name: %s, got %s", s.String(), cipher.Name)
}

if len(cipher.SupportedVersions) != 1 {
t.Fatalf("Expected %d SupportedVersion, got %d", 1, len(cipher.SupportedVersions))
}

if cipher.SupportedVersions[0] != VersionDTLS12 {
t.Fatalf("Expected SupportedVersions 0x%04X, got 0x%04X", VersionDTLS12, cipher.SupportedVersions[0])
}

if cipher.Insecure {
t.Fatalf("Expected Insecure %t, got %t", false, cipher.Insecure)
}
assert.Equal(t, cipher.ID, uint16(s.ID()))
assert.Equal(t, cipher.Name, s.String())
assert.Equal(t, 1, len(cipher.SupportedVersions), "Expected SupportedVersion to be 1")
assert.Equal(t, uint16(VersionDTLS12), cipher.SupportedVersions[0], "Expected SupportedVersion to match")
assert.False(t, cipher.Insecure, "Expected Insecure")
})
}
}
27 changes: 7 additions & 20 deletions cipher_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/transport/v3/dpipe"
"github.com/pion/transport/v3/test"
"github.com/stretchr/testify/assert"
)

func TestCipherSuiteName(t *testing.T) {
Expand All @@ -24,18 +25,12 @@ func TestCipherSuiteName(t *testing.T) {
}

for _, testCase := range testCases {
res := CipherSuiteName(testCase.suite)
if res != testCase.expected {
t.Fatalf("Expected: %s, got %s", testCase.expected, res)
}
assert.Equal(t, testCase.expected, CipherSuiteName(testCase.suite))
}
}

func TestAllCipherSuites(t *testing.T) {
actual := len(allCipherSuites())
if actual == 0 {
t.Fatal()
}
assert.NotEmpty(t, allCipherSuites())
}

// CustomCipher that is just used to assert Custom IDs work.
Expand Down Expand Up @@ -84,18 +79,10 @@ func TestCustomCipherSuite(t *testing.T) {
}, true)

clientResult := <-resultCh

if err != nil {
t.Error(err)
} else {
_ = server.Close()
}

if clientResult.err != nil {
t.Error(clientResult.err)
} else {
_ = clientResult.c.Close()
}
assert.NoError(t, err)
assert.NoError(t, server.Close())
assert.Nil(t, clientResult.err)
assert.NoError(t, clientResult.c.Close())
}

t.Run("Custom ID", func(*testing.T) {
Expand Down
17 changes: 8 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ import (
"testing"

"github.com/pion/dtls/v3/pkg/crypto/selfsign"
"github.com/stretchr/testify/assert"
)

func TestValidateConfig(t *testing.T) { //nolint:cyclop
func TestValidateConfig(t *testing.T) {
cert, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), self signed certificate not generated", err)
assert.NoError(t, err, "TestValidateConfig: Config validation error, self signed certificate not generated")

return
}
dsaPrivateKey := &dsa.PrivateKey{}
err = dsa.GenerateParameters(&dsaPrivateKey.Parameters, rand.Reader, dsa.L1024N160)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), DSA parameters not generated", err)
assert.NoError(t, err, "TestValidateConfig: Config validation error, DSA parameters not generated")

return
}
err = dsa.GenerateKey(dsaPrivateKey, rand.Reader)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), DSA private key not generated", err)
assert.NoError(t, err, "TestValidateConfig: Config validation error, DSA private key not generated")

return
}
rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), RSA private key not generated", err)
assert.NoError(t, err, "TestValidateConfig: Config validation error, RSA private key not generated")

return
}
Expand Down Expand Up @@ -133,11 +134,9 @@ func TestValidateConfig(t *testing.T) { //nolint:cyclop
err := validateConfig(testCase.config)
if testCase.expErr != nil || testCase.wantAnyErr {
if testCase.expErr != nil && !errors.Is(err, testCase.expErr) {
t.Fatalf("TestValidateConfig: Config validation error exp(%v) failed(%v)", testCase.expErr, err)
}
if err == nil {
t.Fatalf("TestValidateConfig: Config validation expected an error")
assert.ErrorIs(t, err, testCase.expErr, "TestValidateConfig")
}
assert.Error(t, err, "TestValidateConfig: Config validation expected an error")
}
})
}
Expand Down
Loading
Loading