1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#![cfg(sqlite_test_sqlcipher)]
use std::str::FromStr;
use sqlx::sqlite::SqliteQueryResult;
use sqlx::{query, Connection, SqliteConnection};
use sqlx::{sqlite::SqliteConnectOptions, ConnectOptions};
use tempfile::TempDir;
async fn new_db_url() -> anyhow::Result<(String, TempDir)> {
let dir = TempDir::new()?;
let filepath = dir.path().join("database.sqlite3");
Ok((format!("sqlite://{}", filepath.display()), dir))
}
async fn fill_db(conn: &mut SqliteConnection) -> anyhow::Result<SqliteQueryResult> {
conn.transaction(|tx| {
Box::pin(async move {
query(
"
CREATE TABLE Company(
Id INT PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Salary REAL
);
",
)
.execute(&mut **tx)
.await?;
query(
r#"
INSERT INTO Company(Id, Name, Salary)
VALUES
(1, "aaa", 111),
(2, "bbb", 222)
"#,
)
.execute(&mut **tx)
.await
})
})
.await
.map_err(|e| e.into())
}
#[sqlx_macros::test]
async fn it_encrypts() -> anyhow::Result<()> {
let (url, _dir) = new_db_url().await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "the_password")
.create_if_missing(true)
.connect()
.await?;
fill_db(&mut conn).await?;
// Create another connection without key, query should fail
let mut conn = SqliteConnectOptions::from_str(&url)?.connect().await?;
assert!(conn
.transaction(|tx| {
Box::pin(async move { query("SELECT * FROM Company;").fetch_all(&mut **tx).await })
})
.await
.is_err());
Ok(())
}
#[sqlx_macros::test]
async fn it_can_store_and_read_encrypted_data() -> anyhow::Result<()> {
let (url, _dir) = new_db_url().await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "the_password")
.create_if_missing(true)
.connect()
.await?;
fill_db(&mut conn).await?;
// Create another connection with valid key
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "the_password")
.connect()
.await?;
let result = conn
.transaction(|tx| {
Box::pin(async move { query("SELECT * FROM Company;").fetch_all(&mut **tx).await })
})
.await?;
assert!(result.len() > 0);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_if_password_is_incorrect() -> anyhow::Result<()> {
let (url, _dir) = new_db_url().await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "the_password")
.create_if_missing(true)
.connect()
.await?;
fill_db(&mut conn).await?;
// Connection with invalid key should not allow to execute queries
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "BADBADBAD")
.connect()
.await?;
assert!(conn
.transaction(|tx| {
Box::pin(async move { query("SELECT * FROM Company;").fetch_all(&mut **tx).await })
})
.await
.is_err());
Ok(())
}
#[sqlx_macros::test]
async fn it_honors_order_of_encryption_pragmas() -> anyhow::Result<()> {
let (url, _dir) = new_db_url().await?;
// Make call of cipher configuration mixed with other pragmas,
// it should have no effect, encryption related pragmas should be
// executed first and allow to establish valid connection
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("cipher_kdf_algorithm", "PBKDF2_HMAC_SHA1")
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
.pragma("cipher_page_size", "1024")
.pragma("key", "the_password")
.foreign_keys(true)
.pragma("kdf_iter", "64000")
.auto_vacuum(sqlx::sqlite::SqliteAutoVacuum::Incremental)
.pragma("cipher_hmac_algorithm", "HMAC_SHA1")
.create_if_missing(true)
.connect()
.await?;
fill_db(&mut conn).await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("dummy", "pragma")
// The cipher configuration set on first connection is
// version 3 of SQLCipher, so for second it's enough to set
// the compatibility mode.
.pragma("cipher_compatibility", "3")
.pragma("key", "the_password")
.connect()
.await?;
let result = conn
.transaction(|tx| {
Box::pin(async move { query("SELECT * FROM COMPANY;").fetch_all(&mut **tx).await })
})
.await?;
assert!(result.len() > 0);
Ok(())
}
#[sqlx_macros::test]
async fn it_allows_to_rekey_the_db() -> anyhow::Result<()> {
let (url, _dir) = new_db_url().await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("key", "the_password")
.create_if_missing(true)
.connect()
.await?;
fill_db(&mut conn).await?;
// The 'pragma rekey' can be called at any time
query("PRAGMA rekey = new_password;")
.execute(&mut conn)
.await?;
let mut conn = SqliteConnectOptions::from_str(&url)?
.pragma("dummy", "pragma")
.pragma("key", "new_password")
.connect()
.await?;
let result = conn
.transaction(|tx| {
Box::pin(async move { query("SELECT * FROM COMPANY;").fetch_all(&mut **tx).await })
})
.await?;
assert!(result.len() > 0);
Ok(())
}
|