1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
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
0, 2, // length
0, 3, // IANA Option
0, 6, // ORO
0, 2, // length
0, 4, // IATA
}
want := OptionCodes{OptionIANA, OptionIATA}
var mo MessageOptions
if err := mo.FromBytes(buf); err != nil {
t.Errorf("FromBytes = %v", err)
} else if got := mo.RequestedOptions(); !reflect.DeepEqual(got, want) {
t.Errorf("ORO = %v, want %v", got, want)
}
}
func TestOptRequestedOptionString(t *testing.T) {
buf := []byte{0, 1, 0, 2}
var o optRequestedOption
err := o.FromBytes(buf)
require.NoError(t, err)
require.Contains(
t,
o.String(),
"Client ID, Server ID",
"String() should contain the options specified",
)
o.OptionCodes = append(o.OptionCodes, 12345)
require.Contains(
t,
o.String(),
"unknown",
"String() should contain 'Unknown' for an illegal option",
)
}
|