Skip to content
pi-c Artifact [49e9a91000]
Login

Artifact [49e9a91000]

Artifact 49e9a910008350d24dc2a7225f9c28705fb9cc6c50ff98b40db7a07ed46d9819:


#!/bin/sh
# Remote control pairing, device management, unpairing, stop/reset, and rekey.
# Usage: test/pic/remote_pair.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
RELAY=${RELAY:?set RELAY to the fake_relay binary}
PHONE=${PHONE:?set PHONE to the fake_phone binary}
SQLITE3=${SQLITE3:-sqlite3}
FAULTS=1
[ "${1:-}" = --no-faults ] && FAULTS=0
here=$(cd "$(dirname "$0")" && pwd)
S=$here/streams
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-remote-pair.XXXXXX")
export HOME="$T/home"
. "$here/remote_lib.sh"

start_relay

# ---- pairing -------------------------------------------------------------------------------
# Without the invite the relay refuses a new mailbox (403), and pairing says which setting to set.
if "$PIC" remote pair --pipe < /dev/null > "$T/noinv.out" 2> "$T/noinv.err"; then fail "invite: paired without one"; fi
grep -qF 'remote.invite' "$T/noinv.err" || fail "invite: message: $(cat "$T/noinv.err")"
"$PIC" settings remote.invite test-invite > /dev/null
printf 'PASS: a relay that wants an invite: pairing names the setting\n'

# The happy path: the two codes match, a yes commits one sealed device row, the phone hears paired,
# and the pairing channel is gone at the relay afterwards.
offer
case "$URL" in "$BASE/#p1."*) ;; *) fail "offer: address $URL" ;; esac
phone_pair "$T/ph1" "Phone One"
await "$PO" '"type":"code"'
await "$T/ph1.out" 'code '
want=$(sed -n 's/^code //p' "$T/ph1.out")
grep -qF "\"code\":\"$want\"" "$PO" || fail "pair: the codes differ: $(cat "$PO") / $want"
grep -qF '"name":"Phone One"' "$PO" || fail "pair: name: $(cat "$PO")"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 0 ] || fail "pair: a row before the yes"
echo y >&3
gone $PH
[ "$rc" = 0 ] || fail "pair: phone exit $rc"
grep -qx paired "$T/ph1.out" || fail "pair: phone: $(cat "$T/ph1.out")"
gone $PP
exec 3>&-
[ "$rc" = 0 ] || fail "pair: pic exit $rc"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 1 ] || fail "pair: rows"
[ "$(uq 'SELECT count(*) FROM picu_remote_key')" = 3 ] || fail "pair: keys"
uq "SELECT sealed FROM picu_remote_device" | grep -qF 'Phone One' && fail "pair: the device row is not sealed"
state | grep -qF 'channels 1 ' || fail "pair: relay channels: $(state)"
"$PIC" remote devices > "$T/devices.out" || fail "devices"
grep -qF 'Phone One' "$T/devices.out" || fail "devices: $(cat "$T/devices.out")"
grep -qF 'not running' "$T/devices.out" || fail "devices: running: $(cat "$T/devices.out")"
printf 'PASS: pairing: matching codes, a yes, one sealed row, the pairing channel deleted\n'

# The same address again: the offer was spent, its channel deleted; the phone hears it is gone.
phone_pair "$T/ph1b" "Phone Again"
gone $PH
[ "$rc" = 4 ] || fail "reused: phone exit $rc: $(cat "$T/ph1b.out")"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 1 ] || fail "reused: rows"
printf 'PASS: a used pairing address is refused by the relay\n'

