From e1070a8b98b31c57e72ea6748d644f030c7aadef Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 22 Apr 2017 16:51:10 +0100 Subject: [PATCH] ecaps2: Add a parser too. --- src/ecaps2.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/ecaps2.rs b/src/ecaps2.rs index fce5e6ea..fabd2e51 100644 --- a/src/ecaps2.rs +++ b/src/ecaps2.rs @@ -4,12 +4,40 @@ extern crate blake2; use disco::{Feature, Identity, Disco}; use data_forms::DataForm; +use hashes::{Hash, parse_hash}; + +use minidom::Element; +use error::Error; +use ns; use self::sha2::{Sha256, Sha512, Digest}; use self::sha3::{Sha3_256, Sha3_512}; use self::blake2::Blake2b; use base64; +#[derive(Debug, Clone)] +pub struct ECaps2 { + hashes: Vec, +} + +pub fn parse_ecaps2(root: &Element) -> Result { + if !root.is("c", ns::ECAPS2) { + return Err(Error::ParseError("This is not an ecaps2 element.")); + } + let mut hashes = vec!(); + for child in root.children() { + if child.is("hash", ns::HASHES) { + let hash = parse_hash(child)?; + hashes.push(hash); + } else { + return Err(Error::ParseError("Unknown child in ecaps2 element.")); + } + } + Ok(ECaps2 { + hashes: hashes, + }) +} + fn compute_item(field: &str) -> Vec { let mut bytes = field.as_bytes().to_vec(); bytes.push(0x1f); @@ -120,9 +148,32 @@ pub fn hash_ecaps2(data: &[u8], algo: &str) -> String { #[cfg(test)] mod tests { use minidom::Element; + use error::Error; use disco; use ecaps2; + #[test] + fn test_parse() { + let elem: Element = "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=".parse().unwrap(); + let ecaps2 = ecaps2::parse_ecaps2(&elem).unwrap(); + assert_eq!(ecaps2.hashes.len(), 2); + assert_eq!(ecaps2.hashes[0].algo, "sha-256"); + assert_eq!(ecaps2.hashes[0].hash, "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4="); + assert_eq!(ecaps2.hashes[1].algo, "sha3-256"); + assert_eq!(ecaps2.hashes[1].hash, "+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8="); + } + + #[test] + fn test_invalid_child() { + let elem: Element = "K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=+sDTQqBmX6iG/X3zjt06fjZMBBqL/723knFIyRf0sg8=".parse().unwrap(); + let error = ecaps2::parse_ecaps2(&elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "Unknown child in ecaps2 element."); + } + #[test] fn test_simple() { let elem: Element = "".parse().unwrap();