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 sha1::Sha1;
9use digest::Digest;
10
11generate_element_with_text!(Handshake, "handshake", COMPONENT,
12 data: PlainText<Option<String>>
13);
14
15impl Handshake {
16 pub fn new() -> Handshake {
17 Handshake {
18 data: None,
19 }
20 }
21
22 pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
23 let input = String::from(stream_id) + password;
24 let hash = Sha1::digest(input.as_bytes());
25 let content = format!("{:x}", hash);
26 Handshake {
27 data: Some(content),
28 }
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35 use try_from::TryFrom;
36 use minidom::Element;
37
38 #[test]
39 fn test_simple() {
40 let elem: Element = "<handshake xmlns='jabber:component:accept'/>".parse().unwrap();
41 let handshake = Handshake::try_from(elem).unwrap();
42 assert_eq!(handshake.data, None);
43
44 let elem: Element = "<handshake xmlns='jabber:component:accept'>Coucou</handshake>".parse().unwrap();
45 let handshake = Handshake::try_from(elem).unwrap();
46 assert_eq!(handshake.data, Some(String::from("Coucou")));
47 }
48
49 #[test]
50 fn test_constructors() {
51 let handshake = Handshake::new();
52 assert_eq!(handshake.data, None);
53
54 let handshake = Handshake::from_password_and_stream_id("123456", "sid");
55 assert_eq!(handshake.data, Some(String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e")));
56 }
57}