# A no at the terminal: the phone hears pair_refused and nothing is written.
offer
phone_pair "$T/ph2" "Phone No"
await "$PO" '"type":"code"'
echo n >&3
gone $PH
[ "$rc" = 3 ] || fail "no: phone exit $rc: $(cat "$T/ph2.out")"
gone $PP
exec 3>&-
[ "$rc" != 0 ] || fail "no: pic exit 0"
grep -qF '"type":"refused"' "$PO" || fail "no: $(cat "$PO")"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 1 ] || fail "no: rows"
state | grep -qF 'channels 1 ' || fail "no: relay channels: $(state)"
printf 'PASS: a no at the terminal: the phone is refused, nothing is written\n'

# The wrong secret: a first message that does not verify is ignored (the phone hears nothing), and
# five end the offer.
offer
for k in 1 2 3 4 5; do phone_pair "$T/bad$k" "Bad $k" wrong-psk; done
await "$PO" 'too many pairing attempts failed'
gone $PP
exec 3>&-
[ "$rc" != 0 ] || fail "wrong psk: pic exit 0"
grep -qF '"type":"code"' "$PO" && fail "wrong psk: a code was shown"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 1 ] || fail "wrong psk: rows"
for k in 1 2 3 4 5; do grep -qF 'paired' "$T/bad$k.out" && fail "wrong psk: phone $k paired"; done
printf 'PASS: a first message under the wrong secret is ignored; five end the offer\n'

# An offer that expires: a phone past its time refuses it itself; one that got as far as the code
# hears pair_refused when it ends, and the channel goes.
PIC_TEST_PAIR_TTL=3 offer
phone_pair "$T/ph3" "Phone Slow"
await "$PO" '"type":"code"'
gone $PH
[ "$rc" = 3 ] || fail "expired: phone exit $rc: $(cat "$T/ph3.out")"
gone $PP
exec 3>&-
grep -qF '"type":"expired"' "$PO" || fail "expired: $(cat "$PO")"
sleep 1
phone_pair "$T/ph3b" "Phone Late"
gone $PH
[ "$rc" = 5 ] || fail "expired: late phone exit $rc: $(cat "$T/ph3b.out")"
state | grep -qF 'channels 1 ' || fail "expired: relay channels: $(state)"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 1 ] || fail "expired: rows"
printf 'PASS: an expired offer: refused to a phone mid-pairing, and by a phone that comes late\n'

# The phone is reconnecting when paired is posted (the relay says Pic-Peer: 0): the pairing channel
# stays until the phone reads again, so paired is not lost with it.
offer
phone_pair "$T/ph4" "Phone Away" "late 2500"
await "$T/ph4.out" away
echo y >&3
gone $PH
[ "$rc" = 0 ] || fail "away: phone exit $rc: $(cat "$T/ph4.out")"
gone $PP
exec 3>&-
[ "$rc" = 0 ] || fail "away: pic exit $rc"
[ "$(uq 'SELECT count(*) FROM picu_remote_device')" = 2 ] || fail "away: rows"
state | grep -qF 'channels 2 ' || fail "away: relay channels: $(state)"
printf 'PASS: paired reaches a phone that was reconnecting; the pairing channel goes after\n'

DEV1=$(uq "SELECT lower(hex(id)) FROM picu_remote_device WHERE paired_ms = (SELECT min(paired_ms) FROM picu_remote_device)")
DEVJ=$("$PIC" remote devices --json)
DEV1B=$("$SQLITE3" :memory: "SELECT substr(json_extract(value, '\$.origin'), 8) FROM json_each(json_extract('$DEVJ', '\$.devices'))
    WHERE json_extract(value, '\$.id') = '$DEV1'")
[ -n "$DEV1B" ] || fail "devices --json: no origin for $DEV1: $DEVJ"


serve "sse:$S/text.sse"
pic ask 'first question' > /dev/null 2> "$T/ask.err" || fail "ask: $(cat "$T/ask.err")"
remote

# A phone renames a device (#111): the name as pairing cleans it (control characters out, trimmed),
# the device list sent to every phone in a session, the terminal's list the same; an empty name or
# an unknown device is refused.
script "$T/ph1" <<EOS
send {"type":"rename_device","payload":{"device":"remote:$DEV1B","name":"  Phone\u0007 Uno  "}}
expect "name":"Phone Uno"
send {"type":"rename_device","payload":{"device":"$DEV1B","name":"   "}}
expect "code":"bad_message"
send {"type":"rename_device","payload":{"device":"remote:AAAAAAAAAAAAAAAAAAAAAA","name":"x"}}
expect "code":"no_device"
EOS
done_ok $PR "rename"
"$PIC" remote devices | grep -q 'Phone Uno' || fail "rename: the terminal's list: $("$PIC" remote devices)"
script "$T/ph1" <<EOS
send {"type":"rename_device","payload":{"device":"$DEV1B","name":"Phone One"}}
expect "name":"Phone One"
EOS
done_ok $PR "rename back"
printf 'PASS: a phone renames a device; every phone and the terminal see the new name\n'
kill "$spid" 2> /dev/null

# ---- pairing from a paired phone (protocol, Amendments) -----------------------------------------
phone_offer() { # the URL in DIR.run's last pair_offer
    await "$1.run" '"type":"pair_offer"'
    URL=$(sed -n 's/.*"type":"pair_offer".*"url":"\([^"]*\)".*/\1/p' "$1.run" | tail -1)
    [ -n "$URL" ] || fail "phone offer: no address: $(cat "$1.run")"
}
rows=$(uq "SELECT count(*) FROM picu_remote_device")

# A paired phone asks for an offer, a new device pairs with it, both show the same six digits, and
# the paired phone's approval pairs it: nobody at the computer. A replay of the approval's record
# does nothing.
script "$T/ph1" <<EOS
send {"type":"devices"}
expect "type":"devices"
send {"type":"pair_offer"}
expect "type":"pair_offer"
expect "type":"pair_request"
send {"type":"pair_approve","payload":{"code":"\$CODE"}}
expect "result":"paired"
expect "name":"Phone Five"
replay
expectnot "type":"error" 2000
EOS
A=$PR
phone_offer "$T/ph1"
case "$URL" in "$BASE/#p1."*) ;; *) fail "phone offer: address $URL" ;; esac
phone_pair "$T/ph5" "Phone Five"
await "$T/ph5.out" 'code '
await "$T/ph1.run" '"type":"pair_request"'
c5=$(sed -n 's/^code //p' "$T/ph5.out" | tr -d ' ')
grep -qF "\"name\":\"Phone Five\",\"code\":\"$c5\"" "$T/ph1.run" || fail "phone pairing: the codes differ: $c5 / $(grep pair_request "$T/ph1.run")"
gone $PH
[ "$rc" = 0 ] || fail "phone pairing: new device exit $rc: $(cat "$T/ph5.out")"
done_ok $A "phone pairing: approver"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = $((rows + 1)) ] || fail "phone pairing: rows"
await "$RO" 'Phone One paired Phone Five'
DEV5B=$(sed -n 's/.*"result":"paired".*"device":"\([^"]*\)".*/\1/p' "$T/ph1.run" | head -1)
[ -n "$DEV5B" ] || fail "phone pairing: no device id: $(grep pair_end "$T/ph1.run")"
script "$T/ph5" <<EOS
send {"type":"ping"}
expect {"type":"pong"}
EOS
done_ok $PR "phone pairing: the new device's session"
printf 'PASS: a paired phone adds a device: the same six digits on both, its approval pairs it, a replay does nothing\n'

