Skip to content

Commit

Permalink
Tests for option deserialization
Browse files Browse the repository at this point in the history
Tests that for the correct option code, the correct deserialization is
applied.

Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 19, 2023
1 parent 8e2923e commit e8932a8
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 5 deletions.
11 changes: 11 additions & 0 deletions dhcpv6/option_4rd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dhcpv6

import (
"net"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -173,4 +174,14 @@ func TestOpt4RDRoundTrip(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, rtOpt)
require.Equal(t, opt, rtOpt)

var mo MessageOptions
mo.Options.Add(&opt)

var got MessageOptions
if err := got.FromBytes(mo.ToBytes()); err != nil {
t.Errorf("FromBytes = %v", err)
} else if !reflect.DeepEqual(mo, got) {
t.Errorf("FromBytes = %v, want %v", got, mo)
}
}
19 changes: 19 additions & 0 deletions dhcpv6/option_clientid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ package dhcpv6

import (
"net"
"reflect"
"testing"

"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
)

func TestParseMessageOptionsWithClientID(t *testing.T) {
buf := []byte{
0, 1, // Client ID option
0, 10, // length
0, 3, // DUID_LL
0, 1, // hwtype ethernet
0, 1, 2, 3, 4, 5, // HW addr
}

want := &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0, 1, 2, 3, 4, 5}}
var mo MessageOptions
if err := mo.FromBytes(buf); err != nil {
t.Errorf("FromBytes = %v", err)
} else if got := mo.ClientID(); !reflect.DeepEqual(got, want) {
t.Errorf("ClientID = %v, want %v", got, want)
}
}

func TestParseOptClientID(t *testing.T) {
data := []byte{
0, 3, // DUID_LL
Expand Down
26 changes: 21 additions & 5 deletions dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ package dhcpv6

import (
"net"
"reflect"
"testing"

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

func TestParseMessageOptionsWithDHCP4oDHCP6Server(t *testing.T) {
ip := net.IP{0x2a, 0x03, 0x28, 0x80, 0xff, 0xfe, 0x00, 0x0c, 0xfa, 0xce, 0xb0, 0x0c, 0x00, 0x00, 0x00, 0x35}
data := append([]byte{
0, 88, // DHCP4oDHCP6 option.
0, 16, // length
}, ip...)

want := []net.IP{ip}
var mo MessageOptions
if err := mo.FromBytes(data); err != nil {
t.Errorf("FromBytes = %v", err)
} else if got := mo.DHCP4oDHCP6Server(); !reflect.DeepEqual(got.DHCP4oDHCP6Servers, want) {
t.Errorf("FromBytes = %v, want %v", got.DHCP4oDHCP6Servers, want)
}
}

func TestParseOptDHCP4oDHCP6Server(t *testing.T) {
data := []byte{
0x2a, 0x03, 0x28, 0x80, 0xff, 0xfe, 0x00, 0x0c, 0xfa, 0xce, 0xb0, 0x0c, 0x00, 0x00, 0x00, 0x35,
Expand All @@ -25,11 +42,10 @@ func TestParseOptDHCP4oDHCP6Server(t *testing.T) {
func TestOptDHCP4oDHCP6ServerToBytes(t *testing.T) {
ip1 := net.ParseIP("2a03:2880:fffe:c:face:b00c:0:35")
ip2 := net.ParseIP("2001:4860:4860::8888")
servers := []net.IP{ip1, ip2}
expected := append([]byte{}, []byte(ip1)...)
expected = append(expected, []byte(ip2)...)
opt := OptDHCP4oDHCP6Server{DHCP4oDHCP6Servers: servers}
require.Equal(t, expected, opt.ToBytes())
opt := OptDHCP4oDHCP6Server{DHCP4oDHCP6Servers: []net.IP{ip1, ip2}}

want := []byte(append(ip1, ip2...))
require.Equal(t, want, opt.ToBytes())
}

func TestParseOptDHCP4oDHCP6ServerParseNoAddr(t *testing.T) {
Expand Down
38 changes: 38 additions & 0 deletions dhcpv6/option_iapd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,50 @@ package dhcpv6

import (
"net"
"reflect"
"testing"
"time"

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

func TestParseMessageWithIAPD(t *testing.T) {
data := []byte{
0, 25, // IAPD option code
0, 41, // length
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
0, 0, 0, 2, // T2
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0, 0, 0, 2, // IAPrefix preferredLifetime
0, 0, 0, 4, // IAPrefix validLifetime
36, // IAPrefix prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
}
var got MessageOptions
if err := got.FromBytes(data); err != nil {
t.Errorf("FromBytes = %v", err)
}

want := &OptIAPD{
IaId: [4]byte{1, 0, 0, 0},
T1: 1 * time.Second,
T2: 2 * time.Second,
Options: PDOptions{Options: Options{&OptIAPrefix{
PreferredLifetime: 2 * time.Second,
ValidLifetime: 4 * time.Second,
Prefix: &net.IPNet{
Mask: net.CIDRMask(36, 128),
IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
},
Options: PrefixOptions{Options: Options{}},
}}},
}
if gotIAPD := got.OneIAPD(); !reflect.DeepEqual(gotIAPD, want) {
t.Errorf("OneIAPD = %v, want %v", gotIAPD, want)
}
}

func TestOptIAPDParseOptIAPD(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
Expand Down
31 changes: 31 additions & 0 deletions dhcpv6/option_nontemporaryaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@ package dhcpv6

import (
"net"
"reflect"
"testing"
"time"

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

func TestParseMessageWithIANA(t *testing.T) {
data := []byte{
0, 3, // IANA option code
0, 40, // length
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
0, 0, 0, 2, // T2
0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, // options
}
var got MessageOptions
if err := got.FromBytes(data); err != nil {
t.Errorf("FromBytes = %v", err)
}

want := &OptIANA{
IaId: [4]byte{1, 0, 0, 0},
T1: 1 * time.Second,
T2: 2 * time.Second,
Options: IdentityOptions{Options: Options{&OptIAAddress{
IPv6Addr: net.IP{0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0},
PreferredLifetime: 2 * time.Second,
ValidLifetime: 4 * time.Second,
Options: AddressOptions{Options: Options{}},
}}},
}
if gotIANA := got.OneIANA(); !reflect.DeepEqual(gotIANA, want) {
t.Errorf("OneIANA = %v, want %v", gotIANA, want)
}
}

func TestOptIANAParseOptIANA(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
Expand Down
19 changes: 19 additions & 0 deletions dhcpv6/option_serverid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ package dhcpv6

import (
"net"
"reflect"
"testing"

"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
)

func TestParseMessageOptionsWithServerID(t *testing.T) {
buf := []byte{
0, 2, // Server ID option
0, 10, // length
0, 3, // DUID_LL
0, 1, // hwtype ethernet
0, 1, 2, 3, 4, 5, // HW addr
}

want := &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0, 1, 2, 3, 4, 5}}
var mo MessageOptions
if err := mo.FromBytes(buf); err != nil {
t.Errorf("FromBytes = %v", err)
} else if got := mo.ServerID(); !reflect.DeepEqual(got, want) {
t.Errorf("ServerID = %v, want %v", got, want)
}
}

func TestParseOptServerID(t *testing.T) {
data := []byte{
0, 3, // DUID_LL
Expand Down

0 comments on commit e8932a8

Please sign in to comment.