63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
package host
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLookupElevationActionIsAClosedWhitelist(t *testing.T) {
|
|
want := map[string]ElevationActionSpec{
|
|
"hyperv.enable": {Action: "hyperv.enable", NeedsReboot: true},
|
|
"hyperv.addAdminGroup": {Action: "hyperv.addAdminGroup", NeedsLogoff: true},
|
|
"sandbox.enable": {Action: "sandbox.enable", NeedsReboot: true},
|
|
}
|
|
for action, spec := range want {
|
|
got, ok := LookupElevationAction(action)
|
|
if !ok || got != spec {
|
|
t.Errorf("LookupElevationAction(%q) = %+v, %v; want %+v", action, got, ok, spec)
|
|
}
|
|
}
|
|
for _, bad := range []string{"", "HYPERV.ENABLE", "hyperv.enable ", "shell.exec", "hyperv.enable; calc", "dism.exe /?", "../hyperv.enable"} {
|
|
if _, ok := LookupElevationAction(bad); ok {
|
|
t.Errorf("LookupElevationAction(%q) accepted an action outside the whitelist", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateElevationTargetUser(t *testing.T) {
|
|
for _, ok := range []string{`DOMAIN\alice`, `DESKTOP-1\bob`, "alice@contoso.com", "alice", "Alice Smith"} {
|
|
if err := ValidateElevationTargetUser(ok); err != nil {
|
|
t.Errorf("ValidateElevationTargetUser(%q) = %v, want nil", ok, err)
|
|
}
|
|
}
|
|
for _, bad := range []string{"", " ", "-flag", "/add", "alice & calc", "alice;whoami", "alice\nbob", "a|b", "a>b", "a`b", "a%PATH%", strings.Repeat("a", 129)} {
|
|
if err := ValidateElevationTargetUser(bad); err == nil {
|
|
t.Errorf("ValidateElevationTargetUser(%q) accepted the account name", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsNetAlreadyMemberOutput(t *testing.T) {
|
|
for _, out := range []string{"System error 1378 has occurred.", "发生系统错误 1378。"} {
|
|
if !isNetAlreadyMemberOutput(out) {
|
|
t.Errorf("%q should count as already a member", out)
|
|
}
|
|
}
|
|
for _, out := range []string{"System error 5 has occurred.", ""} {
|
|
if isNetAlreadyMemberOutput(out) {
|
|
t.Errorf("%q should not count as already a member", out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExecuteElevatedActionRejectsUnknownActions(t *testing.T) {
|
|
res, err := ExecuteElevatedAction(t.Context(), "shell.exec", "")
|
|
if err == nil || res.OK {
|
|
t.Fatalf("an action outside the whitelist ran: %+v, %v", res, err)
|
|
}
|
|
res, err = RunElevatedAction(t.Context(), "reims-go.exe", "", "")
|
|
if err == nil || res.OK {
|
|
t.Fatalf("an empty action ran: %+v, %v", res, err)
|
|
}
|
|
}
|