summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-19 13:59:24 -0800
committerChris K <c@chrisko.ch>2023-02-19 22:39:16 -0800
commit5369909a5de7c157e56e0feebbfde0b190e7a614 (patch)
treed72fdc57ca90546c5578f1949a95c9945615642a
parenta3bc2a6d841c820e7297fe54731f8d91d0721426 (diff)
Tests for option deserialization & getters
Tests that for the correct option code, the correct deserialization is applied. Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/option_4rd_test.go11
-rw-r--r--dhcpv6/option_clientid_test.go19
-rw-r--r--dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go26
-rw-r--r--dhcpv6/option_iapd_test.go38
-rw-r--r--dhcpv6/option_nontemporaryaddress_test.go31
-rw-r--r--dhcpv6/option_requestedoption_test.go20
-rw-r--r--dhcpv6/option_serverid_test.go19
-rw-r--r--dhcpv6/option_temporaryaddress_test.go30
8 files changed, 189 insertions, 5 deletions
diff --git a/dhcpv6/option_4rd_test.go b/dhcpv6/option_4rd_test.go
index 6af9b67..8cd5e01 100644
--- a/dhcpv6/option_4rd_test.go
+++ b/dhcpv6/option_4rd_test.go
@@ -2,6 +2,7 @@ package dhcpv6
import (
"net"
+ "reflect"
"testing"
"github.com/stretchr/testify/require"
@@ -171,4 +172,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)
+ }
}
diff --git a/dhcpv6/option_clientid_test.go b/dhcpv6/option_clientid_test.go
index 84e87d9..dd637ae 100644
--- a/dhcpv6/option_clientid_test.go
+++ b/dhcpv6/option_clientid_test.go
@@ -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
diff --git a/dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go b/dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go
index 85d48e2..e9674c2 100644
--- a/dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go
+++ b/dhcpv6/option_dhcpv4_o_dhcpv6_server_test.go
@@ -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,
@@ -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) {
diff --git a/dhcpv6/option_iapd_test.go b/dhcpv6/option_iapd_test.go
index 2fce999..398a23e 100644
--- a/dhcpv6/option_iapd_test.go
+++ b/dhcpv6/option_iapd_test.go
@@ -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
diff --git a/dhcpv6/option_nontemporaryaddress_test.go b/dhcpv6/option_nontemporaryaddress_test.go
index a1105d3..3e5c55b 100644
--- a/dhcpv6/option_nontemporaryaddress_test.go
+++ b/dhcpv6/option_nontemporaryaddress_test.go
@@ -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
diff --git a/dhcpv6/option_requestedoption_test.go b/dhcpv6/option_requestedoption_test.go
index e5d7bfc..bb837db 100644
--- a/dhcpv6/option_requestedoption_test.go
+++ b/dhcpv6/option_requestedoption_test.go
@@ -1,11 +1,31 @@
package dhcpv6
import (
+ "reflect"
"testing"
"github.com/stretchr/testify/require"
)
+func TestParseMessageOptionsWithORO(t *testing.T) {
+ buf := []byte{
+ 0, 6, // ORO option
+ 0, 2, // length
+ 0, 3, // IANA Option
+ 0, 6, // ORO
+ 0, 2, // length
+ 0, 4, // IATA
+ }
+
+ want := OptionCodes{OptionIANA, OptionIATA}
+ var mo MessageOptions
+ if err := mo.FromBytes(buf); err != nil {
+ t.Errorf("FromBytes = %v", err)
+ } else if got := mo.RequestedOptions(); !reflect.DeepEqual(got, want) {
+ t.Errorf("ORO = %v, want %v", got, want)
+ }
+}
+
func TestOptRequestedOption(t *testing.T) {
expected := []byte{0, 1, 0, 2}
var o optRequestedOption
diff --git a/dhcpv6/option_serverid_test.go b/dhcpv6/option_serverid_test.go
index 3422287..b0250c2 100644
--- a/dhcpv6/option_serverid_test.go
+++ b/dhcpv6/option_serverid_test.go
@@ -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
diff --git a/dhcpv6/option_temporaryaddress_test.go b/dhcpv6/option_temporaryaddress_test.go
index 8704d5c..6d94f8f 100644
--- a/dhcpv6/option_temporaryaddress_test.go
+++ b/dhcpv6/option_temporaryaddress_test.go
@@ -2,12 +2,42 @@ package dhcpv6
import (
"net"
+ "reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
+func TestParseMessageWithIATA(t *testing.T) {
+ data := []byte{
+ 0, 4, // IATA option code
+ 0, 32, // length
+ 1, 0, 0, 0, // IAID
+ // IATA Options
+ 0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IP
+ 0, 0, 0, 2, // PreferredLifetime
+ 0, 0, 0, 4, // ValidLifetime
+ }
+ var got MessageOptions
+ if err := got.FromBytes(data); err != nil {
+ t.Errorf("FromBytes = %v", err)
+ }
+
+ want := &OptIATA{
+ IaId: [4]byte{1, 0, 0, 0},
+ 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 gotIATA := got.OneIATA(); !reflect.DeepEqual(gotIATA, want) {
+ t.Errorf("OneIATA = %v, want %v", gotIATA, want)
+ }
+}
+
func TestOptIATAParseOptIATA(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID