1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-07-01 10:58:44 +02:00
xmpp-rs/src/hashes.rs

87 lines
2.6 KiB
Rust
Raw Normal View History

2017-04-29 23:14:34 +02:00
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
2017-05-06 21:46:11 +02:00
use std::convert::TryFrom;
2017-04-21 05:21:16 +02:00
use minidom::Element;
use error::Error;
use ns;
2017-04-22 18:38:36 +02:00
#[derive(Debug, Clone, PartialEq)]
2017-04-21 05:21:16 +02:00
pub struct Hash {
pub algo: String,
pub hash: String,
}
2017-05-06 21:46:11 +02:00
impl<'a> TryFrom<&'a Element> for Hash {
type Error = Error;
fn try_from(elem: &'a Element) -> Result<Hash, Error> {
if !elem.is("hash", ns::HASHES) {
return Err(Error::ParseError("This is not a hash element."));
}
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in hash element."));
}
let algo = elem.attr("algo").ok_or(Error::ParseError("Mandatory argument 'algo' not present in hash element."))?.to_owned();
let hash = match elem.text().as_ref() {
"" => return Err(Error::ParseError("Hash element shouldnt be empty.")),
text => text.to_owned(),
};
Ok(Hash {
algo: algo,
hash: hash,
})
2017-04-21 05:21:16 +02:00
}
}
2017-05-06 21:46:11 +02:00
impl<'a> Into<Element> for &'a Hash {
fn into(self) -> Element {
Element::builder("hash")
.ns(ns::HASHES)
.attr("algo", self.algo.clone())
.append(self.hash.clone())
.build()
}
2017-04-23 19:36:12 +02:00
}
2017-04-21 05:21:16 +02:00
#[cfg(test)]
mod tests {
2017-05-06 21:46:11 +02:00
use super::*;
2017-04-21 05:21:16 +02:00
#[test]
fn test_simple() {
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
2017-05-06 21:46:11 +02:00
let hash = Hash::try_from(&elem).unwrap();
2017-04-21 05:21:16 +02:00
assert_eq!(hash.algo, "sha-256");
assert_eq!(hash.hash, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
}
#[test]
fn test_unknown() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
2017-05-06 21:46:11 +02:00
let error = Hash::try_from(&elem).unwrap_err();
2017-04-21 05:21:16 +02:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "This is not a hash element.");
}
#[test]
fn test_invalid_child() {
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
2017-05-06 21:46:11 +02:00
let error = Hash::try_from(&elem).unwrap_err();
2017-04-21 05:21:16 +02:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in hash element.");
}
}