//! The payload's two halves, held to each other. //! //! [`SECRET_KINDS`] is DATA (what `cotton-gen` renders the Swift twin from) and //! [`SecretPayload`] is the serde shape (what the sealed bytes actually are). //! Two declarations of one thing is a drift bug waiting to happen, so this file //! is the gate: every kind, every field, both ways. //! //! Every value invented. use super::*; use serde_json::{json, Map, Value}; /// One payload per kind, every field populated, so the serialized keys are the /// whole of what the kind can carry. fn every_field(kind: SecretKind) -> SecretPayload { match kind { SecretKind::Login => SecretPayload::Login { password: "an invented password".into(), notes: Some("a note".into()), totp: Some("AAAABBBBCCCCDDDD".into()), urls: Some("https://a.invented.test\nhttps://b.invented.test".into()), }, SecretKind::ApiKey => SecretPayload::ApiKey { key: "an-invented-key-0000".into(), notes: Some("a note".into()), }, SecretKind::Note => SecretPayload::Note { body: "an invented note".into(), }, SecretKind::Card => SecretPayload::Card { number: "0000000000000000".into(), holder: Some("A Name".into()), expiry: Some("01/30".into()), cvv: Some("000".into()), notes: Some("a note".into()), }, SecretKind::Identity => SecretPayload::Identity { number: "X0000000".into(), id_type: Some("passport".into()), holder: Some("A Name".into()), issued: Some("2020-01-01".into()), expires: Some("2030-01-01".into()), country: Some("Nowhere".into()), notes: Some("a note".into()), }, SecretKind::Document => SecretPayload::Document { password: "an invented password".into(), }, SecretKind::Code => SecretPayload::Code { secret: "otpauth://totp/Invented:ada?secret=AAAABBBBCCCCDDDD".into(), notes: Some("a note".into()), }, SecretKind::Wifi => SecretPayload::Wifi { password: "an invented password".into(), security: Some("WPA3".into()), notes: Some("a note".into()), }, SecretKind::Address => SecretPayload::Address { address: "1 Invented Lane\nNowhere".into(), full_name: Some("A Name".into()), email: Some("a@invented.test".into()), phone: Some("+00 0000".into()), notes: Some("a note".into()), }, SecretKind::File => SecretPayload::File { content: "QU5fSU5WRU5URUQ9dmFsdWUK".into(), file_name: Some("invented.env".into()), store_password: Some("an invented password".into()), key_alias: Some("upload".into()), key_password: Some("another invented password".into()), notes: Some("a note".into()), }, } } fn object(payload: &SecretPayload) -> Map { match serde_json::to_value(payload).expect("serializes") { Value::Object(map) => map, other => panic!("a payload must serialize to an object, got {other}"), } } #[test] fn the_data_half_is_total_over_the_kinds() { assert_eq!(SECRET_KINDS.len(), SecretKind::ALL.len()); for kind in SecretKind::ALL { let spec = kind.spec(); assert_eq!(spec.kind, kind); assert!(!spec.fields.is_empty(), "{kind} declares no fields"); } } #[test] fn the_serde_half_carries_exactly_the_fields_the_data_half_declares() { for kind in SecretKind::ALL { let keys: Vec = object(&every_field(kind)).keys().cloned().collect(); let mut wanted: Vec = vec!["kind".to_string()]; wanted.extend(kind.spec().fields.iter().map(|field| field.name.to_string())); assert_eq!( keys, wanted, "{kind}: the serde shape and SECRET_KINDS disagree about the payload's fields — the \ Swift twin is rendered from the data half and decodes the serde half, so a \ difference here is a payload the phone cannot read" ); } } #[test] fn the_tag_is_the_kinds_own_spelling_and_it_is_the_stores_vocabulary() { for kind in SecretKind::ALL { assert_eq!( object(&every_field(kind))["kind"], json!(kind.as_str()), "the tag must be the same string `cotton_secrets.kind` holds" ); assert_eq!(SecretKind::parse(kind.as_str()), Some(kind)); } assert_eq!(SecretKind::parse("passport"), None); } #[test] fn the_primary_field_is_the_first_field_and_it_is_required() { for kind in SecretKind::ALL { let spec = kind.spec(); assert_eq!( spec.fields[0].name, spec.primary, "{kind}: the primary field is the first one a form asks for" ); assert!(spec.fields[0].required, "{kind}: the primary field is required"); for field in &spec.fields[1..] { assert!( !field.required, "{kind}.{}: only the primary field is required — everything else is a person's \ choice, and a required field they leave blank is a secret they cannot save", field.name ); } } } #[test] fn the_primary_field_reads_back_the_value_the_hint_comes_from() { for kind in SecretKind::ALL { let payload = every_field(kind); let serialized = object(&payload); assert_eq!( serialized[kind.primary_field()], json!(payload.primary_field()), "{kind}: primary_field() must answer the value of the field SECRET_KINDS names" ); assert_eq!(payload.kind(), kind); } } #[test] fn every_payload_round_trips_through_its_wire_form() { for kind in SecretKind::ALL { let payload = every_field(kind); let text = serde_json::to_string(&payload).expect("serializes"); assert_eq!( serde_json::from_str::(&text).expect("parses"), payload, "{kind} does not survive the trip the sealed bytes take" ); } } #[test] fn an_optional_field_left_out_is_absent_rather_than_null() { let payload = SecretPayload::Login { password: "an invented password".into(), notes: None, totp: None, urls: None, }; let text = serde_json::to_string(&payload).expect("serializes"); assert_eq!(text, r#"{"kind":"login","password":"an invented password"}"#); assert_eq!( serde_json::from_str::(&text).expect("parses"), payload, "and an absent field reads back as absent — the Swift twin decodes an optional the same \ way, so a null would be a second spelling of nothing" ); } #[test] fn a_payload_with_no_kind_or_an_unknown_one_is_refused() { for text in [ r#"{"password":"an invented password"}"#, r#"{"kind":"passport","number":"X0000000"}"#, r#"{"kind":"login"}"#, r#"{"kind":"apiKey","key":"an-invented-key"}"#, ] { assert!( serde_json::from_str::(text).is_err(), "parsed {text}" ); } } #[test] fn a_hint_is_the_last_four_characters_and_never_a_character_cut_in_half() { assert_eq!(hint_of("an-invented-key-wxyz"), "wxyz"); assert_eq!(hint_of("wxyz"), "wxyz"); assert_eq!(hint_of("yz"), "yz", "a short value answers with all of it"); assert_eq!(hint_of(""), ""); // Characters, not bytes: cutting four BYTES off the end of this would land // in the middle of one and the answer would not be text at all. assert_eq!(hint_of("passwörd"), "wörd"); assert_eq!(hint_of("aaaa🔒"), "aaa🔒"); } #[test] fn a_payloads_hint_is_the_hint_of_its_primary_field() { for kind in SecretKind::ALL { let payload = every_field(kind); assert_eq!(payload.hint(), hint_of(payload.primary_field())); assert!( payload.hint().chars().count() <= HINT_CHARS, "{kind}: a hint is never longer than four characters — it is what a list shows \ INSTEAD of the secret" ); } }