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
|
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 TestRemoteIDParseAndGetter(t *testing.T) {
for i, tt := range []struct {
buf []byte
err error
want *OptRemoteID
}{
{
buf: []byte{
0, 37, // Remote ID
0, 8, // length
0, 0, 0, 16,
'S', 'L', 'A', 'M',
},
want: &OptRemoteID{
EnterpriseNumber: 16,
RemoteID: []byte("SLAM"),
},
},
{
buf: []byte{
0, 37,
0, 0,
},
err: uio.ErrBufferTooShort,
},
{
buf: []byte{
0, 37,
0, 4,
0, 0, 0, 6,
},
want: &OptRemoteID{
EnterpriseNumber: 6,
RemoteID: []byte{},
},
},
{
buf: []byte{0, 37, 0},
err: uio.ErrUnreadBytes,
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
var ro RelayOptions
if err := ro.FromBytes(tt.buf); !errors.Is(err, tt.err) {
t.Errorf("FromBytes = %v, want %v", err, tt.err)
}
if got := ro.RemoteID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoteID = %v, want %v", got, tt.want)
}
if tt.want != nil {
var m RelayOptions
m.Add(tt.want)
got := m.ToBytes()
if diff := cmp.Diff(tt.buf, got); diff != "" {
t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
}
}
})
}
}
func TestOptRemoteIDString(t *testing.T) {
opt := &OptRemoteID{
EnterpriseNumber: 123,
RemoteID: []byte("Test1234"),
}
str := opt.String()
require.Contains(
t,
str,
"EnterpriseNumber=123",
"String() should contain the enterprisenum",
)
require.Contains(
t,
str,
"RemoteID=0x5465737431323334",
"String() should contain the remoteid bytes",
)
}
|