summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6
diff options
context:
space:
mode:
authorChris Koch <chrisko@google.com>2023-02-25 19:56:45 -0800
committerChris K <c@chrisko.ch>2023-02-27 10:35:19 -0800
commit2c09f487ca14542f3338f631e0d2787c30fc43b6 (patch)
treea70d3196fac5ba63b0aa4dc17920bbf33fdd1c89 /dhcpv6
parentfb47092b04d7e230f4025f210d2389ded83e8b39 (diff)
IAPD: tests for FromBytes, ToBytes, and IAPD Getter
Signed-off-by: Chris Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6')
-rw-r--r--dhcpv6/option_iapd_test.go259
1 files changed, 153 insertions, 106 deletions
diff --git a/dhcpv6/option_iapd_test.go b/dhcpv6/option_iapd_test.go
index 398a23e..da72f0c 100644
--- a/dhcpv6/option_iapd_test.go
+++ b/dhcpv6/option_iapd_test.go
@@ -1,125 +1,172 @@
package dhcpv6
import (
+ "errors"
+ "fmt"
"net"
"reflect"
"testing"
"time"
+ "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
+ "github.com/u-root/uio/uio"
)
-func TestParseMessageWithIAPD(t *testing.T) {
- data := []byte{
- 0, 25, // IAPD option code
- 0, 41, // length
- 1, 0, 0, 0, // IAID
- 0, 0, 0, 1, // T1
- 0, 0, 0, 2, // T2
- 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
- 0, 0, 0, 2, // IAPrefix preferredLifetime
- 0, 0, 0, 4, // IAPrefix validLifetime
- 36, // IAPrefix prefixLength
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
- }
- var got MessageOptions
- if err := got.FromBytes(data); err != nil {
- t.Errorf("FromBytes = %v", err)
- }
-
- want := &OptIAPD{
- IaId: [4]byte{1, 0, 0, 0},
- T1: 1 * time.Second,
- T2: 2 * time.Second,
- Options: PDOptions{Options: Options{&OptIAPrefix{
- PreferredLifetime: 2 * time.Second,
- ValidLifetime: 4 * time.Second,
- Prefix: &net.IPNet{
- Mask: net.CIDRMask(36, 128),
- IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+func TestIAPDParseAndGetter(t *testing.T) {
+ for i, tt := range []struct {
+ buf []byte
+ err error
+ want []*OptIAPD
+ }{
+ {
+ buf: []byte{
+ 0, 25, // IAPD option code
+ 0, 41, // length
+ 1, 0, 0, 0, // IAID
+ 0, 0, 0, 1, // T1
+ 0, 0, 0, 2, // T2
+ 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
+ 0, 0, 0, 2, // IAPrefix preferredLifetime
+ 0, 0, 0, 4, // IAPrefix validLifetime
+ 36, // IAPrefix prefixLength
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
},
- Options: PrefixOptions{Options: Options{}},
- }}},
- }
- if gotIAPD := got.OneIAPD(); !reflect.DeepEqual(gotIAPD, want) {
- t.Errorf("OneIAPD = %v, want %v", gotIAPD, want)
- }
-}
-
-func TestOptIAPDParseOptIAPD(t *testing.T) {
- data := []byte{
- 1, 0, 0, 0, // IAID
- 0, 0, 0, 1, // T1
- 0, 0, 0, 2, // T2
- 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
- 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
- 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
- 36, // IAPrefix prefixLength
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
- }
- var opt OptIAPD
- err := opt.FromBytes(data)
- require.NoError(t, err)
- require.Equal(t, OptionIAPD, opt.Code())
- require.Equal(t, [4]byte{1, 0, 0, 0}, opt.IaId)
- require.Equal(t, time.Second, opt.T1)
- require.Equal(t, 2*time.Second, opt.T2)
-}
-
-func TestOptIAPDParseOptIAPDInvalidLength(t *testing.T) {
- data := []byte{
- 1, 0, 0, 0, // IAID
- 0, 0, 0, 1, // T1
- // truncated from here
- }
- var opt OptIAPD
- err := opt.FromBytes(data)
- require.Error(t, err)
-}
-
-func TestOptIAPDParseOptIAPDInvalidOptions(t *testing.T) {
- data := []byte{
- 1, 0, 0, 0, // IAID
- 0, 0, 0, 1, // T1
- 0, 0, 0, 2, // T2
- 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
- 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
- 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
- 36, // IAPrefix prefixLength
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // IAPrefix ipv6Prefix missing last byte
- }
- var opt OptIAPD
- err := opt.FromBytes(data)
- require.Error(t, err)
-}
+ want: []*OptIAPD{
+ &OptIAPD{
+ IaId: [4]byte{1, 0, 0, 0},
+ T1: 1 * time.Second,
+ T2: 2 * time.Second,
+ Options: PDOptions{Options: Options{&OptIAPrefix{
+ PreferredLifetime: 2 * time.Second,
+ ValidLifetime: 4 * time.Second,
+ Prefix: &net.IPNet{
+ Mask: net.CIDRMask(36, 128),
+ IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+ },
+ Options: PrefixOptions{Options: Options{}},
+ }}},
+ },
+ },
+ },
+ {
+ buf: []byte{
+ 0, 25, // IAPD option code
+ 0, 41, // length
+ 1, 0, 0, 0, // IAID
+ 0, 0, 0, 1, // T1
+ 0, 0, 0, 2, // T2
+ 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
+ 0, 0, 0, 2, // IAPrefix preferredLifetime
+ 0, 0, 0, 4, // IAPrefix validLifetime
+ 36, // IAPrefix prefixLength
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
-func TestOptIAPDToBytes(t *testing.T) {
- oaddr := OptIAPrefix{
- PreferredLifetime: 0xaabbccdd * time.Second,
- ValidLifetime: 0xeeff0011 * time.Second,
- Prefix: &net.IPNet{
- Mask: net.CIDRMask(36, 128),
- IP: net.IPv6loopback,
+ 0, 25, // IAPD option code
+ 0, 41, // length
+ 1, 2, 3, 4, // IAID
+ 0, 0, 0, 5, // T1
+ 0, 0, 0, 6, // T2
+ 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
+ 0, 0, 0, 2, // IAPrefix preferredLifetime
+ 0, 0, 0, 4, // IAPrefix validLifetime
+ 36, // IAPrefix prefixLength
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
+ },
+ want: []*OptIAPD{
+ &OptIAPD{
+ IaId: [4]byte{1, 0, 0, 0},
+ T1: 1 * time.Second,
+ T2: 2 * time.Second,
+ Options: PDOptions{Options: Options{&OptIAPrefix{
+ PreferredLifetime: 2 * time.Second,
+ ValidLifetime: 4 * time.Second,
+ Prefix: &net.IPNet{
+ Mask: net.CIDRMask(36, 128),
+ IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+ },
+ Options: PrefixOptions{Options: Options{}},
+ }}},
+ },
+ &OptIAPD{
+ IaId: [4]byte{1, 2, 3, 4},
+ T1: 5 * time.Second,
+ T2: 6 * time.Second,
+ Options: PDOptions{Options: Options{&OptIAPrefix{
+ PreferredLifetime: 2 * time.Second,
+ ValidLifetime: 4 * time.Second,
+ Prefix: &net.IPNet{
+ Mask: net.CIDRMask(36, 128),
+ IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+ },
+ Options: PrefixOptions{Options: Options{}},
+ }}},
+ },
+ },
},
- }
- opt := OptIAPD{
- IaId: [4]byte{1, 2, 3, 4},
- T1: 12345 * time.Second,
- T2: 54321 * time.Second,
- Options: PDOptions{[]Option{&oaddr}},
- }
+ {
+ buf: nil,
+ want: nil,
+ },
+ {
+ buf: []byte{0, 25, 0, 1, 0},
+ want: nil,
+ err: uio.ErrBufferTooShort,
+ },
+ {
+ buf: []byte{
+ 0, 25, // IAPD option code
+ 0, 8, // length
+ 1, 0, 0, 0, // IAID
+ 0, 0, 0, 1, // T1
+ // truncated from here
+ },
+ want: nil,
+ err: uio.ErrBufferTooShort,
+ },
+ {
+ buf: []byte{
+ 0, 25, // IAPD option code
+ 0, 36, // length
+ 1, 0, 0, 0, // IAID
+ 0, 0, 0, 1, // T1
+ 0, 0, 0, 2, // T2
+ 0, 26, 0, 4, // 26 = IAPrefix Option, 4 = length
+ 0, 0, 0, 2, // IAPrefix preferredLifetime
+ // Missing stuff
+ },
+ want: nil,
+ err: uio.ErrBufferTooShort,
+ },
+ } {
+ 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.IAPD(); !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("IAPD = %v, want %v", got, tt.want)
+ }
+ var wantOne *OptIAPD
+ if len(tt.want) >= 1 {
+ wantOne = tt.want[0]
+ }
+ if got := mo.OneIAPD(); !reflect.DeepEqual(got, wantOne) {
+ t.Errorf("OneIAPD = %v, want %v", got, wantOne)
+ }
- expected := []byte{
- 1, 2, 3, 4, // IA ID
- 0, 0, 0x30, 0x39, // T1 = 12345
- 0, 0, 0xd4, 0x31, // T2 = 54321
- 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
- 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
- 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
- 36, // IAPrefix prefixLength
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
+ if len(tt.want) >= 1 {
+ var b MessageOptions
+ for _, iana := range tt.want {
+ b.Add(iana)
+ }
+ got := b.ToBytes()
+ if diff := cmp.Diff(tt.buf, got); diff != "" {
+ t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
+ }
+ }
+ })
}
- require.Equal(t, expected, opt.ToBytes())
}
func TestOptIAPDString(t *testing.T) {