126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
)
|
|
|
|
func readElevateResult(t *testing.T, path string) host.ElevationResult {
|
|
t.Helper()
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("read result: %v", err)
|
|
}
|
|
var res host.ElevationResult
|
|
if err := json.Unmarshal(data, &res); err != nil {
|
|
t.Fatalf("decode result %q: %v", data, err)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func TestElevateRejectsActionsOutsideTheWhitelist(t *testing.T) {
|
|
called := false
|
|
prev := elevateExec
|
|
elevateExec = func(context.Context, string, string) (host.ElevationResult, error) {
|
|
called = true
|
|
return host.ElevationResult{OK: true}, nil
|
|
}
|
|
t.Cleanup(func() { elevateExec = prev })
|
|
|
|
// Point every data-folder lookup at an empty folder: the helper must not touch it.
|
|
home := t.TempDir()
|
|
t.Setenv("APPDATA", home)
|
|
t.Setenv("LOCALAPPDATA", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("HOME", home)
|
|
|
|
resPath := filepath.Join(t.TempDir(), "result.json")
|
|
for _, action := range []string{"rm-rf", ""} {
|
|
if _, err := run([]string{"__elevate", "--action", action, "--result-file", resPath}); err == nil {
|
|
t.Fatalf("action %q was accepted", action)
|
|
}
|
|
if res := readElevateResult(t, resPath); res.OK {
|
|
t.Fatalf("action %q reported success: %+v", action, res)
|
|
}
|
|
}
|
|
if called {
|
|
t.Fatal("an action outside the whitelist reached the executor")
|
|
}
|
|
entries, err := os.ReadDir(home)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Fatalf("the elevated helper wrote into the data folder candidates: %v", entries)
|
|
}
|
|
}
|
|
|
|
func TestElevateRunsAWhitelistedActionAndWritesTheResult(t *testing.T) {
|
|
prev := elevateExec
|
|
var gotAction, gotUser string
|
|
elevateExec = func(_ context.Context, action, user string) (host.ElevationResult, error) {
|
|
gotAction, gotUser = action, user
|
|
return host.ElevationResult{OK: true, Action: action, RebootRequired: true}, nil
|
|
}
|
|
t.Cleanup(func() { elevateExec = prev })
|
|
|
|
resPath := filepath.Join(t.TempDir(), "result.json")
|
|
if _, err := run([]string{"__elevate", "--action", "hyperv.enable", "--user", `PC\alice`, "--result-file", resPath}); err != nil {
|
|
t.Fatalf("run: %v", err)
|
|
}
|
|
if gotAction != "hyperv.enable" || gotUser != `PC\alice` {
|
|
t.Fatalf("executor got %q, %q", gotAction, gotUser)
|
|
}
|
|
res := readElevateResult(t, resPath)
|
|
if !res.OK || res.Action != "hyperv.enable" || !res.RebootRequired {
|
|
t.Fatalf("result = %+v", res)
|
|
}
|
|
}
|
|
|
|
func TestElevateReportsExecutorFailures(t *testing.T) {
|
|
prev := elevateExec
|
|
elevateExec = func(_ context.Context, action, _ string) (host.ElevationResult, error) {
|
|
return host.ElevationResult{Action: action, ExitCode: 5}, io.ErrUnexpectedEOF
|
|
}
|
|
t.Cleanup(func() { elevateExec = prev })
|
|
|
|
resPath := filepath.Join(t.TempDir(), "result.json")
|
|
if _, err := run([]string{"__elevate", "--action", "sandbox.enable", "--result-file", resPath}); err == nil {
|
|
t.Fatal("a failed action returned no error")
|
|
}
|
|
res := readElevateResult(t, resPath)
|
|
if res.OK || res.Error == "" || res.ExitCode != 5 {
|
|
t.Fatalf("result = %+v", res)
|
|
}
|
|
}
|
|
|
|
func TestElevateIsHiddenFromUsage(t *testing.T) {
|
|
if !IsCommand([]string{"__elevate", "--action", "hyperv.enable"}) {
|
|
t.Fatal("__elevate must run as a command, not open the window")
|
|
}
|
|
old := os.Stdout
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Stdout = w
|
|
printUsage(i18n.EffectiveLocale(i18n.EnUS))
|
|
os.Stdout = old
|
|
_ = w.Close()
|
|
out, err := io.ReadAll(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(out), "__elevate") {
|
|
t.Fatalf("usage mentions the hidden subcommand:\n%s", out)
|
|
}
|
|
}
|