summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_domainsearchlist_test.go
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 21:16:36 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commit7698adce8e9cb94d210c31c81591c9c158339b31 (patch)
tree5348de31f9977cfe6aa3635a96989da76e9f3659 /dhcpv6/option_domainsearchlist_test.go
parent667e3a9e71512220079a51820d57f9e20e9194db (diff)
DomainSearchList: tests for FromBytes, ToBytes, and Getter
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6/option_domainsearchlist_test.go')
-rw-r--r--dhcpv6/option_domainsearchlist_test.go105
1 files changed, 70 insertions, 35 deletions
diff --git a/dhcpv6/option_domainsearchlist_test.go b/dhcpv6/option_domainsearchlist_test.go
index c03759c..3881523 100644
--- a/dhcpv6/option_domainsearchlist_test.go
+++ b/dhcpv6/option_domainsearchlist_test.go
@@ -1,49 +1,84 @@
package dhcpv6
import (
+ "errors"
+ "fmt"
"testing"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
"github.com/insomniacslk/dhcp/rfc1035label"
"github.com/stretchr/testify/require"
+ "github.com/u-root/uio/uio"
)
-func TestParseOptDomainSearchList(t *testing.T) {
- data := []byte{
- 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
- 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0,
- }
- var opt optDomainSearchList
- err := opt.FromBytes(data)
- require.NoError(t, err)
- require.Equal(t, OptionDomainSearchList, opt.Code())
- require.Equal(t, 2, len(opt.DomainSearchList.Labels))
- require.Equal(t, "example.com", opt.DomainSearchList.Labels[0])
- require.Equal(t, "subnet.example.org", opt.DomainSearchList.Labels[1])
- require.Contains(t, opt.String(), "example.com subnet.example.org", "String() should contain the correct domain search output")
-}
-
-func TestOptDomainSearchListToBytes(t *testing.T) {
- expected := []byte{
- 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
- 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0,
- }
- opt := OptDomainSearchList(
- &rfc1035label.Labels{
- Labels: []string{
- "example.com",
- "subnet.example.org",
+func TestDomainSearchListParseAndGetter(t *testing.T) {
+ for i, tt := range []struct {
+ buf []byte
+ err error
+ want *rfc1035label.Labels
+ }{
+ {
+ buf: []byte{
+ 0, 24, // Domain Search List option
+ 0, 33, // length
+ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
+ 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0,
+ },
+ want: &rfc1035label.Labels{
+ Labels: []string{
+ "example.com",
+ "subnet.example.org",
+ },
},
},
- )
- require.Equal(t, expected, opt.ToBytes())
-}
+ {
+ buf: []byte{
+ 0, 24, // Domain Search List option
+ 0, 22, // length
+ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
+ 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', // truncated
+ },
+ err: rfc1035label.ErrBufferTooShort,
+ },
+ {
+ buf: nil,
+ want: nil,
+ },
+ {
+ buf: []byte{0, 24, 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)
+ }
+ got := mo.DomainSearchList()
+ if !cmp.Equal(got, tt.want, cmpopts.IgnoreUnexported(rfc1035label.Labels{})) {
+ t.Errorf("DomainSearchList = %v, want %v", got, tt.want)
+ }
-func TestParseOptDomainSearchListInvalidLength(t *testing.T) {
- data := []byte{
- 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
- 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', // truncated
+ if tt.want != nil {
+ var m MessageOptions
+ m.Add(OptDomainSearchList(tt.want))
+ got := m.ToBytes()
+ if diff := cmp.Diff(tt.buf, got); diff != "" {
+ t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
+ }
+ }
+ })
}
- var opt optDomainSearchList
- err := opt.FromBytes(data)
- require.Error(t, err, "A truncated OptDomainSearchList should return an error")
+}
+
+func TestOptDomainSearchListString(t *testing.T) {
+ opt := OptDomainSearchList(&rfc1035label.Labels{
+ Labels: []string{
+ "example.com",
+ "subnet.example.org",
+ },
+ })
+ require.Contains(t, opt.String(), "example.com subnet.example.org", "String() should contain the correct domain search output")
}