# A rival used the offer first: the approving phone shows the rival's name and digits, the owner's
# device gets no answer, and a no from the phone pairs nobody.
script "$T/ph1" <<EOS
send {"type":"pair_offer"}
expect "type":"pair_offer"
expect "name":"Rival"
mark go-rival
waitfor go-deny
send {"type":"pair_deny"}
expect "result":"refused"
EOS
A=$PR
phone_offer "$T/ph1"
phone_pair "$T/rv" "Rival"
RV=$PH
i=0; until [ -e "$T/ph1/go-rival" ]; do i=$((i + 1)); [ "$i" -lt $TRIES ] || fail "rival: $(cat "$T/ph1.run")"; sleep 0.05; done
phone_pair "$T/own" "Owner's Phone"
gone $PH
[ "$rc" = 6 ] || fail "rival: the owner's device was answered: exit $rc: $(cat "$T/own.out")"
touch "$T/ph1/go-deny"
gone $RV
[ "$rc" = 3 ] || fail "rival: exit $rc: $(cat "$T/rv.out")"
done_ok $A "rival: approver"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = $((rows + 1)) ] || fail "rival: rows"
printf 'PASS: a rival that used the offer first shows its own name and digits on the approving phone; no pairs nobody\n'

# One try: an approval with other digits refuses the offer, and the right digits after it change
# nothing. Another paired phone cannot approve, nor can it take a second offer meanwhile.
script "$T/ph1" <<EOS
send {"type":"pair_offer"}
expect "type":"pair_offer"
expect "type":"pair_request"
mark go-asked
waitfor go-wrong
send {"type":"pair_approve","payload":{"code":"999999x"}}
expect "result":"refused"
send {"type":"pair_approve","payload":{"code":"\$CODE"}}
expect "code":"no_offer"
EOS
A=$PR
phone_offer "$T/ph1"
phone_pair "$T/ph7" "Phone Seven"
await "$T/ph7.out" 'code '
i=0; until [ -e "$T/ph1/go-asked" ]; do i=$((i + 1)); [ "$i" -lt $TRIES ] || fail "one try: $(cat "$T/ph1.run")"; sleep 0.05; done
c7=$(sed -n 's/^code //p' "$T/ph7.out" | tr -d ' ')
P1=$A
script "$T/ph4" <<EOS
send {"type":"pair_approve","payload":{"code":"$c7"}}
expect "code":"no_offer"
send {"type":"pair_offer"}
expect "result":"busy"
EOS
done_ok $PR "another phone's approval"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = $((rows + 1)) ] || fail "another phone's approval: rows"
touch "$T/ph1/go-wrong"
gone $PH
[ "$rc" = 3 ] || fail "one try: exit $rc: $(cat "$T/ph7.out")"
done_ok $P1 "one try: approver"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = $((rows + 1)) ] || fail "one try: rows"
printf 'PASS: only the asking phone approves, once: other digits refuse, a later approval and a rival phone change nothing\n'

# remote.phonePairing off: no offer.
"$PIC" settings remote.phonePairing off > /dev/null
script "$T/ph1" <<EOS
send {"type":"pair_offer"}
expect "result":"off"
EOS
done_ok $PR "phone pairing off"
"$PIC" settings remote.phonePairing on > /dev/null
printf 'PASS: remote.phonePairing off: a phone cannot add a device\n'

# A phone unpairs another: the list is sent again, and the unpaired device cannot come back.
script "$T/ph1" <<EOS
send {"type":"devices"}
expect "Phone Five"
send {"type":"unpair_device","payload":{"device":"$DEV5B"}}
expect "type":"devices"
EOS
done_ok $PR "unpair another"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = "$rows" ] || fail "unpair another: rows"
grep '"type":"devices"' "$T/ph1.run" | tail -1 | grep -qF 'Phone Five' && fail "unpair another: still listed"
await "$RO" 'Phone One unpaired Phone Five'
script "$T/ph5" <<EOS
awaitgone
EOS
gone $PR
[ "$rc" = 4 ] || fail "unpair another: it came back: exit $rc: $(cat "$T/ph5.run")"
printf 'PASS: a phone unpairs another; it cannot connect again, and the device list follows\n'

