2021-05-21 11:55:42 +00:00
|
|
|
//go:build linux
|
2021-06-15 16:42:41 +00:00
|
|
|
// +build linux
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2021-03-16 16:42:15 +00:00
|
|
|
package aghnet
|
2020-12-07 16:48:24 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-30 12:11:57 +00:00
|
|
|
"io/fs"
|
2020-12-07 16:48:24 +00:00
|
|
|
"testing"
|
2022-03-30 12:11:57 +00:00
|
|
|
"testing/fstest"
|
2020-12-07 16:48:24 +00:00
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2020-12-07 16:48:24 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
func TestHasStaticIP(t *testing.T) {
|
|
|
|
const ifaceName = "wlan0"
|
2021-08-13 16:20:17 +00:00
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
const (
|
|
|
|
dhcpcd = "etc/dhcpcd.conf"
|
|
|
|
netifaces = "etc/network/interfaces"
|
|
|
|
)
|
2021-08-13 16:20:17 +00:00
|
|
|
|
2020-12-07 16:48:24 +00:00
|
|
|
testCases := []struct {
|
2022-03-30 12:11:57 +00:00
|
|
|
rootFsys fs.FS
|
|
|
|
name string
|
|
|
|
wantHas assert.BoolAssertionFunc
|
|
|
|
wantErrMsg string
|
2020-12-07 16:48:24 +00:00
|
|
|
}{{
|
2022-03-30 12:11:57 +00:00
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
dhcpcd: &fstest.MapFile{
|
|
|
|
Data: []byte(`#comment` + nl +
|
|
|
|
`# comment` + nl +
|
|
|
|
`interface eth0` + nl +
|
|
|
|
`static ip_address=192.168.0.1/24` + nl +
|
|
|
|
`# interface ` + ifaceName + nl +
|
|
|
|
`static ip_address=192.168.1.1/24` + nl +
|
|
|
|
`# comment` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "dhcpcd_has_not",
|
|
|
|
wantHas: assert.False,
|
|
|
|
wantErrMsg: `no information about static ip`,
|
|
|
|
}, {
|
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
dhcpcd: &fstest.MapFile{
|
|
|
|
Data: []byte(`#comment` + nl +
|
|
|
|
`# comment` + nl +
|
|
|
|
`interface ` + ifaceName + nl +
|
|
|
|
`static ip_address=192.168.0.1/24` + nl +
|
|
|
|
`# interface ` + ifaceName + nl +
|
|
|
|
`static ip_address=192.168.1.1/24` + nl +
|
|
|
|
`# comment` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "dhcpcd_has",
|
|
|
|
wantHas: assert.True,
|
|
|
|
wantErrMsg: ``,
|
2020-12-07 16:48:24 +00:00
|
|
|
}, {
|
2022-03-30 12:11:57 +00:00
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
netifaces: &fstest.MapFile{
|
|
|
|
Data: []byte(`allow-hotplug ` + ifaceName + nl +
|
|
|
|
`#iface enp0s3 inet static` + nl +
|
|
|
|
`# address 192.168.0.200` + nl +
|
|
|
|
`# netmask 255.255.255.0` + nl +
|
|
|
|
`# gateway 192.168.0.1` + nl +
|
|
|
|
`iface ` + ifaceName + ` inet dhcp` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "netifaces_has_not",
|
|
|
|
wantHas: assert.False,
|
|
|
|
wantErrMsg: `no information about static ip`,
|
2021-06-28 14:02:45 +00:00
|
|
|
}, {
|
2022-03-30 12:11:57 +00:00
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
netifaces: &fstest.MapFile{
|
|
|
|
Data: []byte(`allow-hotplug ` + ifaceName + nl +
|
|
|
|
`iface ` + ifaceName + ` inet static` + nl +
|
|
|
|
` address 192.168.0.200` + nl +
|
|
|
|
` netmask 255.255.255.0` + nl +
|
|
|
|
` gateway 192.168.0.1` + nl +
|
|
|
|
`#iface ` + ifaceName + ` inet dhcp` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "netifaces_has",
|
|
|
|
wantHas: assert.True,
|
|
|
|
wantErrMsg: ``,
|
2021-06-28 14:02:45 +00:00
|
|
|
}, {
|
2022-03-30 12:11:57 +00:00
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
netifaces: &fstest.MapFile{
|
|
|
|
Data: []byte(`source hello` + nl +
|
|
|
|
`#iface ` + ifaceName + ` inet static` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
"hello": &fstest.MapFile{
|
|
|
|
Data: []byte(`iface ` + ifaceName + ` inet static` + nl),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "netifaces_another_file",
|
|
|
|
wantHas: assert.True,
|
|
|
|
wantErrMsg: ``,
|
|
|
|
}, {
|
|
|
|
rootFsys: fstest.MapFS{
|
|
|
|
netifaces: &fstest.MapFile{
|
|
|
|
Data: []byte(`source hello` + nl +
|
|
|
|
`iface ` + ifaceName + ` inet static` + nl,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "netifaces_ignore_another",
|
|
|
|
wantHas: assert.True,
|
|
|
|
wantErrMsg: ``,
|
2020-12-07 16:48:24 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-30 12:11:57 +00:00
|
|
|
substRootDirFS(t, tc.rootFsys)
|
|
|
|
|
|
|
|
has, err := IfaceHasStaticIP(ifaceName)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-03-25 15:03:29 +00:00
|
|
|
|
2022-03-30 12:11:57 +00:00
|
|
|
tc.wantHas(t, has)
|
2020-12-07 16:48:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|