2021-09-13 20:05:41 +03:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-26 14:30:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-09-13 20:05:41 +03:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseOpt(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
in string
|
|
|
|
wantOpt dhcpv4.Option
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg string
|
2021-09-13 20:05:41 +03:00
|
|
|
}{{
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "hex_success",
|
|
|
|
in: "6 hex c0a80101c0a80102",
|
|
|
|
wantOpt: dhcpv4.OptGeneric(
|
|
|
|
dhcpv4.GenericOptionCode(6),
|
|
|
|
[]byte{
|
|
|
|
0xC0, 0xA8, 0x01, 0x01,
|
|
|
|
0xC0, 0xA8, 0x01, 0x02,
|
|
|
|
},
|
2021-09-13 20:05:41 +03:00
|
|
|
),
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "ip_success",
|
|
|
|
in: "6 ip 1.2.3.4",
|
|
|
|
wantOpt: dhcpv4.Option{
|
|
|
|
Code: dhcpv4.GenericOptionCode(6),
|
|
|
|
Value: dhcpv4.IP(net.IP{0x01, 0x02, 0x03, 0x04}),
|
|
|
|
},
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "ip_fail_v6",
|
|
|
|
in: "6 ip ::1234",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "invalid option string \"6 ip ::1234\": bad ipv4 address \"::1234\"",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "ips_success",
|
|
|
|
in: "6 ips 192.168.1.1,192.168.1.2",
|
|
|
|
wantOpt: dhcpv4.Option{
|
|
|
|
Code: dhcpv4.GenericOptionCode(6),
|
|
|
|
Value: dhcpv4.IPs([]net.IP{
|
|
|
|
{0xC0, 0xA8, 0x01, 0x01},
|
|
|
|
{0xC0, 0xA8, 0x01, 0x02},
|
|
|
|
}),
|
|
|
|
},
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "text_success",
|
|
|
|
in: "252 text http://192.168.1.1/",
|
2021-09-13 20:05:41 +03:00
|
|
|
wantOpt: dhcpv4.OptGeneric(
|
|
|
|
dhcpv4.GenericOptionCode(252),
|
|
|
|
[]byte("http://192.168.1.1/"),
|
|
|
|
),
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "del_success",
|
|
|
|
in: "61 del",
|
|
|
|
wantOpt: dhcpv4.Option{
|
|
|
|
Code: dhcpv4.GenericOptionCode(dhcpv4.OptionClientIdentifier),
|
|
|
|
Value: dhcpv4.OptionGeneric{Data: nil},
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
|
|
|
name: "bad_parts",
|
|
|
|
in: "6 ip",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: `invalid option string "6 ip": bad option format`,
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "bad_code",
|
|
|
|
in: "256 ip 1.1.1.1",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: `invalid option string "256 ip 1.1.1.1": parsing option code: ` +
|
|
|
|
`strconv.ParseUint: parsing "256": value out of range`,
|
|
|
|
}, {
|
|
|
|
name: "bad_type",
|
|
|
|
in: "6 bad 1.1.1.1",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: `invalid option string "6 bad 1.1.1.1": unknown option type "bad"`,
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "hex_error",
|
|
|
|
in: "6 hex ZZZ",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: `invalid option string "6 hex ZZZ": decoding hex: ` +
|
|
|
|
`encoding/hex: invalid byte: U+005A 'Z'`,
|
|
|
|
}, {
|
|
|
|
name: "ip_error",
|
|
|
|
in: "6 ip 1.2.3.x",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "invalid option string \"6 ip 1.2.3.x\": bad ipv4 address \"1.2.3.x\"",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "ips_error",
|
|
|
|
in: "6 ips 192.168.1.1,192.168.1.x",
|
|
|
|
wantOpt: dhcpv4.Option{},
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: "invalid option string \"6 ips 192.168.1.1,192.168.1.x\": " +
|
|
|
|
"parsing ip at index 1: bad ipv4 address \"192.168.1.x\"",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
opt, err := parseDHCPOption(tc.in)
|
2022-08-26 14:30:31 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-09-13 20:05:41 +03:00
|
|
|
|
2022-08-26 14:30:31 +03:00
|
|
|
// assert.Equal(t, tc.wantOpt.Code.Code(), opt.Code.Code())
|
|
|
|
// assert.Equal(t, tc.wantOpt.Value.ToBytes(), opt.Value.ToBytes())
|
|
|
|
assert.Equal(t, tc.wantOpt, opt)
|
2021-09-13 20:05:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareOptions(t *testing.T) {
|
|
|
|
allDefault := dhcpv4.Options{
|
|
|
|
dhcpv4.OptionNonLocalSourceRouting.Code(): []byte{0},
|
|
|
|
dhcpv4.OptionDefaultIPTTL.Code(): []byte{64},
|
|
|
|
dhcpv4.OptionPerformMaskDiscovery.Code(): []byte{0},
|
|
|
|
dhcpv4.OptionMaskSupplier.Code(): []byte{0},
|
|
|
|
dhcpv4.OptionPerformRouterDiscovery.Code(): []byte{1},
|
|
|
|
dhcpv4.OptionRouterSolicitationAddress.Code(): []byte{224, 0, 0, 2},
|
|
|
|
dhcpv4.OptionBroadcastAddress.Code(): []byte{255, 255, 255, 255},
|
|
|
|
dhcpv4.OptionTrailerEncapsulation.Code(): []byte{0},
|
|
|
|
dhcpv4.OptionEthernetEncapsulation.Code(): []byte{0},
|
|
|
|
dhcpv4.OptionTCPKeepaliveInterval.Code(): []byte{0, 0, 0, 0},
|
|
|
|
dhcpv4.OptionTCPKeepaliveGarbage.Code(): []byte{0},
|
|
|
|
}
|
|
|
|
oneIP, otherIP := net.IP{1, 2, 3, 4}, net.IP{5, 6, 7, 8}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
checks dhcpv4.Options
|
2022-08-26 14:30:31 +03:00
|
|
|
opts []string
|
2021-09-13 20:05:41 +03:00
|
|
|
}{{
|
|
|
|
name: "all_default",
|
|
|
|
checks: allDefault,
|
2022-08-26 14:30:31 +03:00
|
|
|
opts: nil,
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
|
|
|
name: "configured_ip",
|
|
|
|
checks: dhcpv4.Options{
|
|
|
|
dhcpv4.OptionBroadcastAddress.Code(): oneIP,
|
|
|
|
},
|
|
|
|
opts: []string{
|
2022-08-26 14:30:31 +03:00
|
|
|
fmt.Sprintf("%d ip %s", dhcpv4.OptionBroadcastAddress, oneIP),
|
2021-09-13 20:05:41 +03:00
|
|
|
},
|
2022-08-26 14:30:31 +03:00
|
|
|
}, {
|
|
|
|
name: "configured_ips",
|
2021-09-13 20:05:41 +03:00
|
|
|
checks: dhcpv4.Options{
|
|
|
|
dhcpv4.OptionDomainNameServer.Code(): append(oneIP, otherIP...),
|
|
|
|
},
|
2022-08-26 14:30:31 +03:00
|
|
|
opts: []string{
|
|
|
|
fmt.Sprintf("%d ips %s,%s", dhcpv4.OptionDomainNameServer, oneIP, otherIP),
|
|
|
|
},
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-08-26 14:30:31 +03:00
|
|
|
name: "configured_bad",
|
|
|
|
checks: allDefault,
|
2021-09-13 20:05:41 +03:00
|
|
|
opts: []string{
|
|
|
|
"20 hex",
|
|
|
|
"23 hex abc",
|
|
|
|
"32 ips 1,2,3,4",
|
|
|
|
"28 256.256.256.256",
|
|
|
|
},
|
2022-08-26 14:30:31 +03:00
|
|
|
}, {
|
|
|
|
name: "configured_del",
|
|
|
|
checks: dhcpv4.Options{
|
|
|
|
dhcpv4.OptionBroadcastAddress.Code(): nil,
|
|
|
|
},
|
|
|
|
opts: []string{
|
|
|
|
"28 del",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "rewritten_del",
|
|
|
|
checks: dhcpv4.Options{
|
|
|
|
dhcpv4.OptionBroadcastAddress.Code(): []byte{255, 255, 255, 255},
|
|
|
|
},
|
|
|
|
opts: []string{
|
|
|
|
"28 del",
|
|
|
|
"28 ip 255.255.255.255",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "configured_and_del",
|
|
|
|
checks: dhcpv4.Options{
|
|
|
|
123: []byte("cba"),
|
|
|
|
},
|
|
|
|
opts: []string{
|
|
|
|
"123 text abc",
|
|
|
|
"123 del",
|
|
|
|
"123 text cba",
|
|
|
|
},
|
2021-09-13 20:05:41 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-08-26 14:30:31 +03:00
|
|
|
if tc.name == "configured_del" {
|
|
|
|
assert.True(t, true)
|
|
|
|
}
|
2021-09-13 20:05:41 +03:00
|
|
|
opts := prepareOptions(V4ServerConf{
|
|
|
|
// Just to avoid nil pointer dereference.
|
|
|
|
subnet: &net.IPNet{},
|
|
|
|
Options: tc.opts,
|
|
|
|
})
|
|
|
|
for c, v := range tc.checks {
|
2022-08-26 14:30:31 +03:00
|
|
|
val := opts.Get(dhcpv4.GenericOptionCode(c))
|
|
|
|
assert.Lenf(t, val, len(v), "Code: %v", c)
|
|
|
|
assert.Equal(t, v, val)
|
2021-09-13 20:05:41 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|