Skip to content

Commit b2cd5b0

Browse files
committed
Add local encryption (with attach) tests
1 parent c27e6e1 commit b2cd5b0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ jobs:
130130

131131
- name: Run tests
132132
run: cargo nextest run
133+
134+
- name: Run libsql encryption tests
135+
run: cargo test --features encryption --color=always --test encryption test_encryption
133136
# test-custom-pager:
134137
# runs-on: ubuntu-latest
135138
# name: Run Tests

libsql/tests/encryption.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use libsql::{params, Builder};
2+
use libsql_sys::{Cipher, EncryptionConfig};
3+
4+
#[tokio::test]
5+
#[cfg(feature = "encryption")]
6+
async fn test_encryption() {
7+
let tempdir = std::env::temp_dir();
8+
let encrypted_path = tempdir.join("encrypted.db");
9+
let base_path = tempdir.join("base.db");
10+
11+
// lets create an encrypted database
12+
{
13+
let mut db_builder = Builder::new_local(&encrypted_path);
14+
db_builder = db_builder.encryption_config(EncryptionConfig {
15+
cipher: Cipher::Aes256Cbc,
16+
encryption_key: "s3cR3t".into(),
17+
});
18+
let db = db_builder.build().await.unwrap();
19+
20+
let conn = db.connect().unwrap();
21+
conn.execute("CREATE TABLE IF NOT EXISTS messages (text TEXT)", ())
22+
.await
23+
.unwrap();
24+
let params = params!["the only winning move is not to play"];
25+
conn.execute("INSERT INTO messages (text) VALUES (?)", params)
26+
.await
27+
.unwrap();
28+
}
29+
30+
// lets test encryption with ATTACH
31+
{
32+
let db = Builder::new_local(&base_path).build().await.unwrap();
33+
let conn = db.connect().unwrap();
34+
let attach_stmt = format!(
35+
"ATTACH DATABASE '{}' AS encrypted KEY 's3cR3t'",
36+
tempdir.join("encrypted.db").display()
37+
);
38+
conn.execute(&attach_stmt, ()).await.unwrap();
39+
let mut attached_results = conn
40+
.query("SELECT * FROM encrypted.messages", ())
41+
.await
42+
.unwrap();
43+
let row = attached_results.next().await.unwrap().unwrap();
44+
let text: String = row.get(0).unwrap();
45+
assert_eq!(text, "the only winning move is not to play");
46+
}
47+
48+
{
49+
let _ = std::fs::remove_file(&encrypted_path);
50+
let _ = std::fs::remove_file(&base_path);
51+
}
52+
}

0 commit comments

Comments
 (0)