use xso::{AsXml, FromXml};
use crate::ns;
#[derive(FromXml, AsXml, PartialEq, Debug, Default, Clone)]
#[xml(namespace = ns::STREAM, name = "features")]
pub struct StreamFeatures {
#[xml(child(default))]
pub starttls: Option<StartTls>,
#[xml(child(default))]
pub bind: Option<Bind>,
#[xml(child(default))]
pub sasl_mechanisms: SaslMechanisms,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "starttls")]
pub struct StartTls {
#[xml(child(default))]
pub required: Option<RequiredStartTls>,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "required")]
pub struct RequiredStartTls;
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::BIND, name = "bind")]
pub struct Bind;
generate_element!(
#[derive(Default)]
SaslMechanisms, "mechanisms", SASL,
children: [
mechanisms: Vec<SaslMechanism> = ("mechanism", SASL) => SaslMechanism,
]
);
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SASL, name = "mechanism")]
pub struct SaslMechanism {
#[xml(text)]
pub mechanism: String,
}
impl StreamFeatures {
pub fn can_starttls(&self) -> bool {
self.starttls.is_some()
}
pub fn can_bind(&self) -> bool {
self.bind.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(SaslMechanism, 12);
assert_size!(SaslMechanisms, 12);
assert_size!(Bind, 0);
assert_size!(RequiredStartTls, 0);
assert_size!(StartTls, 1);
assert_size!(StreamFeatures, 16);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(SaslMechanism, 24);
assert_size!(SaslMechanisms, 24);
assert_size!(Bind, 0);
assert_size!(RequiredStartTls, 0);
assert_size!(StartTls, 1);
assert_size!(StreamFeatures, 32);
}
#[test]
fn test_required_starttls() {
let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
<required/>
</starttls>
</stream:features>"
.parse()
.unwrap();
let features = StreamFeatures::try_from(elem).unwrap();
assert_eq!(features.can_bind(), false);
assert_eq!(features.sasl_mechanisms.mechanisms.len(), 0);
assert_eq!(features.can_starttls(), true);
assert_eq!(features.starttls.unwrap().required.is_some(), true);
}
#[test]
#[ignore]
fn test_deprecated_compression() {
let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
<compression xmlns='http://jabber.org/features/compress'>
<method>zlib</method>
<method>lzw</method>
</compression>
</stream:features>"
.parse()
.unwrap();
let features = StreamFeatures::try_from(elem).unwrap();
assert_eq!(features.can_bind(), true);
assert_eq!(features.sasl_mechanisms.mechanisms.len(), 0);
assert_eq!(features.can_starttls(), false);
}
#[test]
fn test_empty_features() {
let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>"
.parse()
.unwrap();
let features = StreamFeatures::try_from(elem).unwrap();
assert_eq!(features.can_bind(), false);
assert_eq!(features.sasl_mechanisms.mechanisms.len(), 0);
assert_eq!(features.can_starttls(), false);
}
}