mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-05-07 08:22:57 +03:00
Pull request: 4705 fix opts
Merge in DNS/adguard-home from 4705-fix-opts to master Updates #4705. Squashed commit of the following: commit d3924c443260af3d32d73bd784efff2bf8dd612e Merge: e46198c6e545f3bd
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 5 16:57:38 2022 +0300 Merge branch 'master' into 4705-fix-opts commit e46198c6d8da4dcadabecfd9c1b33cc472efe612 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Sep 5 16:52:20 2022 +0300 dhcpd: immp docs commit 1c1caeaa1b2eb642fa83aa5a88ec041af9963591 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Sat Sep 3 17:31:35 2022 +0300 dhcpd: fix logic, imp docs commit bc74e21b9eb79fe22170b0e02cddcbd4bf78d860 Merge: 280ad10f1fb04376
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Sep 2 18:58:52 2022 +0300 Merge branch 'master' into 4705-fix-opts commit 280ad10f63f954f89b42cdf206a8240f8d4de503 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Sep 2 00:53:38 2022 +0300 dhcpd: imp docs, tests commit 600fa44f35683ba4b340843be13786e9383ead89 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Sep 1 20:24:52 2022 +0300 dhcpd: add new opts commit caf0cc6b370a04e6e002428b49f8d54cba105d5a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Sep 1 18:13:02 2022 +0300 dhcpd: log changes commit 3d2c61d9b8fd19c8d1e4f43ac9aac3cb94cdd4d3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Sep 1 18:09:34 2022 +0300 dhcpd: imp opts
This commit is contained in:
parent
e545f3bdb7
commit
9c9169ac12
6 changed files with 580 additions and 197 deletions
internal/dhcpd
|
@ -7,7 +7,9 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/AdguardTeam/golibs/netutil"
|
||||
"github.com/AdguardTeam/golibs/testutil"
|
||||
"github.com/insomniacslk/dhcp/dhcpv4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -17,150 +19,199 @@ func TestParseOpt(t *testing.T) {
|
|||
testCases := []struct {
|
||||
name string
|
||||
in string
|
||||
wantOpt dhcpv4.Option
|
||||
wantCode dhcpv4.OptionCode
|
||||
wantVal dhcpv4.OptionValue
|
||||
wantErrMsg string
|
||||
}{{
|
||||
name: "hex_success",
|
||||
in: "6 hex c0a80101c0a80102",
|
||||
wantOpt: dhcpv4.OptGeneric(
|
||||
dhcpv4.GenericOptionCode(6),
|
||||
[]byte{
|
||||
0xC0, 0xA8, 0x01, 0x01,
|
||||
0xC0, 0xA8, 0x01, 0x02,
|
||||
},
|
||||
),
|
||||
name: "hex_success",
|
||||
in: "6 hex c0a80101c0a80102",
|
||||
wantCode: dhcpv4.GenericOptionCode(6),
|
||||
wantVal: dhcpv4.OptionGeneric{Data: []byte{
|
||||
0xC0, 0xA8, 0x01, 0x01,
|
||||
0xC0, 0xA8, 0x01, 0x02,
|
||||
}},
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
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}),
|
||||
},
|
||||
name: "ip_success",
|
||||
in: "6 ip 1.2.3.4",
|
||||
wantCode: dhcpv4.GenericOptionCode(6),
|
||||
wantVal: dhcpv4.IP(net.IP{0x01, 0x02, 0x03, 0x04}),
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "ip_fail_v6",
|
||||
in: "6 ip ::1234",
|
||||
wantOpt: dhcpv4.Option{},
|
||||
wantErrMsg: "invalid option string \"6 ip ::1234\": bad ipv4 address \"::1234\"",
|
||||
}, {
|
||||
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},
|
||||
}),
|
||||
},
|
||||
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: "",
|
||||
}, {
|
||||
name: "text_success",
|
||||
in: "252 text http://192.168.1.1/",
|
||||
wantOpt: dhcpv4.OptGeneric(
|
||||
dhcpv4.GenericOptionCode(252),
|
||||
[]byte("http://192.168.1.1/"),
|
||||
),
|
||||
name: "text_success",
|
||||
in: "252 text http://192.168.1.1/",
|
||||
wantCode: dhcpv4.GenericOptionCode(252),
|
||||
wantVal: dhcpv4.String("http://192.168.1.1/"),
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "del_success",
|
||||
in: "61 del",
|
||||
wantOpt: dhcpv4.Option{
|
||||
Code: dhcpv4.GenericOptionCode(dhcpv4.OptionClientIdentifier),
|
||||
Value: dhcpv4.OptionGeneric{Data: nil},
|
||||
},
|
||||
name: "del_success",
|
||||
in: "61 del",
|
||||
wantCode: dhcpv4.GenericOptionCode(dhcpv4.OptionClientIdentifier),
|
||||
wantVal: dhcpv4.OptionGeneric{Data: nil},
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
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),
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "bad_parts",
|
||||
in: "6 ip",
|
||||
wantOpt: dhcpv4.Option{},
|
||||
wantCode: nil,
|
||||
wantVal: nil,
|
||||
wantErrMsg: `invalid option string "6 ip": bad option format`,
|
||||
}, {
|
||||
name: "bad_code",
|
||||
in: "256 ip 1.1.1.1",
|
||||
wantOpt: dhcpv4.Option{},
|
||||
name: "bad_code",
|
||||
in: "256 ip 1.1.1.1",
|
||||
wantCode: nil,
|
||||
wantVal: nil,
|
||||
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{},
|
||||
wantCode: nil,
|
||||
wantVal: nil,
|
||||
wantErrMsg: `invalid option string "6 bad 1.1.1.1": unknown option type "bad"`,
|
||||
}, {
|
||||
name: "hex_error",
|
||||
in: "6 hex ZZZ",
|
||||
wantOpt: dhcpv4.Option{},
|
||||
name: "hex_error",
|
||||
in: "6 hex ZZZ",
|
||||
wantCode: nil,
|
||||
wantVal: nil,
|
||||
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{},
|
||||
wantCode: nil,
|
||||
wantVal: nil,
|
||||
wantErrMsg: "invalid option string \"6 ip 1.2.3.x\": bad ipv4 address \"1.2.3.x\"",
|
||||
}, {
|
||||
name: "ips_error",
|
||||
in: "6 ips 192.168.1.1,192.168.1.x",
|
||||
wantOpt: dhcpv4.Option{},
|
||||
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,
|
||||
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\"",
|
||||
}, {
|
||||
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",
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
opt, err := parseDHCPOption(tc.in)
|
||||
code, val, err := parseDHCPOption(tc.in)
|
||||
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
||||
|
||||
// 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)
|
||||
assert.Equal(t, tc.wantCode, code)
|
||||
assert.Equal(t, tc.wantVal, val)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
opts []string
|
||||
name string
|
||||
wantExplicit dhcpv4.Options
|
||||
opts []string
|
||||
}{{
|
||||
name: "all_default",
|
||||
checks: allDefault,
|
||||
opts: nil,
|
||||
name: "all_default",
|
||||
wantExplicit: nil,
|
||||
opts: nil,
|
||||
}, {
|
||||
name: "configured_ip",
|
||||
checks: dhcpv4.Options{
|
||||
dhcpv4.OptionBroadcastAddress.Code(): oneIP,
|
||||
},
|
||||
wantExplicit: dhcpv4.OptionsFromList(
|
||||
dhcpv4.OptBroadcastAddress(oneIP),
|
||||
),
|
||||
opts: []string{
|
||||
fmt.Sprintf("%d ip %s", dhcpv4.OptionBroadcastAddress, oneIP),
|
||||
},
|
||||
}, {
|
||||
name: "configured_ips",
|
||||
checks: dhcpv4.Options{
|
||||
dhcpv4.OptionDomainNameServer.Code(): append(oneIP, otherIP...),
|
||||
},
|
||||
wantExplicit: dhcpv4.OptionsFromList(
|
||||
dhcpv4.Option{
|
||||
Code: dhcpv4.OptionDomainNameServer,
|
||||
Value: dhcpv4.IPs{oneIP, otherIP},
|
||||
},
|
||||
),
|
||||
opts: []string{
|
||||
fmt.Sprintf("%d ips %s,%s", dhcpv4.OptionDomainNameServer, oneIP, otherIP),
|
||||
},
|
||||
}, {
|
||||
name: "configured_bad",
|
||||
checks: allDefault,
|
||||
name: "configured_bad",
|
||||
wantExplicit: nil,
|
||||
opts: []string{
|
||||
"19 bool yes",
|
||||
"24 dur 3y",
|
||||
"23 u8 256",
|
||||
"23 u16 65536",
|
||||
"20 hex",
|
||||
"23 hex abc",
|
||||
"32 ips 1,2,3,4",
|
||||
|
@ -168,26 +219,29 @@ func TestPrepareOptions(t *testing.T) {
|
|||
},
|
||||
}, {
|
||||
name: "configured_del",
|
||||
checks: dhcpv4.Options{
|
||||
dhcpv4.OptionBroadcastAddress.Code(): nil,
|
||||
},
|
||||
wantExplicit: dhcpv4.OptionsFromList(
|
||||
dhcpv4.OptBroadcastAddress(nil),
|
||||
),
|
||||
opts: []string{
|
||||
"28 del",
|
||||
},
|
||||
}, {
|
||||
name: "rewritten_del",
|
||||
checks: dhcpv4.Options{
|
||||
dhcpv4.OptionBroadcastAddress.Code(): []byte{255, 255, 255, 255},
|
||||
},
|
||||
wantExplicit: dhcpv4.OptionsFromList(
|
||||
dhcpv4.OptBroadcastAddress(netutil.IPv4bcast()),
|
||||
),
|
||||
opts: []string{
|
||||
"28 del",
|
||||
"28 ip 255.255.255.255",
|
||||
},
|
||||
}, {
|
||||
name: "configured_and_del",
|
||||
checks: dhcpv4.Options{
|
||||
123: []byte("cba"),
|
||||
},
|
||||
wantExplicit: dhcpv4.OptionsFromList(
|
||||
dhcpv4.Option{
|
||||
Code: dhcpv4.OptionGeoConf,
|
||||
Value: dhcpv4.String("cba"),
|
||||
},
|
||||
),
|
||||
opts: []string{
|
||||
"123 text abc",
|
||||
"123 del",
|
||||
|
@ -197,18 +251,16 @@ func TestPrepareOptions(t *testing.T) {
|
|||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.name == "configured_del" {
|
||||
assert.True(t, true)
|
||||
}
|
||||
opts := prepareOptions(V4ServerConf{
|
||||
implicit, explicit := prepareOptions(V4ServerConf{
|
||||
// Just to avoid nil pointer dereference.
|
||||
subnet: &net.IPNet{},
|
||||
Options: tc.opts,
|
||||
})
|
||||
for c, v := range tc.checks {
|
||||
val := opts.Get(dhcpv4.GenericOptionCode(c))
|
||||
assert.Lenf(t, val, len(v), "Code: %v", c)
|
||||
assert.Equal(t, v, val)
|
||||
|
||||
assert.Equal(t, tc.wantExplicit, explicit)
|
||||
|
||||
for c := range explicit {
|
||||
assert.NotContains(t, implicit, c)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue