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