2021-07-30 15:27:24 +03:00
|
|
|
//go:build freebsd
|
|
|
|
|
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
2022-03-30 15:11:57 +03:00
|
|
|
"io/fs"
|
2021-07-30 15:27:24 +03:00
|
|
|
"testing"
|
2022-03-30 15:11:57 +03:00
|
|
|
"testing/fstest"
|
2021-07-30 15:27:24 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-31 19:56:50 +03:00
|
|
|
func TestIfaceHasStaticIP(t *testing.T) {
|
2022-03-30 15:11:57 +03:00
|
|
|
const (
|
|
|
|
ifaceName = `em0`
|
|
|
|
rcConf = "etc/rc.conf"
|
|
|
|
)
|
2021-07-30 15:27:24 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
2022-03-30 15:11:57 +03:00
|
|
|
name string
|
|
|
|
rootFsys fs.FS
|
|
|
|
wantHas assert.BoolAssertionFunc
|
2021-07-30 15:27:24 +03:00
|
|
|
}{{
|
2022-03-30 15:11:57 +03:00
|
|
|
name: "simple",
|
|
|
|
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
|
|
|
|
Data: []byte(`ifconfig_` + ifaceName + `="inet 127.0.0.253 netmask 0xffffffff"` + nl),
|
|
|
|
}},
|
|
|
|
wantHas: assert.True,
|
2021-07-30 15:27:24 +03:00
|
|
|
}, {
|
2022-03-30 15:11:57 +03:00
|
|
|
name: "case_insensitiveness",
|
|
|
|
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
|
|
|
|
Data: []byte(`ifconfig_` + ifaceName + `="InEt 127.0.0.253 NeTmAsK 0xffffffff"` + nl),
|
|
|
|
}},
|
|
|
|
wantHas: assert.True,
|
2021-07-30 15:27:24 +03:00
|
|
|
}, {
|
|
|
|
name: "comments_and_trash",
|
2022-03-30 15:11:57 +03:00
|
|
|
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
|
|
|
|
Data: []byte(`# comment 1` + nl +
|
|
|
|
`` + nl +
|
|
|
|
`# comment 2` + nl +
|
|
|
|
`ifconfig_` + ifaceName + `="inet 127.0.0.253 netmask 0xffffffff"` + nl,
|
|
|
|
),
|
|
|
|
}},
|
|
|
|
wantHas: assert.True,
|
2021-07-30 15:27:24 +03:00
|
|
|
}, {
|
|
|
|
name: "aliases",
|
2022-03-30 15:11:57 +03:00
|
|
|
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
|
|
|
|
Data: []byte(`ifconfig_` + ifaceName + `_alias="inet 127.0.0.1/24"` + nl +
|
|
|
|
`ifconfig_` + ifaceName + `="inet 127.0.0.253 netmask 0xffffffff"` + nl,
|
|
|
|
),
|
|
|
|
}},
|
|
|
|
wantHas: assert.True,
|
2021-07-30 15:27:24 +03:00
|
|
|
}, {
|
|
|
|
name: "incorrect_config",
|
2022-03-30 15:11:57 +03:00
|
|
|
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
|
|
|
|
Data: []byte(
|
|
|
|
`ifconfig_` + ifaceName + `="inet6 127.0.0.253 netmask 0xffffffff"` + nl +
|
|
|
|
`ifconfig_` + ifaceName + `="inet 256.256.256.256 netmask 0xffffffff"` + nl +
|
|
|
|
`ifconfig_` + ifaceName + `=""` + nl,
|
|
|
|
),
|
|
|
|
}},
|
|
|
|
wantHas: assert.False,
|
2021-07-30 15:27:24 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-30 15:11:57 +03:00
|
|
|
substRootDirFS(t, tc.rootFsys)
|
|
|
|
|
|
|
|
has, err := IfaceHasStaticIP(ifaceName)
|
2021-07-30 15:27:24 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
tc.wantHas(t, has)
|
2021-07-30 15:27:24 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|