diff options
author | Chris Koch <chrisko@google.com> | 2023-02-25 20:24:38 -0800 |
---|---|---|
committer | Chris K <c@chrisko.ch> | 2023-02-27 10:35:19 -0800 |
commit | 373a15f35014bad0f6ef862e6af7b31056fff6ff (patch) | |
tree | ab2645cdfa2e5f3c9024014c7f44829f59c05c8d | |
parent | d686b3a94668112a88ed62386c84c64ed51670f4 (diff) |
ORO: tests for FromBytes, ToBytes, and ORO Getter
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r-- | dhcpv6/option_requestedoption_test.go | 77 |
1 files changed, 63 insertions, 14 deletions
diff --git a/dhcpv6/option_requestedoption_test.go b/dhcpv6/option_requestedoption_test.go index bb837db..ab6e86b 100644 --- a/dhcpv6/option_requestedoption_test.go +++ b/dhcpv6/option_requestedoption_test.go @@ -1,12 +1,75 @@ package dhcpv6 import ( + "errors" + "fmt" "reflect" "testing" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" + "github.com/u-root/uio/uio" ) +func TestOROParseAndGetter(t *testing.T) { + for i, tt := range []struct { + buf []byte + err error + want OptionCodes + }{ + { + buf: []byte{ + 0, 6, // ORO option + 0, 2, // length + 0, 3, // IANA option + }, + want: OptionCodes{OptionIANA}, + }, + { + buf: []byte{ + 0, 6, // ORO option + 0, 4, // length + 0, 3, // IANA + 0, 4, // IATA + }, + want: OptionCodes{OptionIANA, OptionIATA}, + }, + { + buf: nil, + want: nil, + }, + { + buf: []byte{0, 6, 0, 1, 0}, + want: nil, + err: uio.ErrUnreadBytes, + }, + { + buf: []byte{0, 6, 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.RequestedOptions(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RequestedOptions = %v, want %v", got, tt.want) + } + + if tt.want != nil { + var m MessageOptions + m.Add(OptRequestedOption(tt.want...)) + got := m.ToBytes() + if diff := cmp.Diff(tt.buf, got); diff != "" { + t.Errorf("ToBytes mismatch (-want, +got): %s", diff) + } + } + }) + } +} + func TestParseMessageOptionsWithORO(t *testing.T) { buf := []byte{ 0, 6, // ORO option @@ -26,20 +89,6 @@ func TestParseMessageOptionsWithORO(t *testing.T) { } } -func TestOptRequestedOption(t *testing.T) { - expected := []byte{0, 1, 0, 2} - var o optRequestedOption - err := o.FromBytes(expected) - require.NoError(t, err, "ParseOptRequestedOption() correct options should not error") -} - -func TestOptRequestedOptionParseOptRequestedOptionTooShort(t *testing.T) { - buf := []byte{0, 1, 0} - var o optRequestedOption - err := o.FromBytes(buf) - require.Error(t, err, "A short option should return an error (must be divisible by 2)") -} - func TestOptRequestedOptionString(t *testing.T) { buf := []byte{0, 1, 0, 2} var o optRequestedOption |