diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index b018c1de..603e3b35 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -2,6 +2,13 @@ Bug fixes: +- The `KeyId::key_name` method now returns the key name. In 0.14.0, `key_name` + mistakenly returned the algorithm. + +# 0.14.0 + +Bug fixes: + - The `instance_id` field was removed from `ProtocolInstanceInit` and is now an `Option` for `ProtocolInstance`. It made the `unstable-unspecified` feature non-additive. diff --git a/crates/ruma-common/src/identifiers/key_id.rs b/crates/ruma-common/src/identifiers/key_id.rs index 06f82b74..6bcc08f9 100644 --- a/crates/ruma-common/src/identifiers/key_id.rs +++ b/crates/ruma-common/src/identifiers/key_id.rs @@ -43,7 +43,8 @@ impl KeyId { where &'a K: TryFrom<&'a str>, { - <&'a K>::try_from(&self.as_str()[..self.colon_idx()]).unwrap_or_else(|_| unreachable!()) + <&'a K>::try_from(&self.as_str()[(self.colon_idx() + 1)..]) + .unwrap_or_else(|_| unreachable!()) } fn colon_idx(&self) -> usize { @@ -151,3 +152,38 @@ impl KeyAlgorithm for SigningKeyAlgorithm {} impl KeyAlgorithm for DeviceKeyAlgorithm {} impl KeyAlgorithm for OneTimeKeyAlgorithm {} + +#[cfg(test)] +mod tests { + use assert_matches2::assert_matches; + use ruma_identifiers_validation::Error; + + use super::DeviceKeyId; + + #[test] + fn algorithm_and_key_name_are_correctly_extracted() { + let key_id = DeviceKeyId::parse("ed25519:MYDEVICE").expect("Should parse correctly"); + assert_eq!(key_id.algorithm().as_str(), "ed25519"); + assert_eq!(key_id.key_name(), "MYDEVICE"); + } + + #[test] + fn empty_key_name_is_correctly_extracted() { + let key_id = DeviceKeyId::parse("ed25519:").expect("Should parse correctly"); + assert_eq!(key_id.algorithm().as_str(), "ed25519"); + assert_eq!(key_id.key_name(), ""); + } + + #[test] + fn missing_colon_fails_to_parse() { + let error = DeviceKeyId::parse("ed25519_MYDEVICE").expect_err("Should fail to parse"); + assert_matches!(error, Error::MissingColon); + } + + #[test] + fn empty_algorithm_fails_to_parse() { + let error = DeviceKeyId::parse(":MYDEVICE").expect_err("Should fail to parse"); + // Weirdly, this also reports MissingColon + assert_matches!(error, Error::MissingColon); + } +}