Skip to content

Commit

Permalink
another golden test
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Jul 11, 2024
1 parent bb800b2 commit 5bb6ae0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/encrypted_sled/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ impl EncryptedDb {
{
let nonce = Self::generate_nonce();

self.encrypt_with_nonce(value, nonce)
}

/// create a new [EncryptedRecord] containing an encrypted value and a given nonce.
fn encrypt_with_nonce<V>(&self, value: V, nonce: chacha20poly1305::XNonce) -> EncryptedDbResult<EncryptedRecord>
where
V: Into<IVec>,
{
let mut value = value.into().to_vec();

// encrypt value
Expand Down Expand Up @@ -193,6 +201,8 @@ impl EncryptedDb {

#[cfg(test)]
mod tests {
use chacha20poly1305::{XChaCha20Poly1305, XNonce, KeyInit};

use super::EncryptedDb;
use crate::encrypted_sled::{password::PasswordSalt, Password};

Expand All @@ -205,4 +215,23 @@ mod tests {

goldie::assert_json!(key);
}

#[test]
fn encrypt_with_nonce_known_vector() {
// Create a mock EncryptedDb with a deterministic cipher
let mock_db = EncryptedDb {
kv: sled::Config::new().temporary(true).open().unwrap(),
cipher: XChaCha20Poly1305::new(&chacha20poly1305::Key::from([5u8; 32])),
};

let value = b"test_value";
let nonce = XNonce::from([1u8; 24]);

let encrypted_record = mock_db.encrypt_with_nonce(value, nonce).unwrap();

goldie::assert_json!(&encrypted_record);

let decrypted_value = mock_db.decrypt_record_value(encrypted_record).unwrap();
assert_eq!(decrypted_value.as_ref(), value);
}
}
56 changes: 56 additions & 0 deletions src/encrypted_sled/testdata/encrypt_with_nonce_known_vector.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"encrypted_value": [
98,
227,
247,
125,
18,
128,
210,
249,
235,
248,
0,
112,
6,
3,
156,
239,
40,
46,
208,
108,
246,
75,
59,
68,
48,
2
],
"nonce": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
}

0 comments on commit 5bb6ae0

Please sign in to comment.