From 6952f3adfc6575e71a6333d5f439aa51e2b47be0 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 24 May 2017 21:42:29 +0100 Subject: [PATCH] message_correct: Check for unwanted attributes. --- src/message_correct.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/message_correct.rs b/src/message_correct.rs index 35494f8..8ce747f 100644 --- a/src/message_correct.rs +++ b/src/message_correct.rs @@ -27,6 +27,11 @@ impl TryFrom for Replace { for _ in elem.children() { return Err(Error::ParseError("Unknown child in replace element.")); } + for (attr, _) in elem.attrs() { + if attr != "id" { + return Err(Error::ParseError("Unknown attribute in replace element.")); + } + } let id = get_attr!(elem, "id", required); Ok(Replace { id }) } @@ -36,7 +41,7 @@ impl Into for Replace { fn into(self) -> Element { Element::builder("replace") .ns(ns::MESSAGE_CORRECT) - .attr("id", self.id.clone()) + .attr("id", self.id) .build() } } @@ -51,6 +56,17 @@ mod tests { Replace::try_from(elem).unwrap(); } + #[test] + fn test_invalid_attribute() { + let elem: Element = "".parse().unwrap(); + let error = Replace::try_from(elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "Unknown attribute in replace element."); + } + #[test] fn test_invalid_child() { let elem: Element = "".parse().unwrap();