compat mode that relaxes some of the check_* macros

This commit is contained in:
Astro 2018-12-30 00:36:29 +01:00
commit 95f4ade4ba
17 changed files with 70 additions and 31 deletions

View file

@ -28,3 +28,5 @@ try_from = "0.3.2"
[features]
# Build xmpp-parsers to make components instead of clients.
component = []
# Compatibility mode
compat = []

View file

@ -18,6 +18,7 @@ impl MessagePayload for Attention {}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "compat"))]
use crate::error::Error;
use minidom::Element;
use try_from::TryFrom;
@ -33,6 +34,7 @@ mod tests {
Attention::try_from(elem).unwrap();
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_child() {
let elem: Element = "<attention xmlns='urn:xmpp:attention:0'><coucou/></attention>"
@ -46,6 +48,7 @@ mod tests {
assert_eq!(message, "Unknown child in attention element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<attention xmlns='urn:xmpp:attention:0' coucou=''/>"

View file

@ -112,6 +112,7 @@ mod tests {
assert_eq!(bind, Bind::None);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_resource() {
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"

View file

@ -155,13 +155,16 @@ mod tests {
},
];
let request_elem = elem.clone();
let error = BlocklistRequest::try_from(request_elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in blocklist element.");
#[cfg(not(feature = "compat"))]
{
let request_elem = elem.clone();
let error = BlocklistRequest::try_from(request_elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in blocklist element.");
}
let result_elem = elem.clone();
let result = BlocklistResult::try_from(result_elem).unwrap();
@ -176,6 +179,7 @@ mod tests {
assert_eq!(unblock.items, two_items);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid() {
let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"

View file

@ -233,6 +233,7 @@ mod tests {
);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_child() {
let elem: Element = "<c xmlns='http://jabber.org/protocol/caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash></c>".parse().unwrap();

View file

@ -63,6 +63,7 @@ mod tests {
assert_eq!(message, "This is not a chatstate element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_child() {
let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>"
@ -76,6 +77,7 @@ mod tests {
assert_eq!(message, "Unknown child in chatstate element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>"

View file

@ -510,13 +510,16 @@ mod tests {
};
assert_eq!(message, "Unknown child in received element.");
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''/>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in received element.");
#[cfg(not(feature = "compat"))]
{
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''/>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in received element.");
}
let elem: Element =
"<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' creator='initiator'/>"
@ -575,13 +578,16 @@ mod tests {
};
assert_eq!(message, "This is not a file element.");
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
let error = Checksum::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in checksum element.");
#[cfg(not(feature = "compat"))]
{
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
let error = Checksum::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in checksum element.");
}
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' creator='initiator'><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
let error = Checksum::try_from(elem).unwrap_err();
@ -631,11 +637,14 @@ mod tests {
let elem: Element = "<range xmlns='urn:xmpp:jingle:apps:file-transfer:5' coucou=''/>"
.parse()
.unwrap();
let error = Range::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in range element.");
#[cfg(not(feature = "compat"))]
{
let error = Range::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in range element.");
}
}
}

View file

@ -248,6 +248,7 @@ macro_rules! check_ns_only {
macro_rules! check_no_children {
($elem:ident, $name:tt) => {
#[cfg(not(feature = "compat"))]
for _ in $elem.children() {
return Err(crate::error::Error::ParseError(concat!(
"Unknown child in ",
@ -260,6 +261,7 @@ macro_rules! check_no_children {
macro_rules! check_no_attributes {
($elem:ident, $name:tt) => {
#[cfg(not(feature = "compat"))]
for _ in $elem.attrs() {
return Err(crate::error::Error::ParseError(concat!(
"Unknown attribute in ",
@ -272,6 +274,7 @@ macro_rules! check_no_attributes {
macro_rules! check_no_unknown_attributes {
($elem:ident, $name:tt, [$($attr:tt),*]) => (
#[cfg(not(feature = "compat"))]
for (_attr, _) in $elem.attrs() {
$(
if _attr == $attr {

View file

@ -45,6 +45,7 @@ mod tests {
Replace::try_from(elem).unwrap();
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>"

View file

@ -142,6 +142,7 @@ mod tests {
assert_eq!(elem, elem2);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_muc_invalid_attribute() {
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc' coucou=''/>"

View file

@ -303,6 +303,7 @@ mod tests {
assert!(elem.compare_to(&elem2));
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "
@ -343,6 +344,7 @@ mod tests {
assert_eq!(message, "Required attribute 'code' missing.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_status_invalid_child() {
let elem: Element = "
@ -513,6 +515,7 @@ mod tests {
assert_eq!(reason.0, "Reason".to_owned());
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_reason_invalid_attribute() {
let elem: Element = "
@ -528,6 +531,7 @@ mod tests {
assert_eq!(message, "Unknown attribute in reason element.".to_owned());
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_reason_invalid() {
let elem: Element = "
@ -545,6 +549,7 @@ mod tests {
assert_eq!(message, "Unknown child in reason element.".to_owned());
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_item_invalid_attr() {
let elem: Element = "

View file

@ -14,6 +14,7 @@ generate_elem_id!(
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "compat"))]
use crate::error::Error;
use minidom::Element;
use try_from::TryFrom;
@ -48,6 +49,7 @@ mod tests {
assert_eq!(elem1, elem2);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid() {
let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick'><coucou/></nick>"
@ -61,6 +63,7 @@ mod tests {
assert_eq!(message, "Unknown child in nick element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick' coucou=''/>"

View file

@ -20,6 +20,7 @@ impl IqGetPayload for Ping {}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "compat"))]
use crate::error::Error;
use minidom::Element;
use try_from::TryFrom;
@ -42,6 +43,7 @@ mod tests {
assert_eq!(elem1, elem2);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid() {
let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>"
@ -55,6 +57,7 @@ mod tests {
assert_eq!(message, "Unknown child in ping element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();

View file

@ -570,6 +570,7 @@ mod tests {
assert!(payload.is("test", "invalid"));
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_status_child() {
#[cfg(not(feature = "component"))]
@ -589,6 +590,7 @@ mod tests {
assert_eq!(message, "Unknown child in status element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
#[cfg(not(feature = "component"))]

View file

@ -420,6 +420,7 @@ mod tests {
assert_eq!(message, "Unknown child in event element.");
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid_attribute() {
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event' coucou=''/>"

View file

@ -245,6 +245,7 @@ mod tests {
assert_eq!(roster.items[0].subscription, Subscription::Remove);
}
#[cfg(not(feature = "compat"))]
#[test]
fn test_invalid() {
let elem: Element = "<query xmlns='jabber:iq:roster'><coucou/></query>"

View file

@ -289,6 +289,7 @@ mod tests {
);
}
#[cfg(feature = "compat")]
#[test]
fn failure_with_non_prefixed_text_lang() {
let elem: Element = "<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
@ -299,9 +300,5 @@ mod tests {
.unwrap();
let failure = Failure::try_from(elem).unwrap();
assert_eq!(failure.defined_condition, DefinedCondition::NotAuthorized);
assert_eq!(
failure.texts["en"],
String::from("Invalid username or password")
);
}
}