2022-09-13 20:06:23 +03:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
2021-09-13 20:05:41 +03:00
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"testing"
|
2022-09-05 17:01:33 +03:00
|
|
|
"time"
|
2021-09-13 20:05:41 +03:00
|
|
|
|
2022-09-05 17:01:33 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
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
|
2022-09-05 17:01:33 +03:00
|
|
|
wantCode dhcpv4.OptionCode
|
|
|
|
wantVal dhcpv4.OptionValue
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg string
|
2021-09-13 20:05:41 +03:00
|
|
|
}{{
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "hex_success",
|
|
|
|
in: "6 hex c0a80101c0a80102",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(6),
|
|
|
|
wantVal: dhcpv4.OptionGeneric{Data: []byte{
|
|
|
|
0xC0, 0xA8, 0x01, 0x01,
|
|
|
|
0xC0, 0xA8, 0x01, 0x02,
|
|
|
|
}},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "ip_success",
|
|
|
|
in: "6 ip 1.2.3.4",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(6),
|
|
|
|
wantVal: dhcpv4.IP(net.IP{0x01, 0x02, 0x03, 0x04}),
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "ips_success",
|
|
|
|
in: "6 ips 192.168.1.1,192.168.1.2",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(6),
|
|
|
|
wantVal: dhcpv4.IPs([]net.IP{
|
|
|
|
{0xC0, 0xA8, 0x01, 0x01},
|
|
|
|
{0xC0, 0xA8, 0x01, 0x02},
|
|
|
|
}),
|
|
|
|
wantErrMsg: "",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "text_success",
|
|
|
|
in: "252 text http://192.168.1.1/",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(252),
|
|
|
|
wantVal: dhcpv4.String("http://192.168.1.1/"),
|
2021-09-13 20:05:41 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "del_success",
|
|
|
|
in: "61 del",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionClientIdentifier),
|
|
|
|
wantVal: dhcpv4.OptionGeneric{Data: nil},
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "bool_success",
|
|
|
|
in: "19 bool true",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionIPForwarding),
|
|
|
|
wantVal: dhcpv4.OptionGeneric{Data: []byte{0x01}},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "bool_success_false",
|
|
|
|
in: "19 bool F",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionIPForwarding),
|
|
|
|
wantVal: dhcpv4.OptionGeneric{Data: []byte{0x00}},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "dur_success",
|
|
|
|
in: "24 dur 2h5s",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionPathMTUAgingTimeout),
|
|
|
|
wantVal: dhcpv4.Duration(2*time.Hour + 5*time.Second),
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "u8_success",
|
|
|
|
in: "23 u8 64",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionDefaultIPTTL),
|
|
|
|
wantVal: dhcpv4.OptionGeneric{Data: []byte{0x40}},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "u16_success",
|
|
|
|
in: "22 u16 1234",
|
|
|
|
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionMaximumDatagramAssemblySize),
|
|
|
|
wantVal: dhcpv4.Uint16(1234),
|
2022-08-26 14:30:31 +03:00
|
|
|
wantErrMsg: "",
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
|
|
|
name: "bad_parts",
|
|
|
|
in: "6 ip",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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-09-05 17:01:33 +03:00
|
|
|
name: "bad_code",
|
|
|
|
in: "256 ip 1.1.1.1",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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-09-05 17:01:33 +03:00
|
|
|
name: "hex_error",
|
|
|
|
in: "6 hex ZZZ",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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-09-05 17:01:33 +03:00
|
|
|
name: "ip_error_v6",
|
|
|
|
in: "6 ip ::1234",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
|
|
|
wantErrMsg: "invalid option string \"6 ip ::1234\": bad ipv4 address \"::1234\"",
|
|
|
|
}, {
|
|
|
|
name: "ips_error",
|
|
|
|
in: "6 ips 192.168.1.1,192.168.1.x",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
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\"",
|
2022-09-05 17:01:33 +03:00
|
|
|
}, {
|
|
|
|
name: "bool_error",
|
|
|
|
in: "19 bool yes",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
|
|
|
wantErrMsg: "invalid option string \"19 bool yes\": decoding bool: " +
|
|
|
|
"strconv.ParseBool: parsing \"yes\": invalid syntax",
|
|
|
|
}, {
|
|
|
|
name: "dur_error",
|
|
|
|
in: "24 dur 3y",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
|
|
|
wantErrMsg: "invalid option string \"24 dur 3y\": decoding dur: " +
|
|
|
|
"unmarshaling duration: time: unknown unit \"y\" in duration \"3y\"",
|
|
|
|
}, {
|
|
|
|
name: "u8_error",
|
|
|
|
in: "23 u8 256",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
|
|
|
wantErrMsg: "invalid option string \"23 u8 256\": decoding u8: " +
|
|
|
|
"strconv.ParseUint: parsing \"256\": value out of range",
|
|
|
|
}, {
|
|
|
|
name: "u16_error",
|
|
|
|
in: "23 u16 65536",
|
|
|
|
wantCode: nil,
|
|
|
|
wantVal: nil,
|
|
|
|
wantErrMsg: "invalid option string \"23 u16 65536\": decoding u16: " +
|
|
|
|
"strconv.ParseUint: parsing \"65536\": value out of range",
|
2021-09-13 20:05:41 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-09-05 17:01:33 +03:00
|
|
|
code, val, 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-09-05 17:01:33 +03:00
|
|
|
assert.Equal(t, tc.wantCode, code)
|
|
|
|
assert.Equal(t, tc.wantVal, val)
|
2021-09-13 20:05:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareOptions(t *testing.T) {
|
|
|
|
oneIP, otherIP := net.IP{1, 2, 3, 4}, net.IP{5, 6, 7, 8}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-09-05 17:01:33 +03:00
|
|
|
name string
|
|
|
|
wantExplicit dhcpv4.Options
|
|
|
|
opts []string
|
2021-09-13 20:05:41 +03:00
|
|
|
}{{
|
2022-09-05 17:01:33 +03:00
|
|
|
name: "all_default",
|
|
|
|
wantExplicit: nil,
|
|
|
|
opts: nil,
|
2021-09-13 20:05:41 +03:00
|
|
|
}, {
|
|
|
|
name: "configured_ip",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantExplicit: dhcpv4.OptionsFromList(
|
|
|
|
dhcpv4.OptBroadcastAddress(oneIP),
|
|
|
|
),
|
2021-09-13 20:05:41 +03:00
|
|
|
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",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantExplicit: dhcpv4.OptionsFromList(
|
|
|
|
dhcpv4.Option{
|
|
|
|
Code: dhcpv4.OptionDomainNameServer,
|
|
|
|
Value: dhcpv4.IPs{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-09-05 17:01:33 +03:00
|
|
|
name: "configured_bad",
|
|
|
|
wantExplicit: nil,
|
2021-09-13 20:05:41 +03:00
|
|
|
opts: []string{
|
2022-09-05 17:01:33 +03:00
|
|
|
"19 bool yes",
|
|
|
|
"24 dur 3y",
|
|
|
|
"23 u8 256",
|
|
|
|
"23 u16 65536",
|
2021-09-13 20:05:41 +03:00
|
|
|
"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",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantExplicit: dhcpv4.OptionsFromList(
|
|
|
|
dhcpv4.OptBroadcastAddress(nil),
|
|
|
|
),
|
2022-08-26 14:30:31 +03:00
|
|
|
opts: []string{
|
|
|
|
"28 del",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "rewritten_del",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantExplicit: dhcpv4.OptionsFromList(
|
|
|
|
dhcpv4.OptBroadcastAddress(netutil.IPv4bcast()),
|
|
|
|
),
|
2022-08-26 14:30:31 +03:00
|
|
|
opts: []string{
|
|
|
|
"28 del",
|
|
|
|
"28 ip 255.255.255.255",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "configured_and_del",
|
2022-09-05 17:01:33 +03:00
|
|
|
wantExplicit: dhcpv4.OptionsFromList(
|
|
|
|
dhcpv4.Option{
|
|
|
|
Code: dhcpv4.OptionGeoConf,
|
|
|
|
Value: dhcpv4.String("cba"),
|
|
|
|
},
|
|
|
|
),
|
2022-08-26 14:30:31 +03:00
|
|
|
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-09-05 17:01:33 +03:00
|
|
|
implicit, explicit := prepareOptions(V4ServerConf{
|
2021-09-13 20:05:41 +03:00
|
|
|
// Just to avoid nil pointer dereference.
|
|
|
|
subnet: &net.IPNet{},
|
|
|
|
Options: tc.opts,
|
|
|
|
})
|
2022-09-05 17:01:33 +03:00
|
|
|
|
|
|
|
assert.Equal(t, tc.wantExplicit, explicit)
|
|
|
|
|
|
|
|
for c := range explicit {
|
|
|
|
assert.NotContains(t, implicit, c)
|
2021-09-13 20:05:41 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|