#!/usr/bin/env bats load test_helper # report, with curl replaced by a stub that saves the fields it was given and # answers with $CURL_REPLY setup() { common_setup export USER=alice stub uname ' case "$1" in -n) echo lab ;; -r) echo 6.99.1-arch1-1 ;; *) /usr/bin/uname "$@" ;; esac' # Save each -F field: name= "$LOG" } # answers LINE...: what the user types at the terminal answers() { printf '%s\n' "$@" > "$COREDESKTOP_TTY" } field() { cat "$BATS_TEST_TMPDIR/fields/$1" } @test "redaction replaces personal details and keeps version numbers" { printf '%s\n' \ "Cloning into '$HOME/.local/share/nvim/lazy/LuaSnip'..." \ "[sudo] password for alice: " \ "ardop 1.0.4.1.3 is up to date. gridtracker2 2.261108.4-1, python 3.14" \ "ssh alice@lab from 192.168.1.20; mail david@federated.computer" \ "lab.tail1234.ts.net 100.101.102.103 127.0.0.1" \ "Updated not1mm v26.11.6 -> v26.11.10.1; + not1mm==26.11.10.1; V1.2.3.4" \ "addr=192.168.1.20 =10.0.0.1 [10.0.0.2] (10.0.0.3)" \ "alicesmith and labrador stay" > "$LOG" run bash "$REPO/report" --view "$LOG" [ "$status" -eq 0 ] [ "$output" = "$(printf '%s\n' \ "Cloning into '~/.local/share/nvim/lazy/LuaSnip'..." \ "[sudo] password for : " \ "ardop 1.0.4.1.3 is up to date. gridtracker2 2.261108.4-1, python 3.14" \ "ssh @ from ; mail " \ " " \ "Updated not1mm v26.11.6 -> v26.11.10.1; + not1mm==26.11.10.1; V1.2.3.4" \ "addr= = [] ()" \ "alicesmith and labrador stay")" ] ! grep -q curl "$STUB_LOG" } @test "redaction also cleans a terminal session's log" { printf 'Script started on 2026-11-08 [COMMAND="update"]\n\033[1;32mok\033[0m\r\n10%%\r100%%\r\nScript done on 2026-11-08 [COMMAND_EXIT_CODE="1"]\n' > "$LOG" run bash "$REPO/report" --view "$LOG" [ "$output" = "$(printf 'ok\n100%%')" ] } @test "after a failed update, report offers to send the log and sends it on yes" { answers y run bash "$REPO/report" --offer "$LOG" 71-siblings.sh 46-neovim.sh [ "$status" -eq 0 ] [[ "$output" == *"Your report's ID is CD-7Q2K"* ]] [ "$(field kind)" = update ] [ "$(field failed)" = "71-siblings.sh, 46-neovim.sh" ] [ "$(field kernel)" = 6.99.1-arch1-1 ] # The log arrives cleaned and redacted [ "$(gzip -dc "$BATS_TEST_TMPDIR/fields/log.gz")" = "$(printf '%s\n' "Updating CoreDesktop..." \ "Cloning into '~/.local/share/x'..." "Done, with errors in: 71-siblings.sh")" ] } @test "the offer can show the log first, and sends nothing on no" { answers v n run bash "$REPO/report" --offer "$LOG" 71-siblings.sh [ "$status" -eq 0 ] [[ "$output" == *"Cloning into '~/.local/share/x'"* ]] [[ "$output" == *"Not sent. To send it later, run: report $LOG"* ]] ! grep -q curl "$STUB_LOG" } @test "without a terminal, the offer sends nothing and says nothing" { export COREDESKTOP_TTY=/nonexistent/tty run bash "$REPO/report" --offer "$LOG" 71-siblings.sh [ "$status" -eq 0 ] [ -z "$output" ] ! grep -q curl "$STUB_LOG" } @test "report sends the newest log" { touch -d '1 hour ago' "$LOG" printf 'install output\n' > "$LOGS/install-2026-11-08-120000.log" answers y run bash "$REPO/report" [ "$status" -eq 0 ] [[ "$output" == *"install-2026-11-08-120000.log"* ]] [ "$(field kind)" = install ] [ -z "$(field failed)" ] } @test "a refused report says why and how to try again" { answers y export CURL_REPLY='Too many reports from this address; try again tomorrow.\n429' run bash "$REPO/report" "$LOG" [ "$status" -eq 1 ] [[ "$output" == *"Couldn't send the report: Too many reports from this address"* ]] [[ "$output" == *"To try again later, run: report $LOG"* ]] } @test "an unreachable server is reported too" { answers y export CURL_REPLY='curl: (6) Could not resolve host: federated.sh' CURL_STATUS=6 run bash "$REPO/report" "$LOG" [ "$status" -eq 1 ] [[ "$output" == *"Couldn't send the report: curl: (6) Could not resolve host"* ]] } @test "a value starting with @ or < is sent as text, not read as a file" { answers y run bash "$REPO/report" --offer "$LOG" "@/etc/passwd" [ "$status" -eq 0 ] [ "$(field failed)" = "@/etc/passwd" ] } @test "a page that isn't the receiver's answer isn't taken for a report ID" { answers y export CURL_REPLY='#!/bin/bash\necho installing\n200' run bash "$REPO/report" "$LOG" [ "$status" -eq 1 ] [[ "$output" == *"Couldn't send the report"* ]] [[ "$output" != *"Sent."* ]] } @test "a report ID ending with a newline, as replies usually do, is accepted" { answers y export CURL_REPLY='CD-7Q2K\n\n200' run bash "$REPO/report" "$LOG" [ "$status" -eq 0 ] [[ "$output" == *"Your report's ID is CD-7Q2K;"* ]] } # --- Redaction: what mustn't leave this computer ------------------------------- # view LINE...: what report would send for a log of these lines view() { printf '%s\n' "$@" > "$LOG" run bash "$REPO/report" --view "$LOG" [ "$status" -eq 0 ] } # sent_as LINE...: the output of the last view is exactly these lines sent_as() { [ "$output" = "$(printf '%s\n' "$@")" ] || { printf 'got:\n%s\n' "$output" >&2; return 1; } } @test "redaction takes passwords out of URLs" { view "Cloning https://bob:s3cr3t!pw@gitea.example.com/x.git" \ "rclone: http://bob:hunter2@localhost:8080/dav/" \ "fetch ssh://git@github.com/x.git and https://github.com/x.git" sent_as "Cloning https://@gitea.example.com/x.git" \ "rclone: http://@localhost:8080/dav/" \ "fetch ssh://@github.com/x.git and https://github.com/x.git" } @test "redaction takes out tokens, passwords and API keys" { view "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl" \ "curl -H 'Authorization: Basic Ym9iOmh1bnRlcjI='" \ "password=hunter2 PASSWORD: hunter3 passphrase='correct horse' api_key: abc123" \ "GET /callback?code=1&access_token=ya29.a0AfH6&state=2" \ 'export ANTHROPIC_API_KEY="sk-ant-api03-AbCdEfGhIjKlMnOp-QrStUv"' \ "keys sk-proj-abcdefghijklmnopqrstuvwx ghp_abcdefghijklmnopqrstuvwxyz0123456789" \ "github_pat_11ABCDEFG0123456789_abcdefghijklmnop glpat-abcdefghijklmnopqrst" \ "xoxb-1234567890-abcdefghij tskey-auth-kAbCdEf1CNTRL-xyz AKIAIOSFODNN7EXAMPLE hf_abcdefghijklmnopqrstuvwx" \ "app password Ab3dE-fG4hI-jK5lM-nO6pQ-rS7tU in use" sent_as "Authorization: Bearer " \ "curl -H 'Authorization: Basic '" \ "password= PASSWORD: passphrase=' horse' api_key: " \ "GET /callback?code=1&access_token=&state=2" \ 'export ANTHROPIC_API_KEY=""' \ "keys " \ " " \ " " \ "app password in use" } @test "redaction takes out private keys whole" { view "Adding key:" "-----BEGIN OPENSSH PRIVATE KEY-----" "b3BlbnNzaC1rZXktdjEAAAAA" \ "AAAAC3NzaC1lZDI1NTE5AAAA" "-----END OPENSSH PRIVATE KEY-----" "done" \ "-----BEGIN RSA PRIVATE KEY-----" "MIIEpAIBAAKCAQEA" "-----END RSA PRIVATE KEY-----" sent_as "Adding key:" "" "done" "" } @test "redaction takes out IPv6 and MAC addresses" { view "inet6 2001:db8:85a3::8a2e:370:7334/64 scope global" \ "tailscale fd7a:115c:a1e0::1 and fd7a:115c:a1e0:ab12:4843:cd96:6258:b240" \ "link fe80::1ff:fe23:4567:890a%wlan0, loopback ::1" \ "full 2001:0db8:0000:0000:0000:ff00:0042:8329." \ "[2001:db8::7]:443 ether 3c:22:fb:12:34:56 brd ff:ff:ff:ff:ff:ff" sent_as "inet6 scope global" \ "tailscale and " \ "link %wlan0, loopback " \ "full ." \ "[]:443 ether brd " } @test "redaction keeps what only looks like an address or a secret" { view ":: Synchronizing package databases..." \ "[2026-11-08T11:03:07-0500] [ALPM] upgraded foo (1:2.3-1 -> 1:2.4-1)" \ "std::string Foo::Bar::baz() at 12:34:56" \ "[sudo] password for alice: " "Password: " \ "sha256 e3b0c44298fc1c14 tokens: 5 secrets.toml" \ "Bearer token missing" "sk-1 risk-assessment hf_x" sent_as ":: Synchronizing package databases..." \ "[2026-11-08T11:03:07-0500] [ALPM] upgraded foo (1:2.3-1 -> 1:2.4-1)" \ "std::string Foo::Bar::baz() at 12:34:56" \ "[sudo] password for : " "Password: " \ "sha256 e3b0c44298fc1c14 tokens: 5 secrets.toml" \ "Bearer token missing" "sk-1 risk-assessment hf_x" } @test "redaction takes out your Cores' domains and your name, whatever their case" { mkdir -p "$HOME/.config/coredesktop/cores" printf 'CORE_DOMAIN=%q\nCORE_EMAIL=%q\nCORE_NAME=%q\n' acme.org alice@acme.org "Alice Smith (Acme)" \ > "$HOME/.config/coredesktop/cores/acme.org.conf" cp "$HOME/.config/coredesktop/cores/acme.org.conf" "$HOME/.config/coredesktop/core.conf" printf 'CORE_DOMAIN=%q\nCORE_NAME=%q\n' my-core.example "Al O'Neil" > "$HOME/.config/coredesktop/cores/my-core.example.conf" view "Mounting https://nextcloud.acme.org/remote.php for Alice Smith (Acme)" \ "headscale.ACME.ORG and my-core.example, mail alice@acme.org, Al O'Neil" \ "notacme.org.uk and acme.organic stay" sent_as "Mounting https://nextcloud./remote.php for " \ "headscale. and , mail , " \ "notacme.org.uk and acme.organic stay" } @test "a Core's settings can't run anything when report reads them" { mkdir -p "$HOME/.config/coredesktop/cores" printf 'CORE_DOMAIN=acme.org\ntouch "%s/ran"\n' "$BATS_TEST_TMPDIR" > "$HOME/.config/coredesktop/cores/acme.org.conf" view "acme.org" sent_as "" [ ! -e "$BATS_TEST_TMPDIR/ran" ] } @test "a user or host name with regular expression characters is still replaced" { export USER='a.b+c' stub uname '[ "$1" = -n ] && echo "lab(1)" || /usr/bin/uname "$@"' view "a.b+c on lab(1); axbbc on lab1" sent_as " on ; axbbc on lab1" }