C12.5: mail accounts, add and remove

ElyMail's window lists accounts and adds Gmail, Microsoft, iCloud
(app-specific password), IMAP and POP3 ones; a check logs in to both
servers and reports each. Accounts live in the store as JSON with a
case-insensitive unique address; secrets go to the keychain. Removal
names the cache it deletes, takes the secret first and puts it back
when local removal fails. Originals are set aside until commit and
restored after a crash. Requests are numbered so late results stay
with their own dialog. The account list is keyboard-navigable.
This commit is contained in:
雷电芽衣 committed 2026-10-19 20:17:19 -04:00
1 parent 5068bc07ca
commit 1c225f6812
39 files changed
+3152 -105

No files matched your search

+200
View File
@@ -0,0 +1,200 @@
mod fake;
use base64::{Engine, engine::general_purpose::STANDARD};
use ely_mail_engine::{
Account, Auth, Cached, Error, Fetched, Key, Protocol, Secret, Security, Server, Store,
};
use fake::{Certs, account, certs, expect, send, serve};
fn pop_oauth(address: &str) -> Account {
Account {
address: address.into(),
user: "bo".into(),
auth: Auth::Microsoft,
protocol: Protocol::Pop,
incoming: Server {
host: "pop.example.com".into(),
port: 995,
security: Security::Tls,
},
outgoing: Server {
host: "smtp.example.com".into(),
port: 587,
security: Security::StartTls,
},
}
}
#[test]
fn accounts_come_back_as_added() {
let dir = tempfile::tempdir().unwrap();
let store = Store::open(dir.path()).unwrap();
let ann = account("ann@example.com");
let bo = pop_oauth("bo@example.com");
let first = store.add_account(&ann).unwrap();
let second = store.add_account(&bo).unwrap();
drop(store);
let store = Store::open(dir.path()).unwrap();
assert_eq!(store.accounts().unwrap(), [(first, ann), (second, bo)]);
}
#[test]
fn an_address_is_added_once() {
let dir = tempfile::tempdir().unwrap();
let store = Store::open(dir.path()).unwrap();
store.add_account(&account("ann@example.com")).unwrap();
let err = store
.add_account(&pop_oauth("ANN@Example.com"))
.unwrap_err();
assert!(
matches!(&err, Error::Taken(address) if address == "ANN@Example.com"),
"{err}"
);
assert_eq!(store.accounts().unwrap().len(), 1);
}
#[test]
fn removing_an_account_keeps_what_another_shares() {
let dir = tempfile::tempdir().unwrap();
let mut store = Store::open(dir.path()).unwrap();
let ann = store.add_account(&account("ann@example.com")).unwrap();
let bo = store.add_account(&account("bo@example.com")).unwrap();
let (ann_inbox, bo_inbox) = (
store.mailbox(ann, "INBOX").unwrap(),
store.mailbox(bo, "INBOX").unwrap(),
);
let shared = b"Subject: Both\r\n\r\nshared\r\n";
let own = b"Subject: Ann only\r\n\r\nmine and mine alone\r\n";
let key = |uid| Key::Imap {
uidvalidity: 1,
uid,
};
store
.put(ann_inbox, &key(1), &[], Fetched::Whole(shared))
.unwrap();
store
.put(ann_inbox, &key(2), &[], Fetched::Whole(own))
.unwrap();
let kept = store
.put(bo_inbox, &key(1), &[], Fetched::Whole(shared))
.unwrap();
assert_eq!(
store.cached(ann).unwrap(),
Cached {
messages: 2,
bytes: own.len() as u64
}
);
store.remove_account(ann).unwrap();
assert_eq!(store.accounts().unwrap().len(), 1);
assert_eq!(store.original(kept).unwrap().unwrap(), shared);
assert_eq!(store.sizes().unwrap().originals, shared.len() as u64);
let err = store.remove_account(ann).unwrap_err();
assert!(matches!(err, Error::Store(_)), "{err}");
}
/// IMAP and SMTP scripts that log in and leave.
async fn servers(
certs: &Certs,
imap_answer: &str,
) -> (Account, Vec<tokio::task::JoinHandle<Result<(), String>>>) {
let mut imap = vec![
send("* OK ready\n"),
expect("{tag} LOGIN \"ann@example.com\" \"pw\""),
send(imap_answer),
];
if imap_answer.contains(" OK ") {
imap.extend([expect("{tag} LOGOUT"), send("* BYE\n{tag} OK bye\n")]);
}
let (incoming, imap) = serve(certs, Security::Tls, imap).await;
let smtp = vec![
send("220 mx ready\n"),
expect("EHLO [127.0.0.1]"),
send("250-mx\n250 AUTH PLAIN\n"),
expect(&format!(
"AUTH PLAIN {}",
STANDARD.encode("\0ann@example.com\0pw")
)),
send("235 ok\n"),
expect("QUIT"),
send("221 bye\n"),
];
let (outgoing, smtp) = serve(certs, Security::Tls, smtp).await;
let account = Account {
incoming,
outgoing,
..account("ann@example.com")
};
(account, vec![imap, smtp])
}
#[tokio::test]
async fn a_check_logs_in_to_both_servers() {
let certs = certs();
let (account, scripts) = servers(&certs, "{tag} OK in\n").await;
let checked = account
.check(Secret::Password("pw".into()), &certs.trusted)
.await;
assert!(checked.passed());
for script in scripts {
script.await.unwrap().unwrap();
}
}
#[tokio::test]
async fn each_server_answers_for_itself() {
let certs = certs();
let (account, scripts) =
servers(&certs, "{tag} NO [AUTHENTICATIONFAILED] bad password\n").await;
let checked = account
.check(Secret::Password("pw".into()), &certs.trusted)
.await;
assert!(!checked.passed());
match checked.incoming {
Err(Error::Rejected(text)) => assert!(text.contains("bad password"), "{text}"),
other => panic!("incoming: {:?}", other.err()),
}
checked.outgoing.unwrap();
for script in scripts {
script.await.unwrap().unwrap();
}
}
#[test]
fn what_no_server_could_carry_is_refused() {
account("ann@example.com").valid().unwrap();
let broken = [
Account {
address: "ann".into(),
..account("ann@example.com")
},
Account {
user: String::new(),
..account("ann@example.com")
},
Account {
user: "ann\r\nRSET".into(),
..account("ann@example.com")
},
Account {
incoming: Server {
host: "imap example.com".into(),
port: 993,
security: Security::Tls,
},
..account("ann@example.com")
},
Account {
outgoing: Server {
host: "smtp.example.com".into(),
port: 0,
security: Security::Tls,
},
..account("ann@example.com")
},
];
for account in broken {
let err = account.valid().unwrap_err();
assert!(matches!(err, Error::Invalid(_)), "{err}");
}
}
+73
View File
@@ -0,0 +1,73 @@
mod fake;
use std::{fs, path::Path};
use ely_mail_engine::{Fetched, Key, Store};
use fake::account;
use sha2::{Digest, Sha256};
fn hash(bytes: &[u8]) -> String {
Sha256::digest(bytes)
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
fn parked(dir: &Path, hash: &str) -> std::path::PathBuf {
dir.join("content")
.join(&hash[..2])
.join(format!(".{hash}.gone"))
}
fn key(uid: u32) -> Key {
Key::Imap {
uidvalidity: 1,
uid,
}
}
#[test]
fn a_removal_that_fails_midway_keeps_every_original() {
let dir = tempfile::tempdir().unwrap();
let mut store = Store::open(dir.path()).unwrap();
let ann = store.add_account(&account("ann@example.com")).unwrap();
let inbox = store.mailbox(ann, "INBOX").unwrap();
let one: &[u8] = b"Subject: One\r\n\r\nfirst\r\n";
let two: &[u8] = b"Subject: Two\r\n\r\nsecond\r\n";
let a = store.put(inbox, &key(1), &[], Fetched::Whole(one)).unwrap();
let b = store.put(inbox, &key(2), &[], Fetched::Whole(two)).unwrap();
let last = hash(one).max(hash(two));
fs::create_dir(parked(dir.path(), &last)).unwrap();
store.remove_account(ann).unwrap_err();
assert_eq!(store.accounts().unwrap().len(), 1);
assert_eq!(store.original(a).unwrap().unwrap(), one);
assert_eq!(store.original(b).unwrap().unwrap(), two);
fs::remove_dir(parked(dir.path(), &last)).unwrap();
store.remove_account(ann).unwrap();
assert_eq!(store.sizes().unwrap().originals, 0);
}
#[test]
fn a_restart_puts_back_what_a_crash_left_aside() {
let dir = tempfile::tempdir().unwrap();
let mut store = Store::open(dir.path()).unwrap();
let ann = store.add_account(&account("ann@example.com")).unwrap();
let inbox = store.mailbox(ann, "INBOX").unwrap();
let kept: &[u8] = b"Subject: Kept\r\n\r\nstill named\r\n";
let id = store
.put(inbox, &key(1), &[], Fetched::Whole(kept))
.unwrap();
drop(store);
let named = hash(kept);
let original = dir.path().join("content").join(&named[..2]).join(&named);
fs::rename(&original, parked(dir.path(), &named)).unwrap();
let orphan = hash(b"nobody names this");
let shard = dir.path().join("content").join(&orphan[..2]);
fs::create_dir_all(&shard).unwrap();
fs::write(parked(dir.path(), &orphan), b"nobody names this").unwrap();
let store = Store::open(dir.path()).unwrap();
assert_eq!(store.original(id).unwrap().unwrap(), kept);
assert!(!parked(dir.path(), &named).exists());
assert!(!parked(dir.path(), &orphan).exists());
assert_eq!(store.sizes().unwrap().originals, kept.len() as u64);
}
+18 -1
View File
@@ -5,7 +5,7 @@ pub mod smtp;
use std::sync::Arc;
use ely_mail_engine::{Login, Secret, Security, Server, TlsConnector};
use ely_mail_engine::{Account, Auth, Login, Protocol, Secret, Security, Server, TlsConnector};
use rcgen::{CertificateParams, KeyPair};
use tokio::{
io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader},
@@ -78,6 +78,23 @@ pub fn certs() -> Certs {
}
}
/// An IMAP account on the test address.
pub fn account(address: &str) -> Account {
let server = |port| Server {
host: "127.0.0.1".into(),
port,
security: Security::Tls,
};
Account {
address: address.into(),
user: address.into(),
auth: Auth::Password,
protocol: Protocol::Imap,
incoming: server(993),
outgoing: server(465),
}
}
pub fn login(secret: Secret) -> Login {
Login {
user: "ann@example.com".into(),
+5 -1
View File
@@ -1,3 +1,5 @@
mod fake;
use std::path::Path;
use ely_mail_engine::{Error, Fetched, Key, MailboxId, MessageId, Store};
@@ -23,7 +25,9 @@ fn headers(raw: &[u8]) -> &[u8] {
fn inbox(dir: &Path) -> (Store, MailboxId) {
let store = Store::open(dir).unwrap();
let account = store.add_account("ann@example.com").unwrap();
let account = store
.add_account(&fake::account("ann@example.com"))
.unwrap();
let inbox = store.mailbox(account, "INBOX").unwrap();
(store, inbox)
}
@@ -1,5 +1,7 @@
#![cfg(windows)]
mod fake;
use std::{fs::File, os::windows::fs::OpenOptionsExt, path::Path};
use ely_mail_engine::{Fetched, Key, MailboxId, Store};
@@ -10,7 +12,9 @@ const TWO: &[u8] = b"Subject: two\r\n\r\nsecond body\r\n";
fn inbox(dir: &Path) -> (Store, MailboxId) {
let store = Store::open(dir).unwrap();
let account = store.add_account("ann@example.com").unwrap();
let account = store
.add_account(&fake::account("ann@example.com"))
.unwrap();
let inbox = store.mailbox(account, "INBOX").unwrap();
(store, inbox)
}
+5 -1
View File
@@ -1,3 +1,5 @@
mod fake;
use std::path::Path;
use ely_mail_engine::{Error, Fetched, Key, MailboxId, MessageId, Store};
@@ -17,7 +19,9 @@ fn sample() -> Vec<u8> {
fn inbox(dir: &Path) -> (Store, MailboxId) {
let store = Store::open(dir).unwrap();
let account = store.add_account("ann@example.com").unwrap();
let account = store
.add_account(&fake::account("ann@example.com"))
.unwrap();
let inbox = store.mailbox(account, "INBOX").unwrap();
(store, inbox)
}
+4 -3
View File
@@ -67,9 +67,10 @@ fn a_refused_store_keeps_the_secret_for_this_session() {
assert!(matches!(err, Error::Vault(text) if text.contains("locked")));
let err = vault.remove("a").err().unwrap();
assert!(matches!(err, Error::Vault(_)));
assert!(
vault.get("a").is_err(),
"the session copy is gone after remove"
assert_eq!(
vault.get("a").unwrap().as_deref(),
Some("pw"),
"a failed remove keeps the session copy"
);
}