Files
Reims-Go/internal/guesttools/folder_agent_test.go
T

407 lines
14 KiB
Go

package guesttools
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestGuestAgentFolderMounts(t *testing.T) {
sh, err := exec.LookPath("sh")
if runtime.GOOS == "windows" {
const gitSH = "C:/Program Files/Git/bin/sh.exe"
if _, statErr := os.Stat(gitSH); statErr == nil {
sh, err = gitSH, nil
}
}
if err != nil {
t.Fatal(err)
}
run := func(t *testing.T, script string, args ...string) {
t.Helper()
cmd := exec.Command(sh, args...)
cmd.Dir = t.TempDir()
if err := os.WriteFile(filepath.Join(cmd.Dir, "mount_webdav"), []byte(folderMountWebDAV), 0o755); err != nil {
t.Fatal(err)
}
cmd.Env = append(os.Environ(), "MSYS=winsymlinks:sys")
cmd.Stdin = strings.NewReader(script)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("sh %v: %v\n%s", args, err, out)
}
}
agent, uninstall, install := string(GuestAgentScript()), string(GuestUninstallScript()), string(GuestInstallScript())
if CurrentAgentVersion != 10 || !strings.Contains(agent, `"agent_version\":10,`) ||
!strings.Contains(install, "Reims Guest Tools 8 installed and connected!") {
t.Fatal("missing Guest Tools version 10")
}
for name, script := range map[string]string{"agent": agent, "uninstaller": uninstall, "installer": install} {
t.Run(name+"_syntax", func(t *testing.T) { run(t, script, "-n") })
}
mounts := scriptSection(t, agent, "urlencode() {", "\nhash_file() {") +
scriptSection(t, agent, "\ndrive_mount_failures=", "\nreport_status() {")
t.Run("reconcile", func(t *testing.T) {
run(t, folderShellFixture+mounts+folderReconcileChecks, "-s")
})
t.Run("mount_ownership", func(t *testing.T) {
run(t, folderShellFixture+mounts+folderOwnershipChecks, "-s")
})
t.Run("ejected_drive_and_orphaned_mode", func(t *testing.T) {
run(t, folderShellFixture+mounts+ejectedShareChecks, "-s")
})
for _, name := range []string{"owned", "user_file", "user_link"} {
t.Run("uninstall_"+name, func(t *testing.T) {
run(t, folderShellFixture+folderUninstallSetup+
"\nrun_uninstall() {\n"+uninstall+"\n}\nrun_uninstall\n"+folderUninstallChecks, "-s", "--", name)
})
}
t.Run("uninstall_busy", func(t *testing.T) {
run(t, folderShellFixture+folderUninstallSetup+
"\nrun_uninstall() {\n"+uninstall+"\n}\n"+folderUninstallBusyChecks+folderUninstallChecks, "-s", "--", "owned")
})
install = strings.Replace(install, `SRC="/Volumes/REIMSTOOLS"`, `SRC="$PWD/tools"`, 1)
for _, scenario := range []string{"new_vm", "new_token", "busy", "same_identity"} {
t.Run("install_"+scenario, func(t *testing.T) {
run(t, folderShellFixture+guestInstallSetup+"\nrun_install() {\n"+install+"\n}\n"+guestInstallChecks,
"-s", "--", scenario)
})
}
start, end := strings.Index(agent, "same_guest_identity() ("), strings.Index(agent, "urlencode() {")
if start < 0 || end <= start {
t.Fatal("missing agent identity and config functions")
}
t.Run("agent_keeps_installed_identity", func(t *testing.T) {
config := strings.ReplaceAll(agent[start:end], "/Volumes/REIMSTOOLS/config", "$PWD/tools/config")
run(t, folderShellFixture+guestInstallSetup+`CONF="$APP_DIR/config"`+"\n"+config+`
expect "$REIMS_TOKEN" old-token
expect "$REIMS_HOST_URL" http://old-host:19847
cmp old-config "$CONF"
printf 'REIMS_HOST_URL=http://new-host:19847\nREIMS_TOKEN=old-token\nREIMS_VM=mac\n' > tools/config
load_conf
expect "$REIMS_HOST_URL" http://new-host:19847
cmp tools/config "$CONF"
`, "-s", "--", "new_token")
})
}
const folderShellFixture = `
set -eu
PATH="$PWD:/usr/bin:/bin:$PATH"
HOME="$PWD/home"
APP_DIR="$HOME/Library/Application Support/ReimsGuestTools"
DRIVE_MOUNT_ROOT="$HOME/ReimsWindowsDrives"
FOLDER_MOUNT_ROOT="$HOME/ReimsSharedFolders"
FOLDER_MODE_DIR="$APP_DIR/folder_modes"
FOLDER_DESKTOP_LINK="$HOME/Desktop/ReimsSharedFolders"
TMP_DIR="$PWD/tmp"
REIMS_HOST_URL=http://stub:19847
sync_keymap_carrier() { :; }
REIMS_TOKEN=token
REIMS_VM=mac
CAFFEINATE_PID=""
count=0
body=""
code=200
busy=0
fail_curl=0
fail_mount=0
export fail_mount TMP_DIR
mkdir -p "$HOME/Desktop" "$TMP_DIR"
: > calls
: > mounts
curl() {
format=""
while [ "$#" -gt 1 ]; do
case "$1" in
-w) format="$2"; shift ;;
esac
shift
done
case "$1" in
*/v1/config) printf '{"shared_folders_count":%s,"shared_drives":[]}\n' "$count" ;;
*/v1/folders)
printf 'folders\n' >> calls
[ "$format" = '\n%{http_code}' ] || return 1
[ "$fail_curl" = 0 ] || return 1
printf '%s\n%s' "$body" "$code"
;;
esac
}
mount() { cat mounts; }
umount() {
printf 'umount %s\n' "${1##*/}" >> calls
[ "$busy" = 0 ] || return 1
grep -Fv "on $1 (" mounts > remaining || true
mv remaining mounts
}
launchctl() { :; }
expect() {
if [ "$1" != "$2" ]; then
printf 'expected <%s>, got <%s>\n' "$2" "$1" >&2
cat calls >&2
exit 1
fi
}
`
const folderMountWebDAV = `#!/usr/bin/perl
use strict;
use warnings;
die "Unexpected credential options" unless @ARGV >= 3 && join(" ", splice(@ARGV, 0, 3)) eq "-a 3 -S";
my $mode = "rw";
if (@ARGV == 6 && join(" ", splice(@ARGV, 0, 2)) eq "-o rdonly") {
$mode = "ro";
}
die "Unexpected mount options" unless @ARGV == 4 && shift(@ARGV) eq "-v";
my ($volume, $url, $target) = @ARGV;
my ($id) = $target =~ m{/ReimsSharedFolders/([a-z0-9_-]+)$};
die "Unexpected WebDAV URL" unless defined($id) && $url eq "http://stub:19847/dav-folders/mac%20test/$id/";
open(my $auth, "<&=3") or die "Missing credential descriptor: $!";
seek($auth, 0, 0) or die "Credential descriptor is not seekable: $!";
binmode($auth);
die "Invalid credential payload" unless do { local $/; <$auth> } eq pack("Na5Na5NNN", 5, "reims", 5, "token", 0, 0, 0);
die "Named credential file remains" if glob("$ENV{TMP_DIR}/webdav-auth.*");
open(my $calls, ">>", "calls") or die "Could not record mount call: $!";
print $calls "mount $mode $volume $url\n";
exit 1 if $ENV{fail_mount};
open(my $current, "<", "mounts") or die "Could not read mounts: $!";
die "Mount point already in use" if grep { index($_, " on $target (") >= 0 } <$current>;
open(my $mounts, ">>", "mounts") or die "Could not record mount: $!";
print $mounts "$url on $target (webdav)\n";
`
const folderReconcileChecks = `
REIMS_VM="mac test"
apply_host_config
[ ! -s calls ]
count=1
body="work|ro|10|Work Files"
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "ro|10"
expect "$(readlink "$FOLDER_DESKTOP_LINK")" "$FOLDER_MOUNT_ROOT"
grep -Fx 'mount ro Work Files http://stub:19847/dav-folders/mac%20test/work/' calls
apply_host_config
expect "$(grep -c '^mount ' calls)" 1
count=0
body=""
code=503
apply_host_config
is_drive_mounted "$FOLDER_MOUNT_ROOT/work"
expect "$(grep -c '^umount ' calls || true)" 0
code=200
fail_curl=1
apply_host_config
is_drive_mounted "$FOLDER_MOUNT_ROOT/work"
fail_curl=0
count=1
body="work|rw|11|Work Files"
busy=1
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "ro|10"
expect "$(grep -c '^mount ' calls)" 1
is_drive_mounted "$FOLDER_MOUNT_ROOT/work"
busy=0
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "rw|11"
grep -Fx 'mount rw Work Files http://stub:19847/dav-folders/mac%20test/work/' calls
body="work|ro|12|Work Files"
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "ro|12"
body="work|ro|13|Work Files"
fail_mount=1
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "ro|12"
expect "$(grep -c '^mount ' calls)" 4
fail_mount=0
apply_host_config
expect "$(cat "$FOLDER_MODE_DIR/work")" "ro|13"
count=0
body=""
apply_host_config
[ ! -s mounts ]
[ ! -d "$FOLDER_MOUNT_ROOT/work" ]
[ ! -e "$FOLDER_MODE_DIR/work" ]
[ ! -L "$FOLDER_DESKTOP_LINK" ]
mkdir -p "$FOLDER_MOUNT_ROOT/empty" "$FOLDER_MOUNT_ROOT/foreign" "$FOLDER_MOUNT_ROOT/full"
printf 'user files' > "$FOLDER_MOUNT_ROOT/full/.hidden"
printf 'smb://files/share on %s (smbfs)\n' "$FOLDER_MOUNT_ROOT/foreign" > mounts
printf 'ro|1\n' > "$FOLDER_MODE_DIR/foreign"
count=3
body='empty|ro|1|Empty
foreign|ro|1|Foreign
full|ro|1|Full'
apply_host_config
is_reims_dav_mounted "$FOLDER_MOUNT_ROOT/empty" "/dav-folders/mac%20test/empty/"
expect "$(grep -c '^umount foreign$' calls || true)" 0
expect "$(grep -c 'dav-folders/mac%20test/full/' calls || true)" 0
count=0
body=""
apply_host_config
[ -d "$FOLDER_MOUNT_ROOT/empty" ]
expect "$(grep -Fc "on $FOLDER_MOUNT_ROOT/empty (" mounts || true)" 0
is_drive_mounted "$FOLDER_MOUNT_ROOT/foreign"
expect "$(cat "$FOLDER_MOUNT_ROOT/full/.hidden")" 'user files'
expect "$(grep -c '^umount foreign$' calls || true)" 0
`
const folderUninstallSetup = `
mkdir -p "$FOLDER_MOUNT_ROOT/work" "$FOLDER_MOUNT_ROOT/user"
mkdir -p "$FOLDER_MODE_DIR" "$DRIVE_MOUNT_ROOT/D" "$FOLDER_MOUNT_ROOT/empty" "$FOLDER_MOUNT_ROOT/state-only"
printf 'REIMS_HOST_URL=http://stub:19847\nREIMS_VM=mac\n' > "$APP_DIR/config"
printf 'user files' > "$FOLDER_MOUNT_ROOT/user/keep"
printf 'ro|1\n' > "$FOLDER_MODE_DIR/state-only"
printf 'http://stub:19847/dav-folders/mac/work/ on %s (webdav)\n' "$FOLDER_MOUNT_ROOT/work" > mounts
printf 'smb://files/share on %s (smbfs)\n' "$FOLDER_MOUNT_ROOT/user" >> mounts
ln -s "$FOLDER_MOUNT_ROOT/user" "$HOME/Desktop/Windows D"
case "$1" in
owned) ln -s "$FOLDER_MOUNT_ROOT" "$FOLDER_DESKTOP_LINK" ;;
user_file) printf 'user link location' > "$FOLDER_DESKTOP_LINK" ;;
user_link) ln -s "$FOLDER_MOUNT_ROOT/user" "$FOLDER_DESKTOP_LINK" ;;
esac
`
const folderUninstallChecks = `
expect "$(cat mounts)" "smb://files/share on $FOLDER_MOUNT_ROOT/user (smbfs)"
[ ! -d "$FOLDER_MOUNT_ROOT/work" ]
[ ! -d "$FOLDER_MOUNT_ROOT/state-only" ]
[ -d "$FOLDER_MOUNT_ROOT/empty" ]
[ ! -d "$APP_DIR" ]
expect "$(cat "$FOLDER_MOUNT_ROOT/user/keep")" 'user files'
expect "$(readlink "$HOME/Desktop/Windows D")" "$FOLDER_MOUNT_ROOT/user"
case "$1" in
owned) [ ! -L "$FOLDER_DESKTOP_LINK" ] ;;
user_file) expect "$(cat "$FOLDER_DESKTOP_LINK")" 'user link location' ;;
user_link) expect "$(readlink "$FOLDER_DESKTOP_LINK")" "$FOLDER_MOUNT_ROOT/user" ;;
esac
`
const folderUninstallBusyChecks = `
busy=1
if (run_uninstall) > uninstall_stdout 2> uninstall_stderr; then
echo "busy uninstall succeeded" >&2
exit 1
fi
grep -q 'shares are busy' uninstall_stderr
expect "$(grep -c 'uninstalled' uninstall_stdout || true)" 0
[ -d "$APP_DIR" ]
[ -d "$FOLDER_MODE_DIR" ]
expect "$(readlink "$FOLDER_DESKTOP_LINK")" "$FOLDER_MOUNT_ROOT"
grep -Fq "on $FOLDER_MOUNT_ROOT/work (" mounts
busy=0
run_uninstall
`
const folderOwnershipChecks = `
mkdir -p "$FOLDER_MOUNT_ROOT/work" "$FOLDER_MOUNT_ROOT/notes" "$FOLDER_MOUNT_ROOT/foreign" "$DRIVE_MOUNT_ROOT/D"
printf 'http://other:19847/dav-folders/mac/work/ on %s (webdav)\n' "$FOLDER_MOUNT_ROOT/work" > mounts
printf 'http://stub:19847/dav-folders/mac/work-extra/ on %s (webdav)\n' "$FOLDER_MOUNT_ROOT/notes" >> mounts
printf 'http://stub:19847/dav-folders/mac/foreign/ on %s (smbfs)\n' "$FOLDER_MOUNT_ROOT/foreign" >> mounts
printf 'http://stub:19847/dav/mac/drives/E/ on %s (webdav)\n' "$DRIVE_MOUNT_ROOT/D" >> mounts
count=3
body='work|ro|1|Work
notes|ro|1|Notes
foreign|ro|1|Foreign'
apply_host_config
sync_drive_mounts '{"shared_drives":["D"]}'
[ ! -L "$FOLDER_DESKTOP_LINK" ]
[ ! -L "$HOME/Desktop/Windows D" ]
count=0
body=''
apply_host_config
expect "$(grep -c '^umount ' calls || true)" 0
expect "$(grep -c '^mount ' calls || true)" 0
expect "$(wc -l < mounts | tr -d ' ')" 4
printf 'https://stub:19847/dav/mac/drives/D/ on %s (webdav)\n' "$DRIVE_MOUNT_ROOT/D" > mounts
! is_reims_dav_mounted "$DRIVE_MOUNT_ROOT/D" '/dav/mac/' || exit 1
is_reims_dav_mounted "$DRIVE_MOUNT_ROOT/D" '/dav/mac/drives/D/'
printf 'http://reims:token@stub:19847/dav/mac on %s (webdav)\n' "$DRIVE_MOUNT_ROOT/D" > mounts
is_reims_dav_mounted "$DRIVE_MOUNT_ROOT/D" '/dav/mac/'
`
const ejectedShareChecks = `
mkdir -p "$DRIVE_MOUNT_ROOT/D" "$FOLDER_MODE_DIR"
ln -s "$DRIVE_MOUNT_ROOT/D" "$HOME/Desktop/Windows D"
printf 'http://stub:19847/dav/mac/drives/D/ on %s (webdav)\n' "$DRIVE_MOUNT_ROOT/D" > mounts
busy=1
sync_drive_mounts '{"shared_drives":[]}'
[ -L "$HOME/Desktop/Windows D" ]
[ -d "$DRIVE_MOUNT_ROOT/D" ]
: > mounts
printf 'ro|1\n' > "$FOLDER_MODE_DIR/orphan"
code=503
apply_host_config
[ -f "$FOLDER_MODE_DIR/orphan" ]
[ ! -L "$HOME/Desktop/Windows D" ]
[ ! -d "$DRIVE_MOUNT_ROOT/D" ]
expect "$(grep -c '^umount ' calls)" 1
code=200
apply_host_config
[ ! -e "$FOLDER_MODE_DIR/orphan" ]
expect "$(grep -c '^folders$' calls)" 2
apply_host_config
expect "$(grep -c '^folders$' calls)" 2
`
const guestInstallSetup = `
SHARE_MOUNT="$HOME/ReimsShareMount"
mkdir -p tools "$SHARE_MOUNT" "$DRIVE_MOUNT_ROOT/D" "$FOLDER_MOUNT_ROOT/work" "$FOLDER_MODE_DIR"
printf 'REIMS_HOST_URL=http://old-host:19847\nREIMS_TOKEN=old-token\nREIMS_VM=mac\n' > "$APP_DIR/config"
cp "$APP_DIR/config" old-config
case "$1" in
new_vm) next_vm=clone; next_token=old-token ;;
same_identity) next_vm=mac; next_token=old-token ;;
*) next_vm=mac; next_token=new-token ;;
esac
printf 'REIMS_HOST_URL=http://new-host:19847\nREIMS_TOKEN=%s\nREIMS_VM=%s\n' "$next_token" "$next_vm" > tools/config
: > tools/guest-tools.sh
ln -s "$SHARE_MOUNT" "$HOME/Desktop/ReimsShare"
ln -s "$DRIVE_MOUNT_ROOT/D" "$HOME/Desktop/Windows D"
ln -s "$FOLDER_MOUNT_ROOT" "$FOLDER_DESKTOP_LINK"
printf 'ro|1\n' > "$FOLDER_MODE_DIR/work"
printf 'rw|2\n' > "$FOLDER_MODE_DIR/orphan"
: > "$FOLDER_MODE_DIR/.mount_root"
printf 'http://old-host:19847/dav/mac/ on %s (webdav)\n' "$SHARE_MOUNT" > mounts
printf 'http://old-host:19847/dav/mac/drives/D/ on %s (webdav)\n' "$DRIVE_MOUNT_ROOT/D" >> mounts
printf 'http://old-host:19847/dav-folders/mac/work/ on %s (webdav)\n' "$FOLDER_MOUNT_ROOT/work" >> mounts
cp mounts old-mounts
`
const guestInstallChecks = `
if [ "$1" = busy ]; then
busy=1
mkdir -p "$HOME/Library/LaunchAgents"
printf 'old plist\n' > "$HOME/Library/LaunchAgents/com.reimsgo.guest-tools.plist"
if (run_install) > install_stdout 2> install_stderr; then
echo 'busy install succeeded' >&2
exit 1
fi
grep -q 'shares are busy' install_stderr
cmp old-config "$APP_DIR/config"
cmp old-mounts mounts
[ -L "$HOME/Desktop/Windows D" ]
[ -L "$FOLDER_DESKTOP_LINK" ]
[ -f "$FOLDER_MODE_DIR/work" ]
expect "$(cat "$HOME/Library/LaunchAgents/com.reimsgo.guest-tools.plist")" 'old plist'
busy=0
fi
run_install
cmp tools/config "$APP_DIR/config"
if [ "$1" = same_identity ]; then
cmp old-mounts mounts
[ -f "$FOLDER_MODE_DIR/work" ]
[ ! -s calls ]
else
[ ! -s mounts ]
[ ! -L "$FOLDER_DESKTOP_LINK" ]
[ ! -L "$HOME/Desktop/Windows D" ]
expect "$(readlink "$HOME/Desktop/ReimsShare")" "$HOME/ReimsShare"
[ ! -f "$FOLDER_MODE_DIR/work" ]
[ ! -f "$FOLDER_MODE_DIR/orphan" ]
[ ! -f "$FOLDER_MODE_DIR/.mount_root" ]
fi
`