summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_bootfileurl_test.go
blob: 726fde86839b6d7dee4ff67669bfb47b5fbcd794 (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
package dhcpv6

import (
	"errors"
	"fmt"
	"testing"

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

func TestBootFileURLParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want string
	}{
		{
			buf: []byte{
				0, 59, // Boot File URL
				0, 17, // length
				'h', 't', 't', 'p', ':', '/', '/', 'u', '-', 'r', 'o', 'o', 't', '.', 'o', 'r', 'g',
			},
			want: "http://u-root.org",
		},
		{
			buf: nil,
		},
		{
			buf: []byte{0, 59, 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.BootFileURL(); got != tt.want {
				t.Errorf("BootFileURL = %v, want %v", got, tt.want)
			}

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

func TestOptBootFileURL(t *testing.T) {
	opt := OptBootFileURL("https://insomniac.slackware.it")
	require.Contains(t, opt.String(), "https://insomniac.slackware.it", "String() should contain the correct BootFileUrl output")
}