oxedyne/fe2o3/fe2o3_mail/tests/sync_over_lineio.rs
9.7 KiB, 1 run
created by r537758799:77720, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The sync engine over the client's line I/O, both ways: once over the tokio socket against |
| 2 | //! fe2o3's own IMAP server, through a proxy that records every byte each way, then over an |
| 3 | //! in-memory [`LineIo`] that plays the recording back. Nothing on the wire is typed: the |
| 4 | //! recording is the native client's own conversation, so the replay is held to what a socket |
| 5 | //! really carried, and the two runs must give the same [`Delta`]s. That is what a browser's |
| 6 | //! tunnel relies on: the client and the engine see only bytes, whatever carries them. |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_mail::{ |
| 10 | maildir::MaildirStore, |
| 11 | sync::{ |
| 12 | sync_folder, |
| 13 | Budget, |
| 14 | Caps, |
| 15 | Delta, |
| 16 | MailSession, |
| 17 | Snapshot, |
| 18 | }, |
| 19 | }; |
| 20 | use oxedyne_fe2o3_net::{ |
| 21 | imap::{ |
| 22 | client::{ |
| 23 | ImapClient, |
| 24 | ImapConfig, |
| 25 | Security, |
| 26 | }, |
| 27 | server::ImapServer, |
| 28 | }, |
| 29 | lineio::LineIo, |
| 30 | mail::{ |
| 31 | store::{ |
| 32 | FolderName, |
| 33 | MailStore, |
| 34 | MailUser, |
| 35 | MessageFlags, |
| 36 | }, |
| 37 | user::UserStore, |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | use std::{ |
| 42 | fs, |
| 43 | path::PathBuf, |
| 44 | sync::{ |
| 45 | Arc, |
| 46 | Mutex, |
| 47 | }, |
| 48 | time::Duration, |
| 49 | }; |
| 50 | |
| 51 | use tokio::{ |
| 52 | io::{ |
| 53 | AsyncReadExt, |
| 54 | AsyncWriteExt, |
| 55 | }, |
| 56 | net::{ |
| 57 | TcpListener, |
| 58 | TcpStream, |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | const USER: &str = "alice@test.local"; |
| 64 | const PASS: &str = "correct-horse"; |
| 65 | |
| 66 | #[derive(Clone, Debug)] |
| 67 | struct OneUser; |
| 68 | |
| 69 | impl UserStore for OneUser { |
| 70 | fn authenticate(&self, address: &str, password: &str) -> Outcome<Option<MailUser>> { |
| 71 | if address.eq_ignore_ascii_case(USER) && password == PASS { |
| 72 | return self.lookup(address); |
| 73 | } |
| 74 | Ok(None) |
| 75 | } |
| 76 | |
| 77 | fn lookup(&self, address: &str) -> Outcome<Option<MailUser>> { |
| 78 | if !address.eq_ignore_ascii_case(USER) { |
| 79 | return Ok(None); |
| 80 | } |
| 81 | Ok(Some(MailUser { |
| 82 | local: fmt!("alice"), |
| 83 | domain: fmt!("test.local"), |
| 84 | delivery_key: fmt!("test.local/alice"), |
| 85 | send_as: Vec::new(), |
| 86 | })) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn message(n: usize) -> Vec<u8> { |
| 91 | fmt!("From: bob@example.org\r\n\ |
| 92 | To: alice@test.local\r\n\ |
| 93 | Message-ID: <m{}@example.org>\r\n\ |
| 94 | Subject: message {}\r\n\ |
| 95 | \r\n\ |
| 96 | Body {}, with a line that is not the end\r\n\r\n) {{17}}\r\n", n, n, n).into_bytes() |
| 97 | } |
| 98 | |
| 99 | type Tape = Arc<Mutex<Vec<u8>>>; |
| 100 | |
| 101 | /// Copies one direction, keeping every byte. |
| 102 | async fn pipe(mut from: tokio::net::tcp::OwnedReadHalf, mut to: tokio::net::tcp::OwnedWriteHalf, |
| 103 | tape: Tape) |
| 104 | { |
| 105 | let mut buf = vec![0u8; 16 * 1024]; |
| 106 | loop { |
| 107 | let n = match from.read(&mut buf).await { |
| 108 | Ok(0) | Err(_) => break, |
| 109 | Ok(n) => n, |
| 110 | }; |
| 111 | if let Ok(mut t) = tape.lock() { |
| 112 | t.extend_from_slice(&buf[..n]); |
| 113 | } |
| 114 | if to.write_all(&buf[..n]).await.is_err() { |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | let _ = to.shutdown().await; |
| 119 | } |
| 120 | |
| 121 | /// One proxied connection to `server`: what the client sent, and what it was sent. |
| 122 | async fn proxy(server: std::net::SocketAddr) -> Outcome<(std::net::SocketAddr, Tape, Tape)> { |
| 123 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network); |
| 124 | let addr = res!(listener.local_addr(), IO, Network); |
| 125 | let (up, down): (Tape, Tape) = (Arc::new(Mutex::new(Vec::new())), Arc::new(Mutex::new(Vec::new()))); |
| 126 | let (u, d) = (up.clone(), down.clone()); |
| 127 | tokio::spawn(async move { |
| 128 | let (client, _) = match listener.accept().await { |
| 129 | Ok(x) => x, |
| 130 | Err(_) => return, |
| 131 | }; |
| 132 | let srv = match TcpStream::connect(server).await { |
| 133 | Ok(s) => s, |
| 134 | Err(_) => return, |
| 135 | }; |
| 136 | let (cr, cw) = client.into_split(); |
| 137 | let (sr, sw) = srv.into_split(); |
| 138 | tokio::spawn(pipe(cr, sw, u)); |
| 139 | tokio::spawn(pipe(sr, cw, d)); |
| 140 | }); |
| 141 | Ok((addr, up, down)) |
| 142 | } |
| 143 | |
| 144 | /// A recorded conversation played back: the reads come from the tape, and each write must be |
| 145 | /// the next bytes the native client wrote. |
| 146 | struct Replay { |
| 147 | down: Vec<u8>, |
| 148 | read: usize, |
| 149 | up: Vec<u8>, |
| 150 | wrote: usize, |
| 151 | } |
| 152 | |
| 153 | impl LineIo for Replay { |
| 154 | async fn write_all(&mut self, bytes: &[u8]) -> Outcome<()> { |
| 155 | let end = self.wrote + bytes.len(); |
| 156 | if self.up.get(self.wrote..end) != Some(bytes) { |
| 157 | return Err(err!("The client wrote {:?} where the socket carried {:?}.", |
| 158 | String::from_utf8_lossy(bytes), |
| 159 | String::from_utf8_lossy(&self.up[self.wrote..end.min(self.up.len())]); |
| 160 | Test, Mismatch)); |
| 161 | } |
| 162 | self.wrote = end; |
| 163 | Ok(()) |
| 164 | } |
| 165 | |
| 166 | async fn read_line(&mut self, max: usize) -> Outcome<Vec<u8>> { |
| 167 | let rest = &self.down[self.read..]; |
| 168 | let n = rest.iter().position(|b| *b == b'\n').map(|i| i + 1).unwrap_or(rest.len()).min(max); |
| 169 | let line = rest[..n].to_vec(); |
| 170 | self.read += n; |
| 171 | Ok(line) |
| 172 | } |
| 173 | |
| 174 | async fn read_exact(&mut self, n: usize) -> Outcome<Vec<u8>> { |
| 175 | match self.down.get(self.read..self.read + n) { |
| 176 | Some(b) => { |
| 177 | let out = b.to_vec(); |
| 178 | self.read += n; |
| 179 | Ok(out) |
| 180 | }, |
| 181 | None => Err(err!("The recording ends inside a {}-byte literal.", n; |
| 182 | IO, Network, Read, Interrupted)), |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | async fn starttls(&mut self, _host: &str) -> Outcome<()> { |
| 187 | Err(err!("The recording is of a plain connection."; Test, Invalid)) |
| 188 | } |
| 189 | |
| 190 | async fn redial(&self) -> Outcome<Self> { |
| 191 | Err(err!("A recording cannot be dialled again."; IO, Network, Missing)) |
| 192 | } |
| 193 | |
| 194 | async fn shutdown(&mut self) {} |
| 195 | |
| 196 | fn set_timeout(&mut self, _step: Duration) {} |
| 197 | } |
| 198 | |
| 199 | /// The conversation both runs hold: a first sync of INBOX from nothing, then, after the server |
| 200 | /// has taken a message and had one flagged, a second from the first's cursor. |
| 201 | async fn two_syncs<I: LineIo>(c: &mut ImapClient<I>, between: impl FnOnce() -> Outcome<()>) |
| 202 | -> Outcome<(Delta, Delta)> |
| 203 | { |
| 204 | res!(c.login(USER, PASS).await); |
| 205 | let caps = Caps::from_list(&res!(MailSession::capabilities(c).await)); |
| 206 | let budget = Budget { bodies: true, ..Budget::default() }; |
| 207 | let first = res!(sync_folder(c, &caps, &Snapshot { |
| 208 | mailbox: fmt!("INBOX"), |
| 209 | ..Snapshot::default() |
| 210 | }, &budget).await); |
| 211 | res!(between()); |
| 212 | let mut held = oxedyne_fe2o3_net::imap::uid_set::UidSet::new(); |
| 213 | for m in &first.new { |
| 214 | held.insert(m.uid); |
| 215 | } |
| 216 | let second = res!(sync_folder(c, &caps, &Snapshot { |
| 217 | mailbox: fmt!("INBOX"), |
| 218 | cursor: first.cursor.clone(), |
| 219 | held: held.clone(), |
| 220 | window: held, |
| 221 | }, &budget).await); |
| 222 | res!(c.logout().await); |
| 223 | Ok((first, second)) |
| 224 | } |
| 225 | |
| 226 | #[tokio::test] |
| 227 | async fn a_recorded_socket_session_replays_to_the_same_deltas_00() -> Outcome<()> { |
| 228 | let root = std::env::temp_dir().join(fmt!("fe2o3_sync_lineio_{}", std::process::id())); |
| 229 | let _ = fs::remove_dir_all(&root); |
| 230 | res!(fs::create_dir_all(&root), IO, File); |
| 231 | let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local")); |
| 232 | let user = res!(res!(OneUser.lookup(USER)).ok_or_else(|| err!("No test user."; Test, Bug))); |
| 233 | res!(store.ensure_user(&user)); |
| 234 | let inbox = FolderName::new("INBOX"); |
| 235 | for n in 1..=3 { |
| 236 | res!(store.append(&user, &inbox, &message(n), MessageFlags::default(), None)); |
| 237 | } |
| 238 | |
| 239 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network); |
| 240 | let server = res!(listener.local_addr(), IO, Network); |
| 241 | let srv = ImapServer { |
| 242 | store: store.clone(), |
| 243 | users: OneUser, |
| 244 | hostname: Arc::new(fmt!("test.local")), |
| 245 | }; |
| 246 | tokio::spawn(async move { |
| 247 | while let Ok((sock, peer)) = listener.accept().await { |
| 248 | let s = srv.clone(); |
| 249 | tokio::spawn(async move { let _ = s.run(sock, peer).await; }); |
| 250 | } |
| 251 | }); |
| 252 | |
| 253 | // ── The native client, on the socket, recorded ─────────────── |
| 254 | let (addr, up, down) = res!(proxy(server).await); |
| 255 | let cfg = ImapConfig::new(fmt!("127.0.0.1"), addr.port(), Security::Plain) |
| 256 | .with_addr(addr) |
| 257 | .with_timeout(Duration::from_secs(10)); |
| 258 | let mut c = res!(ImapClient::connect(&cfg).await); |
| 259 | let (s1, s2) = { |
| 260 | let (st, u) = (store.clone(), user.clone()); |
| 261 | res!(two_syncs(&mut c, move || { |
| 262 | res!(st.append(&u, &inbox, &message(4), MessageFlags::default(), None)); |
| 263 | Ok(()) |
| 264 | }).await) |
| 265 | }; |
| 266 | req!(3, s1.new.len(), "the first sync over the socket"); |
| 267 | req!(1, s2.new.len(), "the second sync over the socket"); |
| 268 | |
| 269 | // The proxy has the whole conversation once the client's LOGOUT is answered and the close |
| 270 | // has passed both ways. |
| 271 | let mut tapes = (Vec::new(), Vec::new()); |
| 272 | for _ in 0..50 { |
| 273 | tokio::time::sleep(Duration::from_millis(20)).await; |
| 274 | tapes = (lock_mutex!(up).clone(), lock_mutex!(down).clone()); |
| 275 | if tapes.1.ends_with(b"LOGOUT completed\r\n") { |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | let (up, down) = tapes; |
| 280 | req!(true, down.ends_with(b"LOGOUT completed\r\n"), "the recording is whole"); |
| 281 | |
| 282 | // ── The same conversation, from the recording, in memory ───── |
| 283 | let replay = Replay { down: down.clone(), read: 0, up: up.clone(), wrote: 0 }; |
| 284 | let mut m = res!(ImapClient::over(replay, &cfg).await); |
| 285 | let (r1, r2) = res!(two_syncs(&mut m, || Ok(())).await); |
| 286 | req!(s1, r1, "the first delta differs between socket and replay"); |
| 287 | req!(s2, r2, "the second delta differs between socket and replay"); |
| 288 | |
| 289 | if let Ok(p) = std::env::var("FE2O3_IMAP_TAPE") { |
| 290 | res!(fs::write(fmt!("{}.up", p), &up), IO, File); |
| 291 | res!(fs::write(fmt!("{}.down", p), &down), IO, File); |
| 292 | } |
| 293 | let _ = fs::remove_dir_all(&root); |
| 294 | Ok(()) |
| 295 | } |