summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 23:32:43 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commit12c0d1804c3c046e0e7758ac92806dcb4570435c (patch)
tree52196b9af9ffee0a4db07bfc672947aef847465a
parentc85893e1cfbf875afdc99643c0a55f18187343ce (diff)
InterfaceID: tests for FromBytes, ToBytes, and Getter
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/option_interfaceid_test.go64
1 files 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(),