xmpp_parsers/
component.rs1use xso::{text::FixedHex, AsXml, FromXml};
8
9use crate::ns;
10use digest::Digest;
11use sha1::Sha1;
12
13#[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
15#[xml(namespace = ns::COMPONENT, name = "handshake")]
16pub struct Handshake {
17 #[xml(text(codec = FixedHex<20>))]
24 pub data: Option<[u8; 20]>,
25}
26
27impl Handshake {
28 pub fn new() -> Handshake {
30 Handshake::default()
31 }
32
33 pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
35 let input = String::from(stream_id) + password;
36 let hash = Sha1::digest(input.as_bytes());
37 Handshake {
38 data: Some(hash.into()),
39 }
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46 use minidom::Element;
47
48 #[test]
49 fn test_size() {
50 assert_size!(Handshake, 21);
51 }
52
53 #[test]
54 fn test_simple() {
55 let elem: Element = "<handshake xmlns='jabber:component:accept'/>"
56 .parse()
57 .unwrap();
58 let handshake = Handshake::try_from(elem).unwrap();
59 assert_eq!(handshake.data, None);
60
61 let elem: Element = "<handshake xmlns='jabber:component:accept'>9accec263ab84a43c6037ccf7cd48cb1d3f6df8e</handshake>"
62 .parse()
63 .unwrap();
64 let handshake = Handshake::try_from(elem).unwrap();
65 assert_eq!(
66 handshake.data,
67 Some([
68 0x9a, 0xcc, 0xec, 0x26, 0x3a, 0xb8, 0x4a, 0x43, 0xc6, 0x03, 0x7c, 0xcf, 0x7c, 0xd4,
69 0x8c, 0xb1, 0xd3, 0xf6, 0xdf, 0x8e
70 ])
71 );
72 }
73
74 #[test]
75 fn test_constructors() {
76 let handshake = Handshake::new();
77 assert_eq!(handshake.data, None);
78
79 let handshake = Handshake::from_password_and_stream_id("123456", "sid");
80 assert_eq!(
81 handshake.data,
82 Some([
83 0x9a, 0xcc, 0xec, 0x26, 0x3a, 0xb8, 0x4a, 0x43, 0xc6, 0x03, 0x7c, 0xcf, 0x7c, 0xd4,
84 0x8c, 0xb1, 0xd3, 0xf6, 0xdf, 0x8e
85 ])
86 );
87 }
88}