157 lines
5.5 KiB
Go
157 lines
5.5 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGuestAgentDriveMount(t *testing.T) {
|
|
shBin, err := exec.LookPath("sh")
|
|
if err != nil {
|
|
shBin = filepath.Join(os.Getenv("ProgramFiles"), "Git", "bin", "sh.exe")
|
|
if _, err := os.Stat(shBin); err != nil {
|
|
t.Skip("POSIX shell is unavailable")
|
|
}
|
|
}
|
|
lockShellFixture(t)
|
|
|
|
dir := t.TempDir()
|
|
helper := `#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
die "Unexpected mount options" unless @ARGV == 9 && "@ARGV[0..5]" eq "-a 3 -S -o rdonly -v";
|
|
my ($drive) = $ARGV[6] =~ /^Windows ([A-Z])$/;
|
|
my ($url, $target) = @ARGV[7..8];
|
|
die "Unexpected WebDAV URL" unless $url eq "http://10.0.2.2:19847/dav/mac%20test/drives/$drive/";
|
|
die "Unexpected mount point" unless $target eq "$ENV{DRIVE_MOUNT_ROOT}/$drive";
|
|
open(my $auth, "<&=3") or die "Missing credential descriptor: $!";
|
|
seek($auth, 0, 0) or die "Credential descriptor is not seekable: $!";
|
|
binmode($auth);
|
|
local $/;
|
|
die "Invalid credential payload" unless <$auth> eq pack("Na5Na64NNN", 5, "reims", 64, "a" x 64, 0, 0, 0);
|
|
if ($drive eq "O" && -f $ENV{FAIL_O}) {
|
|
print STDERR "Authentication failed\n";
|
|
exit 22;
|
|
}
|
|
open(my $mounts, ">>", $ENV{MOUNT_STATE}) or die "Could not record mount: $!";
|
|
print $mounts "$url on $target (webdav, read-only)\n";
|
|
`
|
|
if err := os.WriteFile(filepath.Join(dir, "mount_webdav"), []byte(helper), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
script := string(GuestAgentScript())
|
|
if DriveShareAgentVersion != 8 || !strings.Contains(script, `"agent_version\":`+strconv.Itoa(CurrentAgentVersion)+",") {
|
|
t.Fatal("Windows drive sharing requires version 8 and the generated agent must report the current version")
|
|
}
|
|
harness := `set -eu
|
|
PATH="$PWD:/usr/bin:/bin:$PATH"
|
|
command -v perl >/dev/null 2>&1 || exit 77
|
|
HOME="$PWD/home with spaces"
|
|
DRIVE_MOUNT_ROOT="$HOME/ReimsWindowsDrives"
|
|
TMP_DIR="$PWD/tmp"
|
|
MOUNT_STATE="$PWD/mounts"
|
|
FAIL_O="$PWD/fail-O"
|
|
export DRIVE_MOUNT_ROOT MOUNT_STATE FAIL_O
|
|
REIMS_HOST_URL="http://10.0.2.2:19847/"
|
|
REIMS_VM="mac test"
|
|
REIMS_TOKEN="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
mkdir -p "$DRIVE_MOUNT_ROOT" "$HOME/Desktop" "$TMP_DIR"
|
|
: >"$MOUNT_STATE"
|
|
: >"$FAIL_O"
|
|
mount() { cat "$MOUNT_STATE"; }
|
|
umount() {
|
|
[ ! -f "$PWD/busy-${1##*/}" ] || return 1
|
|
awk -v point="$1" 'index($0, " on " point " (webdav") == 0' "$MOUNT_STATE" >"$MOUNT_STATE.next"
|
|
mv "$MOUNT_STATE.next" "$MOUNT_STATE"
|
|
}
|
|
` + scriptSection(t, script, "urlencode() {", "\nhash_file() {") +
|
|
scriptSection(t, script, "\ndrive_mount_failures=", "\nmount_shared_folder() {") + `
|
|
sync_drive_mounts '{"shared_drives":["F","O","P"]}'
|
|
sync_drive_mounts '{"shared_drives":["F","O","P"]}'
|
|
test "$(wc -l <"$MOUNT_STATE")" -eq 2
|
|
test "$(readlink "$HOME/Desktop/Windows F")" = "$DRIVE_MOUNT_ROOT/F"
|
|
test ! -L "$HOME/Desktop/Windows O"
|
|
|
|
rm "$FAIL_O"
|
|
sync_drive_mounts '{"shared_drives":["O","P"]}'
|
|
test "$(wc -l <"$MOUNT_STATE")" -eq 2
|
|
test "$(readlink "$HOME/Desktop/Windows O")" = "$DRIVE_MOUNT_ROOT/O"
|
|
test ! -e "$DRIVE_MOUNT_ROOT/F"
|
|
test ! -L "$HOME/Desktop/Windows F"
|
|
test -z "$(ls -A "$TMP_DIR")"
|
|
|
|
ln -s "$TMP_DIR" "$DRIVE_MOUNT_ROOT/F"
|
|
sync_drive_mounts '{"shared_drives":["F","O","P"]}'
|
|
sync_drive_mounts '{"shared_drives":["O","P"]}'
|
|
test -L "$DRIVE_MOUNT_ROOT/F"
|
|
test "$(wc -l <"$MOUNT_STATE")" -eq 2
|
|
|
|
: >"$PWD/busy-P"
|
|
sync_drive_mounts '{"shared_drives":["O"]}'
|
|
test -L "$HOME/Desktop/Windows P"
|
|
test "$(wc -l <"$MOUNT_STATE")" -eq 2
|
|
rm "$PWD/busy-P"
|
|
mkdir "$DRIVE_MOUNT_ROOT/Q"
|
|
printf '/dev/disk2 on %s (apfs, local)\n' "$DRIVE_MOUNT_ROOT/Q" >>"$MOUNT_STATE"
|
|
sync_drive_mounts '{"shared_drives":["O","Q"]}'
|
|
test ! -e "$DRIVE_MOUNT_ROOT/P"
|
|
test ! -L "$HOME/Desktop/Windows P"
|
|
test ! -e "$HOME/Desktop/Windows Q"
|
|
unmount_unused_drives ""
|
|
test -d "$DRIVE_MOUNT_ROOT/Q"
|
|
test "$(wc -l <"$MOUNT_STATE")" -eq 1
|
|
`
|
|
if err := os.WriteFile(filepath.Join(dir, "test.sh"), []byte(harness), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, shBin, "test.sh")
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "LC_ALL=C", "MSYS=winsymlinks:lnk")
|
|
out, err := cmd.CombinedOutput()
|
|
if exit, ok := err.(*exec.ExitError); ok && exit.ExitCode() == 77 {
|
|
t.Skip("Perl is unavailable for reading the seekable credential descriptor")
|
|
}
|
|
const wantLog = "Windows drive O: mount failed (exit 22)\nAuthentication failed\n" +
|
|
"Windows drive F: mount failed (exit 1)\nMount point is a symbolic link: "
|
|
lines := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")
|
|
if err != nil || len(lines) != 4 || !strings.HasPrefix(string(out), wantLog) ||
|
|
!strings.HasSuffix(lines[3], "/home with spaces/ReimsWindowsDrives/F") {
|
|
t.Fatalf("guest drive mount failed: %v\n%s", err, out)
|
|
}
|
|
}
|
|
|
|
func TestShellSingleQuoteAndAgentVersion(t *testing.T) {
|
|
if !strings.Contains(string(GuestAgentScript()), `\"agent_version\":`+strconv.Itoa(CurrentAgentVersion)+",") {
|
|
t.Fatal("GuestAgentScript must report the current version")
|
|
}
|
|
if DriveShareAgentVersion != 8 {
|
|
t.Fatalf("DriveShareAgentVersion = %d, want 8", DriveShareAgentVersion)
|
|
}
|
|
got := shellSingleQuote(`mac's "$(id)"` + " `uname` \\")
|
|
want := `'mac'\''s "$(id)"` + " `uname` \\'"
|
|
if got != want {
|
|
t.Fatalf("shellSingleQuote = %s, want %s", got, want)
|
|
}
|
|
}
|
|
|
|
func scriptSection(t *testing.T, script, start, end string) string {
|
|
t.Helper()
|
|
startAt := strings.Index(script, start)
|
|
if startAt < 0 {
|
|
t.Fatalf("missing script section %q", start)
|
|
}
|
|
section, _, ok := strings.Cut(script[startAt:], end)
|
|
if !ok {
|
|
t.Fatalf("missing end of script section %q", start)
|
|
}
|
|
return section + "\n"
|
|
}
|