hashes.rs

  1// Copyright (c) 2017 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 try_from::TryFrom;
  8use std::str::FromStr;
  9
 10use minidom::{Element, IntoAttributeValue};
 11
 12use error::Error;
 13
 14use ns;
 15use helpers::Base64;
 16
 17#[allow(non_camel_case_types)]
 18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 19pub enum Algo {
 20    Sha_1,
 21    Sha_256,
 22    Sha_512,
 23    Sha3_256,
 24    Sha3_512,
 25    Blake2b_256,
 26    Blake2b_512,
 27    Unknown(String),
 28}
 29
 30impl FromStr for Algo {
 31    type Err = Error;
 32
 33    fn from_str(s: &str) -> Result<Algo, Error> {
 34        Ok(match s {
 35            "" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
 36
 37            "sha-1" => Algo::Sha_1,
 38            "sha-256" => Algo::Sha_256,
 39            "sha-512" => Algo::Sha_512,
 40            "sha3-256" => Algo::Sha3_256,
 41            "sha3-512" => Algo::Sha3_512,
 42            "blake2b-256" => Algo::Blake2b_256,
 43            "blake2b-512" => Algo::Blake2b_512,
 44            value => Algo::Unknown(value.to_owned()),
 45        })
 46    }
 47}
 48
 49impl From<Algo> for String {
 50    fn from(algo: Algo) -> String {
 51        String::from(match algo {
 52            Algo::Sha_1 => "sha-1",
 53            Algo::Sha_256 => "sha-256",
 54            Algo::Sha_512 => "sha-512",
 55            Algo::Sha3_256 => "sha3-256",
 56            Algo::Sha3_512 => "sha3-512",
 57            Algo::Blake2b_256 => "blake2b-256",
 58            Algo::Blake2b_512 => "blake2b-512",
 59            Algo::Unknown(text) => return text,
 60        })
 61    }
 62}
 63
 64impl IntoAttributeValue for Algo {
 65    fn into_attribute_value(self) -> Option<String> {
 66        Some(String::from(self))
 67    }
 68}
 69
 70generate_element_with_text!(
 71    #[derive(PartialEq)]
 72    Hash, "hash", ns::HASHES,
 73    [
 74        algo: Algo = "algo" => required
 75    ],
 76    hash: Base64<Vec<u8>>
 77);
 78
 79#[cfg(test)]
 80mod tests {
 81    use super::*;
 82    use base64;
 83
 84    #[test]
 85    fn test_simple() {
 86        let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
 87        let hash = Hash::try_from(elem).unwrap();
 88        assert_eq!(hash.algo, Algo::Sha_256);
 89        assert_eq!(hash.hash, base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap());
 90    }
 91
 92    #[test]
 93    fn test_unknown() {
 94        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
 95        let error = Hash::try_from(elem).unwrap_err();
 96        let message = match error {
 97            Error::ParseError(string) => string,
 98            _ => panic!(),
 99        };
100        assert_eq!(message, "This is not a hash element.");
101    }
102
103    #[test]
104    fn test_invalid_child() {
105        let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
106        let error = Hash::try_from(elem).unwrap_err();
107        let message = match error {
108            Error::ParseError(string) => string,
109            _ => panic!(),
110        };
111        assert_eq!(message, "Unknown child in hash element.");
112    }
113}