#!/usr/bin/env bats load test_helper # Taildrop: Files' "Send with Tailscale" script, and the captive portal helper, # with a fake tailscale setup() { common_setup stub notify-send echo one > "$BATS_TEST_TMPDIR/one.txt" echo two > "$BATS_TEST_TMPDIR/two.txt" mkdir "$BATS_TEST_TMPDIR/folder" stub tailscale 'if [ "$3" = --targets ]; then printf "100.64.0.2\tlaptop\n100.64.0.3\tdesktop\n"; fi' stub zenity 'echo 100.64.0.3' } @test "Send with Tailscale sends the chosen files to the chosen computer" { NAUTILUS_SCRIPT_SELECTED_FILE_PATHS="$BATS_TEST_TMPDIR/one.txt"$'\n'"$BATS_TEST_TMPDIR/folder"$'\n'"$BATS_TEST_TMPDIR/two.txt"$'\n' \ run bash "$REPO/system/send-with-tailscale" [ "$status" -eq 0 ] called tailscale file cp --update-interval=0 -- "$BATS_TEST_TMPDIR/one.txt" "$BATS_TEST_TMPDIR/two.txt" 100.64.0.3: grep -q '^notify-send .*Sent to desktop 2 files$' "$STUB_LOG" } @test "Send with Tailscale does nothing when the choice is cancelled" { stub zenity 'exit 1' run bash "$REPO/system/send-with-tailscale" "$BATS_TEST_TMPDIR/one.txt" [ "$status" -eq 0 ] uncalled tailscale file cp --update-interval=0 -- "$BATS_TEST_TMPDIR/one.txt" 100.64.0.3: } @test "Send with Tailscale says when no computer can take files" { stub tailscale run bash "$REPO/system/send-with-tailscale" "$BATS_TEST_TMPDIR/one.txt" [ "$status" -eq 1 ] grep -q '^notify-send .*No computer to send to' "$STUB_LOG" not_called zenity } @test "Send with Tailscale says when sending fails" { stub tailscale 'if [ "$3" = --targets ]; then printf "100.64.0.3\tdesktop\n"; else echo "no route"; exit 1; fi' run bash "$REPO/system/send-with-tailscale" "$BATS_TEST_TMPDIR/one.txt" [ "$status" -eq 1 ] grep -q "^notify-send .*Couldn't send to desktop no route$" "$STUB_LOG" } # --- Captive portal ---------------------------------------------------------- portal_setup() { export CAPTIVE_PORTAL_STATE="$BATS_TEST_TMPDIR/paused" export EXIT_NODE="${EXIT_NODE-100.64.0.9/32}" stub logger stub tailscale 'if [ "$1" = status ]; then if [ -n "$EXIT_NODE" ]; then echo "{\"ExitNodeStatus\": {\"TailscaleIPs\": [\"$EXIT_NODE\"]}}"; else echo "{}"; fi fi' } @test "at a captive portal, the exit node is paused, and comes back once online" { portal_setup CONNECTIVITY_STATE=PORTAL run bash "$REPO/system/captive-portal" wlan0 connectivity-change [ "$status" -eq 0 ] called tailscale set --exit-node= [ "$(cat "$CAPTIVE_PORTAL_STATE")" = 100.64.0.9 ] EXIT_NODE="" CONNECTIVITY_STATE=FULL run bash "$REPO/system/captive-portal" wlan0 connectivity-change [ "$status" -eq 0 ] called tailscale set --exit-node=100.64.0.9 [ ! -e "$CAPTIVE_PORTAL_STATE" ] } @test "a captive portal changes nothing without an exit node" { EXIT_NODE="" portal_setup CONNECTIVITY_STATE=PORTAL run bash "$REPO/system/captive-portal" wlan0 connectivity-change CONNECTIVITY_STATE=FULL run bash "$REPO/system/captive-portal" wlan0 connectivity-change uncalled tailscale set --exit-node= [ ! -e "$CAPTIVE_PORTAL_STATE" ] } @test "an exit node chosen while at the portal is left alone" { portal_setup echo 100.64.0.9 > "$CAPTIVE_PORTAL_STATE" EXIT_NODE=100.64.0.7/32 CONNECTIVITY_STATE=FULL run bash "$REPO/system/captive-portal" wlan0 connectivity-change [ "$status" -eq 0 ] refute grep -q '^tailscale set' "$STUB_LOG" [ ! -e "$CAPTIVE_PORTAL_STATE" ] } @test "the captive portal helper ignores other events" { portal_setup CONNECTIVITY_STATE=PORTAL run bash "$REPO/system/captive-portal" wlan0 up [ "$status" -eq 0 ] not_called tailscale }