AdGuardHome/internal/aghnet/net_freebsd_test.go
Eugene Burkov c70f941bf8 Pull request: 2846 cover aghnet vol.4
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.4 to master

Updates #2846.

Squashed commit of the following:

commit 576ef857628a403ce1478c10a4aad23985c09613
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 31 19:38:57 2022 +0300

    aghnet: imp code

commit 5b4b17ff52867aaab2c9d30a0fc7fc2fe31ff4d5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 31 14:58:34 2022 +0300

    aghnet: imp coverage
2022-03-31 19:56:50 +03:00

78 lines
1.8 KiB
Go

//go:build freebsd
// +build freebsd
package aghnet
import (
"io/fs"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIfaceHasStaticIP(t *testing.T) {
const (
ifaceName = `em0`
rcConf = "etc/rc.conf"
)
testCases := []struct {
name string
rootFsys fs.FS
wantHas assert.BoolAssertionFunc
}{{
name: "simple",
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
Data: []byte(`ifconfig_` + ifaceName + `="inet 127.0.0.253 netmask 0xffffffff"` + nl),
}},
wantHas: assert.True,
}, {
name: "case_insensitiveness",
rootFsys: fstest.MapFS{rcConf: &fstest.MapFile{
Data: []byte(`ifconfig_` + ifaceName + `="InEt 127.0.0.253 NeTmAsK 0xffffffff"` + nl),
}},
wantHas: assert.True,
}, {
name: "comments_and_trash",
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,
}, {
name: "aliases",
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,
}, {
name: "incorrect_config",
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,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
substRootDirFS(t, tc.rootFsys)
has, err := IfaceHasStaticIP(ifaceName)
require.NoError(t, err)
tc.wantHas(t, has)
})
}
}