summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 22:51:59 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commitd5e54b6d24ce62c4333daa11463251c83f5a0b73 (patch)
treeb0988df57d1db29e3243ca729933abba2e4cb938
parent63d2cd37109b243c57eb445b93076b08ebff7748 (diff)
FQDN: tests for FromBytes, ToBytes, and Getter
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r--dhcpv6/option_fqdn_test.go97
1 files changed, 71 insertions, 26 deletions
diff --git a/dhcpv6/option_fqdn_test.go b/dhcpv6/option_fqdn_test.go
index 0bb2ab4..6fca08d 100644
--- a/dhcpv6/option_fqdn_test.go
+++ b/dhcpv6/option_fqdn_test.go
@@ -1,43 +1,88 @@
package dhcpv6
import (
- "bytes"
+ "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 TestParseOptFQDN(t *testing.T) {
- data := []byte{
- 0, // Flags
- 4, 'c', 'n', 'o', 's', 9, 'l', 'o', 'c', 'a', 'l',
- 'h', 'o', 's', 't', 0,
- }
- var opt OptFQDN
- err := opt.FromBytes(data)
+func TestFQDNParseAndGetter(t *testing.T) {
+ for i, tt := range []struct {
+ buf []byte
+ err error
+ want *OptFQDN
+ }{
+ {
+ buf: []byte{
+ 0, 39, // FQDN option
+ 0, 34, // length
+ 0, // flags
+ 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: &OptFQDN{
+ Flags: 0,
+ DomainName: &rfc1035label.Labels{
+ Labels: []string{
+ "example.com",
+ "subnet.example.org",
+ },
+ },
+ },
+ },
+ {
+ buf: []byte{
+ 0, 39, // FQDN
+ 0, 23, // length
+ 0, // flags
+ 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, 39, 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.FQDN()
+ if !cmp.Equal(got, tt.want, cmpopts.IgnoreUnexported(rfc1035label.Labels{})) {
+ t.Errorf("FQDN = %v, want %v", got, tt.want)
+ }
- require.NoError(t, err)
- require.Equal(t, OptionFQDN, opt.Code())
- require.Equal(t, uint8(0), opt.Flags)
- require.Equal(t, "cnos.localhost", opt.DomainName.Labels[0])
- require.Equal(t, "FQDN: {Flags=0 DomainName=[cnos.localhost]}", opt.String())
+ 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)
+ }
+ }
+ })
+ }
}
-func TestOptFQDNToBytes(t *testing.T) {
- opt := OptFQDN{
- Flags: 0,
+func TestOptFQDNString(t *testing.T) {
+ opt := &OptFQDN{
DomainName: &rfc1035label.Labels{
Labels: []string{"cnos.localhost"},
},
}
- want := []byte{
- 0, // Flags
- 4, 'c', 'n', 'o', 's', 9, 'l', 'o', 'c', 'a', 'l',
- 'h', 'o', 's', 't', 0,
- }
- b := opt.ToBytes()
- if !bytes.Equal(b, want) {
- t.Fatalf("opt.ToBytes()=%v, want %v", b, want)
- }
+ require.Equal(t, "FQDN: {Flags=0 DomainName=[cnos.localhost]}", opt.String())
}