diff options
author | Christopher Koch <chrisko@google.com> | 2018-12-29 14:48:10 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-24 08:05:49 +0000 |
commit | c90ab10024ada840e24bb028a3405961e8e4c26a (patch) | |
tree | 9b8af0c1b80ee6efc112921f9a14b92d6c73f8eb /dhcpv4/bsdp/bsdp_option_misc_test.go | |
parent | 2be5cae32d33f01ddecf6f167a9c0e5290e6d58f (diff) |
dhcpv4: nicer API for option parsing.
From:
r := d.GetOneOption(OptionRouter).(*OptRouter).Routers
d.UpdateOption(&OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}}})
To:
r := GetRouter(d.Options)
d.UpdateOption(OptRouter(net.IP{192, 168, 0, 1}, ...))
Diffstat (limited to 'dhcpv4/bsdp/bsdp_option_misc_test.go')
-rw-r--r-- | dhcpv4/bsdp/bsdp_option_misc_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/dhcpv4/bsdp/bsdp_option_misc_test.go b/dhcpv4/bsdp/bsdp_option_misc_test.go new file mode 100644 index 0000000..dfa81b5 --- /dev/null +++ b/dhcpv4/bsdp/bsdp_option_misc_test.go @@ -0,0 +1,95 @@ +package bsdp + +import ( + "net" + "testing" + + "github.com/insomniacslk/dhcp/dhcpv4" + "github.com/stretchr/testify/require" +) + +func TestOptReplyPort(t *testing.T) { + o := OptReplyPort(1234) + require.Equal(t, OptionReplyPort, o.Code, "Code") + require.Equal(t, []byte{4, 210}, o.Value.ToBytes(), "ToBytes") + require.Equal(t, "BSDP Reply Port: 1234", o.String()) +} + +func TestGetReplyPort(t *testing.T) { + o := VendorOptions{dhcpv4.OptionsFromList(OptReplyPort(1234))} + port, err := GetReplyPort(o.Options) + require.NoError(t, err) + require.Equal(t, uint16(1234), port) + + port, err = GetReplyPort(dhcpv4.Options{}) + require.Error(t, err, "no reply port present") +} + +func TestOptServerPriority(t *testing.T) { + o := OptServerPriority(1234) + require.Equal(t, OptionServerPriority, o.Code, "Code") + require.Equal(t, []byte{4, 210}, o.Value.ToBytes(), "ToBytes") + require.Equal(t, "BSDP Server Priority: 1234", o.String()) +} + +func TestGetServerPriority(t *testing.T) { + o := VendorOptions{dhcpv4.OptionsFromList(OptServerPriority(1234))} + prio, err := GetServerPriority(o.Options) + require.NoError(t, err) + require.Equal(t, uint16(1234), prio) + + prio, err = GetServerPriority(dhcpv4.Options{}) + require.Error(t, err, "no server prio present") +} + +func TestOptMachineName(t *testing.T) { + o := OptMachineName("foo") + require.Equal(t, OptionMachineName, o.Code, "Code") + require.Equal(t, []byte("foo"), o.Value.ToBytes(), "ToBytes") + require.Equal(t, "BSDP Machine Name: foo", o.String()) +} + +func TestGetMachineName(t *testing.T) { + o := VendorOptions{dhcpv4.OptionsFromList(OptMachineName("foo"))} + require.Equal(t, "foo", GetMachineName(o.Options)) + require.Equal(t, "", GetMachineName(dhcpv4.Options{})) +} + +func TestOptVersion(t *testing.T) { + o := OptVersion(Version1_1) + require.Equal(t, OptionVersion, o.Code, "Code") + require.Equal(t, []byte{1, 1}, o.Value.ToBytes(), "ToBytes") + require.Equal(t, "BSDP Version: 1.1", o.String()) +} + +func TestGetVersion(t *testing.T) { + o := VendorOptions{dhcpv4.OptionsFromList(OptVersion(Version1_1))} + ver, err := GetVersion(o.Options) + require.NoError(t, err) + require.Equal(t, ver, Version1_1) + + ver, err = GetVersion(dhcpv4.Options{}) + require.Error(t, err, "no version present") + + ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{}}) + require.Error(t, err, "empty version field") + + ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{1}}) + require.Error(t, err, "version option too short") + + ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{1, 2, 3}}) + require.Error(t, err, "version option too long") +} + +func TestOptServerIdentifier(t *testing.T) { + o := OptServerIdentifier(net.IP{1, 1, 1, 1}) + require.Equal(t, OptionServerIdentifier, o.Code, "Code") + require.Equal(t, []byte{1, 1, 1, 1}, o.Value.ToBytes(), "ToBytes") + require.Equal(t, "BSDP Server Identifier: 1.1.1.1", o.String()) +} + +func TestGetServerIdentifier(t *testing.T) { + o := VendorOptions{dhcpv4.OptionsFromList(OptServerIdentifier(net.IP{1, 1, 1, 1}))} + require.Equal(t, net.IP{1, 1, 1, 1}, GetServerIdentifier(o.Options)) + require.Equal(t, net.IP(nil), GetServerIdentifier(dhcpv4.Options{})) +} |