1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use helpers::PlainText;
8use ns;
9
10use sha1::Sha1;
11use digest::Digest;
12
13generate_element_with_text!(Handshake, "handshake", ns::COMPONENT,
14 data: PlainText<Option<String>>
15);
16
17impl Handshake {
18 pub fn new() -> Handshake {
19 Handshake {
20 data: None,
21 }
22 }
23
24 pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
25 let input = String::from(stream_id) + password;
26 let hash = Sha1::digest(input.as_bytes());
27 let content = format!("{:x}", hash);
28 Handshake {
29 data: Some(content),
30 }
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use try_from::TryFrom;
38 use minidom::Element;
39
40 #[test]
41 fn test_simple() {
42 let elem: Element = "<handshake xmlns='jabber:component:accept'/>".parse().unwrap();
43 let handshake = Handshake::try_from(elem).unwrap();
44 assert_eq!(handshake.data, None);
45
46 let elem: Element = "<handshake xmlns='jabber:component:accept'>Coucou</handshake>".parse().unwrap();
47 let handshake = Handshake::try_from(elem).unwrap();
48 assert_eq!(handshake.data, Some(String::from("Coucou")));
49 }
50
51 #[test]
52 fn test_constructors() {
53 let handshake = Handshake::new();
54 assert_eq!(handshake.data, None);
55
56 let handshake = Handshake::from_password_and_stream_id("123456", "sid");
57 assert_eq!(handshake.data, Some(String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e")));
58 }
59}