/*
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
  for more details.

  You should have received a copy of the GNU General Public License
  in gpl.txt.  If not, see http://www.gnu.org/licenses.
*/

package rtp

import (
	"bytes"
	"testing"
)

// TestVersion checks that we can correctly get the version from an RTP packet.
func TestVersion(t *testing.T) {
	const expect = 1
	got := version((&Packet{V: expect}).Bytes(nil))
	if got != expect {
		t.Errorf("unexpected version for RTP packet. Got: %v\n Want: %v\n", got, expect)
	}
}

// TestCsrcCount checks that we can correctly obtain the csrc count from an
// RTP packet.
func TestCsrcCount(t *testing.T) {
	const ver, expect = 2, 2

	pkt := (&Packet{
		V:    ver,
		CC:   expect,
		CSRC: make([][4]byte, expect),
	}).Bytes(nil)

	got := csrcCount(pkt)
	if got != expect {
		t.Errorf("unexpected csrc count for RTP packet. Got: %v\n Want: %v\n", got, expect)
	}
}

// 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 := &Packet{
		V: ver,
		X: true,
		Extension: ExtensionHeader{
			ID:     0,
			Header: make([][4]byte, 0),
		},
	}

	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")
	}
}

// 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{
		(&Packet{
			V:       ver,
			Payload: expect,
		}).Bytes(nil),

		(&Packet{
			V:       ver,
			CC:      3,
			CSRC:    make([][4]byte, 3),
			Payload: expect,
		}).Bytes(nil),

		(&Packet{
			V: ver,
			X: true,
			Extension: ExtensionHeader{
				ID:     0,
				Header: make([][4]byte, 3),
			},
			Payload: expect,
		}).Bytes(nil),

		(&Packet{
			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)
		}
	}
}