summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 20:10:03 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commit35f1aa318ee0ad26084ca7642ba5102539a7d7a9 (patch)
treea4bd8e611ce89625cd7aa7bbda23000d52a234ee
parent2c09f487ca14542f3338f631e0d2787c30fc43b6 (diff)
ClientArchType: tests for FromBytes, ToBytes, and Getter
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/option_archtype_test.go83
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) {