Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

Commit

Permalink
deps/kms: add tests for Sample KMS
Browse files Browse the repository at this point in the history
Also, use rstest dep v0.17.0 for all subcrates under this repo

Signed-off-by: Xynnn007 <[email protected]>
  • Loading branch information
Xynnn007 committed May 11, 2023
1 parent 8717e7b commit ef5ab4d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kbs-types = { git = "https://github.com/mkulke/kbs-types", branch = "mkulke/add-
lazy_static = "1.4.0"
log = "0.4.14"
resource_uri = { path = "deps/resource_uri" }
rstest = "0.17.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = { version = "0.24.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion coco_keyprovider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ shadow-rs = "0.5.25"
tonic-build = "0.5"

[dev-dependencies]
rstest = "0.17.0"
rstest.workspace = true

[features]
2 changes: 1 addition & 1 deletion deps/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rand = { version = "0.8.5" }
sha2 = { version = "0.10" }

[dev-dependencies]
rstest = "0.17.0"
rstest.workspace = true

[features]
default = ["rust-crypto"]
Expand Down
1 change: 1 addition & 0 deletions deps/kms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ uuid = { version = "1.3.0", features = ["fast-rng", "v4"], optional = true }
zeroize = { version = "1.6.0", optional = true }

[dev-dependencies]
rstest.workspace = true
tokio = { version = "1.0", features = ["rt", "macros" ] }

[features]
Expand Down
37 changes: 37 additions & 0 deletions deps/kms/src/plugins/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,40 @@ impl Default for SampleKms {
}
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;

use crate::{plugins::sample::SampleKms, KMS};

#[rstest]
#[case(b"this is a test plaintext")]
#[case(b"this is a another test plaintext")]
#[tokio::test]
async fn key_lifetime(#[case] plaintext: &[u8]) {
let mut kms = SampleKms::default();
let keyid = kms.generate_key().await.expect("generate key");
let ciphertext = kms.encrypt(plaintext, &keyid).await.expect("encrypt");
let decrypted = kms.decrypt(&ciphertext, &keyid).await.expect("decrypt");
assert_eq!(decrypted, plaintext);
}

#[tokio::test]
async fn encrypt_with_an_non_existent_key() {
let mut kms = SampleKms::default();
let ciphertext = kms.encrypt(b"a test text", "an-non-existent-key-id").await;
assert!(ciphertext.is_err())
}

#[tokio::test]
async fn decrypt_with_an_non_existent_key() {
let mut kms = SampleKms::default();
let keyid = kms.generate_key().await.expect("generate key");
let ciphertext = kms.encrypt(b"a test text", &keyid).await.expect("encrypt");

// Use a fake key id to decrypt
let decrypted = kms.decrypt(&ciphertext, "an-non-existent-key-id").await;
assert!(decrypted.is_err())
}
}
2 changes: 1 addition & 1 deletion kbc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ zeroize.workspace = true

[dev-dependencies]
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
rstest = "0.16.0"
rstest.workspace = true

[build-dependencies]
tonic-build = { version = "0.8.0", optional = true }
Expand Down

0 comments on commit ef5ab4d

Please sign in to comment.