()
| 242 | |
| 243 | #[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 244 | async fn test_sql_change_passphrase() -> Result<()> { |
| 245 | use tempfile::tempdir; |
| 246 | |
| 247 | // The context is used only for logging. |
| 248 | let t = TestContext::new().await; |
| 249 | |
| 250 | // Create a separate empty database for testing. |
| 251 | let dir = tempdir()?; |
| 252 | let dbfile = dir.path().join("testdb.sqlite"); |
| 253 | let sql = Sql::new(dbfile.clone()); |
| 254 | |
| 255 | sql.open(&t, "foo".to_string()) |
| 256 | .await |
| 257 | .context("failed to open the database first time")?; |
| 258 | sql.close().await; |
| 259 | |
| 260 | // Change the passphrase from "foo" to "bar". |
| 261 | let sql = Sql::new(dbfile.clone()); |
| 262 | sql.open(&t, "foo".to_string()) |
| 263 | .await |
| 264 | .context("failed to open the database second time")?; |
| 265 | sql.change_passphrase("bar".to_string()) |
| 266 | .await |
| 267 | .context("failed to change passphrase")?; |
| 268 | |
| 269 | // Test that at least two connections are still working. |
| 270 | // This ensures that not only the connection which changed the password is working, |
| 271 | // but other connections as well. |
| 272 | { |
| 273 | let lock = sql.pool.read().await; |
| 274 | let pool = lock.as_ref().unwrap(); |
| 275 | let query_only = true; |
| 276 | let conn1 = pool.get(query_only).await?; |
| 277 | let conn2 = pool.get(query_only).await?; |
| 278 | conn1 |
| 279 | .query_row("SELECT count(*) FROM sqlite_master", [], |_row| Ok(())) |
| 280 | .unwrap(); |
| 281 | conn2 |
| 282 | .query_row("SELECT count(*) FROM sqlite_master", [], |_row| Ok(())) |
| 283 | .unwrap(); |
| 284 | } |
| 285 | |
| 286 | sql.close().await; |
| 287 | |
| 288 | let sql = Sql::new(dbfile); |
| 289 | |
| 290 | // Test that old passphrase is not working. |
| 291 | assert!(sql.open(&t, "foo".to_string()).await.is_err()); |
| 292 | |
| 293 | // Open the database with the new passphrase. |
| 294 | sql.check_passphrase("bar".to_string()).await?; |
| 295 | sql.open(&t, "bar".to_string()) |
| 296 | .await |
| 297 | .context("failed to open the database third time")?; |
| 298 | sql.close().await; |
| 299 | |
| 300 | Ok(()) |
| 301 | } |
nothing calls this directly
no test coverage detected