2021-05-21 04:55:42 -07:00
|
|
|
//go:build linux
|
2021-06-15 09:42:41 -07:00
|
|
|
// +build linux
|
2021-05-21 04:55:42 -07:00
|
|
|
|
2021-03-16 09:42:15 -07:00
|
|
|
package aghnet
|
2020-12-07 09:48:24 -07:00
|
|
|
|
|
|
|
import (
|
2022-03-30 05:11:57 -07:00
|
|
|
"io/fs"
|
2020-12-07 09:48:24 -07:00
|
|
|
"testing"
|
2022-03-30 05:11:57 -07:00
|
|
|
"testing/fstest"
|
2020-12-07 09:48:24 -07:00
|
|
|
|
2022-03-30 05:11:57 -07:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2020-12-07 09:48:24 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-30 05:11:57 -07:00
|
|
|
func TestHasStaticIP(t *testing.T) {
|
|
|
|
const ifaceName = "wlan0"
|
2021-08-13 09:20:17 -07:00
|
|
|
|
2022-03-30 05:11:57 -07:00
|
|
|
const (
|
|
|
|
dhcpcd = "etc/dhcpcd.conf"
|
|
|
|
netifaces = "etc/network/interfaces"
|
|
|
|
)
|
2021-08-13 09:20:17 -07:00
|
|
|
|
2020-12-07 09:48:24 -07:00
|
|
|
testCases := []struct {
|
2022-03-30 05:11:57 -07:00
|
|
|
rootFsys fs.FS
|
|
|
|
name string
|
|
|
|
wantHas assert.BoolAssertionFunc
|
|
|
|
wantErrMsg string
|
2020-12-07 09:48:24 -07:00
|
|
|
}{{
|
2022-03-30 05:11:57 -07: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 09:48:24 -07:00
|
|
|
}, {
|
2022-03-30 05:11:57 -07: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 07:02:45 -07:00
|
|
|
}, {
|
2022-03-30 05:11:57 -07: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 07:02:45 -07:00
|
|
|
}, {
|
2022-03-30 05:11:57 -07: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 09:48:24 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-30 05:11:57 -07:00
|
|
|
substRootDirFS(t, tc.rootFsys)
|
|
|
|
|
|
|
|
has, err := IfaceHasStaticIP(ifaceName)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-03-25 08:03:29 -07:00
|
|
|
|
2022-03-30 05:11:57 -07:00
|
|
|
tc.wantHas(t, has)
|
2020-12-07 09:48:24 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|