Feat: conditional permissions behavior (#28)
* Fix: don't need sudo if we're root + other aesthetics
* Heavy refactoring, see PR #28
* Fix: avoid silent fatalities
demo: https://tcp.ac/i/JMSUc.gif
* Fix: Inverse check on `IsRoot`
* D.R.Y: check for permissions error in `common.ErrorCheck`
Reduce cognitive complexity.
* Fix: Issue with copying
* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646535918
* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646606680 and https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646594105
* Revert "Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646606680 and https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646594105"
This reverts commit ce15213009
.
* Resolve https://github.com/HikariKnight/quickpassthrough/pull/28#discussion_r1646730751
This commit is contained in:
parent
4d0086df41
commit
6c48a35180
21 changed files with 604 additions and 255 deletions
61
pkg/command/command_test.go
Normal file
61
pkg/command/command_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const fakeSudo = `#!/bin/sh
|
||||
"$@" -qptest`
|
||||
|
||||
const fakeUtil = `#!/bin/sh
|
||||
echo "$@"
|
||||
if [ "$4" = "-qptest" ]; then exit 0; else exit 1; fi`
|
||||
|
||||
func setupExecTestEnv(t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
fakeSudoPath := filepath.Join(tmpDir, "sudo")
|
||||
fakeUtilPath := filepath.Join(tmpDir, "util")
|
||||
|
||||
if err := os.WriteFile(fakeSudoPath, []byte(fakeSudo), 0755); err != nil {
|
||||
t.Fatalf("failed to write fake sudo stub: %s", err.Error())
|
||||
}
|
||||
if err := os.WriteFile(fakeUtilPath, []byte(fakeUtil), 0755); err != nil {
|
||||
t.Fatalf("failed to write fake util stub: %s", err.Error())
|
||||
}
|
||||
t.Setenv("PATH", tmpDir+":"+os.Getenv("PATH"))
|
||||
|
||||
return fakeSudoPath, fakeUtilPath
|
||||
}
|
||||
|
||||
func TestExecAndLogSudo(t *testing.T) {
|
||||
_, fakeUtilPath := setupExecTestEnv(t)
|
||||
|
||||
args := []string{"i am a string with spaces", "i came to ruin parsers and chew bubble gum", "and I'm all out of bubblegum."}
|
||||
|
||||
t.Run("is_not_root", func(t *testing.T) {
|
||||
if err := ExecAndLogSudo(false, false, "util", args...); err != nil {
|
||||
t.Errorf("unexpected error: %s", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("is_root", func(t *testing.T) {
|
||||
newFakeUtil := strings.Replace(fakeUtil, "exit 1", "exit 0", 1)
|
||||
newFakeUtil = strings.Replace(newFakeUtil, "exit 0", "exit 1", 1)
|
||||
if err := os.WriteFile(fakeUtilPath, []byte(newFakeUtil), 0755); err != nil {
|
||||
t.Fatalf("failed to overwrite fake util with modified stub: %s", err.Error())
|
||||
}
|
||||
if err := ExecAndLogSudo(false, false, "util", args...); err == nil {
|
||||
t.Errorf("expected error when using modified util with sudo, got nil")
|
||||
}
|
||||
|
||||
if err := ExecAndLogSudo(true, true, "util", args...); err != nil {
|
||||
t.Errorf("unexpected error: %s", err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue