Skip to content

Commit

Permalink
Add a few tests, to improve coverage
Browse files Browse the repository at this point in the history
specifically, tests for stats.go, certificate.go and sdpsemantics.go
  • Loading branch information
wdouglass authored and Sean-Der committed May 13, 2022
1 parent 0c8ce8e commit d07baa8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
18 changes: 18 additions & 0 deletions certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ func TestGenerateCertificateExpires(t *testing.T) {
assert.Contains(t, x509Cert.statsID, "certificate")
}

func TestBadCertificate(t *testing.T) {
var nokey interface{}
badcert, err := NewCertificate(nokey, x509.Certificate{})
assert.Nil(t, badcert)
assert.Error(t, err)

sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)

badcert, err = NewCertificate(sk, x509.Certificate{})
assert.Nil(t, badcert)
assert.Error(t, err)

c0 := Certificate{}
c1 := Certificate{}
assert.False(t, c0.Equals(c1))
}

func TestPEM(t *testing.T) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
Expand Down
38 changes: 37 additions & 1 deletion sdpsemantics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package webrtc

import (
"encoding/json"
"errors"
"strings"
"testing"
Expand All @@ -19,18 +20,53 @@ func TestSDPSemantics_String(t *testing.T) {
value SDPSemantics
expectedString string
}{
{SDPSemantics(42), unknownStr},
{SDPSemanticsUnifiedPlanWithFallback, "unified-plan-with-fallback"},
{SDPSemanticsPlanB, "plan-b"},
{SDPSemanticsUnifiedPlan, "unified-plan"},
}

assert.Equal(t,
unknownStr,
SDPSemantics(42).String(),
)

for i, testCase := range testCases {
assert.Equal(t,
testCase.expectedString,
testCase.value.String(),
"testCase: %d %v", i, testCase,
)
assert.Equal(t,
testCase.value,
newSDPSemantics(testCase.expectedString),
"testCase: %d %v", i, testCase,
)
}
}

func TestSDPSemantics_JSON(t *testing.T) {
testCases := []struct {
value SDPSemantics
JSON []byte
}{
{SDPSemanticsUnifiedPlanWithFallback, []byte("\"unified-plan-with-fallback\"")},
{SDPSemanticsPlanB, []byte("\"plan-b\"")},
{SDPSemanticsUnifiedPlan, []byte("\"unified-plan\"")},
}

for i, testCase := range testCases {
res, err := json.Marshal(testCase.value)
assert.NoError(t, err)
assert.Equal(t,
testCase.JSON,
res,
"testCase: %d %v", i, testCase,
)

var v SDPSemantics
err = json.Unmarshal(testCase.JSON, &v)
assert.NoError(t, err)
assert.Equal(t, v, testCase.value)
}
}

Expand Down
41 changes: 41 additions & 0 deletions stats_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/pion/ice/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -200,6 +201,46 @@ func signalPairForStats(pcOffer *PeerConnection, pcAnswer *PeerConnection) error
}
}

func TestStatsConvertState(t *testing.T) {
testCases := []struct {
ice ice.CandidatePairState
stats StatsICECandidatePairState
}{
{
ice.CandidatePairStateWaiting,
StatsICECandidatePairStateWaiting,
},
{
ice.CandidatePairStateInProgress,
StatsICECandidatePairStateInProgress,
},
{
ice.CandidatePairStateFailed,
StatsICECandidatePairStateFailed,
},
{
ice.CandidatePairStateSucceeded,
StatsICECandidatePairStateSucceeded,
},
}

s, err := toStatsICECandidatePairState(ice.CandidatePairState(42))

assert.Error(t, err)
assert.Equal(t,
StatsICECandidatePairState("Unknown"),
s)
for i, testCase := range testCases {
s, err := toStatsICECandidatePairState(testCase.ice)
assert.NoError(t, err)
assert.Equal(t,
testCase.stats,
s,
"testCase: %d %v", i, testCase,
)
}
}

func TestPeerConnection_GetStats(t *testing.T) {
offerPC, answerPC, err := newPair()
assert.NoError(t, err)
Expand Down

0 comments on commit d07baa8

Please sign in to comment.