mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 06:55:48 +03:00
c4a13b92d2
Merge in DNS/adguard-home from 3157-excessive-ptrs to master Updates #3157. Squashed commit of the following: commit 6803988240dca2f147bb80a5b3f78d7749d2fa14 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:50:01 2022 +0300 aghnet: and again commit 1a7f4d1dbc8fd4d3ae620349917526a75fa71b47 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:49:20 2022 +0300 aghnet: docs again commit d88da1fc7135f3cd03aff10b02d9957c8ffdfd30 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:47:36 2022 +0300 aghnet: imp docs commit c45dbc7800e882c6c4110aab640c32b03046f89a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:41:19 2022 +0300 aghnet: keep alphabetical order commit b61781785d096ef43f60fb4f1905a4ed3cdf7c68 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 13:50:56 2022 +0300 aghnet: imp code quality commit 578dbd71ed2f2089c69343d7d4bf8bbc29150ace Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 12 17:02:38 2022 +0300 aghnet: imp arp container
261 lines
5.8 KiB
Go
261 lines
5.8 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"io/fs"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIfaceHasStaticIP(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
shell mapShell
|
|
ifaceName string
|
|
wantHas assert.BoolAssertionFunc
|
|
wantErrMsg string
|
|
}{{
|
|
name: "success",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "IP address: 1.2.3.4\nSubnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
},
|
|
ifaceName: "en0",
|
|
wantHas: assert.False,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
name: "success_static",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "Manual Configuration\nIP address: 1.2.3.4\n" +
|
|
"Subnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
},
|
|
ifaceName: "en0",
|
|
wantHas: assert.True,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
name: "reports_error",
|
|
shell: theOnlyCmd(
|
|
"networksetup -listallhardwareports",
|
|
0,
|
|
"",
|
|
errors.Error("can't list"),
|
|
),
|
|
ifaceName: "en0",
|
|
wantHas: assert.False,
|
|
wantErrMsg: `could not find hardware port for en0`,
|
|
}, {
|
|
name: "port_error",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: errors.Error("can't get"),
|
|
out: ``,
|
|
code: 0,
|
|
},
|
|
},
|
|
ifaceName: "en0",
|
|
wantHas: assert.False,
|
|
wantErrMsg: `can't get`,
|
|
}, {
|
|
name: "port_bad_output",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "nothing meaningful",
|
|
code: 0,
|
|
},
|
|
},
|
|
ifaceName: "en0",
|
|
wantHas: assert.False,
|
|
wantErrMsg: `could not find hardware port info`,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
substShell(t, tc.shell.RunCmd)
|
|
|
|
has, err := IfaceHasStaticIP(tc.ifaceName)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
tc.wantHas(t, has)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIfaceSetStaticIP(t *testing.T) {
|
|
succFsys := fstest.MapFS{
|
|
"etc/resolv.conf": &fstest.MapFile{
|
|
Data: []byte(`nameserver 1.1.1.1`),
|
|
},
|
|
}
|
|
panicFsys := &aghtest.FS{
|
|
OnOpen: func(name string) (fs.File, error) { panic("not implemented") },
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
shell mapShell
|
|
fsys fs.FS
|
|
wantErrMsg string
|
|
}{{
|
|
name: "success",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "IP address: 1.2.3.4\nSubnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -setdnsservers hwport 1.1.1.1": {
|
|
err: nil,
|
|
out: "",
|
|
code: 0,
|
|
},
|
|
"networksetup -setmanual hwport 1.2.3.4 255.255.255.0 1.2.3.1": {
|
|
err: nil,
|
|
out: "",
|
|
code: 0,
|
|
},
|
|
},
|
|
fsys: succFsys,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
name: "static_already",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "Manual Configuration\nIP address: 1.2.3.4\n" +
|
|
"Subnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
},
|
|
fsys: panicFsys,
|
|
wantErrMsg: `ip address is already static`,
|
|
}, {
|
|
name: "reports_error",
|
|
shell: theOnlyCmd(
|
|
"networksetup -listallhardwareports",
|
|
0,
|
|
"",
|
|
errors.Error("can't list"),
|
|
),
|
|
fsys: panicFsys,
|
|
wantErrMsg: `could not find hardware port for en0`,
|
|
}, {
|
|
name: "resolv_conf_error",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "IP address: 1.2.3.4\nSubnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
},
|
|
fsys: fstest.MapFS{
|
|
"etc/resolv.conf": &fstest.MapFile{
|
|
Data: []byte("this resolv.conf is invalid"),
|
|
},
|
|
},
|
|
wantErrMsg: `found no dns servers in etc/resolv.conf`,
|
|
}, {
|
|
name: "set_dns_error",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "IP address: 1.2.3.4\nSubnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -setdnsservers hwport 1.1.1.1": {
|
|
err: errors.Error("can't set"),
|
|
out: "",
|
|
code: 0,
|
|
},
|
|
},
|
|
fsys: succFsys,
|
|
wantErrMsg: `can't set`,
|
|
}, {
|
|
name: "set_manual_error",
|
|
shell: mapShell{
|
|
"networksetup -listallhardwareports": {
|
|
err: nil,
|
|
out: "Hardware Port: hwport\nDevice: en0\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -getinfo hwport": {
|
|
err: nil,
|
|
out: "IP address: 1.2.3.4\nSubnet mask: 255.255.255.0\nRouter: 1.2.3.1\n",
|
|
code: 0,
|
|
},
|
|
"networksetup -setdnsservers hwport 1.1.1.1": {
|
|
err: nil,
|
|
out: "",
|
|
code: 0,
|
|
},
|
|
"networksetup -setmanual hwport 1.2.3.4 255.255.255.0 1.2.3.1": {
|
|
err: errors.Error("can't set"),
|
|
out: "",
|
|
code: 0,
|
|
},
|
|
},
|
|
fsys: succFsys,
|
|
wantErrMsg: `can't set`,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
substShell(t, tc.shell.RunCmd)
|
|
substRootDirFS(t, tc.fsys)
|
|
|
|
err := IfaceSetStaticIP("en0")
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|