mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-29 18:38:56 +03:00
e54fc9b1e9
Merge in DNS/adguard-home from web-addr-cmdline to master Squashed commit of the following: commit 27652dbfae227f9a9f4d921f14af1e5897d7830d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 17:51:43 2023 +0400 home: web-addr opt commit b234d108e70fed3ff11eeb4986946f8a50dda515 Merge: eef1b5cbb66345e855
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 17:10:57 2023 +0400 Merge remote-tracking branch 'origin/master' into web-addr-cmdline # Conflicts: # CHANGELOG.md commit eef1b5cbb9f7dd9819c8038800a4bbcf3ff575aa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 17:04:12 2023 +0400 all: docs Updates #4231. commit b4adb0bae82e44101f7b6685a29926c3d02e95af Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 16:58:12 2023 +0400 all: docker Updates #4231. commit a4012fddc50a3f5143136df8f3b865d7e1b9087c Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 11:24:02 2023 +0400 all: docker commit bd5cc3308298b1022dea501c2fe79d96df24ce2a Merge: 36841dd85123ca8738
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jun 22 11:22:51 2023 +0400 Merge remote-tracking branch 'origin/master' into web-addr-cmdline commit 36841dd85fe1d006e6ec518329cb8ceea0915599 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jun 21 11:53:40 2023 +0400 all: docker commit 5c4756a56d251f1486b92f333f11485b5f4807f4 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jun 21 11:50:41 2023 +0400 all: imp docs commit 7b6fba81f2d5a354bdf259d812d04f3bd64de0c6 Merge: 54c119024ca313521d
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jun 21 11:47:36 2023 +0400 Merge remote-tracking branch 'origin/master' into web-addr-cmdline commit 54c119024f3999d5c4dec06b21e3bb78803bf388 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 15:57:54 2023 +0400 home: imp code commit c87e0a690376aa6a28a018c3cbbb5de0ad16333d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 15:56:45 2023 +0400 all: imp docs commit de240d00e334f7fd3dbf39ac08472183deeb32db Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 15:54:27 2023 +0400 all: docker commit 568d5371e73fb6c5ba19a036e7a5135764f6538b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 13:38:53 2023 +0400 all: docs commit 407e230c8e3bc293e8d533ca5db1f28fec4b3c54 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 13:24:23 2023 +0400 home: deprecate opts commit 6e628fad6f4ee9f23213eeb55bec500db4435175 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jun 20 13:18:10 2023 +0400 home: web-addr opt
234 lines
7.3 KiB
Go
234 lines
7.3 KiB
Go
package home
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func testParseOK(t *testing.T, ss ...string) options {
|
|
t.Helper()
|
|
|
|
o, _, err := parseCmdOpts("", ss)
|
|
require.NoError(t, err)
|
|
|
|
return o
|
|
}
|
|
|
|
func testParseErr(t *testing.T, descr string, ss ...string) {
|
|
t.Helper()
|
|
|
|
_, _, err := parseCmdOpts("", ss)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func testParseParamMissing(t *testing.T, param string) {
|
|
t.Helper()
|
|
|
|
testParseErr(t, fmt.Sprintf("%s parameter missing", param), param)
|
|
}
|
|
|
|
func TestParseVerbose(t *testing.T) {
|
|
assert.False(t, testParseOK(t).verbose, "empty is not verbose")
|
|
assert.True(t, testParseOK(t, "-v").verbose, "-v is verbose")
|
|
assert.True(t, testParseOK(t, "--verbose").verbose, "--verbose is verbose")
|
|
}
|
|
|
|
func TestParseConfigFilename(t *testing.T) {
|
|
assert.Equal(t, "", testParseOK(t).confFilename, "empty is no config filename")
|
|
assert.Equal(t, "path", testParseOK(t, "-c", "path").confFilename, "-c is config filename")
|
|
testParseParamMissing(t, "-c")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "--config", "path").confFilename, "--config is config filename")
|
|
testParseParamMissing(t, "--config")
|
|
}
|
|
|
|
func TestParseWorkDir(t *testing.T) {
|
|
assert.Equal(t, "", testParseOK(t).workDir, "empty is no work dir")
|
|
assert.Equal(t, "path", testParseOK(t, "-w", "path").workDir, "-w is work dir")
|
|
testParseParamMissing(t, "-w")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "--work-dir", "path").workDir, "--work-dir is work dir")
|
|
testParseParamMissing(t, "--work-dir")
|
|
}
|
|
|
|
func TestParseBindHost(t *testing.T) {
|
|
wantAddr := netip.MustParseAddr("1.2.3.4")
|
|
|
|
assert.Zero(t, testParseOK(t).bindHost, "empty is not host")
|
|
assert.Equal(t, wantAddr, testParseOK(t, "-h", "1.2.3.4").bindHost, "-h is host")
|
|
testParseParamMissing(t, "-h")
|
|
|
|
assert.Equal(t, wantAddr, testParseOK(t, "--host", "1.2.3.4").bindHost, "--host is host")
|
|
testParseParamMissing(t, "--host")
|
|
}
|
|
|
|
func TestParseBindPort(t *testing.T) {
|
|
assert.Equal(t, 0, testParseOK(t).bindPort, "empty is port 0")
|
|
assert.Equal(t, 65535, testParseOK(t, "-p", "65535").bindPort, "-p is port")
|
|
testParseParamMissing(t, "-p")
|
|
|
|
assert.Equal(t, 65535, testParseOK(t, "--port", "65535").bindPort, "--port is port")
|
|
testParseParamMissing(t, "--port")
|
|
|
|
testParseErr(t, "not an int", "-p", "x")
|
|
testParseErr(t, "hex not supported", "-p", "0x100")
|
|
testParseErr(t, "port negative", "-p", "-1")
|
|
testParseErr(t, "port too high", "-p", "65536")
|
|
testParseErr(t, "port too high", "-p", "4294967297") // 2^32 + 1
|
|
testParseErr(t, "port too high", "-p", "18446744073709551617") // 2^64 + 1
|
|
}
|
|
|
|
func TestParseBindAddr(t *testing.T) {
|
|
wantAddrPort := netip.MustParseAddrPort("1.2.3.4:8089")
|
|
|
|
assert.Zero(t, testParseOK(t).bindAddr, "empty is not web-addr")
|
|
|
|
assert.Equal(t, wantAddrPort, testParseOK(t, "--web-addr", "1.2.3.4:8089").bindAddr)
|
|
assert.Equal(t, netip.MustParseAddrPort("1.2.3.4:0"), testParseOK(t, "--web-addr", "1.2.3.4:0").bindAddr)
|
|
testParseParamMissing(t, "-web-addr")
|
|
|
|
testParseErr(t, "not an int", "--web-addr", "1.2.3.4:x")
|
|
testParseErr(t, "hex not supported", "--web-addr", "1.2.3.4:0x100")
|
|
testParseErr(t, "port negative", "--web-addr", "1.2.3.4:-1")
|
|
testParseErr(t, "port too high", "--web-addr", "1.2.3.4:65536")
|
|
testParseErr(t, "port too high", "--web-addr", "1.2.3.4:4294967297") // 2^32 + 1
|
|
testParseErr(t, "port too high", "--web-addr", "1.2.3.4:18446744073709551617") // 2^64 + 1
|
|
}
|
|
|
|
func TestParseLogfile(t *testing.T) {
|
|
assert.Equal(t, "", testParseOK(t).logFile, "empty is no log file")
|
|
assert.Equal(t, "path", testParseOK(t, "-l", "path").logFile, "-l is log file")
|
|
assert.Equal(t, "path", testParseOK(t, "--logfile", "path").logFile, "--logfile is log file")
|
|
}
|
|
|
|
func TestParsePidfile(t *testing.T) {
|
|
assert.Equal(t, "", testParseOK(t).pidFile, "empty is no pid file")
|
|
assert.Equal(t, "path", testParseOK(t, "--pidfile", "path").pidFile, "--pidfile is pid file")
|
|
}
|
|
|
|
func TestParseCheckConfig(t *testing.T) {
|
|
assert.False(t, testParseOK(t).checkConfig, "empty is not check config")
|
|
assert.True(t, testParseOK(t, "--check-config").checkConfig, "--check-config is check config")
|
|
}
|
|
|
|
func TestParseDisableUpdate(t *testing.T) {
|
|
assert.False(t, testParseOK(t).disableUpdate, "empty is not disable update")
|
|
assert.True(t, testParseOK(t, "--no-check-update").disableUpdate, "--no-check-update is disable update")
|
|
}
|
|
|
|
func TestParsePerformUpdate(t *testing.T) {
|
|
assert.False(t, testParseOK(t).performUpdate, "empty is not perform update")
|
|
assert.True(t, testParseOK(t, "--update").performUpdate, "--update is perform update")
|
|
}
|
|
|
|
// TODO(e.burkov): Remove after v0.108.0.
|
|
func TestParseDisableMemoryOptimization(t *testing.T) {
|
|
o, eff, err := parseCmdOpts("", []string{"--no-mem-optimization"})
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, eff)
|
|
assert.Zero(t, o)
|
|
}
|
|
|
|
func TestParseService(t *testing.T) {
|
|
assert.Equal(t, "", testParseOK(t).serviceControlAction, "empty is not service cmd")
|
|
assert.Equal(t, "cmd", testParseOK(t, "-s", "cmd").serviceControlAction, "-s is service cmd")
|
|
assert.Equal(t, "cmd", testParseOK(t, "--service", "cmd").serviceControlAction, "--service is service cmd")
|
|
}
|
|
|
|
func TestParseGLInet(t *testing.T) {
|
|
assert.False(t, testParseOK(t).glinetMode, "empty is not GL-Inet mode")
|
|
assert.True(t, testParseOK(t, "--glinet").glinetMode, "--glinet is GL-Inet mode")
|
|
}
|
|
|
|
func TestParseUnknown(t *testing.T) {
|
|
testParseErr(t, "unknown word", "x")
|
|
testParseErr(t, "unknown short", "-x")
|
|
testParseErr(t, "unknown long", "--x")
|
|
testParseErr(t, "unknown triple", "---x")
|
|
testParseErr(t, "unknown plus", "+x")
|
|
testParseErr(t, "unknown dash", "-")
|
|
}
|
|
|
|
func TestOptsToArgs(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
opts options
|
|
}{{
|
|
name: "empty",
|
|
args: []string{},
|
|
opts: options{},
|
|
}, {
|
|
name: "config_filename",
|
|
args: []string{"-c", "path"},
|
|
opts: options{confFilename: "path"},
|
|
}, {
|
|
name: "work_dir",
|
|
args: []string{"-w", "path"},
|
|
opts: options{workDir: "path"},
|
|
}, {
|
|
name: "bind_host",
|
|
opts: options{bindHost: netip.MustParseAddr("1.2.3.4")},
|
|
args: []string{"-h", "1.2.3.4"},
|
|
}, {
|
|
name: "bind_port",
|
|
args: []string{"-p", "666"},
|
|
opts: options{bindPort: 666},
|
|
}, {
|
|
name: "web-addr",
|
|
args: []string{"--web-addr", "1.2.3.4:8080"},
|
|
opts: options{bindAddr: netip.MustParseAddrPort("1.2.3.4:8080")},
|
|
}, {
|
|
name: "log_file",
|
|
args: []string{"-l", "path"},
|
|
opts: options{logFile: "path"},
|
|
}, {
|
|
name: "pid_file",
|
|
args: []string{"--pidfile", "path"},
|
|
opts: options{pidFile: "path"},
|
|
}, {
|
|
name: "disable_update",
|
|
args: []string{"--no-check-update"},
|
|
opts: options{disableUpdate: true},
|
|
}, {
|
|
name: "perform_update",
|
|
args: []string{"--update"},
|
|
opts: options{performUpdate: true},
|
|
}, {
|
|
name: "control_action",
|
|
args: []string{"-s", "run"},
|
|
opts: options{serviceControlAction: "run"},
|
|
}, {
|
|
name: "glinet_mode",
|
|
args: []string{"--glinet"},
|
|
opts: options{glinetMode: true},
|
|
}, {
|
|
name: "multiple",
|
|
args: []string{
|
|
"-c", "config",
|
|
"-w", "work",
|
|
"-s", "run",
|
|
"--pidfile", "pid",
|
|
"--no-check-update",
|
|
},
|
|
opts: options{
|
|
serviceControlAction: "run",
|
|
confFilename: "config",
|
|
workDir: "work",
|
|
pidFile: "pid",
|
|
disableUpdate: true,
|
|
},
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result := optsToArgs(tc.opts)
|
|
assert.ElementsMatch(t, tc.args, result)
|
|
})
|
|
}
|
|
}
|