crypto: Add a memory only crypto store.

master
Damir Jelić 2020-03-18 10:42:46 +01:00
parent 3b1f863419
commit 6fb29451ce
1 changed files with 52 additions and 11 deletions

View File

@ -21,7 +21,8 @@ static DATABASE_NAME: &str = "matrix-sdk-crypto.db";
impl SqliteStore { impl SqliteStore {
async fn open<P: AsRef<Path>>(user_id: &str, device_id: &str, path: P) -> Result<SqliteStore> { async fn open<P: AsRef<Path>>(user_id: &str, device_id: &str, path: P) -> Result<SqliteStore> {
SqliteStore::open_helper(user_id, device_id, path, None).await let url = SqliteStore::path_to_url(path)?;
SqliteStore::open_helper(user_id, device_id, url.as_ref(), None).await
} }
async fn open_with_passphrase<P: AsRef<Path>>( async fn open_with_passphrase<P: AsRef<Path>>(
@ -30,19 +31,35 @@ impl SqliteStore {
path: P, path: P,
passphrase: String, passphrase: String,
) -> Result<SqliteStore> { ) -> Result<SqliteStore> {
SqliteStore::open_helper(user_id, device_id, path, Some(Zeroizing::new(passphrase))).await let url = SqliteStore::path_to_url(path)?;
SqliteStore::open_helper(
user_id,
device_id,
url.as_ref(),
Some(Zeroizing::new(passphrase)),
)
.await
} }
async fn open_helper<P: AsRef<Path>>( async fn open_in_memory(user_id: &str, device_id: &str) -> Result<SqliteStore> {
SqliteStore::open_helper(user_id, device_id, "sqlite::memory:", None).await
}
fn path_to_url<P: AsRef<Path>>(path: P) -> Result<Url> {
let url = Url::from_directory_path(path.as_ref()).expect("Can't create URL from directory");
let url = url
.join(DATABASE_NAME)
.expect("Can't append database name to URL");
Ok(url)
}
async fn open_helper(
user_id: &str, user_id: &str,
device_id: &str, device_id: &str,
path: P, sqlite_url: &str,
passphrase: Option<Zeroizing<String>>, passphrase: Option<Zeroizing<String>>,
) -> Result<SqliteStore> { ) -> Result<SqliteStore> {
let url = Url::from_directory_path(path.as_ref()).unwrap(); let connection = SqliteConnection::connect(sqlite_url).await.unwrap();
let url = url.join(DATABASE_NAME).unwrap();
let connection = SqliteConnection::connect(url.as_ref()).await.unwrap();
let store = SqliteStore { let store = SqliteStore {
user_id: Arc::new(user_id.to_owned()), user_id: Arc::new(user_id.to_owned()),
device_id: Arc::new(device_id.to_owned()), device_id: Arc::new(device_id.to_owned()),
@ -114,8 +131,23 @@ impl CryptoStore for SqliteStore {
) )
.bind(&*self.user_id) .bind(&*self.user_id)
.bind(&*self.device_id) .bind(&*self.device_id)
.bind(&pickle)
.bind(acc.shared)
.execute(&mut *connection)
.await
.unwrap();
query(
"UPDATE account
SET pickle = ?,
shared = ?
WHERE user_id = ? and
device_id = ?",
)
.bind(pickle) .bind(pickle)
.bind(true) .bind(acc.shared)
.bind(&*self.user_id)
.bind(&*self.device_id)
.execute(&mut *connection) .execute(&mut *connection)
.await .await
.unwrap(); .unwrap();
@ -132,14 +164,23 @@ mod test {
use super::{Account, CryptoStore, SqliteStore}; use super::{Account, CryptoStore, SqliteStore};
static USER_ID: &str = "@example:localhost";
static DEVICE_ID: &str = "DEVICEID";
async fn get_store() -> SqliteStore { async fn get_store() -> SqliteStore {
let tmpdir = tempdir().unwrap(); let tmpdir = tempdir().unwrap();
let tmpdir_path = tmpdir.path().to_str().unwrap(); let tmpdir_path = tmpdir.path().to_str().unwrap();
SqliteStore::open("@example:localhost", "DEVICEID", tmpdir_path) SqliteStore::open(USER_ID, DEVICE_ID, tmpdir_path)
.await .await
.expect("Can't create store") .expect("Can't create store")
} }
async fn get_memory_store() -> SqliteStore {
SqliteStore::open_in_memory(USER_ID, DEVICE_ID)
.await
.expect("Can't create memory store")
}
fn get_account() -> Arc<Mutex<Account>> { fn get_account() -> Arc<Mutex<Account>> {
let account = Account::new(); let account = Account::new();
Arc::new(Mutex::new(account)) Arc::new(Mutex::new(account))
@ -167,7 +208,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn load_account() { async fn load_account() {
let store = get_store().await; let store = get_memory_store().await;
let account = get_account(); let account = get_account();
store store