diff options
author | Chris Koch <chrisko@google.com> | 2023-02-25 20:18:28 -0800 |
---|---|---|
committer | Chris K <c@chrisko.ch> | 2023-02-27 10:35:19 -0800 |
commit | d686b3a94668112a88ed62386c84c64ed51670f4 (patch) | |
tree | cce7e8b4d8f3fe14caf525e42265c98f7e3237db /dhcpv6 | |
parent | 35f1aa318ee0ad26084ca7642ba5102539a7d7a9 (diff) |
Status: tests for FromBytes, ToBytes, and Status Getter
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_statuscode_test.go | 82 |
1 files changed, 55 insertions, 27 deletions
diff --git a/dhcpv6/option_statuscode_test.go b/dhcpv6/option_statuscode_test.go index 4582909..3c99e4f 100644 --- a/dhcpv6/option_statuscode_test.go +++ b/dhcpv6/option_statuscode_test.go @@ -1,41 +1,69 @@ 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 TestParseOptStatusCode(t *testing.T) { - data := []byte{ - 0, 5, // StatusUseMulticast - 'u', 's', 'e', ' ', 'm', 'u', 'l', 't', 'i', 'c', 'a', 's', 't', - } - var opt OptStatusCode - err := opt.FromBytes(data) - require.NoError(t, err) - require.Equal(t, iana.StatusUseMulticast, opt.StatusCode) - require.Equal(t, "use multicast", opt.StatusMessage) -} +func TestStatusCodeParseAndGetter(t *testing.T) { + for i, tt := range []struct { + buf []byte + err error + want *OptStatusCode + }{ + { + buf: []byte{ + 0, 13, // StatusCode option + 0, 15, // length + 0, 5, // StatusUseMulticast + 'u', 's', 'e', ' ', 'm', 'u', 'l', 't', 'i', 'c', 'a', 's', 't', + }, + want: &OptStatusCode{ + StatusCode: iana.StatusUseMulticast, + StatusMessage: "use multicast", + }, + }, + { + buf: nil, + want: nil, + }, + { + buf: []byte{0, 13, 0, 1, 0}, + want: nil, + err: uio.ErrBufferTooShort, + }, + { + buf: []byte{0, 13, 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.Status(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Status = %v, want %v", got, tt.want) + } -func TestOptStatusCodeToBytes(t *testing.T) { - expected := []byte{ - 0, 0, // StatusSuccess - 's', 'u', 'c', 'c', 'e', 's', 's', - } - opt := OptStatusCode{ - StatusCode: iana.StatusSuccess, - StatusMessage: "success", + if tt.want != nil { + var m MessageOptions + m.Add(tt.want) + got := m.ToBytes() + if diff := cmp.Diff(tt.buf, got); diff != "" { + t.Errorf("ToBytes mismatch (-want, +got): %s", diff) + } + } + }) } - actual := opt.ToBytes() - require.Equal(t, expected, actual) -} - -func TestOptStatusCodeParseOptStatusCodeTooShort(t *testing.T) { - var opt OptStatusCode - err := opt.FromBytes([]byte{0}) - require.Error(t, err, "ParseOptStatusCode: Expected error on truncated option") } func TestOptStatusCodeString(t *testing.T) { |