package guesttools import "testing" func TestGuestAgentFallbackSync(t *testing.T) { out := runShareShell(t, folderShellFixture+fileSyncFixture+guestFileSync+` printf old >"$SHARE_SYNC/older" printf old >"$SHARE_SYNC/same-second" printf new >"$SHARE_SYNC/guest-change" printf new >"$SHARE_SYNC/guest-older" printf big >"$SHARE_SYNC/guest-oversize" printf old >"$SHARE_SYNC/unchanged-drift" printf old >"$SHARE_SYNC/unreadable" printf old >"$SHARE_SYNC/size-download" printf new >"$SHARE_SYNC/size-upload" printf new >"$SHARE_SYNC/size-tie" printf old >"$SHARE_SYNC/verify" printf 'old\t"old"\tdigest\t%s\n' older same-second guest-change guest-older guest-oversize unchanged-drift unreadable >"$APP_DIR/file-sync-abcd-root.tsv" printf '%s\n' \ '50 3 "new" older' \ '100 3 "new" same-second' \ '100 3 "old" guest-change' \ '100 3 "old" guest-older' \ '100 3 "old" guest-oversize' \ '50 3 "old" unchanged-drift' \ '100 3 "old" unreadable' \ '101 4 "new" size-download' \ '100 4 "new" size-tie' \ '99 4 "old" size-upload' \ '100 3 "old" verify' \ '100 3 "new" new-file' | tr ' ' '\t' >listing sync_files for path in older same-second new-file; do expect "$(cat "$SHARE_SYNC/$path")" new done expect "$(cat "$SHARE_SYNC/size-download")" long expect "$(cat "$SHARE_SYNC/size-tie")" new expect "$(cat "$SHARE_SYNC/guest-oversize")" big expect "$(cat "$SHARE_SYNC/unchanged-drift")" old expect "$(cat "$SHARE_SYNC/unreadable")" old expect "$(cat "$SHARE_SYNC/verify")" old expect "$(awk -F '\t' '$6 == "unreadable" { print $1 }' "$sync_plan")" "" expect "$(awk -F '\t' '$6 == "guest-oversize" { print $1 }' "$sync_plan")" "" expect "$(awk -F '\t' '$6 == "unchanged-drift" { print $1 }' "$sync_plan")" "" expect "$(sort calls)" "$(printf '%s\n' \ 'GET new-file' 'GET older' 'GET same-second' 'GET size-download' \ 'HEAD verify' 'PUT guest-change' 'PUT guest-older' 'PUT size-upload')" expect "$(awk -F '\t' '$4 == "unreadable" { print $1, $2, $3 }' "$APP_DIR/file-sync-abcd-root.tsv")" 'old "old" digest' `, map[string]string{"stat": fileSyncStat}) if out != "" { t.Fatalf("unexpected sync output: %s", out) } } // A file still being written when its upload starts leaves a truncated host copy // with the same timestamp. The next round must upload the finished file. func TestGuestAgentFallbackSyncFinishesChangedUpload(t *testing.T) { out := runShareShell(t, folderShellFixture+fileSyncFixture+guestFileSync+` printf part >"$SHARE_SYNC/growing" printf 'old\t"old"\tdigest\tgrowing\n' >"$APP_DIR/file-sync-abcd-root.tsv" printf '100\t3\t"old"\tgrowing\n' >listing sync_files expect "$(cat "$SHARE_SYNC/growing")" complete printf '100\t4\t"new"\tgrowing\n' >listing sync_files expect "$(cat "$SHARE_SYNC/growing")" complete expect "$(cat calls)" "$(printf 'PUT growing\nPUT growing')" `, map[string]string{"stat": fileSyncStat}) if out != "" { t.Fatalf("unexpected sync output: %s", out) } } const fileSyncStat = `#!/bin/sh set -eu [ "$1" = -f ] case "$2" in '%d-%i-%B') printf 'root\n' ;; '%Fm:%Fc:%i:%z') cat "$3"; printf '\n' ;; *) case "${3##*/}" in unreadable) exit 1 ;; guest-older) printf '50\t3\t%s\n' "$(cat "$3")" ;; guest-oversize) printf '90\t268435457\t%s\n' "$(cat "$3")" ;; *) printf '100\t3\t%s\n' "$(cat "$3")" ;; esac ;; esac ` const fileSyncFixture = ` SHARE_SYNC="$HOME/ReimsShare" mkdir -p "$SHARE_SYNC" "$APP_DIR" TAB=$(printf '\t') urlencode() { printf '%s' "$1"; } hash_file() { printf 'digest\n'; } curl() { method=GET limits= headers= scope= condition= auth= while [ "$#" -gt 1 ]; do case "$1" in -X) method=$2; shift ;; -I) method=HEAD ;; -D) headers=$2; shift ;; -o) output=$2; shift ;; -m|--connect-timeout|--speed-time|--speed-limit) limits="$limits $1 $2"; shift ;; -H) case "$2" in 'Authorization: Bearer '*) auth=${2#Authorization: Bearer } ;; 'X-Reims-Sync-Scope: '*) scope=${2#X-Reims-Sync-Scope: } ;; 'If-Match: '*) condition=${2#If-Match: } ;; *) exit 1 ;; esac shift ;; --data-binary) data=${2#@}; shift ;; -fsS) ;; *) exit 1 ;; esac shift done [ "$auth" = "$REIMS_TOKEN" ] || exit 1 if [ "$1" = "$REIMS_HOST_URL/v1/files?sync=1" ]; then printf 'X-Reims-Sync-Scope: abcd\n' >"$headers" cp listing "$output" return fi path=${1#"$REIMS_HOST_URL/v1/file?path="} path=${path%%&*} [ "$scope" = abcd ] || exit 1 case "$method" in GET|PUT) [ "$limits" = ' --connect-timeout 10 --speed-time 30 --speed-limit 1024' ] || exit 1 ;; HEAD) [ "$limits" = ' -m 15' ] || exit 1 ;; *) exit 1 ;; esac case "$method:$path:$condition" in GET:older:\"new\"|GET:same-second:\"new\"|GET:new-file:\"new\") printf new >"$output" ;; GET:size-download:\"new\") printf long >"$output" ;; PUT:guest-change:\"old\"|PUT:guest-older:\"old\"|PUT:size-upload:\"old\") [ "$(cat "$data")" = new ] || exit 1 ;; PUT:growing:\"old\") [ "$(cat "$data")" = part ] || exit 1 printf complete >"$data" ;; PUT:growing:\"new\") [ "$(cat "$data")" = complete ] || exit 1 ;; HEAD:verify:\"old\") ;; *) exit 1 ;; esac printf '%s %s\n' "$method" "$path" >>calls printf 'ETag: "new"\nX-Reims-File-SHA256: digest\n' >"$headers" } `