1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::{num::NonZeroU8, str::FromStr};
use ruma_identifiers_validation::{key_algorithms::DeviceKeyAlgorithm, Error};
use crate::DeviceId;
#[derive(Clone, Debug)]
pub struct DeviceKeyId {
full_id: Box<str>,
colon_idx: NonZeroU8,
}
impl DeviceKeyId {
pub fn algorithm(&self) -> DeviceKeyAlgorithm {
DeviceKeyAlgorithm::from_str(&self.full_id[..self.colon_idx.get() as usize]).unwrap()
}
pub fn device_id(&self) -> &DeviceId {
(&self.full_id[self.colon_idx.get() as usize + 1..]).into()
}
}
fn try_from<S>(key_id: S) -> Result<DeviceKeyId, Error>
where
S: AsRef<str> + Into<Box<str>>,
{
let colon_idx = ruma_identifiers_validation::device_key_id::validate(key_id.as_ref())?;
Ok(DeviceKeyId { full_id: key_id.into(), colon_idx })
}
common_impls!(DeviceKeyId, try_from, "Device key ID with algorithm and device ID");
#[cfg(test)]
mod test {
use std::convert::TryFrom;
use ruma_identifiers_validation::{key_algorithms::DeviceKeyAlgorithm, Error};
#[cfg(feature = "serde")]
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::DeviceKeyId;
#[test]
fn convert_device_key_id() {
assert_eq!(
DeviceKeyId::try_from("ed25519:JLAFKJWSCS")
.expect("Failed to create device key ID.")
.as_ref(),
"ed25519:JLAFKJWSCS"
);
}
#[cfg(feature = "serde")]
#[test]
fn serialize_device_key_id() {
let device_key_id = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap();
let serialized = to_json_value(device_key_id).unwrap();
let expected = json!("ed25519:JLAFKJWSCS");
assert_eq!(serialized, expected);
}
#[cfg(feature = "serde")]
#[test]
fn deserialize_device_key_id() {
let deserialized: DeviceKeyId = from_json_value(json!("ed25519:JLAFKJWSCS")).unwrap();
let expected = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap();
assert_eq!(deserialized, expected);
}
#[test]
fn missing_key_algorithm() {
assert_eq!(DeviceKeyId::try_from(":JLAFKJWSCS").unwrap_err(), Error::UnknownKeyAlgorithm);
}
#[test]
fn missing_delimiter() {
assert_eq!(
DeviceKeyId::try_from("ed25519|JLAFKJWSCS").unwrap_err(),
Error::MissingDeviceKeyDelimiter,
);
}
#[test]
fn unknown_key_algorithm() {
assert_eq!(
DeviceKeyId::try_from("signed_curve25510:JLAFKJWSCS").unwrap_err(),
Error::UnknownKeyAlgorithm,
);
}
#[test]
fn empty_device_id_ok() {
assert!(DeviceKeyId::try_from("ed25519:").is_ok());
}
#[test]
fn valid_key_algorithm() {
let device_key_id = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap();
assert_eq!(device_key_id.algorithm(), DeviceKeyAlgorithm::Ed25519);
}
#[test]
fn valid_device_id() {
let device_key_id = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap();
assert_eq!(device_key_id.device_id(), "JLAFKJWSCS");
}
}