2026-11-20 21:47:33 +01:00
|
|
|
//! End-to-end in-process test: two real HTTP servers (memory store) and three clients.
|
|
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use meow_client::{Event, MeowClient, State};
|
|
|
|
|
use meow_crypto::password::KdfParams;
|
|
|
|
|
use meow_server::store::MemoryStore;
|
|
|
|
|
use meow_server::{encode_card, AppState, Limits};
|
|
|
|
|
use tokio::sync::broadcast;
|
|
|
|
|
|
|
|
|
|
async fn spawn_server() -> String {
|
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
|
|
|
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
|
|
|
let keys = meow_core::server::ServerKeys::generate();
|
|
|
|
|
let (notify, _) = broadcast::channel(1024);
|
|
|
|
|
let state = Arc::new(AppState {
|
|
|
|
|
card: encode_card(&keys, &url, "test", ""),
|
|
|
|
|
keys,
|
|
|
|
|
store: Arc::new(MemoryStore::new(notify.clone(), 1000)),
|
|
|
|
|
notify,
|
|
|
|
|
limits: Limits::default(),
|
|
|
|
|
});
|
|
|
|
|
tokio::spawn(meow_server::serve(listener, state));
|
|
|
|
|
url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tmpdir() -> PathBuf {
|
|
|
|
|
let d = std::env::temp_dir().join(format!("meow-test-{}", rand_suffix()));
|
|
|
|
|
std::fs::create_dir_all(&d).unwrap();
|
|
|
|
|
d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn rand_suffix() -> String {
|
|
|
|
|
meow_crypto::random_bytes::<8>()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|b| format!("{b:02x}"))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn client(dir: &std::path::Path, name: &str, servers: &[String]) -> MeowClient {
|
|
|
|
|
let mut c = MeowClient::create(
|
|
|
|
|
&dir.join(format!("{name}.mwid2")),
|
|
|
|
|
name,
|
|
|
|
|
"pw",
|
|
|
|
|
KdfParams::INSECURE_TEST,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
for s in servers {
|
|
|
|
|
c.add_server(s).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
c
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 23:17:15 +01:00
|
|
|
fn no_receipts(events: Vec<Event>) -> Vec<Event> {
|
|
|
|
|
events
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|e| !matches!(e, Event::Receipt { .. }))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn no_receipt_settings(c: &mut MeowClient) {
|
|
|
|
|
c.set_settings(meow_client::Settings {
|
|
|
|
|
delivered_receipts: false,
|
|
|
|
|
read_receipts: false,
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 21:47:33 +01:00
|
|
|
fn texts(events: &[Event]) -> Vec<String> {
|
|
|
|
|
events
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|e| match e {
|
|
|
|
|
Event::Message { text, .. } => Some(text.clone()),
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn file_and_server_invitations_then_messaging() {
|
|
|
|
|
let s1 = spawn_server().await;
|
|
|
|
|
let s2 = spawn_server().await;
|
|
|
|
|
let servers = vec![s1.clone(), s2.clone()];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut remote = client(&dir, "remote", &servers).await;
|
|
|
|
|
|
|
|
|
|
// ── file invitation ──
|
2026-11-20 23:29:41 +01:00
|
|
|
let offer = bob
|
|
|
|
|
.create_invitation("alice", &[], "hello alice", 3600, None)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2026-11-20 21:47:33 +01:00
|
|
|
assert_eq!(alice.inspect_invitation(&offer).unwrap().initiator_name, "bob");
|
2026-11-20 23:29:41 +01:00
|
|
|
let answer = alice.answer_invitation(&offer, None, &[], None).await.unwrap();
|
2026-11-20 21:47:33 +01:00
|
|
|
let confirm = bob.accept_answer(&answer).await.unwrap();
|
|
|
|
|
assert_eq!(bob.contact("alice").unwrap().state, State::ConfirmSent);
|
|
|
|
|
bob.send_text("alice", "sent before finalize").await.unwrap();
|
|
|
|
|
alice.finalize_invitation(&confirm).await.unwrap();
|
|
|
|
|
assert_eq!(alice.contact("bob").unwrap().state, State::Active);
|
|
|
|
|
|
|
|
|
|
let ev = bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert!(ev.contains(&Event::ContactActivated {
|
|
|
|
|
contact: "alice".into()
|
|
|
|
|
}));
|
|
|
|
|
assert_eq!(bob.contact("alice").unwrap().state, State::Active);
|
|
|
|
|
|
|
|
|
|
// ── messaging, delivered on both servers but processed once ──
|
|
|
|
|
bob.send_text("alice", "hello").await.unwrap();
|
|
|
|
|
let r = bob.send_text("alice", "again").await.unwrap();
|
|
|
|
|
assert_eq!(r.delivered_to, 2);
|
|
|
|
|
let ev = alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(texts(&ev), vec!["sent before finalize", "hello", "again"]);
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
|
|
alice.send_text("bob", "hi bob").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["hi bob"]);
|
|
|
|
|
|
|
|
|
|
// ── file ──
|
|
|
|
|
let f = dir.join("note.txt");
|
|
|
|
|
std::fs::write(&f, b"file content").unwrap();
|
|
|
|
|
bob.send_file("alice", &f).await.unwrap();
|
|
|
|
|
let ev = alice.sync(Duration::ZERO).await.unwrap();
|
2026-11-20 22:54:13 +01:00
|
|
|
let file_id = ev
|
|
|
|
|
.iter()
|
|
|
|
|
.find_map(|e| match e {
|
|
|
|
|
Event::File {
|
|
|
|
|
contact,
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
|
data,
|
|
|
|
|
} if contact == "bob" && name == "note.txt" => {
|
|
|
|
|
assert_eq!(data, b"file content");
|
|
|
|
|
Some(id.clone())
|
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
|
|
|
|
.expect("file event");
|
|
|
|
|
// The content is kept in the store.
|
|
|
|
|
assert_eq!(alice.file("bob", &file_id).unwrap().unwrap(), b"file content");
|
2026-11-20 21:47:33 +01:00
|
|
|
|
|
|
|
|
// ── server invitation ──
|
|
|
|
|
let url = bob
|
2026-11-20 23:29:41 +01:00
|
|
|
.publish_invitation("remote", &[], "via server", "secret", 3600, None)
|
2026-11-20 21:47:33 +01:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(remote
|
2026-11-20 23:29:41 +01:00
|
|
|
.accept_published_invitation(&url, "wrong", None, &[], None)
|
2026-11-20 21:47:33 +01:00
|
|
|
.await
|
|
|
|
|
.is_err());
|
|
|
|
|
let who = remote
|
2026-11-20 23:29:41 +01:00
|
|
|
.accept_published_invitation(&url, "secret", None, &[], None)
|
2026-11-20 21:47:33 +01:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(who, "bob");
|
2026-11-20 23:17:15 +01:00
|
|
|
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
|
2026-11-20 21:47:33 +01:00
|
|
|
assert_eq!(
|
|
|
|
|
ev,
|
|
|
|
|
vec![Event::InvitationAccepted {
|
|
|
|
|
contact: "remote".into()
|
|
|
|
|
}]
|
|
|
|
|
);
|
2026-11-20 23:17:15 +01:00
|
|
|
let ev = no_receipts(remote.sync(Duration::ZERO).await.unwrap());
|
2026-11-20 21:47:33 +01:00
|
|
|
assert_eq!(ev, vec![Event::ContactActivated { contact: "bob".into() }]);
|
2026-11-20 23:17:15 +01:00
|
|
|
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
|
2026-11-20 21:47:33 +01:00
|
|
|
assert_eq!(
|
|
|
|
|
ev,
|
|
|
|
|
vec![Event::ContactActivated {
|
|
|
|
|
contact: "remote".into()
|
|
|
|
|
}]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// ── long poll ──
|
|
|
|
|
let path = dir.join("alice.mwid2");
|
|
|
|
|
drop(alice);
|
|
|
|
|
let waiter = tokio::spawn(async move {
|
2026-11-20 22:54:13 +01:00
|
|
|
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
|
2026-11-20 21:47:33 +01:00
|
|
|
let start = std::time::Instant::now();
|
|
|
|
|
let ev = alice.sync(Duration::from_secs(20)).await.unwrap();
|
|
|
|
|
(texts(&ev), start.elapsed())
|
|
|
|
|
});
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
|
|
|
|
bob.send_text("alice", "wake up").await.unwrap();
|
|
|
|
|
let (got, elapsed) = waiter.await.unwrap();
|
|
|
|
|
assert_eq!(got, vec!["wake up"]);
|
|
|
|
|
assert!(elapsed < Duration::from_secs(10), "long poll took {elapsed:?}");
|
|
|
|
|
|
|
|
|
|
// ── one process at a time per identity ──
|
|
|
|
|
assert!(matches!(
|
2026-11-20 22:54:13 +01:00
|
|
|
MeowClient::open(&dir.join("bob.mwid2"), "pw").await,
|
2026-11-20 21:47:33 +01:00
|
|
|
Err(meow_client::Error::Locked(_))
|
|
|
|
|
));
|
|
|
|
|
drop(bob);
|
|
|
|
|
|
|
|
|
|
// ── persistence & wrong password ──
|
2026-11-20 22:54:13 +01:00
|
|
|
assert!(MeowClient::open(&dir.join("bob.mwid2"), "nope").await.is_err());
|
|
|
|
|
let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw").await.unwrap();
|
2026-11-20 21:47:33 +01:00
|
|
|
assert_eq!(bob2.contacts().len(), 2);
|
|
|
|
|
assert_eq!(bob2.history("alice").unwrap().len(), 6); // 5 sent + "hi bob"
|
|
|
|
|
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 22:00:54 +01:00
|
|
|
|
|
|
|
|
// ───────────── at-least-once delivery (roadmap lot 3.8) ─────────────
|
|
|
|
|
|
|
|
|
|
/// Memory store whose acknowledgements can be "lost" on demand.
|
|
|
|
|
struct FlakyStore {
|
|
|
|
|
inner: MemoryStore,
|
|
|
|
|
drop_acks: std::sync::atomic::AtomicBool,
|
2026-11-20 23:09:05 +01:00
|
|
|
/// Refuse deposits (the server answers with an error).
|
|
|
|
|
fail_posts: std::sync::atomic::AtomicBool,
|
2026-11-20 22:00:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl meow_server::store::ServerStore for FlakyStore {
|
|
|
|
|
async fn post(&self, mb: [u8; 32], payload: Vec<u8>, now: u64) -> meow_server::store::StoreResult<()> {
|
2026-11-20 23:09:05 +01:00
|
|
|
if self.fail_posts.load(std::sync::atomic::Ordering::SeqCst) {
|
|
|
|
|
return Err(meow_server::store::StoreError::Backend("down".into()));
|
|
|
|
|
}
|
2026-11-20 22:00:54 +01:00
|
|
|
self.inner.post(mb, payload, now).await
|
|
|
|
|
}
|
|
|
|
|
async fn fetch(
|
|
|
|
|
&self,
|
2026-11-21 11:02:02 +01:00
|
|
|
mbs: &[([u8; 32], Vec<u8>)],
|
2026-11-20 22:00:54 +01:00
|
|
|
limit: usize,
|
|
|
|
|
) -> meow_server::store::StoreResult<Vec<meow_proto::v2::StoredMessage>> {
|
|
|
|
|
self.inner.fetch(mbs, limit).await
|
|
|
|
|
}
|
|
|
|
|
async fn ack(&self, mb: [u8; 32], ids: &[Vec<u8>]) -> meow_server::store::StoreResult<()> {
|
|
|
|
|
if self.drop_acks.load(std::sync::atomic::Ordering::SeqCst) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
self.inner.ack(mb, ids).await
|
|
|
|
|
}
|
|
|
|
|
async fn mark_request(&self, id: &[u8], ttl: Duration) -> meow_server::store::StoreResult<bool> {
|
|
|
|
|
self.inner.mark_request(id, ttl).await
|
|
|
|
|
}
|
|
|
|
|
async fn put_invitation(
|
|
|
|
|
&self,
|
|
|
|
|
payload: Vec<u8>,
|
|
|
|
|
password: &str,
|
|
|
|
|
ttl: Duration,
|
|
|
|
|
code_len: usize,
|
|
|
|
|
) -> meow_server::store::StoreResult<String> {
|
|
|
|
|
self.inner.put_invitation(payload, password, ttl, code_len).await
|
|
|
|
|
}
|
|
|
|
|
async fn get_invitation(&self, code: &str, password: &str) -> meow_server::store::StoreResult<Vec<u8>> {
|
|
|
|
|
self.inner.get_invitation(code, password).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 23:09:05 +01:00
|
|
|
async fn spawn_flaky_server() -> (String, Arc<FlakyStore>) {
|
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
|
|
|
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
|
|
|
let keys = meow_core::server::ServerKeys::generate();
|
|
|
|
|
let (notify, _) = broadcast::channel(1024);
|
|
|
|
|
let store = Arc::new(FlakyStore {
|
|
|
|
|
inner: MemoryStore::new(notify.clone(), 1000),
|
|
|
|
|
drop_acks: false.into(),
|
|
|
|
|
fail_posts: false.into(),
|
|
|
|
|
});
|
|
|
|
|
let state = Arc::new(AppState {
|
|
|
|
|
card: encode_card(&keys, &url, "flaky", ""),
|
|
|
|
|
keys,
|
|
|
|
|
store: store.clone(),
|
|
|
|
|
notify,
|
|
|
|
|
limits: Limits::default(),
|
|
|
|
|
});
|
|
|
|
|
tokio::spawn(meow_server::serve(listener, state));
|
|
|
|
|
(url, store)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 22:00:54 +01:00
|
|
|
/// What a client that crashed right after receiving the response would have done:
|
|
|
|
|
/// pull without ever acknowledging.
|
|
|
|
|
async fn raw_pull(
|
|
|
|
|
server: &meow_core::server::ServerInfo,
|
|
|
|
|
mailbox: &meow_crypto::SigningKeyPair,
|
|
|
|
|
) -> Vec<meow_proto::v2::StoredMessage> {
|
|
|
|
|
use meow_proto::v2::{self, request::Body as Req, response::Body as Resp};
|
|
|
|
|
let rid = meow_crypto::random_bytes::<16>().to_vec();
|
|
|
|
|
let ts = std::time::SystemTime::now()
|
|
|
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_secs();
|
|
|
|
|
let req = v2::Request {
|
|
|
|
|
request_id: rid.clone(),
|
|
|
|
|
timestamp: ts,
|
|
|
|
|
body: Some(Req::Pull(v2::Pull {
|
|
|
|
|
auths: vec![meow_core::rpc::mailbox_auth(mailbox, server, &rid, vec![], ts)],
|
|
|
|
|
wait_seconds: 0,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
let t = meow_client::transport::Transport::new().unwrap();
|
|
|
|
|
match t.call_request(server, req, Duration::ZERO).await.unwrap() {
|
|
|
|
|
Resp::PullResult(p) => p.messages,
|
|
|
|
|
other => panic!("unexpected {other:?}"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn no_message_lost_when_the_client_dies() {
|
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
|
|
|
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
|
|
|
let keys = meow_core::server::ServerKeys::generate();
|
|
|
|
|
let (notify, _) = broadcast::channel(1024);
|
|
|
|
|
let store = Arc::new(FlakyStore {
|
|
|
|
|
inner: MemoryStore::new(notify.clone(), 1000),
|
|
|
|
|
drop_acks: false.into(),
|
2026-11-20 23:09:05 +01:00
|
|
|
fail_posts: false.into(),
|
2026-11-20 22:00:54 +01:00
|
|
|
});
|
|
|
|
|
let limits = Limits {
|
|
|
|
|
max_pull_messages: 2, // force the "more" path
|
|
|
|
|
..Limits::default()
|
|
|
|
|
};
|
|
|
|
|
let state = Arc::new(AppState {
|
|
|
|
|
card: encode_card(&keys, &url, "flaky", ""),
|
|
|
|
|
keys,
|
|
|
|
|
store: store.clone(),
|
|
|
|
|
notify,
|
|
|
|
|
limits,
|
|
|
|
|
});
|
|
|
|
|
tokio::spawn(meow_server::serve(listener, state));
|
|
|
|
|
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let servers = vec![url];
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
2026-11-20 23:17:15 +01:00
|
|
|
// This test counts what stays on the server: no receipts traffic.
|
|
|
|
|
no_receipt_settings(&mut bob).await;
|
|
|
|
|
no_receipt_settings(&mut alice).await;
|
2026-11-20 23:29:41 +01:00
|
|
|
let offer = bob.create_invitation("alice", &[], "", 3600, None).await.unwrap();
|
|
|
|
|
let answer = alice.answer_invitation(&offer, None, &[], None).await.unwrap();
|
2026-11-20 22:00:54 +01:00
|
|
|
let confirm = bob.accept_answer(&answer).await.unwrap();
|
|
|
|
|
alice.finalize_invitation(&confirm).await.unwrap();
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
|
|
|
|
|
let server = alice.servers()[0].clone();
|
|
|
|
|
let mailbox = alice.contact("bob").unwrap().keys.as_ref().unwrap().mailbox.clone();
|
|
|
|
|
|
|
|
|
|
// 1. A client receives messages then dies before saving: nothing is lost.
|
|
|
|
|
for t in ["m1", "m2", "m3"] {
|
|
|
|
|
bob.send_text("alice", t).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(raw_pull(&server, &mailbox).await.len(), 2); // limit 2, more = true
|
|
|
|
|
let ev = alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(texts(&ev), vec!["m1", "m2", "m3"]); // several rounds ("more")
|
|
|
|
|
// Everything processed has been acknowledged and deleted.
|
|
|
|
|
assert!(raw_pull(&server, &mailbox).await.is_empty());
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
|
|
// 2. The acknowledgement is lost: the redelivered message is not processed twice,
|
|
|
|
|
// even after a restart (pending acks are persisted with the state).
|
|
|
|
|
store.drop_acks.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
bob.send_text("alice", "m4").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["m4"]);
|
|
|
|
|
assert_eq!(raw_pull(&server, &mailbox).await.len(), 1);
|
|
|
|
|
let path = dir.join("alice.mwid2");
|
|
|
|
|
drop(alice);
|
2026-11-20 22:54:13 +01:00
|
|
|
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
|
2026-11-20 22:00:54 +01:00
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
assert!(raw_pull(&server, &mailbox).await.is_empty());
|
|
|
|
|
assert_eq!(alice.history("bob").unwrap().len(), 4);
|
|
|
|
|
assert_eq!(store.inner.stored_messages(), 0);
|
|
|
|
|
|
|
|
|
|
// 3. Mailboxes that disappear still get their last messages acknowledged:
|
|
|
|
|
// the temporary answer mailbox of a server invitation...
|
|
|
|
|
let mut carol = client(&dir, "carol", &servers).await;
|
2026-11-20 23:17:15 +01:00
|
|
|
no_receipt_settings(&mut carol).await;
|
2026-11-20 23:29:41 +01:00
|
|
|
let link = bob.publish_invitation("carol", &[], "", "", 3600, None).await.unwrap();
|
|
|
|
|
carol
|
|
|
|
|
.accept_published_invitation(&link, "", None, &[], None)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2026-11-20 22:00:54 +01:00
|
|
|
bob.sync(Duration::ZERO).await.unwrap(); // step 2 → 3, answer mailbox retired
|
|
|
|
|
carol.sync(Duration::ZERO).await.unwrap(); // step 3 → 4
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap(); // ack
|
|
|
|
|
carol.send_text("bob", "hi").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["hi"]);
|
|
|
|
|
assert_eq!(store.inner.stored_messages(), 0);
|
|
|
|
|
// ...and a contact removed while an ack is still pending.
|
|
|
|
|
store.drop_acks.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
bob.send_text("alice", "bye").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["bye"]);
|
|
|
|
|
store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
alice.remove_contact("bob").await.unwrap();
|
|
|
|
|
assert_eq!(store.inner.stored_messages(), 1);
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
assert_eq!(store.inner.stored_messages(), 0);
|
|
|
|
|
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 22:54:13 +01:00
|
|
|
|
|
|
|
|
// ───────────── hidden spaces (roadmap lot 4.5) ─────────────
|
|
|
|
|
|
|
|
|
|
/// File invitation from `a` to `b`, completed.
|
|
|
|
|
async fn connect(a: &mut MeowClient, b: &mut MeowClient, b_name: &str) {
|
2026-11-20 23:29:41 +01:00
|
|
|
let offer = a.create_invitation(b_name, &[], "", 3600, None).await.unwrap();
|
|
|
|
|
let answer = b.answer_invitation(&offer, None, &[], None).await.unwrap();
|
2026-11-20 22:54:13 +01:00
|
|
|
let confirm = a.accept_answer(&answer).await.unwrap();
|
|
|
|
|
b.finalize_invitation(&confirm).await.unwrap();
|
|
|
|
|
a.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn names(c: &MeowClient) -> Vec<String> {
|
|
|
|
|
c.contacts().iter().map(|k| k.name.clone()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn hidden_contacts() {
|
|
|
|
|
let servers = vec![spawn_server().await];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let path = dir.join("alice.mwid2");
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
let mut carol = client(&dir, "carol", &servers).await;
|
|
|
|
|
connect(&mut alice, &mut bob, "bob").await;
|
|
|
|
|
connect(&mut alice, &mut carol, "carol").await;
|
|
|
|
|
bob.send_text("alice", "before hiding").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["before hiding"]);
|
|
|
|
|
|
|
|
|
|
// Hide bob, with his history.
|
|
|
|
|
alice.hide_contact("bob", "bob-pw").await.unwrap();
|
|
|
|
|
assert!(alice.is_hidden("bob").unwrap());
|
|
|
|
|
assert!(!alice.is_hidden("carol").unwrap());
|
|
|
|
|
assert_eq!(alice.history("bob").unwrap().len(), 1);
|
|
|
|
|
drop(alice);
|
|
|
|
|
|
|
|
|
|
// Without the hidden password, bob does not exist and is not even polled.
|
|
|
|
|
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
|
|
|
|
|
assert_eq!(names(&alice), vec!["carol"]);
|
|
|
|
|
assert!(alice.history("bob").is_err());
|
|
|
|
|
bob.send_text("alice", "while locked").await.unwrap();
|
|
|
|
|
carol.send_text("alice", "hi").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["hi"]);
|
|
|
|
|
|
|
|
|
|
// With it, bob is back and his messages were waiting on the server.
|
|
|
|
|
assert!(!alice.unlock("wrong").await.unwrap());
|
|
|
|
|
assert!(alice.unlock("bob-pw").await.unwrap());
|
|
|
|
|
assert_eq!(names(&alice), vec!["carol", "bob"]);
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["while locked"]);
|
|
|
|
|
alice.send_text("bob", "from the hidden side").await.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
texts(&bob.sync(Duration::ZERO).await.unwrap()),
|
|
|
|
|
vec!["from the hidden side"]
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(alice.history("bob").unwrap().len(), 3);
|
|
|
|
|
|
|
|
|
|
// Several contacts can share a hidden space.
|
|
|
|
|
alice.hide_contact("carol", "bob-pw").await.unwrap();
|
|
|
|
|
drop(alice);
|
|
|
|
|
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
|
|
|
|
|
assert!(names(&alice).is_empty());
|
|
|
|
|
assert!(alice.unlock("bob-pw").await.unwrap());
|
|
|
|
|
assert_eq!(names(&alice), vec!["bob", "carol"]);
|
|
|
|
|
|
|
|
|
|
// Back to the main space.
|
|
|
|
|
alice.reveal_contact("bob").await.unwrap();
|
|
|
|
|
drop(alice);
|
|
|
|
|
let alice = MeowClient::open(&path, "pw").await.unwrap();
|
|
|
|
|
assert_eq!(names(&alice), vec!["bob"]);
|
|
|
|
|
assert_eq!(alice.history("bob").unwrap().len(), 3);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 23:09:05 +01:00
|
|
|
|
|
|
|
|
// ───────────── persistent send queue (roadmap lot 5.5) ─────────────
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn offline_messages_are_queued_and_retried() {
|
|
|
|
|
use std::sync::atomic::Ordering::SeqCst;
|
|
|
|
|
let (url, store) = spawn_flaky_server().await;
|
|
|
|
|
let servers = vec![url];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let path = dir.join("bob.mwid2");
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
connect(&mut alice, &mut bob, "bob").await;
|
|
|
|
|
|
|
|
|
|
// Server refuses deposits: sending still succeeds, the message is queued.
|
|
|
|
|
store.fail_posts.store(true, SeqCst);
|
|
|
|
|
let r1 = bob.send_text("alice", "queued 1").await.unwrap();
|
|
|
|
|
let r2 = bob.send_text("alice", "queued 2").await.unwrap();
|
|
|
|
|
assert_eq!((r1.delivered_to, r2.delivered_to), (0, 0));
|
|
|
|
|
let pending = bob.pending_sends();
|
|
|
|
|
assert_eq!(pending.len(), 2);
|
|
|
|
|
assert!(pending.iter().all(|p| p.contact == "alice" && p.attempts >= 1));
|
|
|
|
|
assert_eq!(pending[0].message_id, r1.message_id);
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
|
|
// The queue survives a restart; not yet due (backoff), so a sync does not send.
|
|
|
|
|
drop(bob);
|
|
|
|
|
let mut bob = MeowClient::open(&path, "pw").await.unwrap();
|
|
|
|
|
assert_eq!(bob.pending_sends().len(), 2);
|
|
|
|
|
store.fail_posts.store(false, SeqCst);
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(bob.pending_sends().len(), 2);
|
|
|
|
|
// Forced flush: both delivered (in one Post), in order.
|
|
|
|
|
assert!(bob.flush_outbox().await.unwrap().is_empty());
|
|
|
|
|
assert!(bob.pending_sends().is_empty());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
texts(&alice.sync(Duration::ZERO).await.unwrap()),
|
|
|
|
|
vec!["queued 1", "queued 2"]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Due items are retried by the next send.
|
|
|
|
|
bob.set_retry_policy(meow_client::RetryPolicy {
|
|
|
|
|
base: Duration::from_secs(1),
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
store.fail_posts.store(true, SeqCst);
|
|
|
|
|
bob.send_text("alice", "late").await.unwrap();
|
|
|
|
|
store.fail_posts.store(false, SeqCst);
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(2500)).await;
|
|
|
|
|
let r = bob.send_text("alice", "now").await.unwrap();
|
|
|
|
|
assert_eq!(r.delivered_to, 1);
|
|
|
|
|
assert!(bob.pending_sends().is_empty());
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["late", "now"]);
|
|
|
|
|
|
|
|
|
|
// An item that expires undelivered is reported.
|
|
|
|
|
bob.set_retry_policy(meow_client::RetryPolicy {
|
|
|
|
|
max_age: Duration::ZERO,
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
store.fail_posts.store(true, SeqCst);
|
|
|
|
|
let r = bob.send_text("alice", "lost").await.unwrap();
|
|
|
|
|
assert_eq!(r.delivered_to, 0);
|
|
|
|
|
let ev = bob.flush_outbox().await.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ev,
|
|
|
|
|
vec![Event::SendFailed {
|
|
|
|
|
contact: "alice".into(),
|
|
|
|
|
message_id: r.message_id
|
|
|
|
|
}]
|
|
|
|
|
);
|
|
|
|
|
assert!(bob.pending_sends().is_empty());
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 23:17:15 +01:00
|
|
|
|
|
|
|
|
// ───────────── receipts (roadmap lot 5.6) ─────────────
|
|
|
|
|
|
|
|
|
|
fn statuses(c: &MeowClient, name: &str, outbound: bool) -> Vec<meow_client::Status> {
|
|
|
|
|
c.history(name)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|h| h.outbound == outbound)
|
|
|
|
|
.map(|h| h.status)
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn delivery_and_read_receipts() {
|
|
|
|
|
use meow_client::{ReceiptKind, Settings, Status};
|
|
|
|
|
let servers = vec![spawn_server().await, spawn_server().await];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
connect(&mut alice, &mut bob, "bob").await;
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
|
|
|
|
|
let m1 = alice.send_text("bob", "one").await.unwrap().message_id;
|
|
|
|
|
let m2 = alice.send_text("bob", "two").await.unwrap().message_id;
|
|
|
|
|
assert_eq!(statuses(&alice, "bob", true), vec![Status::Sent, Status::Sent]);
|
|
|
|
|
|
|
|
|
|
// Bob receives (from 2 servers, processed once): one delivery receipt.
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["one", "two"]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
statuses(&bob, "alice", false),
|
|
|
|
|
vec![Status::Delivered, Status::Delivered]
|
|
|
|
|
);
|
|
|
|
|
let ev = alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ev,
|
|
|
|
|
vec![Event::Receipt {
|
|
|
|
|
contact: "bob".into(),
|
|
|
|
|
kind: ReceiptKind::Delivered,
|
|
|
|
|
message_ids: vec![m1.clone(), m2.clone()]
|
|
|
|
|
}]
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
statuses(&alice, "bob", true),
|
|
|
|
|
vec![Status::Delivered, Status::Delivered]
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-21 09:48:54 +01:00
|
|
|
let summary = bob.conversation_summary("alice").unwrap();
|
|
|
|
|
assert_eq!(summary.unread, 2);
|
|
|
|
|
assert!(matches!(summary.last.unwrap().content, meow_client::Content::Text(ref t) if t == "two"));
|
2026-11-20 23:17:15 +01:00
|
|
|
// Bob reads.
|
|
|
|
|
assert_eq!(bob.mark_read("alice").await.unwrap(), 2);
|
|
|
|
|
assert_eq!(bob.mark_read("alice").await.unwrap(), 0);
|
|
|
|
|
assert_eq!(statuses(&bob, "alice", false), vec![Status::Read, Status::Read]);
|
2026-11-21 09:48:54 +01:00
|
|
|
assert_eq!(bob.conversation_summary("alice").unwrap().unread, 0);
|
2026-11-20 23:17:15 +01:00
|
|
|
let ev = alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ev,
|
|
|
|
|
vec![Event::Receipt {
|
|
|
|
|
contact: "bob".into(),
|
|
|
|
|
kind: ReceiptKind::Read,
|
|
|
|
|
message_ids: vec![m1, m2]
|
|
|
|
|
}]
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(statuses(&alice, "bob", true), vec![Status::Read, Status::Read]);
|
|
|
|
|
// Receipts are not acknowledged by receipts.
|
|
|
|
|
assert!(bob.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
|
|
// Receipts can be turned off (persisted with the identity).
|
|
|
|
|
let off = Settings {
|
|
|
|
|
delivered_receipts: false,
|
|
|
|
|
read_receipts: false,
|
|
|
|
|
};
|
|
|
|
|
bob.set_settings(off).await.unwrap();
|
|
|
|
|
let path = dir.join("bob.mwid2");
|
|
|
|
|
drop(bob);
|
|
|
|
|
let mut bob = MeowClient::open(&path, "pw").await.unwrap();
|
|
|
|
|
assert_eq!(bob.settings(), off);
|
|
|
|
|
alice.send_text("bob", "three").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["three"]);
|
|
|
|
|
assert_eq!(bob.mark_read("alice").await.unwrap(), 1);
|
|
|
|
|
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
|
|
|
|
|
assert_eq!(statuses(&alice, "bob", true)[2], Status::Sent);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 23:22:25 +01:00
|
|
|
|
|
|
|
|
// ───────────── background task and event stream (roadmap lot 5.9) ─────────────
|
|
|
|
|
|
|
|
|
|
async fn next_message(rx: &mut tokio::sync::broadcast::Receiver<Event>) -> String {
|
|
|
|
|
tokio::time::timeout(Duration::from_secs(10), async {
|
|
|
|
|
loop {
|
|
|
|
|
if let Event::Message { text, .. } = rx.recv().await.unwrap() {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.expect("no message within 10 s")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn background_sync_publishes_events() {
|
|
|
|
|
use std::sync::atomic::Ordering::SeqCst;
|
|
|
|
|
let (url, store) = spawn_flaky_server().await;
|
|
|
|
|
let servers = vec![url];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
connect(&mut alice, &mut bob, "bob").await;
|
|
|
|
|
|
|
|
|
|
// Bob runs in the background with a long long-poll.
|
|
|
|
|
let opts = meow_client::BackgroundOptions {
|
|
|
|
|
long_poll: Duration::from_secs(20),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
bob.set_retry_policy(meow_client::RetryPolicy {
|
|
|
|
|
base: Duration::from_secs(1),
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
let bob = bob.start(opts);
|
|
|
|
|
let mut rx = bob.subscribe();
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(300)).await;
|
|
|
|
|
|
|
|
|
|
// Pushed to the subscriber without any explicit sync.
|
|
|
|
|
alice.send_text("bob", "hello background").await.unwrap();
|
|
|
|
|
assert_eq!(next_message(&mut rx).await, "hello background");
|
|
|
|
|
|
|
|
|
|
// The lock is free while the task waits on the server: sending is immediate.
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(300)).await;
|
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
|
let r = bob.lock().await.send_text("alice", "reply").await.unwrap();
|
|
|
|
|
assert_eq!(r.delivered_to, 1);
|
|
|
|
|
assert!(
|
|
|
|
|
start.elapsed() < Duration::from_secs(3),
|
|
|
|
|
"send waited {:?}",
|
|
|
|
|
start.elapsed()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["reply"]);
|
|
|
|
|
|
|
|
|
|
// Alice's read receipt reaches Bob's subscribers too.
|
|
|
|
|
alice.mark_read("bob").await.unwrap();
|
|
|
|
|
let got = tokio::time::timeout(Duration::from_secs(10), async {
|
|
|
|
|
loop {
|
|
|
|
|
if let Event::Receipt { kind, .. } = rx.recv().await.unwrap() {
|
|
|
|
|
if kind == meow_client::ReceiptKind::Read {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
assert!(got.is_ok(), "no read receipt event");
|
|
|
|
|
|
|
|
|
|
// A message queued while the server refuses deposits is retried by the task.
|
|
|
|
|
store.fail_posts.store(true, SeqCst);
|
|
|
|
|
let r = bob.lock().await.send_text("alice", "retried").await.unwrap();
|
|
|
|
|
assert_eq!(r.delivered_to, 0);
|
|
|
|
|
store.fail_posts.store(false, SeqCst);
|
|
|
|
|
// The task shortens its long-poll to retry on time (no other traffic needed).
|
|
|
|
|
let delivered = tokio::time::timeout(Duration::from_secs(10), async {
|
|
|
|
|
loop {
|
|
|
|
|
if bob.lock().await.pending_sends().is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
assert!(delivered.is_ok(), "queued message not retried");
|
|
|
|
|
assert!(texts(&alice.sync(Duration::ZERO).await.unwrap()).contains(&"retried".to_string()));
|
|
|
|
|
|
|
|
|
|
// Stopping gives the client back, usable as before.
|
|
|
|
|
let mut bob = bob.stop().await;
|
|
|
|
|
alice.send_text("bob", "after stop").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["after stop"]);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 23:29:41 +01:00
|
|
|
|
|
|
|
|
// ───────────── invitations straight into hidden spaces (roadmap lot 5.10) ─────────────
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn invitations_straight_into_hidden_spaces() {
|
|
|
|
|
let servers = vec![spawn_server().await];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
|
|
|
|
|
// Server flow, hidden on both sides.
|
|
|
|
|
let url = alice
|
|
|
|
|
.publish_invitation("bob", &[], "", "", 3600, Some("a-secret"))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(alice.is_hidden("bob").unwrap());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
bob.accept_published_invitation(&url, "", None, &[], Some("b-secret"))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap(),
|
|
|
|
|
"alice"
|
|
|
|
|
);
|
|
|
|
|
assert!(bob.is_hidden("alice").unwrap());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
no_receipts(alice.sync(Duration::ZERO).await.unwrap()),
|
|
|
|
|
vec![Event::InvitationAccepted { contact: "bob".into() }]
|
|
|
|
|
);
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
bob.send_text("alice", "secret hello").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["secret hello"]);
|
|
|
|
|
|
|
|
|
|
// File flow, hidden on the answering side only.
|
|
|
|
|
let mut carol = client(&dir, "carol", &servers).await;
|
|
|
|
|
let offer = alice.create_invitation("carol", &[], "", 3600, None).await.unwrap();
|
|
|
|
|
carol
|
|
|
|
|
.answer_invitation(&offer, None, &[], Some("c-secret"))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(carol.is_hidden("alice").unwrap());
|
|
|
|
|
|
|
|
|
|
// Reopened without the space passwords: nothing; with them: the contacts.
|
|
|
|
|
drop((alice, bob, carol));
|
|
|
|
|
let path = |name: &str| dir.join(format!("{name}.mwid2"));
|
|
|
|
|
let mut alice = MeowClient::open(&path("alice"), "pw").await.unwrap();
|
|
|
|
|
let mut bob = MeowClient::open(&path("bob"), "pw").await.unwrap();
|
|
|
|
|
let carol = MeowClient::open(&path("carol"), "pw").await.unwrap();
|
|
|
|
|
assert_eq!(names(&alice), vec!["carol"]);
|
|
|
|
|
assert!(names(&bob).is_empty());
|
|
|
|
|
assert!(names(&carol).is_empty());
|
|
|
|
|
assert!(alice.unlock("a-secret").await.unwrap());
|
|
|
|
|
assert!(bob.unlock("b-secret").await.unwrap());
|
|
|
|
|
assert_eq!(alice.contact("bob").unwrap().state, State::Active);
|
|
|
|
|
assert_eq!(alice.history("bob").unwrap().len(), 1);
|
|
|
|
|
alice.send_text("bob", "still secret").await.unwrap();
|
|
|
|
|
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["still secret"]);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-20 23:48:30 +01:00
|
|
|
|
|
|
|
|
/// The background task must notice state changes made through the handle (here, a
|
|
|
|
|
/// first contact while it was idling), not only after its 30 s wait.
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn background_task_wakes_up_on_new_contacts() {
|
|
|
|
|
let servers = vec![spawn_server().await];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let alice = client(&dir, "alice", &servers)
|
|
|
|
|
.await
|
|
|
|
|
.start(meow_client::BackgroundOptions::default());
|
|
|
|
|
let bob = client(&dir, "bob", &servers)
|
|
|
|
|
.await
|
|
|
|
|
.start(meow_client::BackgroundOptions::default());
|
|
|
|
|
let alice_rx = alice.subscribe();
|
|
|
|
|
let bob_rx = bob.subscribe();
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(300)).await; // both idle, nothing to poll
|
|
|
|
|
|
|
|
|
|
let url = alice
|
|
|
|
|
.lock()
|
|
|
|
|
.await
|
|
|
|
|
.publish_invitation("bob", &[], "", "", 3600, None)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
bob.lock()
|
|
|
|
|
.await
|
|
|
|
|
.accept_published_invitation(&url, "", None, &[], None)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
async fn activated(mut rx: tokio::sync::broadcast::Receiver<Event>, who: &str) {
|
|
|
|
|
tokio::time::timeout(Duration::from_secs(10), async {
|
|
|
|
|
loop {
|
|
|
|
|
if let Event::ContactActivated { .. } = rx.recv().await.unwrap() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.unwrap_or_else(|_| panic!("{who} not activated in time"))
|
|
|
|
|
}
|
|
|
|
|
let (a, b) = (activated(alice_rx, "alice"), activated(bob_rx, "bob"));
|
|
|
|
|
tokio::join!(a, b);
|
|
|
|
|
alice.stop().await;
|
|
|
|
|
bob.stop().await;
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-21 10:07:16 +01:00
|
|
|
|
|
|
|
|
/// One-shot invitation (QR code, file, nearby): only the offer travels out of band.
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn one_shot_invitation_completes_through_servers() {
|
|
|
|
|
let servers = vec![spawn_server().await];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
let offer = alice
|
|
|
|
|
.create_invitation("Bob", &[], "scan me", 3600, None)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(bob.accept_invitation(&offer, None, &[], None).await.unwrap(), "alice");
|
|
|
|
|
// Accepting twice is refused.
|
|
|
|
|
assert!(bob.accept_invitation(&offer, Some("other"), &[], None).await.is_err());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
no_receipts(alice.sync(Duration::ZERO).await.unwrap()),
|
|
|
|
|
vec![Event::InvitationAccepted { contact: "Bob".into() }]
|
|
|
|
|
);
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(alice.contact("Bob").unwrap().state, State::Active);
|
|
|
|
|
assert_eq!(bob.contact("alice").unwrap().state, State::Active);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
alice.contact("Bob").unwrap().safety_number(),
|
|
|
|
|
bob.contact("alice").unwrap().safety_number()
|
|
|
|
|
);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-21 11:02:02 +01:00
|
|
|
|
|
|
|
|
// ───────────── background watch (roadmap lot 7.7) ─────────────
|
|
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn background_watch_peeks_without_consuming() {
|
|
|
|
|
let url = spawn_server().await;
|
|
|
|
|
let servers = vec![url.clone()];
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut alice = client(&dir, "alice", &servers).await;
|
|
|
|
|
let mut bob = client(&dir, "bob", &servers).await;
|
|
|
|
|
let mut carol = client(&dir, "carol", &servers).await;
|
|
|
|
|
connect(&mut alice, &mut bob, "bob").await;
|
|
|
|
|
connect(&mut alice, &mut carol, "carol").await;
|
|
|
|
|
alice.hide_contact("carol", "carol-secret").await.unwrap();
|
|
|
|
|
bob.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
carol.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
alice.sync(Duration::ZERO).await.unwrap();
|
|
|
|
|
|
|
|
|
|
let path = dir.join("alice.watch");
|
|
|
|
|
let key = [7u8; 32];
|
|
|
|
|
alice.enable_watch(&path, key).unwrap();
|
|
|
|
|
// Encrypted, and padded: neither the servers nor the number of contacts show.
|
|
|
|
|
let raw = std::fs::read(&path).unwrap();
|
|
|
|
|
assert!(!raw.windows(9).any(|w| w == b"127.0.0.1"));
|
|
|
|
|
assert!(
|
|
|
|
|
meow_client::watch_check(&path, [8u8; 32], Duration::ZERO)
|
|
|
|
|
.await
|
|
|
|
|
.is_err(),
|
|
|
|
|
"wrong key"
|
|
|
|
|
);
|
|
|
|
|
// Baseline (whatever was pending), then nothing new.
|
|
|
|
|
meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap();
|
|
|
|
|
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
|
|
|
|
|
|
|
|
|
// A new message is counted once, and not consumed.
|
|
|
|
|
bob.send_text("alice", "ping").await.unwrap();
|
|
|
|
|
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 1);
|
|
|
|
|
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
|
|
|
|
|
|
|
|
|
// Hidden contacts are never watched.
|
|
|
|
|
carol.send_text("alice", "from the hidden side").await.unwrap();
|
|
|
|
|
assert_eq!(meow_client::watch_check(&path, key, Duration::ZERO).await.unwrap(), 0);
|
|
|
|
|
|
|
|
|
|
// The app still gets everything (nothing was acknowledged by the watcher).
|
|
|
|
|
let got = texts(&alice.sync(Duration::ZERO).await.unwrap());
|
|
|
|
|
assert!(got.contains(&"ping".to_string()) && got.contains(&"from the hidden side".to_string()));
|
|
|
|
|
|
|
|
|
|
// Long-poll: wakes up as soon as something arrives.
|
|
|
|
|
let p = path.clone();
|
|
|
|
|
let waiter = tokio::spawn(async move {
|
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
|
(
|
|
|
|
|
meow_client::watch_check(&p, key, Duration::from_secs(20))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap(),
|
|
|
|
|
start.elapsed(),
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
|
|
|
|
bob.send_text("alice", "wake").await.unwrap();
|
|
|
|
|
let (n, elapsed) = waiter.await.unwrap();
|
|
|
|
|
assert_eq!(n, 1);
|
|
|
|
|
assert!(elapsed < Duration::from_secs(10), "{elapsed:?}");
|
|
|
|
|
|
|
|
|
|
alice.disable_watch().unwrap();
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
2026-11-21 17:27:41 +01:00
|
|
|
|
|
|
|
|
/// Adding something that is not a Meow v2 server gives a clear error.
|
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
|
|
|
async fn adding_a_non_meow_server_is_reported_clearly() {
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
// A v1-like server: answers every path with an HTML page (or a 404 for the second).
|
|
|
|
|
fn fake(status: &'static str, body: &'static str) -> String {
|
|
|
|
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
|
let url = format!("http://{}", l.local_addr().unwrap());
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
for mut s in l.incoming().flatten() {
|
|
|
|
|
let mut buf = [0u8; 1024];
|
|
|
|
|
let _ = s.read(&mut buf);
|
|
|
|
|
let _ = write!(
|
|
|
|
|
s,
|
|
|
|
|
"HTTP/1.1 {status}\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
|
|
|
|
body.len()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
url
|
|
|
|
|
}
|
|
|
|
|
let dir = tmpdir();
|
|
|
|
|
let mut c = client(&dir, "alice", &[]).await;
|
|
|
|
|
let html = fake("200 OK", "<html><body>meow v1</body></html>");
|
|
|
|
|
assert!(matches!(
|
|
|
|
|
c.add_server(&html).await,
|
|
|
|
|
Err(meow_client::Error::NotAMeowServer(_))
|
|
|
|
|
));
|
|
|
|
|
let missing = fake("404 Not Found", "not found");
|
|
|
|
|
assert!(matches!(
|
|
|
|
|
c.add_server(&missing).await,
|
|
|
|
|
Err(meow_client::Error::NotAMeowServer(_))
|
|
|
|
|
));
|
|
|
|
|
let closed = {
|
|
|
|
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
|
format!("http://{}", l.local_addr().unwrap())
|
|
|
|
|
}; // listener dropped: nothing listens there
|
|
|
|
|
let err = c.add_server(&closed).await.unwrap_err();
|
|
|
|
|
assert!(matches!(err, meow_client::Error::Unreachable(_)), "{err}");
|
|
|
|
|
assert!(c.servers().is_empty());
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|