# Unpaired from the phone: its row goes, and its channel at the relay.
script "$T/ph4" <<EOS
send {"type":"ping"}
expect {"type":"pong"}
send {"type":"unpair"}
awaitgone
EOS
gone $PR
[ "$rc" = 4 ] || fail "unpair from the phone: exit $rc: $(cat "$T/ph4.run")"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = 1 ] || fail "unpair from the phone: rows"
state | grep -qF 'channels 1 ' || fail "unpair from the phone: relay channels: $(state)"
printf 'PASS: a phone that forgets this computer is unpaired, its channel deleted\n'

# Unpaired at the terminal, no passphrase asked: the running pic remote tells the phone, ends its
# session and deletes its channel; the phone cannot come back.
script "$T/ph1" <<EOS
send {"type":"ping"}
expect {"type":"pong"}
mark go-unpair
expect {"type":"unpaired"}
awaitgone
EOS
i=0; until [ -e "$T/ph1/go-unpair" ]; do i=$((i + 1)); [ "$i" -lt $TRIES ] || fail "unpair: $(cat "$T/ph1.run")"; sleep 0.05; done
env -u PIC_TEST_PASSPHRASE "$PIC" remote unpair "$(printf %.8s "$DEV1")" < /dev/null > "$T/unpair.out" 2> "$T/unpair.err" ||
    fail "unpair: $(cat "$T/unpair.err")"
gone $PR
[ "$rc" = 4 ] || fail "unpair: phone exit $rc: $(cat "$T/ph1.run")"
await "$RO" 'Phone One was unpaired'
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = 0 ] || fail "unpair: rows"
state | grep -qF 'channels 0 ' || fail "unpair: relay channels: $(state)"
script "$T/ph1" <<EOS
awaitgone
EOS
gone $PR
[ "$rc" = 4 ] || fail "unpair: the phone came back: exit $rc: $(cat "$T/ph1.run")"
printf 'PASS: unpaired at the terminal: the phone is told, its session and channel end, it cannot return\n'

# Unpair every device from a phone: one paired at the terminal adds another, which unpairs both.
term_pair() { # DIR NAME: pair at the terminal, with a yes
    offer
    phone_pair "$1" "$2"
    await "$PO" '"type":"code"'
    echo y >&3
    gone $PH
    [ "$rc" = 0 ] || fail "pair $2: phone exit $rc: $(cat "$1.out")"
    gone $PP
    exec 3>&-
}
term_pair "$T/ph9" "Phone Nine"
await "$RO" 'serving Phone Nine'
script "$T/ph9" <<EOS
send {"type":"pair_offer"}
expect "type":"pair_offer"
expect "type":"pair_request"
send {"type":"pair_approve","payload":{"code":"\$CODE"}}
expect "result":"paired"
expect {"type":"unpaired"}
awaitgone
EOS
P9=$PR
phone_offer "$T/ph9"
phone_pair "$T/ph10" "Phone Ten"
gone $PH
[ "$rc" = 0 ] || fail "unpair all: pairing: exit $rc: $(cat "$T/ph10.out")"
script "$T/ph10" <<EOS
send {"type":"unpair_all"}
expect {"type":"unpaired"}
awaitgone
EOS
gone $PR
[ "$rc" = 4 ] || fail "unpair all: the asking phone: exit $rc: $(cat "$T/ph10.run")"
gone $P9
[ "$rc" = 4 ] || fail "unpair all: the other phone: exit $rc: $(cat "$T/ph9.run")"
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = 0 ] || fail "unpair all: rows"
state | grep -qF 'channels 0 ' || fail "unpair all: relay channels: $(state)"
await "$RO" 'Phone Ten unpaired every device'
printf 'PASS: a phone unpairs every device, itself included: each is told, every row and channel goes\n'

# And at the computer: pic remote unpair --all, no passphrase.
term_pair "$T/ph11" "Phone Eleven"
await "$RO" 'serving Phone Eleven'
env -u PIC_TEST_PASSPHRASE "$PIC" remote unpair --all < /dev/null > "$T/unpairall.out" 2> "$T/unpairall.err" ||
    fail "unpair --all: $(cat "$T/unpairall.err")"
