109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package guest
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPublicKeyIsPersistentAndValid(t *testing.T) {
|
|
store := NewSSH(t.TempDir())
|
|
first, err := store.PublicKey()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := store.PublicKey()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first != second || !strings.HasPrefix(first, "ssh-ed25519 ") {
|
|
t.Fatalf("public key was not stable Ed25519: first=%q second=%q", first, second)
|
|
}
|
|
info, err := os.Stat(filepath.Join(store.root, "keys", "id_ed25519"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runtime.GOOS != "windows" && info.Mode().Perm()&0o077 != 0 {
|
|
t.Fatalf("private key permissions are too broad: %o", info.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
func TestConfinedGuestPath(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{name: "relative", path: "work/project", want: "/Users/test/work/project"},
|
|
{name: "absolute inside home", path: "/Users/test/Documents/out.txt", want: "/Users/test/Documents/out.txt"},
|
|
{name: "traversal", path: "../../Library/secret", wantErr: true},
|
|
{name: "absolute outside home", path: "/etc/passwd", wantErr: true},
|
|
{name: "empty", path: "", wantErr: true},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, err := confinedPath("/Users/test", test.path)
|
|
if (err != nil) != test.wantErr || got != test.want {
|
|
t.Fatalf("confinedPath(%q) = %q, %v", test.path, got, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOutputWriterReturnsFullOrHeadAndTail(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "output-*.log")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
writer := &outputWriter{file: file}
|
|
input := strings.Repeat("a", 9<<10) + strings.Repeat("z", 9<<10)
|
|
if _, err := writer.Write([]byte(input)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
output := writer.result()
|
|
if !writer.truncated() || !strings.HasPrefix(output, strings.Repeat("a", 8<<10)) || !strings.HasSuffix(output, strings.Repeat("z", 8<<10)) {
|
|
t.Fatal("large output did not preserve the first and last 8 KiB")
|
|
}
|
|
|
|
file2, err := os.CreateTemp(t.TempDir(), "small-output-*.log")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file2.Close()
|
|
small := &outputWriter{file: file2}
|
|
if _, err := small.Write([]byte("short output")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := small.result(); got != "short output" || small.truncated() {
|
|
t.Fatalf("small result = %q, truncated=%v", got, small.truncated())
|
|
}
|
|
}
|
|
|
|
func TestReadLogPages(t *testing.T) {
|
|
root := t.TempDir()
|
|
logs := filepath.Join(root, "logs")
|
|
if err := os.Mkdir(logs, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const logID = "guest-exec-test.log"
|
|
content := strings.Repeat("x", 10000)
|
|
if err := os.WriteFile(filepath.Join(logs, logID), []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := NewSSH(root)
|
|
page, err := store.ReadLog(logID, 8000, 8192)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if page["text"] != strings.Repeat("x", 2000) || page["next_offset"] != int64(10000) || page["eof"] != true {
|
|
t.Fatalf("unexpected final log page: %+v", page)
|
|
}
|
|
if _, err := store.ReadLog("..\\outside", 0, 10); err == nil {
|
|
t.Fatal("accepted path traversal as a log id")
|
|
}
|
|
}
|