2019-07-17 15:30:52 +00:00
|
|
|
// Copyright (c) 2019 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/.
|
|
|
|
|
2024-06-23 07:52:41 +00:00
|
|
|
use xso::{FromXml, IntoXml};
|
|
|
|
|
2019-07-17 15:30:52 +00:00
|
|
|
use crate::message::MessagePayload;
|
2024-06-23 07:52:41 +00:00
|
|
|
use crate::ns;
|
2019-07-17 15:30:52 +00:00
|
|
|
use crate::presence::PresencePayload;
|
|
|
|
|
2024-06-23 07:52:41 +00:00
|
|
|
/// Unique identifier given to a MUC participant.
|
|
|
|
///
|
|
|
|
/// It allows clients to identify a MUC participant across reconnects and
|
|
|
|
/// renames. It thus prevents impersonification of anonymous users.
|
|
|
|
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
|
|
|
|
#[xml(namespace = ns::OID, name = "occupant-id")]
|
|
|
|
pub struct OccupantId {
|
|
|
|
/// The id associated to the sending user by the MUC service.
|
|
|
|
#[xml(attribute)]
|
|
|
|
pub id: String,
|
|
|
|
}
|
2019-07-17 15:30:52 +00:00
|
|
|
|
|
|
|
impl MessagePayload for OccupantId {}
|
|
|
|
impl PresencePayload for OccupantId {}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-09-25 08:28:44 +00:00
|
|
|
use crate::Element;
|
2024-06-21 14:27:43 +00:00
|
|
|
use xso::error::{Error, FromElementError};
|
2019-07-17 15:30:52 +00:00
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(OccupantId, 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(OccupantId, 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let origin_id = OccupantId::try_from(elem).unwrap();
|
|
|
|
assert_eq!(origin_id.id, "coucou");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_child() {
|
2024-06-23 07:52:41 +00:00
|
|
|
let elem: Element =
|
|
|
|
"<occupant-id xmlns='urn:xmpp:occupant-id:0' id='foo'><coucou/></occupant-id>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-07-17 15:30:52 +00:00
|
|
|
let error = OccupantId::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-07-17 15:30:52 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-06-23 07:52:41 +00:00
|
|
|
assert_eq!(message, "Unknown child in OccupantId element.");
|
2019-07-17 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_id() {
|
2019-10-22 23:32:41 +00:00
|
|
|
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-07-17 15:30:52 +00:00
|
|
|
let error = OccupantId::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-07-17 15:30:52 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2024-06-23 07:52:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
message,
|
|
|
|
"Required attribute field 'id' on OccupantId element missing."
|
|
|
|
);
|
2019-07-17 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let occupant_id = OccupantId {
|
|
|
|
id: String::from("coucou"),
|
|
|
|
};
|
|
|
|
let elem2 = occupant_id.into();
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
}
|