2023-10-02 13:21:16 +03:00
|
|
|
package dhcpsvc_test
|
|
|
|
|
|
|
|
import (
|
2024-07-09 20:04:24 +03:00
|
|
|
"path/filepath"
|
2023-10-02 13:21:16 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfig_Validate(t *testing.T) {
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := filepath.Join(t.TempDir(), "leases.json")
|
|
|
|
|
2023-10-02 13:21:16 +03:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
conf *dhcpsvc.Config
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "nil_config",
|
|
|
|
conf: nil,
|
|
|
|
wantErrMsg: "config is nil",
|
|
|
|
}, {
|
|
|
|
name: "disabled",
|
|
|
|
conf: &dhcpsvc.Config{},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "empty",
|
|
|
|
conf: &dhcpsvc.Config{
|
2024-07-03 15:29:54 +03:00
|
|
|
Enabled: true,
|
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: `bad domain name "": domain name is empty`,
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: nil,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "no_interfaces",
|
|
|
|
wantErrMsg: "no interfaces specified",
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: nil,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "no_interfaces",
|
|
|
|
wantErrMsg: "no interfaces specified",
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": nil,
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "nil_interface",
|
|
|
|
wantErrMsg: `interface "eth0": config is nil`,
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: nil,
|
|
|
|
IPv6: &dhcpsvc.IPv6Config{Enabled: false},
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "nil_ipv4",
|
|
|
|
wantErrMsg: `interface "eth0": ipv4: config is nil`,
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: &dhcpsvc.IPv4Config{Enabled: false},
|
|
|
|
IPv6: nil,
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "nil_ipv6",
|
|
|
|
wantErrMsg: `interface "eth0": ipv6: config is nil`,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, tc.conf.Validate())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|