Skip to content

Commit

Permalink
Unit tests and check improvements
Browse files Browse the repository at this point in the history
- Wrote VP8 Unmarshal test (should be 100% coverage)
- Wrote min tests
- Improved error checking on VP8 unmarshal
- Added myself to the README

Relates to #1
  • Loading branch information
Antonito committed Feb 27, 2019
1 parent ac76f37 commit 03c9968
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Check out the **[contributing wiki](https://github.com/pions/webrtc/wiki/Contrib
* [Woodrow Douglass](https://github.com/wdouglass) *RTCP, RTP improvements, G.722 support, Bugfixes*
* [Michael MacDonald](https://github.com/mjmac)
* [Luke Curley](https://github.com/kixelated) *Performance*
* [Antoine Baché](https://github.com/Antonito) *Fixed crashes*

### License
MIT License - see [LICENSE](LICENSE) for full text
20 changes: 20 additions & 0 deletions codecs/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package codecs

import (
"testing"

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

func TestCommon_Min(t *testing.T) {
assert := assert.New(t)

res := min(1, -1)
assert.Equal(res, -1, "-1 < 1")

res = min(1, 2)
assert.Equal(res, 1, "1 < 2")

res = min(3, 3)
assert.Equal(res, 3, "3 == 3")
}
9 changes: 6 additions & 3 deletions codecs/vp8_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ type VP8Packet struct {
// Unmarshal parses the passed byte slice and stores the result in the VP8Packet this method is called upon
func (p *VP8Packet) Unmarshal(packet *rtp.Packet) ([]byte, error) {
payload := packet.Payload
payloadLen := len(payload)

// If lower than the required header size, malformed header
if len(payload) < 4 {
return nil, fmt.Errorf("empty payload")
if payloadLen < 4 {
return nil, fmt.Errorf("Payload is not large enough to container header")
}

payloadIndex := 0
Expand Down Expand Up @@ -121,6 +121,9 @@ func (p *VP8Packet) Unmarshal(packet *rtp.Packet) ([]byte, error) {
payloadIndex++
}

if payloadIndex >= payloadLen {
return nil, fmt.Errorf("Payload is not large enough")
}
p.Payload = payload[payloadIndex:]
return p.Payload, nil
}
94 changes: 94 additions & 0 deletions codecs/vp8_packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package codecs

import (
"fmt"
"testing"

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

func TestVP8Packet_Unmarshal(t *testing.T) {
assert := assert.New(t)
pck := VP8Packet{}

errSmallerThanHeaderLen := fmt.Errorf("Payload is not large enough to container header")
errPayloadTooSmall := fmt.Errorf("Payload is not large enough")

// Nil payload
raw, err := pck.Unmarshal(&rtp.Packet{
Payload: nil,
})
assert.Nil(raw, "Result should be nil in case of error")
assert.Equal(err, errSmallerThanHeaderLen, "Error shouldn't nil in case of error")

// Empty payload
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: nil,
})
assert.Nil(raw, "Result should be nil in case of error")
assert.Equal(err, errSmallerThanHeaderLen, "Error shouldn't nil in case of error")

// Payload smaller than header size
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x00, 0x11, 0x22},
})
assert.Nil(raw, "Result should be nil in case of error")
assert.Equal(err, errSmallerThanHeaderLen, "Error shouldn't nil in case of error")

// Normal payload
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, only X
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x00, 0x00, 0x00},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, X and I
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x80, 0x00, 0x00},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, X and I, PID 16bits
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x80, 0x81, 0x00},
})
assert.Nil(raw, "Result should be nil in case of error")
assert.Equal(err, errPayloadTooSmall, "Error shouldn't nil in case of error")

// Header size, X and L
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x40, 0x00, 0x00},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, X and T
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x20, 0x00, 0x00},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, X and K
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0x80, 0x10, 0x00, 0x00},
})
assert.NotNil(raw, "Result shouldn't be nil in case of succeess")
assert.Nil(err, "Error should be nil in case of success")

// Header size, all flags
raw, err = pck.Unmarshal(&rtp.Packet{
Payload: []byte{0xff, 0xff, 0x00, 0x00},
})
assert.Nil(raw, "Result should be nil in case of error")
assert.Equal(err, errPayloadTooSmall, "Error shouldn't nil in case of error")
}

0 comments on commit 03c9968

Please sign in to comment.