summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 22:38:49 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commitc76427858ad8c431c90b5ea9909dc1e4632a426c (patch)
treec8fe1d5c564834bbddcdcff9d9d0b372e4c39d57
parent71b4bcb073d27c5ce2bfee1293282b0acde88ce1 (diff)
ElapsedTime: tests for FromBytes, ToBytes, and Getter
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/option_elapsedtime_test.go83
1 files changed, 57 insertions, 26 deletions
diff --git a/dhcpv6/option_elapsedtime_test.go b/dhcpv6/option_elapsedtime_test.go
index b0d2dc1..5e95cda 100644
--- a/dhcpv6/option_elapsedtime_test.go
+++ b/dhcpv6/option_elapsedtime_test.go
@@ -1,29 +1,69 @@
package dhcpv6
import (
- "bytes"
+ "errors"
+ "fmt"
+ "reflect"
"testing"
"time"
- "github.com/stretchr/testify/require"
+ "github.com/google/go-cmp/cmp"
+ "github.com/u-root/uio/uio"
)
-func TestOptElapsedTime(t *testing.T) {
- var opt optElapsedTime
- err := opt.FromBytes([]byte{0xaa, 0xbb})
- if err != nil {
- t.Fatal(err)
- }
- if elapsedTime := opt.ElapsedTime; elapsedTime != 0xaabb*10*time.Millisecond {
- t.Fatalf("Invalid elapsed time. Expected 0xaabb, got %v", elapsedTime)
- }
-}
+func TestElapsedTimeParseAndGetter(t *testing.T) {
+ for i, tt := range []struct {
+ buf []byte
+ err error
+ want time.Duration
+ }{
+ {
+ buf: []byte{
+ 0, 8, // Elapsed Time option
+ 0, 2, // length
+ 0, 2,
+ },
+ want: 20 * time.Millisecond,
+ },
+ {
+ buf: []byte{
+ 0, 8, // Elapsed Time option
+ 0, 1, // length
+ 0,
+ },
+ err: uio.ErrBufferTooShort,
+ },
+ {
+ buf: []byte{
+ 0, 8, // Elapsed Time option
+ 0, 3, // length
+ 0, 2, 2,
+ },
+ err: uio.ErrUnreadBytes,
+ },
+ {
+ buf: []byte{0, 8, 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.ElapsedTime(); !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("ElapsedTime = %v, want %v", got, tt.want)
+ }
-func TestOptElapsedTimeToBytes(t *testing.T) {
- opt := OptElapsedTime(0)
- expected := []byte{0, 0}
- if toBytes := opt.ToBytes(); !bytes.Equal(expected, toBytes) {
- t.Fatalf("Invalid ToBytes output. Expected %v, got %v", expected, toBytes)
+ if tt.err == nil {
+ var m MessageOptions
+ m.Add(OptElapsedTime(tt.want))
+ got := m.ToBytes()
+ if diff := cmp.Diff(tt.buf, got); diff != "" {
+ t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
+ }
+ }
+ })
}
}
@@ -34,12 +74,3 @@ func TestOptElapsedTimeString(t *testing.T) {
t.Fatalf("Invalid elapsed time string. Expected %v, got %v", expected, optString)
}
}
-
-func TestOptElapsedTimeParseInvalidOption(t *testing.T) {
- var opt optElapsedTime
- err := opt.FromBytes([]byte{0xaa})
- require.Error(t, err, "A short option should return an error")
-
- err = opt.FromBytes([]byte{0xaa, 0xbb, 0xcc})
- require.Error(t, err, "An option with too many bytes should return an error")
-}