rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;

use super::{CodeRefused, DeviceName, PairingCodeSet};

/// The time every test starts at, in Unix seconds.
const MINTED_AT: u64 = 1_800_000_000;

// Covers: a code redeems once, as the name it was minted with, and is gone
// after, so a second redeem of the same code, which a replay or a second
// device would make, pairs nothing. The store holds only its hash.
// Owner: daemon_http::pairing_codes.
#[test]
fn a_code_redeems_once() {
    let mut codes = PairingCodeSet::default();
    let named = codes
        .mint(DeviceName::chosen("laptop".to_owned()).unwrap(), MINTED_AT)
        .unwrap();
    let unnamed = codes.mint(DeviceName::FromDevice, MINTED_AT).unwrap();
    let saved = serde_json::to_string(&codes).unwrap();
    assert!(!saved.contains(named.code.secret()), "{saved}");
    assert_ne!(named.code, unnamed.code);

    let later = MINTED_AT + 1;
    assert_eq!(
        [
            codes.take(named.code.secret(), later),
            codes.take(unnamed.code.secret(), later),
            codes.take(named.code.secret(), later),
            codes.take("rho_code_00000000000000000000000000000000", later),
        ],
        [
            Ok(DeviceName::Chosen("laptop".to_owned())),
            Ok(DeviceName::FromDevice),
            Err(CodeRefused::Unknown),
            Err(CodeRefused::Unknown),
        ]
    );
    assert_eq!(codes, PairingCodeSet::default());
}

// Covers: a code is good for exactly 10 minutes on the clock it is given,
// an expired one is refused as expired and dies when presented, and every
// change prunes the other expired codes so the set does not grow.
// Owner: daemon_http::pairing_codes.
#[test]
fn a_code_expires_after_ten_minutes_and_expired_codes_are_pruned() {
    let expiry = MINTED_AT + 10 * 60;
    let mut codes = PairingCodeSet::default();
    let last_second = codes.mint(DeviceName::FromDevice, MINTED_AT).unwrap();
    let expired = codes.mint(DeviceName::FromDevice, MINTED_AT).unwrap();
    let pruned = codes.mint(DeviceName::FromDevice, MINTED_AT).unwrap();
    assert_eq!(last_second.expires_at, expiry);
    assert_eq!(
        codes.take(last_second.code.secret(), expiry - 1),
        Ok(DeviceName::FromDevice)
    );
    assert_eq!(
        codes.take(expired.code.secret(), expiry),
        Err(CodeRefused::Expired)
    );
    // Presenting the expired code pruned the third one, expired as well.
    assert_eq!(
        codes.take(pruned.code.secret(), expiry),
        Err(CodeRefused::Unknown)
    );

    let stale = codes.mint(DeviceName::FromDevice, MINTED_AT).unwrap();
    let fresh = codes.mint(DeviceName::FromDevice, expiry).unwrap();
    assert_eq!(fresh.pruned, 1);
    assert_eq!(
        [
            codes.take(stale.code.secret(), expiry),
            codes.take(fresh.code.secret(), expiry),
        ],
        [Err(CodeRefused::Unknown), Ok(DeviceName::FromDevice)]
    );
}

// Covers: a name chosen at pairing is refused before any code is minted
// when `rho daemon token` would refuse it, so no code carries a name the
// redeem could not give a token.
// Owner: daemon_http::pairing_codes.
#[test]
fn a_chosen_name_is_checked_before_minting() {
    for bad in ["", "has space", "bad/slash", &"a".repeat(65)] {
        assert!(DeviceName::chosen(bad.to_owned()).is_err(), "{bad:?}");
    }
}