1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use crate::message::MessagePayload;
8use crate::presence::PresencePayload;
9
10generate_element!(
11 /// Unique identifier given to a MUC participant.
12 ///
13 /// It allows clients to identify a MUC participant across reconnects and
14 /// renames. It thus prevents impersonification of anonymous users.
15 OccupantId, "occupant-id", OID,
16
17 attributes: [
18 /// The id associated to the sending user by the MUC service.
19 id: Required<String> = "id",
20 ]
21);
22
23impl MessagePayload for OccupantId {}
24impl PresencePayload for OccupantId {}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29 use crate::util::error::Error;
30 use crate::Element;
31 use std::convert::TryFrom;
32
33 #[cfg(target_pointer_width = "32")]
34 #[test]
35 fn test_size() {
36 assert_size!(OccupantId, 12);
37 }
38
39 #[cfg(target_pointer_width = "64")]
40 #[test]
41 fn test_size() {
42 assert_size!(OccupantId, 24);
43 }
44
45 #[test]
46 fn test_simple() {
47 let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
48 .parse()
49 .unwrap();
50 let origin_id = OccupantId::try_from(elem).unwrap();
51 assert_eq!(origin_id.id, "coucou");
52 }
53
54 #[test]
55 fn test_invalid_child() {
56 let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'><coucou/></occupant-id>"
57 .parse()
58 .unwrap();
59 let error = OccupantId::try_from(elem).unwrap_err();
60 let message = match error {
61 Error::ParseError(string) => string,
62 _ => panic!(),
63 };
64 assert_eq!(message, "Unknown child in occupant-id element.");
65 }
66
67 #[test]
68 fn test_invalid_id() {
69 let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'/>"
70 .parse()
71 .unwrap();
72 let error = OccupantId::try_from(elem).unwrap_err();
73 let message = match error {
74 Error::ParseError(string) => string,
75 _ => panic!(),
76 };
77 assert_eq!(message, "Required attribute 'id' missing.");
78 }
79
80 #[test]
81 fn test_serialise() {
82 let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
83 .parse()
84 .unwrap();
85 let occupant_id = OccupantId {
86 id: String::from("coucou"),
87 };
88 let elem2 = occupant_id.into();
89 assert_eq!(elem, elem2);
90 }
91}