Skip to content

Commit

Permalink
Separate KeyId mocking from IntegrationTestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Dec 31, 2023
1 parent a9e6039 commit 85fc60a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
23 changes: 18 additions & 5 deletions crates/rops/src/integration/age.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ mod key_id {
rops_file_builder.age_key_ids.push(self)
}
}

#[cfg(feature = "test-utils")]
mod mock {
use super::*;

impl MockDisplayTestUtil for age::x25519::Recipient {
fn mock_display() -> String {
"age1se5ghfycr4n8kcwc3qwf234ymvmr2lex2a99wh8gpfx97glwt9hqch4569".to_string()
}
}

impl MockTestUtil for age::x25519::Recipient {
fn mock() -> Self {
Self::mock_display().parse().unwrap()
}
}
}
}

pub use config::AgeConfig;
Expand Down Expand Up @@ -142,7 +159,7 @@ mod config {
impl MockTestUtil for AgeConfig {
fn mock() -> Self {
Self {
key_id: AgeIntegration::mock_key_id(),
key_id: MockTestUtil::mock(),
}
}
}
Expand All @@ -154,10 +171,6 @@ mod mock {
use super::*;

impl IntegrationTestUtils for AgeIntegration {
fn mock_key_id_str() -> impl AsRef<str> {
"age1se5ghfycr4n8kcwc3qwf234ymvmr2lex2a99wh8gpfx97glwt9hqch4569"
}

fn mock_private_key_str() -> impl AsRef<str> {
"AGE-SECRET-KEY-1EQUCGFZH8UZKSZ0Z5N5T234YRNDT4U9H7QNYXWRRNJYDDVXE6FWSCPGNJ7"
}
Expand Down
4 changes: 0 additions & 4 deletions crates/rops/src/integration/aws_kms/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ mod mock {
use super::*;

impl IntegrationTestUtils for AwsKmsIntegration {
fn mock_key_id_str() -> impl AsRef<str> {
AwsKeyId::mock_display()
}

fn mock_private_key_str() -> impl AsRef<str> {
AwsPrivateKey::mock_display()
}
Expand Down
16 changes: 10 additions & 6 deletions crates/rops/src/integration/test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ macro_rules! generate_integration_test_suite {

#[test]
fn parses_key_id() {
$integration::parse_key_id($integration::mock_key_id_str().as_ref()).unwrap();
$integration::parse_key_id(&<$integration as Integration>::KeyId::mock_display()).unwrap();
}

#[test]
fn encrypts_data_key() {
$integration::set_mock_private_key_env_var();

let expected_data_key = DataKey::mock();
let encrypted_data_key = $integration::encrypt_data_key(&$integration::mock_key_id(), &expected_data_key).unwrap();
let found_data_key = $integration::decrypt_data_key(&$integration::mock_key_id(), &encrypted_data_key)
let encrypted_data_key =
$integration::encrypt_data_key(&<$integration as Integration>::KeyId::mock(), &expected_data_key).unwrap();
let found_data_key = $integration::decrypt_data_key(&<$integration as Integration>::KeyId::mock(), &encrypted_data_key)
.unwrap()
.unwrap();

Expand All @@ -29,9 +30,12 @@ macro_rules! generate_integration_test_suite {

assert_eq!(
DataKey::mock(),
$integration::decrypt_data_key(&$integration::mock_key_id(), $integration::mock_encrypted_data_key_str())
.unwrap()
.unwrap()
$integration::decrypt_data_key(
&<$integration as Integration>::KeyId::mock(),
$integration::mock_encrypted_data_key_str()
)
.unwrap()
.unwrap()
);
}

Expand Down
6 changes: 0 additions & 6 deletions crates/rops/src/integration/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::*;

pub trait IntegrationTestUtils: Integration {
fn mock_key_id_str() -> impl AsRef<str>;

fn mock_private_key_str() -> impl AsRef<str>;

fn mock_encrypted_data_key_str() -> &'static str;
Expand All @@ -11,10 +9,6 @@ pub trait IntegrationTestUtils: Integration {
std::env::set_var(Self::private_key_env_var_name(), Self::mock_private_key_str().as_ref())
}

fn mock_key_id() -> Self::KeyId {
Self::parse_key_id(Self::mock_key_id_str().as_ref()).unwrap()
}

fn mock_private_key() -> Self::PrivateKey {
Self::parse_private_key(Self::mock_private_key_str()).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rops/src/rops_file/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
RopsFileBuilder::<YamlFileFormat>::new(RopsFileFormatMap::<DecryptedMap, YamlFileFormat>::mock().into_inner_map())
.with_partial_encryption(MockTestUtil::mock())
.mac_only_encrypted()
.add_integration_key::<AgeIntegration>(AgeIntegration::mock_key_id())
.add_integration_key::<AgeIntegration>(MockTestUtil::mock())
.encrypt::<AES256GCM, SHA512>()
.unwrap()
.decrypt::<YamlFileFormat>()
Expand Down
2 changes: 1 addition & 1 deletion crates/rops/src/rops_file/format/json/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod metadata {
{{
\"recipient\": \"{}\"
}}",
AgeIntegration::mock_key_id_str().as_ref()
<AgeIntegration as Integration>::KeyId::mock_display()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rops/src/rops_file/format/yaml/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod metadata {
#[cfg(feature = "age")]
impl MockFileFormatUtil<YamlFileFormat> for AgeConfig {
fn mock_format_display() -> String {
format!("recipient: {}", AgeIntegration::mock_key_id_str().as_ref())
format!("recipient: {}", <AgeIntegration as Integration>::KeyId::mock_display())
}
}

Expand Down

0 comments on commit 85fc60a

Please sign in to comment.