summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/duid_test.go
blob: 23ce709d5cbd231bc0dd4e5a1f950a1ba3288ebd (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
package dhcpv6

import (
	"bytes"
	"testing"
)

func TestDuidUuid(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
	}
	duid, err := DuidFromBytes(buf)
	if err != nil {
		t.Fatal(err)
	}
	if dt := duid.Type; dt != DUID_UUID {
		t.Fatalf("Invalid Preferred Lifetime. Expected 4, got %d", dt)
	}
	if uuid := duid.Uuid; !bytes.Equal(uuid, buf[2:]) {
		t.Fatalf("Invalid UUID. Expected %v, got %v", buf[2:], uuid)
	}
	if mac := duid.LinkLayerAddr; mac != nil {
		t.Fatalf("Invalid MAC. Expected nil, got %v", mac)
	}
}

func TestDuidUuidToBytes(t *testing.T) {
	uuid := []byte{0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09}
	expected := []byte{00, 04}
	expected = append(expected, uuid...)
	duid := Duid{
		Type: DUID_UUID,
		Uuid: uuid,
	}
	toBytes := duid.ToBytes()
	if !bytes.Equal(toBytes, expected) {
		t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes)
	}
}