use digest::Digest;
use sha1::Sha1;
use crate::core::{FromXml, IntoXml};
use crate::ns::COMPONENT;
#[derive(FromXml, IntoXml, Debug, Clone, PartialEq, Default)]
#[xml(namespace = COMPONENT, name = "handshake")]
pub struct Handshake {
#[xml(text)]
pub data: String,
}
impl Handshake {
pub fn new() -> Handshake {
Handshake::default()
}
pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
let input = String::from(stream_id) + password;
let hash = Sha1::digest(input.as_bytes());
let content = format!("{:x}", hash);
Handshake { data: content }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Handshake, 12);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Handshake, 24);
}
#[test]
fn test_simple() {
let elem: Element = "<handshake xmlns='jabber:component:accept'/>"
.parse()
.unwrap();
let handshake = Handshake::try_from(elem).unwrap();
assert_eq!(handshake.data, "");
let elem: Element = "<handshake xmlns='jabber:component:accept'>Coucou</handshake>"
.parse()
.unwrap();
let handshake = Handshake::try_from(elem).unwrap();
assert_eq!(handshake.data, String::from("Coucou"));
}
#[test]
fn test_constructors() {
let handshake = Handshake::new();
assert_eq!(handshake.data, "");
let handshake = Handshake::from_password_and_stream_id("123456", "sid");
assert_eq!(
handshake.data,
String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e")
);
}
}