38 lines
1.4 KiB
Go
38 lines
1.4 KiB
Go
package config
|
|
|
|
import "testing"
|
|
|
|
func TestValidateCommands(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
command HostCommand
|
|
valid bool
|
|
}{
|
|
{"mtr", HostCommand{AliasPrefix: "m", Args: []string{"mtr", "{address}"}}, true},
|
|
{"ping", HostCommand{Args: []string{"ping", "-c", "3", "{host}"}}, true},
|
|
{"inspect", HostCommand{Args: []string{"echo", "{target}"}}, true},
|
|
{"", HostCommand{Args: []string{"echo", "{host}"}}, false},
|
|
{"bad name", HostCommand{Args: []string{"echo", "{host}"}}, false},
|
|
{"netssh", HostCommand{Args: []string{"echo", "{host}"}}, false},
|
|
{"bad", HostCommand{AliasPrefix: "m;", Args: []string{"mtr", "{address}"}}, false},
|
|
{"bad", HostCommand{}, false},
|
|
{"bad", HostCommand{Args: []string{"", "{host}"}}, false},
|
|
{"bad", HostCommand{Args: []string{"{host}", "{address}"}}, false},
|
|
{"bad", HostCommand{Args: []string{"mtr", "{adress}"}}, false},
|
|
{"bad", HostCommand{Args: []string{"mtr"}}, false},
|
|
{"bad", HostCommand{Args: []string{"mtr", "{host}\x00"}}, false},
|
|
} {
|
|
cfg := Config{Commands: map[string]HostCommand{tc.name: tc.command}}
|
|
if err := cfg.ValidateCommands(); (err == nil) != tc.valid {
|
|
t.Errorf("%+v: %v", tc, err)
|
|
}
|
|
}
|
|
cfg := Config{Commands: map[string]HostCommand{
|
|
"mtr": {AliasPrefix: "m", Args: []string{"mtr", "{address}"}},
|
|
"ping": {AliasPrefix: "m", Args: []string{"ping", "{address}"}},
|
|
}}
|
|
if cfg.ValidateCommands() == nil {
|
|
t.Fatal("duplicate prefixes accepted")
|
|
}
|
|
}
|