61 lines · 3.3 KB · RustRaw
use super::super::fixtures_tests::*;use super::*;use crate::vault::wrap::public_of;#[test]fn a_shared_dek_opens_only_for_its_table_and_row_and_key() { let wrapped = wrap_dek_for(&public_of(&SHARE_SECRET), &dek(), "note", "n1").unwrap(); assert_eq!(open_shared_dek(&SHARE_SECRET, &wrapped, "note", "n1").unwrap().expose(), dek().expose()); assert_eq!(open_shared_dek(&SHARE_SECRET, &wrapped, "note", "n2").unwrap_err(), SealError::DoesNotOpen); assert_eq!(open_shared_dek(&SHARE_SECRET, &wrapped, "message", "n1").unwrap_err(), SealError::DoesNotOpen); assert_eq!(open_shared_dek(&[12; 32], &wrapped, "note", "n1").unwrap_err(), SealError::DoesNotOpen);}#[test]fn the_share_private_half_round_trips_under_the_vmk_only_as_its_person() { let sealed = seal_share_private(&vmk(), 2, "AbCdEfGh", &SHARE_SECRET).unwrap(); assert_eq!(*open_share_private(&vmk(), 2, "AbCdEfGh", &sealed).unwrap(), SHARE_SECRET); assert!(open_share_private(&vmk(), 2, "Zzzzzzzz", &sealed).is_err()); assert!(open_share_private(&vmk(), 1, "AbCdEfGh", &sealed).is_err());}#[test]fn the_sharing_key_is_the_dks_and_the_databases_and_no_other() { let a = sharing_public(&dk(), DATABASE).unwrap(); assert_eq!(a, sharing_public(&dk(), DATABASE).unwrap()); assert_ne!(a, sharing_public(&dk(), OTHER_DATABASE).unwrap()); assert_ne!(a, sharing_public(&crate::seal::DataKey::from_bytes([3; 32]), DATABASE).unwrap()); assert_eq!(public_of(&sharing_secret(&dk(), DATABASE).unwrap()), a); // Its own domain: not the table key nor the blind key of any name. assert_ne!(*sharing_secret(&dk(), DATABASE).unwrap(), *crate::seal::table_key(&dk(), DATABASE, "share", 1).unwrap()); assert_eq!(key_from_text(&key_text(&a)), Some(a));}#[test]fn a_box_sealed_to_a_person_opens_only_with_their_sharing_key_and_names_the_sender() { let sender = *sharing_secret(&dk(), DATABASE).unwrap(); let friend_dk = crate::seal::DataKey::from_bytes([3; 32]); let friend = *sharing_secret(&friend_dk, OTHER_DATABASE).unwrap(); let boxed = seal_to_person(&sender, &public_of(&friend), "send:abc12345", b"{\"title\":\"Grandma's dal\"}").unwrap(); assert!(boxed.starts_with(SENT_PREFIX)); assert!(!boxed.contains("dal")); let opened = open_from_person(&friend, &boxed, "send:abc12345").unwrap(); assert_eq!(opened.payload, b"{\"title\":\"Grandma's dal\"}"); assert_eq!(opened.sender, public_of(&sender)); // Another person, another context, a tampered box: one answer. assert_eq!(open_from_person(&sender, &boxed, "send:abc12345").unwrap_err(), SealError::DoesNotOpen); assert_eq!(open_from_person(&friend, &boxed, "send:other999").unwrap_err(), SealError::DoesNotOpen); let mut bytes: Vec<char> = boxed.chars().collect(); let last = bytes.len() - 1; bytes[last] = if bytes[last] == 'A' { 'B' } else { 'A' }; assert_eq!(open_from_person(&friend, &bytes.into_iter().collect::<String>(), "send:abc12345").unwrap_err(), SealError::DoesNotOpen); // A chat box is never a sent box. let chat = crate::chat::seal::seal(&sender, &public_of(&friend), b"x").unwrap(); assert!(open_from_person(&friend, &chat, "send:abc12345").is_err());}#[test]fn a_box_to_a_small_order_key_is_refused_not_made() { let sender = *sharing_secret(&dk(), DATABASE).unwrap(); assert!(seal_to_person(&sender, &[0u8; 32], "send:x", b"y").is_err());}