From 12c0d1804c3c046e0e7758ac92806dcb4570435c Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sat, 25 Feb 2023 23:32:43 -0800 Subject: InterfaceID: tests for FromBytes, ToBytes, and Getter Signed-off-by: Chris Koch --- dhcpv6/option_interfaceid_test.go | 64 ++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/dhcpv6/option_interfaceid_test.go b/dhcpv6/option_interfaceid_test.go index b38cadb..640c06a 100644 --- a/dhcpv6/option_interfaceid_test.go +++ b/dhcpv6/option_interfaceid_test.go @@ -1,30 +1,64 @@ package dhcpv6 import ( - "bytes" + "errors" + "fmt" + "reflect" "testing" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" + "github.com/u-root/uio/uio" ) -func TestParseOptInterfaceID(t *testing.T) { - expected := []byte("DSLAM01 eth2/1/01/21") - var opt optInterfaceID - if err := opt.FromBytes(expected); err != nil { - t.Fatal(err) - } - if url := opt.ID; !bytes.Equal(url, expected) { - t.Fatalf("Invalid Interface ID. Expected %v, got %v", expected, url) +func TestInterfaceIDParseAndGetter(t *testing.T) { + for i, tt := range []struct { + buf []byte + err error + want []byte + }{ + { + buf: []byte{ + 0, 18, // Interface ID + 0, 4, // length + 'S', 'L', 'A', 'M', + }, + want: []byte("SLAM"), + }, + { + buf: []byte{ + 0, 18, + 0, 0, + }, + }, + { + buf: []byte{0, 18, 0}, + err: uio.ErrUnreadBytes, + }, + } { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + var ro RelayOptions + if err := ro.FromBytes(tt.buf); !errors.Is(err, tt.err) { + t.Errorf("FromBytes = %v, want %v", err, tt.err) + } + if got := ro.InterfaceID(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("InterfaceID = %v, want %v", got, tt.want) + } + + if tt.want != nil { + var m RelayOptions + m.Add(OptInterfaceID(tt.want)) + got := m.ToBytes() + if diff := cmp.Diff(tt.buf, got); diff != "" { + t.Errorf("ToBytes mismatch (-want, +got): %s", diff) + } + } + }) } } func TestOptInterfaceID(t *testing.T) { - want := []byte("DSLAM01 eth2/1/01/21") - opt := OptInterfaceID(want) - if got := opt.ToBytes(); !bytes.Equal(got, want) { - t.Fatalf("%s.ToBytes() = %v, want %v", opt, got, want) - } - + opt := OptInterfaceID([]byte("DSLAM01 eth2/1/01/21")) require.Contains( t, opt.String(), -- cgit v1.2.3