From e03a5a89e78c30951dbfb584c8f2e54adc7a43c3 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 30 Apr 2017 22:45:39 +0100 Subject: [PATCH] presence: Prevent two statuses from having the same lang. --- src/presence.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/presence.rs b/src/presence.rs index 16c5f0f..fb26d94 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -165,7 +165,9 @@ pub fn parse_presence(root: &Element) -> Result { return Err(Error::ParseError("Unknown child in status element.")); } let lang = elem.attr("xml:lang").unwrap_or("").to_owned(); - statuses.insert(lang, elem.text()); + if let Some(_) = statuses.insert(lang, elem.text()) { + return Err(Error::ParseError("Status element present twice for the same xml:lang.")); + } } else if elem.is("priority", ns::JABBER_CLIENT) { if priority.is_some() { return Err(Error::ParseError("More than one priority element in a presence.")); @@ -339,6 +341,17 @@ mod tests { assert_eq!(presence.statuses["fr"], "Là!"); } + #[test] + fn test_invalid_multiple_statuses() { + let elem: Element = "Here!Là!".parse().unwrap(); + let error = presence::parse_presence(&elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "Status element present twice for the same xml:lang."); + } + #[test] fn test_priority() { let elem: Element = "-1".parse().unwrap();