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
|
package dhcpv4
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParseOption(t *testing.T) {
option := []byte{5, 4, 192, 168, 1, 254} // DNS option
opt, err := ParseOption(option)
require.NoError(t, err)
generic := opt.(*OptionGeneric)
require.Equal(t, OptionNameServer, generic.Code())
require.Equal(t, []byte{192, 168, 1, 254}, generic.Data)
require.Equal(t, 4, generic.Length())
require.Equal(t, "Name Server -> [192 168 1 254]", generic.String())
}
func TestParseOptionZeroLength(t *testing.T) {
option := []byte{}
_, err := ParseOption(option)
require.Error(t, err, "should get error from zero-length options")
}
func TestParseOptionShortOption(t *testing.T) {
option := []byte{53, 1}
_, err := ParseOption(option)
require.Error(t, err, "should get error from short options")
}
func TestOptionsFromBytes(t *testing.T) {
options := []byte{
99, 130, 83, 99, // Magic Cookie
5, 4, 192, 168, 1, 1, // DNS
255, // end
0, 0, 0, //padding
}
opts, err := OptionsFromBytes(options)
require.NoError(t, err)
require.Equal(t, 5, len(opts))
require.Equal(t, opts[0].(*OptionGeneric), &OptionGeneric{OptionCode: OptionNameServer, Data: []byte{192, 168, 1, 1}})
require.Equal(t, opts[1].(*OptionGeneric), &OptionGeneric{OptionCode: OptionEnd})
require.Equal(t, opts[2].(*OptionGeneric), &OptionGeneric{OptionCode: OptionPad})
require.Equal(t, opts[3].(*OptionGeneric), &OptionGeneric{OptionCode: OptionPad})
require.Equal(t, opts[4].(*OptionGeneric), &OptionGeneric{OptionCode: OptionPad})
}
func TestOptionsFromBytesZeroLength(t *testing.T) {
options := []byte{}
_, err := OptionsFromBytes(options)
require.Error(t, err)
}
func TestOptionsFromBytesBadMagicCookie(t *testing.T) {
options := []byte{1, 2, 3, 4}
_, err := OptionsFromBytes(options)
require.Error(t, err)
}
func TestOptionsFromBytesShortOption(t *testing.T) {
options := []byte{
99, 130, 83, 99, // Magic Cookie
5, 4, 192, 168, // DNS
}
_, err := OptionsFromBytes(options)
require.Error(t, err)
}
|