first commit
This commit is contained in:
commit
6d3ea3e2a6
43 files changed
+8204
No files matched your search
@@ -0,0 +1,182 @@
|
||||
//! 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
|
||||
}
|
||||
|
||||
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 ──
|
||||
let offer = bob.create_invitation("alice", &[], "hello alice", 3600).await.unwrap();
|
||||
assert_eq!(alice.inspect_invitation(&offer).unwrap().initiator_name, "bob");
|
||||
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
|
||||
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();
|
||||
assert!(ev.contains(&Event::File {
|
||||
contact: "bob".into(),
|
||||
name: "note.txt".into(),
|
||||
data: b"file content".to_vec()
|
||||
}));
|
||||
|
||||
// ── server invitation ──
|
||||
let url = bob
|
||||
.publish_invitation("remote", &[], "via server", "secret", 3600)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(remote
|
||||
.accept_published_invitation(&url, "wrong", None, &[])
|
||||
.await
|
||||
.is_err());
|
||||
let who = remote
|
||||
.accept_published_invitation(&url, "secret", None, &[])
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(who, "bob");
|
||||
let ev = bob.sync(Duration::ZERO).await.unwrap();
|
||||
assert_eq!(
|
||||
ev,
|
||||
vec![Event::InvitationAccepted {
|
||||
contact: "remote".into()
|
||||
}]
|
||||
);
|
||||
let ev = remote.sync(Duration::ZERO).await.unwrap();
|
||||
assert_eq!(ev, vec![Event::ContactActivated { contact: "bob".into() }]);
|
||||
let ev = bob.sync(Duration::ZERO).await.unwrap();
|
||||
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 {
|
||||
let mut alice = MeowClient::open(&path, "pw", KdfParams::INSECURE_TEST).await.unwrap();
|
||||
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!(
|
||||
MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST).await,
|
||||
Err(meow_client::Error::Locked(_))
|
||||
));
|
||||
drop(bob);
|
||||
|
||||
// ── persistence & wrong password ──
|
||||
assert!(
|
||||
MeowClient::open(&dir.join("bob.mwid2"), "nope", KdfParams::INSECURE_TEST)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw", KdfParams::INSECURE_TEST)
|
||||
.await
|
||||
.unwrap();
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user