grep -qx 'unpaired 1 device' "$T/unpairall.out" || fail "unpair --all: $(cat "$T/unpairall.out")"
await "$RO" 'Phone Eleven was unpaired'
[ "$(uq "SELECT count(*) FROM picu_remote_device")" = 0 ] || fail "unpair --all: rows"
[ "$(uq "SELECT count(*) FROM picu_remote_key")" = 3 ] || fail "unpair --all: the keys went too"
printf 'PASS: pic remote unpair --all unpairs every phone and keeps the keys\n'

# SIGTERM: pic remote says it stopped and exits 0, and nothing is left to clean up.
kill "$RP"
gone "$RP"
[ "$rc" = 0 ] || fail "stop: exit $rc"
grep -qF 'pic remote: stopped' "$RO" || fail "stop: $(cat "$RO")"
# Reset while one runs (no phone is paired now, so it waits for one): it is stopped first, not left
# serving keys that are gone.
"$PIC" remote > "$T/rwait.out" 2> "$T/rwait.err" &
RP=$!
bg $RP
await "$T/rwait.out" 'waiting'
"$PIC" remote reset > "$T/reset.out" 2> "$T/reset.err" || fail "reset: $(cat "$T/reset.err")"
gone "$RP"
[ "$rc" = 0 ] || fail "reset: the running pic remote: exit $rc"
"$PIC" remote status | grep -qF 'is not running' || fail "reset: status: $("$PIC" remote status)"
[ "$(uq "SELECT count(*) FROM picu_remote_key")" = 0 ] || fail "reset: keys"
state | grep -qF 'mailboxes 0 ' || fail "reset: relay: $(state)"
printf 'PASS: pic remote stops on SIGTERM; reset stops a running one and deletes the keys and the mailbox\n'

# ---- pairing against a reconnect, a second phone and odd names; a session that ignores bye ----
codes_match() { # PHONE_OUT WHAT: the phone's code is the one pic showed
    await "$1" 'code '
    want=$(sed -n 's/^code //p' "$1")
    grep -qF "\"code\":\"$want\"" "$PO" || fail "$2: the codes differ: $(cat "$PO") / $want"
}
refuse() { # PHONE_OUT WHAT: answer no; the phone hears it under the keys it made
    echo n >&3
    gone $PH
    [ "$rc" = 3 ] || fail "$2: phone exit $rc: $(cat "$1")"
    gone $PP
    exec 3>&-
}
# The phone sends its first message again after it has the second (its stream reconnected): pic
# answers with the second it made, and does not make new keys the phone never sees.
offer
phone_pair "$T/pr1" "Phone Resend" resend
await "$PO" '"type":"code"'
codes_match "$T/pr1.out" resend
refuse "$T/pr1.out" resend
printf 'PASS: a first message sent again gets the same answer, and the pairing goes on\n'

# A second phone that scanned the same code posts its first message mid-pairing: it is ignored.
offer
phone_pair "$T/pr2" "Phone First" rival
await "$PO" '"type":"code"'
codes_match "$T/pr2.out" rival
grep -qF '"name":"Phone First"' "$PO" || fail "rival: name: $(cat "$PO")"
grep -qF 'Rival' "$PO" && fail "rival: the second phone was heard: $(cat "$PO")"
refuse "$T/pr2.out" rival
printf 'PASS: a second phone'"'"'s first message cannot take over a pairing under way\n'

# A second first message whose channel is 12 bytes leaves the paired phone's channel as it was.
offer
phone_pair "$T/pr3" "Phone Chan" rival-badchan
await "$PO" '"type":"code"'
echo y >&3
gone $PH
[ "$rc" = 0 ] || fail "short channel: phone exit $rc: $(cat "$T/pr3.out")"
gone $PP
exec 3>&-
chan=$(sed -n 's/^channel //p' "$T/pr3.out")
[ "$(curl -s -o /dev/null -w '%{http_code}' -X POST "$BASE/_test/channel?$chan")" = 204 ] ||
    fail "short channel: the phone's channel $chan is not at the relay"
