2019-04-05 07:03:47 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
parse_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
parse_test.go provides testing for behaviour of functionality in parse.go.
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2019-04-07 07:05:46 +03:00
|
|
|
for more details.
|
2019-04-05 07:03:47 +03:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rtp
|
2019-04-05 07:44:32 +03:00
|
|
|
|
|
|
|
import (
|
2019-04-05 08:41:40 +03:00
|
|
|
"bytes"
|
2019-04-05 07:44:32 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestVersion checks that we can correctly get the version from an RTP packet.
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
const expect = 1
|
2019-04-05 08:41:40 +03:00
|
|
|
got := version((&Pkt{V: expect}).Bytes(nil))
|
2019-04-05 07:44:32 +03:00
|
|
|
if got != expect {
|
|
|
|
t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", got, expect)
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 07:53:00 +03:00
|
|
|
|
|
|
|
// TestCsrcCount checks that we can correctly obtain the csrc count from an
|
|
|
|
// RTP packet.
|
|
|
|
func TestCsrcCount(t *testing.T) {
|
|
|
|
const ver, expect = 2, 2
|
2019-04-05 08:41:40 +03:00
|
|
|
|
2019-04-05 07:53:00 +03:00
|
|
|
pkt := (&Pkt{
|
|
|
|
V: ver,
|
|
|
|
CC: expect,
|
|
|
|
CSRC: make([][4]byte, expect),
|
|
|
|
}).Bytes(nil)
|
2019-04-05 08:41:40 +03:00
|
|
|
|
2019-04-05 07:53:00 +03:00
|
|
|
got := csrcCount(pkt)
|
|
|
|
if got != expect {
|
|
|
|
t.Errorf("unexpected csrc count for RTP packet. Got: %v\n Want: %v\n", got, expect)
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 08:16:05 +03:00
|
|
|
|
|
|
|
// TestHasExt checks the behaviour of hasExt with an RTP packet that has the
|
|
|
|
// extension indicator true, and one with the extension indicator set to false.
|
|
|
|
func TestHasExt(t *testing.T) {
|
|
|
|
const ver = 2
|
|
|
|
|
|
|
|
// First check for when there is an extension field.
|
|
|
|
pkt := &Pkt{
|
|
|
|
V: ver,
|
|
|
|
X: true,
|
|
|
|
Extension: ExtensionHeader{
|
|
|
|
ID: 0,
|
|
|
|
Header: make([][4]byte, 0),
|
|
|
|
},
|
|
|
|
}
|
2019-04-05 08:41:40 +03:00
|
|
|
|
2019-04-05 08:16:05 +03:00
|
|
|
got := hasExt(pkt.Bytes(nil))
|
|
|
|
if !got {
|
|
|
|
t.Error("RTP packet did not have true extension indicator as expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check when there is not an extension field.
|
|
|
|
pkt.X = false
|
|
|
|
got = hasExt(pkt.Bytes(nil))
|
|
|
|
if got {
|
|
|
|
t.Error("did not expect to have extension indicator as true")
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 08:23:22 +03:00
|
|
|
|
|
|
|
// TestExtHeaderLen checks for a correct extension header len for an RTP packet.
|
|
|
|
func TestExtHeaderLen(t *testing.T) {
|
|
|
|
const ver, expect = 2, 3
|
2019-04-05 08:41:40 +03:00
|
|
|
|
2019-04-05 08:23:22 +03:00
|
|
|
pkt := (&Pkt{
|
|
|
|
V: ver,
|
|
|
|
X: true,
|
|
|
|
Extension: ExtensionHeader{
|
|
|
|
ID: 0,
|
|
|
|
Header: make([][4]byte, expect),
|
|
|
|
},
|
|
|
|
}).Bytes(nil)
|
2019-04-05 08:41:40 +03:00
|
|
|
|
2019-04-05 08:23:22 +03:00
|
|
|
got := extHeaderLen(pkt)
|
|
|
|
if got != expect {
|
|
|
|
t.Errorf("Unexpected extension header len. Got: %v\n Want: %v\n", got, expect)
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 08:41:40 +03:00
|
|
|
|
|
|
|
// TestPayload checks that we can correctly get the payload of an RTP packet
|
|
|
|
// using Payload for a variety of RTP packet configurations.
|
|
|
|
func TestPayload(t *testing.T) {
|
|
|
|
const ver = 2
|
|
|
|
expect := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
|
|
|
|
|
|
|
|
testPkts := [][]byte{
|
|
|
|
(&Pkt{
|
|
|
|
V: ver,
|
|
|
|
Payload: expect,
|
|
|
|
}).Bytes(nil),
|
|
|
|
|
|
|
|
(&Pkt{
|
|
|
|
V: ver,
|
|
|
|
CC: 3,
|
|
|
|
CSRC: make([][4]byte, 3),
|
|
|
|
Payload: expect,
|
|
|
|
}).Bytes(nil),
|
|
|
|
|
|
|
|
(&Pkt{
|
|
|
|
V: ver,
|
|
|
|
X: true,
|
|
|
|
Extension: ExtensionHeader{
|
|
|
|
ID: 0,
|
|
|
|
Header: make([][4]byte, 3),
|
|
|
|
},
|
|
|
|
Payload: expect,
|
|
|
|
}).Bytes(nil),
|
|
|
|
|
|
|
|
(&Pkt{
|
|
|
|
V: ver,
|
|
|
|
CC: 3,
|
|
|
|
CSRC: make([][4]byte, 3),
|
|
|
|
Extension: ExtensionHeader{
|
|
|
|
ID: 0,
|
|
|
|
Header: make([][4]byte, 3),
|
|
|
|
},
|
|
|
|
Payload: expect,
|
|
|
|
}).Bytes(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range testPkts {
|
|
|
|
got, err := Payload(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error from Payload with pkt: %v", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(got, expect) {
|
|
|
|
t.Errorf("unexpected payload data from RTP packet: %v.\n Got: %v\n Want: %v\n", i, got, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|