diff options
-rw-r--r-- | dhcpv6/option_archtype_test.go | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/dhcpv6/option_archtype_test.go b/dhcpv6/option_archtype_test.go index 8ebd18f..b8a39fd 100644 --- a/dhcpv6/option_archtype_test.go +++ b/dhcpv6/option_archtype_test.go @@ -1,37 +1,74 @@ package dhcpv6 import ( + "errors" + "fmt" + "reflect" "testing" + "github.com/google/go-cmp/cmp" "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" + "github.com/u-root/uio/uio" ) -func TestParseOptClientArchType(t *testing.T) { - data := []byte{ - 0, 6, // EFI_IA32 - } - var opt optClientArchType - err := opt.FromBytes(data) - require.NoError(t, err) - require.Equal(t, iana.EFI_IA32, opt.Archs[0]) -} - -func TestParseOptClientArchTypeInvalid(t *testing.T) { - data := []byte{42} - var opt optClientArchType - err := opt.FromBytes(data) - require.Error(t, err) -} +func TestArchTypeParseAndGetter(t *testing.T) { + for i, tt := range []struct { + buf []byte + err error + want iana.Archs + }{ + { + buf: []byte{ + 0, 61, // Client Arch Types option + 0, 2, // length + 0, 7, // EFI_X86_64 + }, + want: iana.Archs{iana.EFI_X86_64}, + }, + { + buf: []byte{ + 0, 61, // Client Arch Types option + 0, 4, // length + 0, 7, // EFI_X86_64 + 0, 8, // EFI_XSCALE + }, + want: iana.Archs{iana.EFI_X86_64, iana.EFI_XSCALE}, + }, + { + buf: nil, + want: nil, + }, + { + buf: []byte{0, 61, 0, 1, 0}, + want: nil, + err: uio.ErrUnreadBytes, + }, + { + buf: []byte{0, 61, 0}, + want: nil, + 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.ArchTypes(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ArchTypes = %v, want %v", got, tt.want) + } -func TestOptClientArchTypeParseAndToBytes(t *testing.T) { - data := []byte{ - 0, 8, // EFI_XSCALE + if tt.want != nil { + var m MessageOptions + m.Add(OptClientArchType(tt.want...)) + got := m.ToBytes() + if diff := cmp.Diff(tt.buf, got); diff != "" { + t.Errorf("ToBytes mismatch (-want, +got): %s", diff) + } + } + }) } - var opt optClientArchType - err := opt.FromBytes(data) - require.NoError(t, err) - require.Equal(t, data, opt.ToBytes()) } func TestOptClientArchType(t *testing.T) { |