2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::delay::Delay;
|
|
|
|
use crate::message::Message;
|
2017-04-29 02:50:49 +00:00
|
|
|
|
2018-07-31 22:51:38 +00:00
|
|
|
generate_element!(
|
|
|
|
/// Contains a forwarded stanza, either standalone or part of another
|
|
|
|
/// extension (such as carbons).
|
|
|
|
Forwarded, "forwarded", FORWARD,
|
|
|
|
children: [
|
|
|
|
/// When the stanza originally got sent.
|
|
|
|
delay: Option<Delay> = ("delay", DELAY) => Delay,
|
2017-04-29 02:50:49 +00:00
|
|
|
|
2018-07-31 22:51:38 +00:00
|
|
|
// XXX: really? Option?
|
|
|
|
/// The stanza being forwarded.
|
|
|
|
stanza: Option<Message> = ("message", DEFAULT_NS) => Message
|
2017-04-29 02:50:49 +00:00
|
|
|
|
2018-07-31 22:51:38 +00:00
|
|
|
// TODO: also handle the two other stanza possibilities.
|
|
|
|
]
|
|
|
|
);
|
2017-04-29 02:50:49 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 19:53:43 +00:00
|
|
|
use super::*;
|
2024-07-24 18:28:22 +00:00
|
|
|
use minidom::Element;
|
2024-06-21 14:27:43 +00:00
|
|
|
use xso::error::{Error, FromElementError};
|
2017-04-29 02:50:49 +00:00
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2024-04-15 15:03:57 +00:00
|
|
|
assert_size!(Forwarded, 140);
|
2018-10-28 12:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2024-04-15 15:03:57 +00:00
|
|
|
assert_size!(Forwarded, 264);
|
2018-10-26 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 02:50:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
Forwarded::try_from(elem).unwrap();
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_child() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Forwarded::try_from(elem).unwrap_err();
|
2017-04-29 02:50:49 +00:00
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-29 02:50:49 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown child in forwarded element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
let forwarded = Forwarded {
|
|
|
|
delay: None,
|
|
|
|
stanza: None,
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2 = forwarded.into();
|
2017-04-29 02:50:49 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
2019-11-28 17:00:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize_with_delay_and_stanza() {
|
2019-12-05 20:19:19 +00:00
|
|
|
let reference: Element = "<forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
|
|
|
|
2019-12-30 05:55:32 +00:00
|
|
|
let elem: Element =
|
|
|
|
"<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-12-05 20:19:19 +00:00
|
|
|
let delay = Delay::try_from(elem).unwrap();
|
|
|
|
|
|
|
|
let forwarded = Forwarded {
|
|
|
|
delay: Some(delay),
|
2019-12-30 05:55:32 +00:00
|
|
|
stanza: Some(message),
|
2019-12-05 20:19:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let serialized: Element = forwarded.into();
|
|
|
|
assert_eq!(serialized, reference);
|
2019-11-28 17:00:40 +00:00
|
|
|
}
|
2024-08-03 11:28:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_duplicate_delay() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25+00:00'/><delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let error = Forwarded::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
message,
|
|
|
|
"Element forwarded must not have more than one delay child."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_duplicate_message() {
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let error = Forwarded::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
message,
|
|
|
|
"Element forwarded must not have more than one message child."
|
|
|
|
);
|
|
|
|
}
|
2017-04-29 02:50:49 +00:00
|
|
|
}
|