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
use crate::error as asn1err;
use crate::tag::Tag;
use crate::traits::Asn1Object;
pub static GENERALSTRING_TAG_NUMBER: u8 = 0x1b;
pub type GeneralString = String;
impl Asn1Object for String {
fn tag() -> Tag {
return Tag::new_primitive_universal(GENERALSTRING_TAG_NUMBER);
}
fn build_value(&self) -> Vec<u8> {
return self.as_bytes().to_vec();
}
fn parse_value(&mut self, raw: &[u8]) -> asn1err::Result<()> {
*self = String::from_utf8(raw.to_vec())?;
return Ok(());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build() {
assert_eq!(
vec![
0x1b, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31, 0x40, 0x72, 0x73,
0x61, 0x2e, 0x63, 0x6f, 0x6d
],
GeneralString::from("test1@rsa.com").build()
);
}
#[test]
fn test_parse() {
assert_eq!(
GeneralString::from("test1@rsa.com"),
String::parse(&[
0x1b, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31, 0x40, 0x72, 0x73,
0x61, 0x2e, 0x63, 0x6f, 0x6d
])
.unwrap()
.1
);
}
#[test]
fn test_parse_empty_value() {
assert_eq!(
GeneralString::from("".to_string()),
String::parse(&[0x1b, 0x00]).unwrap().1
);
}
#[test]
fn test_parse_with_excesive_bytes() {
let rest: &[u8] = &[0x22, 0x22, 0x22];
assert_eq!(
(rest, GeneralString::from("test1@rsa.com".to_string())),
String::parse(&[
0x1b, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31, 0x40, 0x72, 0x73,
0x61, 0x2e, 0x63, 0x6f, 0x6d, 0x22, 0x22, 0x22
])
.unwrap()
);
}
#[should_panic(expected = "Utf8Error")]
#[test]
fn test_parse_non_ascii_characters() {
String::parse(&[0x1b, 0x1, 0xff]).unwrap();
}
#[should_panic(expected = "UnmatchedTag")]
#[test]
fn test_parse_with_invalid_tag() {
String::parse(&[0x7, 0x1, 0x0]).unwrap();
}
}