Cotton code
101 lines · 5.0 KB · RustRaw
//! Cotton Secrets' sharing decisions. Every number and key invented.use super::*;use chrono::TimeZone;fn at(day: u32, hour: u32) -> DateTime<Utc> {    Utc.with_ymd_and_hms(2026, 11, day, hour, 0, 0).unwrap()}const ME: &str = "+15550100001";const THEM: &str = "+15550100002";#[test]fn both_phones_show_the_same_six_words_and_another_key_shows_others() {    let (mine, theirs, other) = ([1u8; 32], [2u8; 32], [3u8; 32]);    let a = check_words(&Side { number: ME, key: &mine }, &Side { number: THEM, key: &theirs });    let b = check_words(&Side { number: THEM, key: &theirs }, &Side { number: ME, key: &mine });    assert_eq!(a, b, "the same words whoever asks");    assert_eq!(a.len(), 6);    assert!(a.iter().all(|word| EFF_LARGE.lines().any(|known| known == word)));    let swapped = check_words(&Side { number: ME, key: &mine }, &Side { number: THEM, key: &other });    assert_ne!(a, swapped, "a server that handed out another key shows other words");}#[test]fn a_check_code_reads_back_and_anything_else_is_refused() {    let key = [7u8; 32];    let code = check_code(&Side { number: THEM, key: &key });    assert_eq!(read_code(&code), Some((THEM.to_string(), key)));    assert_eq!(read_code("https://invented.test"), None);    assert_eq!(read_code("cotton-check:1:nonsense:abc"), None);    assert_eq!(key_print(&key).len(), 16);}#[test]fn until_choices_end_where_they_say_and_a_past_date_is_refused() {    let now = at(25, 12);    assert_eq!(until_of("day", now), Ok(Some(at(26, 12))));    assert_eq!(until_of("week", now), Ok(Some(Utc.with_ymd_and_hms(2026, 12, 2, 12, 0, 0).unwrap())));    assert_eq!(until_of("taken", now), Ok(None));    assert_eq!(until_of("2026-11-30", now), Ok(Some(Utc.with_ymd_and_hms(2026, 11, 30, 23, 59, 59).unwrap())));    assert_eq!(until_of("2026-11-01", now), Err(()));    assert_eq!(until_of("soon", now), Err(()));}#[test]fn a_share_holds_until_it_ends_and_is_still_known_only_if_its_value_was_fetched() {    let now = at(25, 12);    let live = Facts { delivered_at: Some(at(20, 9)), opens: 3, until: Some(at(30, 0)), ..Facts::default() };    assert_eq!(live.standing(now), Standing::Holds);    let taken_unseen = Facts { delivered_at: Some(at(20, 9)), taken_back_at: Some(at(24, 9)), ..Facts::default() };    assert_eq!(taken_unseen.standing(now), Standing::Ended, "a card alone is no value seen");    let taken_seen = Facts { opens: 2, ..taken_unseen.clone() };    assert_eq!(taken_seen.standing(now), Standing::StillKnown);    let changed = Facts { changed_at: Some(at(24, 10)), ..taken_seen.clone() };    assert_eq!(changed.standing(now), Standing::Changed);    let changed_before = Facts { changed_at: Some(at(23, 10)), ..taken_seen };    assert_eq!(changed_before.standing(now), Standing::StillKnown, "a change before the take back is not the change after it");    let expired_seen = Facts { delivered_at: Some(at(20, 9)), opens: 1, until: Some(at(22, 0)), ..Facts::default() };    assert_eq!(expired_seen.standing(now), Standing::StillKnown, "an ended share that was seen is known, as after a take back");}#[test]fn a_take_back_says_only_what_the_server_saw() {    assert_eq!(before_take(&Facts::default()), Before::NeverReceived);    assert_eq!(before_take(&Facts { delivered_at: Some(at(20, 9)), ..Facts::default() }), Before::Received);    assert_eq!(before_take(&Facts { delivered_at: Some(at(20, 9)), opens: 3, ..Facts::default() }), Before::Opened(3));}#[test]fn a_change_goes_to_the_providers_own_page_or_the_site() {    assert_eq!(change_at(None, Some("sk_test_INVENTED")), Some(("Stripe".into(), Some("https://dashboard.stripe.com/apikeys".into()))));    assert_eq!(change_at(Some("https://github.com/"), None).map(|(name, _)| name), Some("GitHub".into()));    assert_eq!(change_at(Some("console.firebase.google.com"), None).map(|(name, _)| name), Some("Firebase".into()));    assert_eq!(change_at(Some("mail.invented.test"), None), Some(("mail.invented.test".into(), Some("https://mail.invented.test".into()))));    assert_eq!(change_at(None, None), None);}#[test]fn passkeys_are_never_shared_and_contexts_bind_the_share_and_version() {    assert!(!shareable("passkey"));    assert!(shareable("api_key") && shareable("file"));    assert_eq!(card_context("s1"), "secret-card:s1");    assert_ne!(value_context("s1", 1), value_context("s1", 2));    assert_eq!(May::parse("copy"), Some(May::Copy));    assert_eq!(May::parse("share"), None, "no passing on");}#[test]fn a_card_and_a_value_travel_as_json() {    let card = ShareCard { name: "Invented deploy key".into(), kind: "api_key".into(), site: None, username: None, from_name: Some("A".into()), may: May::Copy, until: None, fields: None };    let back: ShareCard = serde_json::from_str(&serde_json::to_string(&card).unwrap()).unwrap();    assert_eq!(back, card);    let value = ShareValue { version: 2, payload: SecretPayload::ApiKey { key: "an-invented-key".into(), notes: None } };    let json = serde_json::to_value(&value).unwrap();    assert_eq!(json["payload"]["kind"], "api_key");    assert_eq!(serde_json::from_value::<ShareValue>(json).unwrap(), value);}