summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp/bsdp_option_generic_test.go
blob: 5abcfbda905edd4a7ed4abdcf674145618252fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package bsdp

import (
	"testing"

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

func TestParseOptGeneric(t *testing.T) {
	// Empty bytestream produces error
	_, err := ParseOptGeneric([]byte{})
	require.Error(t, err, "error from empty bytestream")
}

func TestOptGenericCode(t *testing.T) {
	o := OptGeneric{
		OptionCode: OptionMessageType,
		Data:       []byte{byte(MessageTypeList)},
	}
	require.Equal(t, OptionMessageType, o.Code())
}

func TestOptGenericData(t *testing.T) {
	o := OptGeneric{
		OptionCode: OptionServerIdentifier,
		Data:       []byte{192, 168, 0, 1},
	}
	require.Equal(t, []byte{192, 168, 0, 1}, o.Data)
}

func TestOptGenericToBytes(t *testing.T) {
	o := OptGeneric{
		OptionCode: OptionServerIdentifier,
		Data:       []byte{192, 168, 0, 1},
	}
	serialized := o.ToBytes()
	expected := []byte{3, 4, 192, 168, 0, 1}
	require.Equal(t, expected, serialized)
}

func TestOptGenericString(t *testing.T) {
	o := OptGeneric{
		OptionCode: OptionServerIdentifier,
		Data:       []byte{192, 168, 0, 1},
	}
	require.Equal(t, "BSDP Server Identifier -> [192 168 0 1]", o.String())
}

func TestOptGenericStringUnknown(t *testing.T) {
	o := OptGeneric{
		OptionCode: 102, // Returend option code.
		Data:       []byte{5},
	}
	require.Equal(t, "Unknown -> [5]", o.String())
}

func TestOptGenericLength(t *testing.T) {
	filename := "some_machine_name"
	o := OptGeneric{
		OptionCode: OptionMachineName,
		Data:       []byte(filename),
	}
	require.Equal(t, len(filename), o.Length())
}