summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/duid_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv6/duid_test.go')
-rw-r--r--dhcpv6/duid_test.go88
1 files changed, 77 insertions, 11 deletions
diff --git a/dhcpv6/duid_test.go b/dhcpv6/duid_test.go
index 23ce709..f28b26b 100644
--- a/dhcpv6/duid_test.go
+++ b/dhcpv6/duid_test.go
@@ -2,27 +2,93 @@ package dhcpv6
import (
"bytes"
+ "net"
"testing"
+
+ "github.com/insomniacslk/dhcp/iana"
+ "github.com/stretchr/testify/require"
)
-func TestDuidUuid(t *testing.T) {
+func TestDuidInvalidTooShort(t *testing.T) {
+ // too short DUID at all (must be at least 2 bytes)
+ _, err := DuidFromBytes([]byte{0})
+ require.Error(t, err)
+
+ // too short DUID_LL (must be at least 4 bytes)
+ _, err = DuidFromBytes([]byte{0, 3, 0xa})
+ require.Error(t, err)
+
+ // too short DUID_EN (must be at least 6 bytes)
+ _, err = DuidFromBytes([]byte{0, 2, 0xa, 0xb, 0xc})
+ require.Error(t, err)
+
+ // too short DUID_LLT (must be at least 8 bytes)
+ _, err = DuidFromBytes([]byte{0, 1, 0xa, 0xb, 0xc, 0xd, 0xe})
+ require.Error(t, err)
+
+ // too short DUID_UUID (must be at least 18 bytes)
+ _, err = DuidFromBytes([]byte{0, 4, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf})
+ require.Error(t, err)
+}
+
+func TestDuidLLTFromBytes(t *testing.T) {
buf := []byte{
- 0x00, 0x04, // type
- 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, // uuid
+ 0, 1, // DUID_LLT
+ 0, 1, // HwTypeEthernet
+ 0x01, 0x02, 0x03, 0x04, // time
+ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, // link-layer addr
}
duid, err := DuidFromBytes(buf)
- if err != nil {
- t.Fatal(err)
+ require.NoError(t, err)
+ require.Equal(t, 14, duid.Length())
+ require.Equal(t, DUID_LLT, duid.Type)
+ require.Equal(t, uint32(0x01020304), duid.Time)
+ require.Equal(t, iana.HwTypeEthernet, duid.HwType)
+ require.Equal(t, net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, duid.LinkLayerAddr)
+}
+
+func TestDuidLLFromBytes(t *testing.T) {
+ buf := []byte{
+ 0, 3, // DUID_LL
+ 0, 1, // HwTypeEthernet
+ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, // link-layer addr
}
- if dt := duid.Type; dt != DUID_UUID {
- t.Fatalf("Invalid Preferred Lifetime. Expected 4, got %d", dt)
+ duid, err := DuidFromBytes(buf)
+ require.NoError(t, err)
+ require.Equal(t, 10, duid.Length())
+ require.Equal(t, DUID_LL, duid.Type)
+ require.Equal(t, iana.HwTypeEthernet, duid.HwType)
+ require.Equal(t, net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, duid.LinkLayerAddr)
+}
+
+func TestDuidUuidFromBytes(t *testing.T) {
+ buf := []byte{
+ 0x00, 0x04, // DUID_UUID
}
- if uuid := duid.Uuid; !bytes.Equal(uuid, buf[2:]) {
- t.Fatalf("Invalid UUID. Expected %v, got %v", buf[2:], uuid)
+ uuid := []byte{0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08}
+ buf = append(buf, uuid...)
+ duid, err := DuidFromBytes(buf)
+ require.NoError(t, err)
+ require.Equal(t, 18, duid.Length())
+ require.Equal(t, DUID_UUID, duid.Type)
+ require.Equal(t, uuid, duid.Uuid)
+}
+
+func TestDuidLLTToBytes(t *testing.T) {
+ expected := []byte{
+ 0, 1, // DUID_LLT
+ 0, 1, // HwTypeEthernet
+ 0x01, 0x02, 0x03, 0x04, // time
+ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, // link-layer addr
}
- if mac := duid.LinkLayerAddr; mac != nil {
- t.Fatalf("Invalid MAC. Expected nil, got %v", mac)
+ duid := Duid{
+ Type: DUID_LLT,
+ HwType: iana.HwTypeEthernet,
+ Time: uint32(0x01020304),
+ LinkLayerAddr: []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
}
+ toBytes := duid.ToBytes()
+ require.Equal(t, expected, toBytes)
}
func TestDuidUuidToBytes(t *testing.T) {