2026-09-10 01:43:02 -04:00
|
|
|
//! M1 store: identity on `self`, friends keyed by pubkey, onion is locator.
|
|
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::sync::{Mutex, MutexGuard};
|
|
|
|
|
|
|
|
|
|
use onionwire::Store;
|
|
|
|
|
|
|
|
|
|
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
|
|
|
|
|
|
struct TempHome {
|
|
|
|
|
dir: tempfile::TempDir,
|
|
|
|
|
_guard: MutexGuard<'static, ()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TempHome {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let guard = ENV_LOCK.lock().expect("env lock");
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("ONIONWIRE_HOME", dir.path());
|
2026-09-10 17:19:50 -04:00
|
|
|
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
2026-09-10 01:43:02 -04:00
|
|
|
}
|
|
|
|
|
Self { dir, _guard: guard }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn path(&self) -> &Path {
|
|
|
|
|
self.dir.path()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for TempHome {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_HOME");
|
2026-09-10 17:19:50 -04:00
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
2026-09-10 01:43:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mode(path: &Path) -> u32 {
|
|
|
|
|
fs::metadata(path).unwrap().permissions().mode() & 0o777
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pk(tag: u8) -> [u8; 32] {
|
|
|
|
|
let mut k = [0u8; 32];
|
|
|
|
|
k[0] = tag;
|
|
|
|
|
k
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn first_run_creates_0700_dirs_and_self_row() {
|
|
|
|
|
let home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
|
|
|
|
|
let arti = home.path().join("arti");
|
2026-09-10 20:30:18 -04:00
|
|
|
let cache = home.path().join("cache");
|
2026-09-10 01:43:02 -04:00
|
|
|
let db = home.path().join("onionwire.db");
|
|
|
|
|
assert!(arti.is_dir(), "arti dir");
|
2026-09-10 20:30:18 -04:00
|
|
|
assert!(cache.is_dir(), "cache dir");
|
2026-09-10 01:43:02 -04:00
|
|
|
assert!(db.is_file(), "onionwire.db");
|
|
|
|
|
assert_eq!(mode(home.path()), 0o700);
|
|
|
|
|
assert_eq!(mode(&arti), 0o700);
|
2026-09-10 20:30:18 -04:00
|
|
|
assert_eq!(mode(&cache), 0o700);
|
2026-09-10 01:43:02 -04:00
|
|
|
|
|
|
|
|
let me = store.self_identity().expect("self");
|
|
|
|
|
assert_eq!(me.identity_pk.len(), 32);
|
|
|
|
|
assert_eq!(me.identity_sk.len(), 32);
|
2026-09-12 13:36:21 -04:00
|
|
|
assert_ne!(me.identity_pk, me.identity_sk.to_vec());
|
2026-09-10 01:43:02 -04:00
|
|
|
assert_eq!(me.onion, "");
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 0);
|
|
|
|
|
|
|
|
|
|
drop(store);
|
|
|
|
|
let store2 = Store::open().expect("reopen");
|
|
|
|
|
let me2 = store2.self_identity().expect("self again");
|
|
|
|
|
assert_eq!(me.identity_pk, me2.identity_pk);
|
|
|
|
|
assert_eq!(me.identity_sk, me2.identity_sk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn insert_friend_by_pubkey() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "aaa.onion", Some("alice"))
|
|
|
|
|
.expect("insert");
|
|
|
|
|
let f = store.get_friend(&pk(1)).expect("row").expect("found");
|
|
|
|
|
assert_eq!(f.onion, "aaa.onion");
|
|
|
|
|
assert_eq!(f.petname.as_deref(), Some("alice"));
|
|
|
|
|
assert_eq!(f.pubkey, pk(1));
|
|
|
|
|
assert!(!f.fingerprint.is_empty());
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn same_pubkey_different_onion_updates_locator() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "old.onion", Some("alice"))
|
|
|
|
|
.expect("insert");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "new.onion", None)
|
|
|
|
|
.expect("update");
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 1);
|
|
|
|
|
let f = store.get_friend(&pk(1)).expect("row").expect("found");
|
|
|
|
|
assert_eq!(f.onion, "new.onion");
|
|
|
|
|
assert_eq!(f.petname.as_deref(), Some("alice"), "petname is local");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unknown_pubkey_inserts_new_row() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "a.onion", None).expect("first");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(2), "b.onion", None)
|
|
|
|
|
.expect("second");
|
|
|
|
|
assert_eq!(store.friend_count().unwrap(), 2);
|
|
|
|
|
assert_eq!(store.get_friend(&pk(1)).unwrap().unwrap().onion, "a.onion");
|
|
|
|
|
assert_eq!(store.get_friend(&pk(2)).unwrap().unwrap().onion, "b.onion");
|
|
|
|
|
}
|
2026-09-10 02:07:40 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn open_at_two_homes_are_independent() {
|
|
|
|
|
let a = tempfile::tempdir().expect("a");
|
|
|
|
|
let b = tempfile::tempdir().expect("b");
|
2026-09-10 17:19:50 -04:00
|
|
|
let sa = onionwire::Store::open_at_with_passphrase(a.path(), "a-pass").expect("open a");
|
|
|
|
|
let sb = onionwire::Store::open_at_with_passphrase(b.path(), "b-pass").expect("open b");
|
2026-09-10 02:07:40 -04:00
|
|
|
let ia = sa.self_identity().unwrap();
|
|
|
|
|
let ib = sb.self_identity().unwrap();
|
|
|
|
|
assert_ne!(ia.identity_pk, ib.identity_pk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn incoming_message_persists_plaintext() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
store.append_message(&pk(1), "in", b"hello wire").unwrap();
|
|
|
|
|
let msgs = store.list_messages(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(msgs.len(), 1);
|
|
|
|
|
assert_eq!(msgs[0].dir, "in");
|
|
|
|
|
assert_eq!(msgs[0].plaintext, b"hello wire");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn self_has_x25519_prekey_and_set_onion() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let me = store.self_identity().unwrap();
|
|
|
|
|
assert_eq!(me.prekey_pk.len(), 32);
|
|
|
|
|
assert_eq!(me.prekey_sk.len(), 32);
|
2026-09-12 13:36:21 -04:00
|
|
|
assert_ne!(me.prekey_pk, me.prekey_sk.to_vec());
|
2026-09-10 02:07:40 -04:00
|
|
|
store.set_onion("abc.onion").unwrap();
|
|
|
|
|
assert_eq!(store.self_identity().unwrap().onion, "abc.onion");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn friend_prekey_lookup() {
|
|
|
|
|
let _home = TempHome::new();
|
|
|
|
|
let store = Store::open().expect("open");
|
|
|
|
|
let spk = [9u8; 32];
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
store.set_friend_prekey(&pk(1), &spk).unwrap();
|
|
|
|
|
let f = store.get_friend_by_prekey(&spk).unwrap().expect("found");
|
|
|
|
|
assert_eq!(f.pubkey, pk(1));
|
|
|
|
|
assert_eq!(f.prekey, spk);
|
|
|
|
|
}
|
2026-09-10 14:50:01 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn open_uses_wal_journal() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
2026-09-10 17:19:50 -04:00
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
2026-09-10 14:50:01 -04:00
|
|
|
assert_eq!(store.journal_mode().unwrap().to_ascii_lowercase(), "wal");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn corrupt_db_fails_closed() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
std::fs::write(dir.path().join("onionwire.db"), b"not a sqlite database").unwrap();
|
2026-09-10 17:19:50 -04:00
|
|
|
assert!(Store::open_at_with_passphrase(dir.path(), "onionwire-test").is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn home_contains_bytes(home: &Path, needle: &[u8]) -> bool {
|
|
|
|
|
let Ok(rd) = fs::read_dir(home) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
for ent in rd.flatten() {
|
|
|
|
|
let name = ent.file_name();
|
|
|
|
|
let name = name.to_string_lossy();
|
|
|
|
|
if !name.starts_with("onionwire.db") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let Ok(bytes) = fs::read(ent.path()) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
if bytes.windows(needle.len()).any(|w| w == needle) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty_passphrase_is_rejected() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let err = match Store::open_at_with_passphrase(dir.path(), "") {
|
|
|
|
|
Ok(_) => panic!("empty passphrase should fail"),
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
};
|
|
|
|
|
assert!(
|
|
|
|
|
err.to_string().to_ascii_lowercase().contains("empty"),
|
|
|
|
|
"got {err}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn append_message_db_file_does_not_contain_plaintext() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "correct-horse").expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
let needle = b"needle-plaintext-xyzzy-at-rest";
|
|
|
|
|
store.append_message(&pk(1), "out", needle).unwrap();
|
|
|
|
|
let msgs = store.list_messages(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(msgs.len(), 1);
|
|
|
|
|
assert_eq!(msgs[0].plaintext, needle);
|
|
|
|
|
drop(store);
|
|
|
|
|
assert!(
|
|
|
|
|
!home_contains_bytes(dir.path(), needle),
|
|
|
|
|
"sqlite files must not contain chat plaintext"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 13:00:18 -04:00
|
|
|
fn hex(bytes: &[u8]) -> String {
|
|
|
|
|
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn db_files(home: &Path) -> Vec<(String, Vec<u8>)> {
|
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
for ent in fs::read_dir(home).expect("read_dir").flatten() {
|
|
|
|
|
let name = ent.file_name().to_string_lossy().to_string();
|
|
|
|
|
if !name.starts_with("onionwire.db") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
out.push((name, fs::read(ent.path()).expect("read db file")));
|
|
|
|
|
}
|
|
|
|
|
out.sort();
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn store_contains(home: &Path, needle: &[u8]) -> bool {
|
|
|
|
|
db_files(home)
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|(_, bytes)| bytes.windows(needle.len()).any(|w| w == needle))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn whole_store_file_is_encrypted_at_rest() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "at-rest-pass").expect("open");
|
|
|
|
|
let me = store.self_identity().unwrap();
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(7), "friend.onion", Some("alice"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
store
|
|
|
|
|
.append_message(&pk(7), "out", b"body-at-rest-xyz")
|
|
|
|
|
.unwrap();
|
|
|
|
|
store
|
|
|
|
|
.append_message(&pk(7), "in", b"reply-at-rest-xyz")
|
|
|
|
|
.unwrap();
|
|
|
|
|
drop(store);
|
|
|
|
|
|
|
|
|
|
for (needle, what) in [
|
|
|
|
|
(b"body-at-rest-xyz".to_vec(), "chat body"),
|
|
|
|
|
(b"reply-at-rest-xyz".to_vec(), "inbound chat body"),
|
|
|
|
|
(hex(&pk(7)).into_bytes(), "friend pubkey hex"),
|
|
|
|
|
(b"friend.onion".to_vec(), "friend locator"),
|
2026-09-12 13:36:21 -04:00
|
|
|
(me.identity_sk.to_vec(), "identity secret key"),
|
2026-09-12 13:00:18 -04:00
|
|
|
] {
|
|
|
|
|
assert!(
|
|
|
|
|
!store_contains(dir.path(), &needle),
|
|
|
|
|
"onionwire.db* still contains the {what}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn wrong_passphrase_touches_nothing_and_leaves_store_readable() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "right-pass").expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
store
|
|
|
|
|
.append_message(&pk(1), "in", b"secret-chat-body")
|
|
|
|
|
.unwrap();
|
|
|
|
|
drop(store);
|
|
|
|
|
let before = db_files(dir.path());
|
|
|
|
|
assert!(!before.is_empty(), "expected store files on disk");
|
|
|
|
|
|
|
|
|
|
let err = match Store::open_at_with_passphrase(dir.path(), "wrong-pass") {
|
|
|
|
|
Ok(_) => panic!("wrong passphrase should fail"),
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
};
|
|
|
|
|
let msg = err.to_string().to_ascii_lowercase();
|
|
|
|
|
assert!(
|
|
|
|
|
msg.contains("passphrase") || msg.contains("decrypt") || msg.contains("wrong"),
|
|
|
|
|
"got {err}"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
db_files(dir.path()),
|
|
|
|
|
before,
|
|
|
|
|
"a failed unlock must not modify the store files"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "right-pass").expect("reopen");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
store.list_messages(&pk(1)).unwrap()[0].plaintext,
|
|
|
|
|
b"secret-chat-body"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn encrypted_store_without_salt_file_refuses_to_guess() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "at-rest-pass").expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
let me = store.self_identity().unwrap();
|
|
|
|
|
drop(store);
|
|
|
|
|
|
|
|
|
|
fs::remove_file(dir.path().join("store_salt")).expect("remove salt");
|
|
|
|
|
let err = match Store::open_at_with_passphrase(dir.path(), "at-rest-pass") {
|
|
|
|
|
Ok(_) => panic!("an encrypted store without its salt must not open"),
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
};
|
|
|
|
|
assert!(err.to_string().contains("store_salt"), "got {err}");
|
|
|
|
|
|
|
|
|
|
// The salt is only a salt: putting it back restores access, no data lost.
|
|
|
|
|
fs::write(dir.path().join("store_salt"), [0u8; 16]).unwrap();
|
|
|
|
|
let err = match Store::open_at_with_passphrase(dir.path(), "at-rest-pass") {
|
|
|
|
|
Ok(_) => panic!("a wrong salt must not silently open the store"),
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
};
|
|
|
|
|
assert!(
|
|
|
|
|
err.to_string().to_ascii_lowercase().contains("passphrase"),
|
|
|
|
|
"a wrong salt must not silently open the store: {err}"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(me.identity_sk.len(), 32);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 17:19:50 -04:00
|
|
|
#[test]
|
|
|
|
|
fn wrong_passphrase_cannot_open_or_list() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "right-pass").expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
store
|
|
|
|
|
.append_message(&pk(1), "in", b"secret-chat-body")
|
|
|
|
|
.unwrap();
|
|
|
|
|
drop(store);
|
|
|
|
|
let err = match Store::open_at_with_passphrase(dir.path(), "wrong-pass") {
|
|
|
|
|
Ok(_) => panic!("wrong passphrase should fail"),
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
};
|
|
|
|
|
let msg = err.to_string().to_ascii_lowercase();
|
|
|
|
|
assert!(
|
|
|
|
|
msg.contains("passphrase") || msg.contains("decrypt") || msg.contains("wrong"),
|
|
|
|
|
"got {err}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn reopen_with_same_passphrase_decrypts() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("open");
|
|
|
|
|
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
|
|
|
|
store.append_message(&pk(1), "out", b"hello again").unwrap();
|
|
|
|
|
drop(store);
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("reopen");
|
|
|
|
|
let msgs = store.list_messages(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(msgs[0].plaintext, b"hello again");
|
2026-09-10 14:50:01 -04:00
|
|
|
}
|
2026-09-10 21:10:50 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn env_passphrase_skips_prompt() {
|
|
|
|
|
let _g = ENV_LOCK.lock().expect("env lock");
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "from-env");
|
|
|
|
|
}
|
|
|
|
|
let mut prompted = false;
|
|
|
|
|
let got = onionwire::resolve_store_passphrase(|_| {
|
|
|
|
|
prompted = true;
|
|
|
|
|
Ok("from-tty".into())
|
|
|
|
|
});
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
|
|
|
|
}
|
2026-09-12 13:36:21 -04:00
|
|
|
assert_eq!(got.unwrap().as_str(), "from-env");
|
2026-09-10 21:10:50 -04:00
|
|
|
assert!(!prompted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty_env_passphrase_is_rejected_without_prompt() {
|
|
|
|
|
let _g = ENV_LOCK.lock().expect("env lock");
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "");
|
|
|
|
|
}
|
|
|
|
|
let mut prompted = false;
|
|
|
|
|
let err = onionwire::resolve_store_passphrase(|_| {
|
|
|
|
|
prompted = true;
|
|
|
|
|
Ok("from-tty".into())
|
|
|
|
|
})
|
|
|
|
|
.unwrap_err();
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
|
|
|
|
}
|
|
|
|
|
assert!(err.to_string().contains("empty"), "got {err}");
|
|
|
|
|
assert!(!prompted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn missing_env_reads_secret_and_strips_newline() {
|
|
|
|
|
let _g = ENV_LOCK.lock().expect("env lock");
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
|
|
|
|
}
|
|
|
|
|
let got = onionwire::resolve_store_passphrase(|prompt| {
|
|
|
|
|
assert!(prompt.contains("store passphrase"), "prompt {prompt}");
|
|
|
|
|
Ok("secret-from-tty\n".into())
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2026-09-12 13:36:21 -04:00
|
|
|
assert_eq!(got.as_str(), "secret-from-tty");
|
2026-09-10 21:10:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn missing_env_empty_secret_is_rejected() {
|
|
|
|
|
let _g = ENV_LOCK.lock().expect("env lock");
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
|
|
|
|
}
|
|
|
|
|
let err = onionwire::resolve_store_passphrase(|_| Ok(String::new())).unwrap_err();
|
|
|
|
|
assert!(err.to_string().contains("empty"), "got {err}");
|
|
|
|
|
}
|
2026-09-16 18:30:32 -04:00
|
|
|
|
|
|
|
|
/// A stand-in for `RatchetState::to_bytes()`: version byte then secrets. The
|
|
|
|
|
/// store must treat it as opaque, so the exact bytes are the contract.
|
|
|
|
|
fn ratchet_blob(tag: u8) -> Vec<u8> {
|
|
|
|
|
let mut blob = Vec::with_capacity(64);
|
|
|
|
|
blob.push(1);
|
|
|
|
|
blob.extend_from_slice(&[tag; 63]);
|
|
|
|
|
blob
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ratchet_save_load_round_trips_exact_bytes() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "a.onion", None)
|
|
|
|
|
.expect("friend");
|
|
|
|
|
let blob = ratchet_blob(0x11);
|
|
|
|
|
store.save_ratchet(&pk(1), &blob).expect("save");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
store.load_ratchet(&pk(1)).expect("load"),
|
|
|
|
|
Some(blob.clone())
|
|
|
|
|
);
|
|
|
|
|
drop(store);
|
|
|
|
|
assert!(
|
|
|
|
|
!store_contains(dir.path(), &blob),
|
|
|
|
|
"ratchet secrets must not sit in the db file in the clear"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ratchet_load_for_friend_without_a_row_is_none() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "a.onion", None)
|
|
|
|
|
.expect("friend");
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(1)).expect("known friend"), None);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
store.load_ratchet(&pk(2)).expect("unknown friend"),
|
|
|
|
|
None,
|
|
|
|
|
"an unknown peer is not an error, just no session"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ratchet_sessions_are_per_friend() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "a.onion", None)
|
|
|
|
|
.expect("friend a");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(2), "b.onion", None)
|
|
|
|
|
.expect("friend b");
|
|
|
|
|
let a = ratchet_blob(0xaa);
|
|
|
|
|
let b = ratchet_blob(0xbb);
|
|
|
|
|
store.save_ratchet(&pk(1), &a).expect("save a");
|
|
|
|
|
store.save_ratchet(&pk(2), &b).expect("save b");
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), Some(a.clone()));
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(2)).unwrap(), Some(b.clone()));
|
|
|
|
|
|
|
|
|
|
store.clear_ratchet(&pk(1)).expect("clear a");
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), None);
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(2)).unwrap(), Some(b));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ratchet_clear_on_missing_row_is_a_no_op() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "a.onion", None)
|
|
|
|
|
.expect("friend");
|
|
|
|
|
store.clear_ratchet(&pk(1)).expect("no row to clear");
|
|
|
|
|
store
|
|
|
|
|
.clear_ratchet(&pk(9))
|
|
|
|
|
.expect("unknown friend is not an error");
|
|
|
|
|
let blob = ratchet_blob(0x22);
|
|
|
|
|
store.save_ratchet(&pk(1), &blob).expect("save");
|
|
|
|
|
store.clear_ratchet(&pk(1)).expect("clear");
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ratchet_state_survives_reopening_the_store() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let blob = ratchet_blob(0x33);
|
|
|
|
|
{
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("open");
|
|
|
|
|
store
|
|
|
|
|
.upsert_friend(&pk(1), "a.onion", None)
|
|
|
|
|
.expect("friend");
|
|
|
|
|
store.append_message(&pk(1), "in", b"hello").expect("msg");
|
|
|
|
|
store.save_ratchet(&pk(1), &blob).expect("save");
|
|
|
|
|
}
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("reopen");
|
|
|
|
|
assert_eq!(store.load_ratchet(&pk(1)).expect("load"), Some(blob));
|
|
|
|
|
assert_eq!(store.list_messages(&pk(1)).unwrap()[0].plaintext, b"hello");
|
|
|
|
|
}
|
store: a wallet catalogue in the encrypted db
Wallets were only ever a file in $ONIONWIRE_HOME/wallet plus the
single-row wallet_child config for the one the child is currently
pointed at, so a created wallet was not remembered and a second create
had nowhere to go.
Add a 'wallets' table (id, name, identity_pubkey, onion_locator,
wallet_filename, created_at) and CRUD on Store: create_wallet,
get_wallet_by_id, list_wallets, delete_wallet. wallet_filename is
UNIQUE so a second wallet cannot silently reuse the first one's file,
and identity_pubkey is deliberately not unique (identity is the roster
key, a wallet is a file; the locator may rotate or be empty).
No new crate: the store is already rusqlite + SQLCipher with a raw
PRAGMA key, and sqlx has no SQLCipher path, so a second pool would be a
second unencrypted way in.
Tests: 5 store cases in tests/store.rs (round-trip, coexistence across
a reopen, duplicate filename, delete, blank label/filename) plus the
at_rest upgrade case in src/store.rs.
2026-09-21 10:21:48 -04:00
|
|
|
|
|
|
|
|
// ---- the wallet catalogue (`wallets`) --------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Several wallet *files* live under one identity: the row here is what makes a
|
|
|
|
|
// wallet survive a restart, because the wallet child's own config only ever
|
|
|
|
|
// describes the one it is pointed at right now.
|
|
|
|
|
|
|
|
|
|
fn new_wallet(filename: &str, name: Option<&str>) -> onionwire::NewWallet {
|
|
|
|
|
onionwire::NewWallet {
|
|
|
|
|
name: name.map(str::to_string),
|
|
|
|
|
identity_pubkey: pk(7),
|
|
|
|
|
onion_locator: "walletloc.onion".to_string(),
|
|
|
|
|
wallet_filename: filename.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn create_wallet_stores_every_field_and_reads_it_back() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
|
|
|
|
|
|
|
|
|
|
let row = store
|
|
|
|
|
.create_wallet(&new_wallet("savings", Some("Savings")))
|
|
|
|
|
.expect("create");
|
|
|
|
|
assert!(row.id > 0, "id assigned: {row:?}");
|
|
|
|
|
assert!(row.created_at > 0, "created_at stamped: {row:?}");
|
|
|
|
|
assert_eq!(row.wallet_filename, "savings");
|
|
|
|
|
assert_eq!(row.name.as_deref(), Some("Savings"));
|
|
|
|
|
assert_eq!(row.identity_pubkey, pk(7));
|
|
|
|
|
assert_eq!(row.onion_locator, "walletloc.onion");
|
|
|
|
|
|
|
|
|
|
let got = store
|
|
|
|
|
.get_wallet_by_id(row.id)
|
|
|
|
|
.expect("get")
|
|
|
|
|
.expect("row exists");
|
|
|
|
|
assert_eq!(got, row, "the stored row is what came back");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn multiple_wallets_coexist_and_survive_a_reopen() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let (spending, savings) = {
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
|
|
|
|
|
let spending = store
|
|
|
|
|
.create_wallet(&new_wallet("spending", Some("Spending")))
|
|
|
|
|
.expect("spending");
|
|
|
|
|
let savings = store
|
|
|
|
|
.create_wallet(&new_wallet("savings", None))
|
|
|
|
|
.expect("savings");
|
|
|
|
|
assert_ne!(spending.id, savings.id, "two wallets, two rows");
|
|
|
|
|
(spending.id, savings.id)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("reopen");
|
|
|
|
|
let rows = store.list_wallets().expect("list");
|
|
|
|
|
assert_eq!(rows.len(), 2, "both wallets remembered: {rows:?}");
|
|
|
|
|
assert_eq!(rows[0].id, spending, "creation order");
|
|
|
|
|
assert_eq!(rows[1].id, savings);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
rows[1].name, None,
|
|
|
|
|
"an unlabelled wallet round-trips as None"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(rows[1].wallet_filename, "savings");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_second_wallet_cannot_reuse_a_wallet_filename() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
|
|
|
|
|
let first = store
|
|
|
|
|
.create_wallet(&new_wallet("onionwire", None))
|
|
|
|
|
.expect("first");
|
|
|
|
|
|
|
|
|
|
let err = store
|
|
|
|
|
.create_wallet(&new_wallet("onionwire", Some("Clash")))
|
|
|
|
|
.expect_err("a duplicate filename must not overwrite the first wallet");
|
|
|
|
|
assert!(
|
|
|
|
|
err.to_string().contains("onionwire"),
|
|
|
|
|
"names the file: {err}"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let rows = store.list_wallets().expect("list");
|
|
|
|
|
assert_eq!(rows.len(), 1, "the rejected insert left one row: {rows:?}");
|
|
|
|
|
assert_eq!(rows[0], first, "the first wallet is untouched");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn delete_wallet_removes_only_that_row() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
|
|
|
|
|
let a = store.create_wallet(&new_wallet("a", None)).expect("a");
|
|
|
|
|
let b = store.create_wallet(&new_wallet("b", None)).expect("b");
|
|
|
|
|
|
|
|
|
|
assert!(store.delete_wallet(a.id).expect("delete"), "row was there");
|
|
|
|
|
assert_eq!(store.get_wallet_by_id(a.id).expect("get"), None);
|
|
|
|
|
let rows = store.list_wallets().expect("list");
|
|
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
|
assert_eq!(rows[0].id, b.id);
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
!store
|
|
|
|
|
.delete_wallet(9999)
|
|
|
|
|
.expect("unknown id is not an error"),
|
|
|
|
|
"nothing to delete reports false"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_wallet_without_a_label_or_locator_stores_nulls_and_rejects_a_blank_filename() {
|
|
|
|
|
let dir = tempfile::tempdir().expect("tempdir");
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir.path(), "wallet-pass").expect("open");
|
|
|
|
|
|
|
|
|
|
let mut blank = new_wallet("blank", Some(" "));
|
|
|
|
|
blank.onion_locator = String::new();
|
|
|
|
|
let row = store.create_wallet(&blank).expect("blank label");
|
|
|
|
|
assert_eq!(row.name, None, "a whitespace label is no label");
|
|
|
|
|
assert_eq!(row.onion_locator, "", "a locator may be empty pre-publish");
|
|
|
|
|
|
|
|
|
|
let err = store
|
|
|
|
|
.create_wallet(&new_wallet(" ", None))
|
|
|
|
|
.expect_err("an empty filename cannot name a wallet file");
|
|
|
|
|
assert!(err.to_string().contains("filename"), "{err}");
|
|
|
|
|
}
|