printf 'PASS: the device channel is the paired phone'"'"'s; a short one in a second message changes nothing\n'

# A name with C1 and bidi controls is shown with them replaced, and quoted at the terminal.
offer
phone_pair "$T/pr4" 'Odd‮Name\u0085⁦x'
await "$PO" '"type":"code"'
grep -qF '"name":"Odd?Name??x"' "$PO" || fail "name: $(cat "$PO")"
refuse "$T/pr4.out" name
if [ -n "${DRIVE:-}" ]; then
    # At the terminal: a y typed before the question is not its answer, and the name is quoted.
    mkfifo "$T/pr5.in"
    "$DRIVE" "$T/pr5.tx" 200 60 "$PIC" remote pair < "$T/pr5.in" > "$T/pr5.drv" 2>&1 &
    DP=$!
    bg $DP
    exec 4> "$T/pr5.in"
    printf '%s\n' 'expect add your other devices' 'send y\r' >&4
    await "$T/pr5.tx" 'add your other devices'
    URL=$(grep -ao "$BASE/#p1\.[A-Za-z0-9_-]*" "$T/pr5.tx" | awk '{ if (length($0) > length(u)) u = $0 } END { print u }')
    phone_pair "$T/pr5" 'Odd‏Name'
    printf '%s\n' 'expect any other key does not' 'wait 1000' 'send n' 'wait 500' >&4
    exec 4>&-
    gone $PH
    [ "$rc" = 3 ] || fail "typeahead: phone exit $rc: $(cat "$T/pr5.out")"
    gone $DP
    grep -aqF 'Pair "Odd?Name"? It should show' "$T/pr5.tx" || fail "name: prompt: $(cat "$T/pr5.tx")"
fi
printf 'PASS: a name'"'"'s control and bidi characters are replaced, and the prompt quotes it\n'

# A phone that ignores bye (asked to start again) loses its session after the grace.
export PIC_TEST_RC_REKEY_MS=1500
remote
unset PIC_TEST_RC_REKEY_MS
export FAKE_PHONE_IGNORE_BYE=1
script "$T/pr3" <<EOS
send {"type":"ping"}
expect "type":"pong"
expect "type":"bye"
sleep 3500
send {"type":"ping"}
expectreset
EOS
unset FAKE_PHONE_IGNORE_BYE
done_ok $PR "bye ignored"
sed -n '/"type":"bye"/,$p' "$T/pr3.run" | grep -qx reset || fail "bye ignored: the session lived on: $(cat "$T/pr3.run")"
kill "$RP"
gone "$RP"
printf 'PASS: a phone that ignores bye loses its session: its next record is answered with a reset\n'

# Reset with a phone connected: the running pic remote tells it, as unpair does, before it stops
# and the mailbox goes; the phone says it was unpaired, not that the relay lost its pairing.
remote
script "$T/pr3" <<EOS
send {"type":"ping"}
expect {"type":"pong"}
mark go-reset
expect {"type":"unpaired"}
awaitgone
EOS
i=0; until [ -e "$T/pr3/go-reset" ]; do i=$((i + 1)); [ "$i" -lt $TRIES ] || fail "reset: $(cat "$T/pr3.run")"; sleep 0.05; done
"$PIC" remote reset > "$T/reset2.out" 2> "$T/reset2.err" || fail "reset: $(cat "$T/reset2.err")"
gone $PR
[ "$rc" = 4 ] && grep -qF '<- {"type":"unpaired"}' "$T/pr3.run" || fail "reset: the phone was not told: exit $rc: $(cat "$T/pr3.run")"
gone "$RP"
printf 'PASS: reset tells a connected phone it is unpaired before the mailbox goes\n'