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