Skip to content

Commit

Permalink
Refactor decode test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Jan 12, 2020
1 parent c4c2ed5 commit 73d6fec
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions protocols/ospf/packetv3/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/stretchr/testify/require"
)

type test struct {
type decodeTest struct {
name string
input []byte
wantFail bool
expected interface{}
expected *ospf.OSPFv3Message
}

func runTest(t *testing.T, testCase test, src, dst net.IP) {
func runDecodeTest(t *testing.T, testCase decodeTest, src, dst net.IP) {
t.Run(testCase.name, func(t *testing.T) {
buf := bytes.NewBuffer(testCase.input)
msg, _, err := ospf.DeserializeOSPFv3Message(buf, src, dst)
Expand All @@ -28,6 +28,7 @@ func runTest(t *testing.T, testCase test, src, dst net.IP) {

require.NoError(t, err)
assert.Equal(t, testCase.expected, msg)
assert.Len(t, testCase.input, int(testCase.expected.PacketLength))
})
}

Expand All @@ -36,7 +37,7 @@ func routerID(o1, o2, o3, o4 uint8) ospf.ID {
}

func TestDecodeHello(t *testing.T) {
tests := []test{
tests := []decodeTest{
{
name: "Default",
input: []byte{
Expand Down Expand Up @@ -262,12 +263,12 @@ func TestDecodeHello(t *testing.T) {
require.NoError(t, err)

for _, test := range tests {
runTest(t, test, src, dst)
runDecodeTest(t, test, src, dst)
}
}

func TestDecodeDBDesc(t *testing.T) {
tests := []test{
tests := []decodeTest{
{
name: "Default",
input: []byte{
Expand Down Expand Up @@ -475,12 +476,12 @@ func TestDecodeDBDesc(t *testing.T) {
require.NoError(t, err)

for _, test := range tests {
runTest(t, test, src, dst)
runDecodeTest(t, test, src, dst)
}
}

func TestDecodeLSRequest(t *testing.T) {
tests := []test{
tests := []decodeTest{
{
name: "Default",
input: []byte{
Expand Down Expand Up @@ -547,7 +548,7 @@ func TestDecodeLSRequest(t *testing.T) {
require.NoError(t, err)

for _, test := range tests {
runTest(t, test, src, dst)
runDecodeTest(t, test, src, dst)
}
}

Expand All @@ -559,7 +560,7 @@ func mustIP(ip net.IP, err error) net.IP {
}

func TestDecodeLSUpdate(t *testing.T) {
tests := []test{
tests := []decodeTest{
{
name: "Default",
input: []byte{
Expand Down Expand Up @@ -789,12 +790,12 @@ func TestDecodeLSUpdate(t *testing.T) {
require.NoError(t, err)

for _, test := range tests {
runTest(t, test, src, dst)
runDecodeTest(t, test, src, dst)
}
}

func TestDecodeLSAck(t *testing.T) {
tests := []test{
tests := []decodeTest{
{
name: "Default",
input: []byte{
Expand Down Expand Up @@ -974,6 +975,6 @@ func TestDecodeLSAck(t *testing.T) {
require.NoError(t, err)

for _, test := range tests {
runTest(t, test, src, dst)
runDecodeTest(t, test, src, dst)
}
}

0 comments on commit 73d6fec

Please sign in to comment.