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
|
package dhcpv6
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
"github.com/u-root/uio/uio"
)
type optionsWithStatusCode interface {
Status() *OptStatusCode
ToBytes() []byte
}
type optionsPtr[O any] interface {
*O
FromBytes([]byte) error
Add(o Option)
}
type testCase struct {
buf []byte
err error
want *OptStatusCode
}
func testParseStatus[MO optionsWithStatusCode, OA optionsPtr[MO]](t *testing.T, tt testCase) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
var mo MO
if err := OA(&mo).FromBytes(tt.buf); !errors.Is(err, tt.err) {
t.Errorf("FromBytes = %v, want %v", err, tt.err)
}
if got := mo.Status(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Status = %v, want %v", got, tt.want)
}
if tt.want != nil {
var m MO
OA(&m).Add(tt.want)
got := m.ToBytes()
if diff := cmp.Diff(tt.buf, got); diff != "" {
t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
}
}
}
}
func TestStatusCodeParseAndGetter(t *testing.T) {
for i, tt := range []testCase{
{
buf: []byte{
0, 13, // StatusCode option
0, 15, // length
0, 5, // StatusUseMulticast
'u', 's', 'e', ' ', 'm', 'u', 'l', 't', 'i', 'c', 'a', 's', 't',
},
want: &OptStatusCode{
StatusCode: iana.StatusUseMulticast,
StatusMessage: "use multicast",
},
},
{
buf: nil,
want: nil,
},
{
buf: []byte{0, 13, 0, 1, 0},
want: nil,
err: uio.ErrBufferTooShort,
},
{
buf: []byte{0, 13, 0},
want: nil,
err: uio.ErrUnreadBytes,
},
} {
t.Run(fmt.Sprintf("MO-%d", i), testParseStatus[MessageOptions, *MessageOptions](t, tt))
t.Run(fmt.Sprintf("IO-%d", i), testParseStatus[IdentityOptions, *IdentityOptions](t, tt))
t.Run(fmt.Sprintf("AO-%d", i), testParseStatus[AddressOptions, *AddressOptions](t, tt))
t.Run(fmt.Sprintf("PDO-%d", i), testParseStatus[PDOptions, *PDOptions](t, tt))
t.Run(fmt.Sprintf("PO-%d", i), testParseStatus[PrefixOptions, *PrefixOptions](t, tt))
}
}
func TestOptStatusCodeString(t *testing.T) {
data := []byte{
0, 5, // StatusUseMulticast
'u', 's', 'e', ' ', 'm', 'u', 'l', 't', 'i', 'c', 'a', 's', 't',
}
var opt OptStatusCode
err := opt.FromBytes(data)
require.NoError(t, err)
require.Contains(
t,
opt.String(),
"Code=UseMulticast (5); Message=use multicast",
"String() should contain the code and message",
)
}
|