//! Full protocol flow without any I/O: invitation then bidirectional messaging. use meow_core::contact::ContactState; use meow_core::proto::{peer_envelope, user_message}; use meow_core::server::{ServerInfo, ServerKeys}; use meow_core::{invitation, messaging}; fn server() -> ServerInfo { ServerInfo::from_card(&ServerKeys::generate().card("http://localhost:8080", "s", "", 1)).unwrap() } fn ratchet(env: &meow_core::proto::PeerEnvelope) -> &meow_core::proto::RatchetMessage { match env.body.as_ref().unwrap() { peer_envelope::Body::Ratchet(r) => r, _ => panic!("not a ratchet message"), } } fn text_of(m: &meow_core::proto::UserMessage) -> &str { match m.body.as_ref().unwrap() { user_message::Body::Text(t) => &t.text, _ => panic!("not text"), } } #[test] fn full_invitation_and_messaging() { let now = 1_000_000; let srv = server(); // 1. bob invites alice let offer = invitation::create_offer("alice", "bob", vec![srv.clone()], "hi!", now, 3600).unwrap(); let mut bob_side = offer.contact; assert_eq!(bob_side.state, ContactState::OfferSent); let info = invitation::inspect_offer(&offer.offer_bytes, now).unwrap(); assert_eq!(info.initiator_name, "bob"); assert_eq!(info.message, "hi!"); // 2. alice answers let (mut alice_side, answer) = invitation::answer(&offer.offer_bytes, "bob", "alice", vec![srv.clone()], now).unwrap(); assert_eq!(alice_side.state, ContactState::AnswerSent); assert!(!alice_side.can_send()); // 3. bob accepts: he can send right away let confirm = invitation::accept(&mut bob_side, &answer, now).unwrap(); assert_eq!(bob_side.state, ContactState::ConfirmSent); let early = messaging::encrypt(&mut bob_side, &messaging::text("early bird", 1)).unwrap(); // 4. alice finalizes and produces the ack let ack = invitation::finalize(&mut alice_side, &confirm).unwrap(); assert_eq!(alice_side.state, ContactState::Active); assert_eq!(alice_side.transcript, bob_side.transcript); assert_eq!(alice_side.safety_number(), bob_side.safety_number()); // the early message is readable after finalisation let m = messaging::decrypt(&mut alice_side, ratchet(&early)).unwrap(); assert_eq!(text_of(&m), "early bird"); // bob gets the ack → active messaging::decrypt(&mut bob_side, ratchet(&ack)).unwrap(); assert_eq!(bob_side.state, ContactState::Active); // conversation for i in 0..3 { let e = messaging::encrypt(&mut alice_side, &messaging::text(&format!("a{i}"), 2)).unwrap(); assert_eq!( text_of(&messaging::decrypt(&mut bob_side, ratchet(&e)).unwrap()), format!("a{i}") ); let e = messaging::encrypt(&mut bob_side, &messaging::text(&format!("b{i}"), 3)).unwrap(); assert_eq!( text_of(&messaging::decrypt(&mut alice_side, ratchet(&e)).unwrap()), format!("b{i}") ); } // deposits target the peer's mailbox let d = messaging::deposit_for(&bob_side, &early).unwrap(); assert_eq!(d.mailbox, alice_side.keys.as_ref().unwrap().mailbox.public().to_vec()); } #[test] fn tampered_answer_is_rejected() { let now = 10; let srv = server(); let offer = invitation::create_offer("alice", "bob", vec![srv.clone()], "", now, 60).unwrap(); let mut bob_side = offer.contact; let (_, mut answer) = invitation::answer(&offer.offer_bytes, "bob", "alice", vec![srv.clone()], now).unwrap(); let sealed = answer.sealed.as_mut().unwrap(); let n = sealed.ciphertext.len(); sealed.ciphertext[n - 1] ^= 1; assert!(invitation::accept(&mut bob_side, &answer, now).is_err()); assert_eq!(bob_side.state, ContactState::OfferSent); } #[test] fn confirm_for_another_invitee_is_rejected() { let now = 10; let srv = server(); let offer = invitation::create_offer("alice", "bob", vec![srv.clone()], "", now, 60).unwrap(); let mut bob_side = offer.contact; let (mut alice_side, answer) = invitation::answer(&offer.offer_bytes, "bob", "alice", vec![srv.clone()], now).unwrap(); // mallory answers the same offer too; bob accepts mallory's answer let (_, mallory_answer) = invitation::answer(&offer.offer_bytes, "bob", "mallory", vec![srv.clone()], now).unwrap(); let confirm = invitation::accept(&mut bob_side, &mallory_answer, now).unwrap(); // the confirm is sealed to mallory: alice cannot use it assert!(invitation::finalize(&mut alice_side, &confirm).is_err()); assert_eq!(alice_side.state, ContactState::AnswerSent); // and bob cannot accept twice assert!(invitation::accept(&mut bob_side, &answer, now).is_err()); } #[test] fn expired_offer() { let srv = server(); let offer = invitation::create_offer("alice", "bob", vec![srv.clone()], "", 100, 10).unwrap(); assert!(invitation::inspect_offer(&offer.offer_bytes, 111).is_err()); assert!(invitation::answer(&offer.offer_bytes, "bob", "alice", vec![srv], 111).is_err()); }