summaryrefslogtreecommitdiff
path: root/src/key
diff options
context:
space:
mode:
authorStuart Stock <stuart@int08h.com>2018-10-21 15:04:56 -0500
committerStuart Stock <stuart@int08h.com>2018-10-21 15:04:56 -0500
commit44e6212e3480d2f3b15f30434abc892adcf3836f (patch)
treeddd3e4a80243acb008319c8c8b76bd37fc6516f9 /src/key
parentb22a6f055f4578b96224ff2372a0ba0c5942a00f (diff)
downloadroughenough-44e6212e3480d2f3b15f30434abc892adcf3836f.zip
Add tests for envelope cypto and some enums
Diffstat (limited to 'src/key')
-rw-r--r--src/key/mod.rs37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/key/mod.rs b/src/key/mod.rs
index 6bb3eb5..5ce0296 100644
--- a/src/key/mod.rs
+++ b/src/key/mod.rs
@@ -55,14 +55,45 @@ impl Display for KeyProtection {
}
impl FromStr for KeyProtection {
- type Err = ();
+ type Err = String;
- fn from_str(s: &str) -> Result<KeyProtection, ()> {
+ fn from_str(s: &str) -> Result<KeyProtection, String> {
match s {
"plaintext" => Ok(KeyProtection::Plaintext),
s if s.starts_with("arn:") => Ok(KeyProtection::AwsKmsEnvelope(s.to_string())),
s if s.starts_with("projects/") => Ok(KeyProtection::GoogleKmsEnvelope(s.to_string())),
- _ => Err(()),
+ s => Err(format!("unknown KeyProtection '{}'", s)),
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use key::KeyProtection;
+ use std::str::FromStr;
+
+ #[test]
+ fn convert_from_string() {
+ let arn =
+ "arn:aws:kms:some-aws-region:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab";
+ let resource_id =
+ "projects/key-project/locations/global/keyRings/key-ring/cryptoKeys/my-key";
+
+ match KeyProtection::from_str("plaintext") {
+ Ok(KeyProtection::Plaintext) => (),
+ e => panic!("unexpected result {:?}", e),
+ };
+ match KeyProtection::from_str(arn) {
+ Ok(KeyProtection::AwsKmsEnvelope(msg)) => assert_eq!(msg, arn),
+ e => panic!("unexpected result {:?}", e),
+ }
+ match KeyProtection::from_str(resource_id) {
+ Ok(KeyProtection::GoogleKmsEnvelope(msg)) => assert_eq!(msg, resource_id),
+ e => panic!("unexpected result {:?}", e),
+ }
+ match KeyProtection::from_str("frobble") {
+ Err(msg) => assert!(msg.contains("unknown KeyProtection")),
+ e => panic!("unexpected result {:?}", e),
}
}
}