summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_bootfileparam_test.go
blob: d2e92a7db14c3bdbce3c7f20f1a9773ae6577f14 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package dhcpv6

import (
	"bytes"
	"errors"
	"fmt"
	"reflect"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/u-root/uio/uio"
)

func TestBootFileParamLargeParameter(t *testing.T) {
	param := []string{
		"foo=bar",
		strings.Repeat("a", 1<<16),
	}
	var m MessageOptions
	m.Add(OptBootFileParam(param...))
	want := append([]byte{
		0, 60, // Boot File Param
		0, 9, // length
		0, 7,
	}, []byte("foo=bar")...)

	got := m.ToBytes()
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
	}
}

func joinBytes(p ...[]byte) []byte {
	return bytes.Join(p, nil)
}

func TestBootFileParamParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want []string
	}{
		{
			buf: joinBytes([]byte{
				0, 60, // Boot File Param
				0, 25, // length
				0, 14,
			}, []byte("root=/dev/sda1"), []byte{0, 7}, []byte("foo=bar")),
			want: []string{"root=/dev/sda1", "foo=bar"},
		},
		{
			buf: nil,
		},
		{
			buf: []byte{0, 60, 0},
			err: uio.ErrUnreadBytes,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var mo MessageOptions
			if err := mo.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if got := mo.BootFileParam(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("BootFileParam = %v, want %v", got, tt.want)
			}

			if tt.want != nil {
				var m MessageOptions
				m.Add(OptBootFileParam(tt.want...))
				got := m.ToBytes()
				if diff := cmp.Diff(tt.buf, got); diff != "" {
					t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
				}
			}
		})
	}
}