Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
* ntp timestamp conversion
* RTP Extension validation
* H264 payloader

Relates to #15
  • Loading branch information
wdouglass committed Apr 2, 2019
1 parent a8ed4cb commit a19ba75
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
25 changes: 19 additions & 6 deletions codecs/h264_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

func TestH264Payloader_Payload(t *testing.T) {
pck := H264Payloader{}
payload := []byte{0x90, 0x90, 0x90}
smallpayload := []byte{0x90, 0x90, 0x90}
multiplepayload := []byte{0x00, 0x00, 0x01, 0x90,
0x00, 0x00, 0x01, 0x90}

// Positive MTU, nil payload
res := pck.Payload(1, nil)
Expand All @@ -15,32 +17,43 @@ func TestH264Payloader_Payload(t *testing.T) {
}

// Negative MTU, small payload
res = pck.Payload(0, payload)
res = pck.Payload(0, smallpayload)
if len(res) != 0 {
t.Fatal("Generated payload should be empty")
}

// 0 MTU, small payload
res = pck.Payload(0, payload)
res = pck.Payload(0, smallpayload)
if len(res) != 0 {
t.Fatal("Generated payload should be empty")
}

// Positive MTU, small payload
res = pck.Payload(1, payload)
res = pck.Payload(1, smallpayload)
if len(res) != 0 {
t.Fatal("Generated payload should be empty")
}

// Positive MTU, small payload
res = pck.Payload(5, payload)
res = pck.Payload(5, smallpayload)
if len(res) != 1 {
t.Fatal("Generated payload shouldn't be empty")
}
if len(res[0]) != len(payload) {
if len(res[0]) != len(smallpayload) {
t.Fatal("Generated payload should be the same size as original payload size")
}

// Multiple NALU in a single payload
res = pck.Payload(5, multiplepayload)
if len(res) != 2 {
t.Fatal("2 nal units should be broken out")
}
for i := 0; i < 2; i++ {
if len(res[i]) != 1 {
t.Fatalf("Payload %d of 2 is packed incorrectly", i+1)
}
}

// Nalu type 9 or 12
res = pck.Payload(5, []byte{0x09, 0x00, 0x00})
if len(res) != 0 {
Expand Down
11 changes: 11 additions & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func TestExtension(t *testing.T) {
t.Fatal("Unmarshal did not error on packet with invalid extension length")
}

p = &Packet{Header: Header{
Extension: true,
ExtensionProfile: 3,
ExtensionPayload: []byte{0},
},
Payload: []byte{},
}
if _, err := p.Marshal(); err == nil {
t.Fatal("Marshal did not error on packet with invalid extension length")
}

}

func BenchmarkMarshal(b *testing.B) {
Expand Down
26 changes: 26 additions & 0 deletions packetizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package rtp

import (
"testing"
"time"

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

func TestNtpConversion(t *testing.T) {
loc := time.FixedZone("UTC-5", -5*60*60)

tests := []struct {
t time.Time
n uint64
}{
{t: time.Date(1985, time.June, 23, 4, 0, 0, 0, loc), n: 0xa0c65b1000000000},
{t: time.Date(1999, time.December, 31, 23, 59, 59, 500000, loc), n: 0xbc18084f0020c49b},
{t: time.Date(2019, time.March, 27, 13, 39, 30, 8675309, loc), n: 0xe04641e202388b88},
}

for _, in := range tests {
out := toNtpTime(in.t)
assert.Equal(t, in.n, out)
}
}

0 comments on commit a19ba75

Please sign in to comment.