blocking.rs

  1// Copyright (c) 2017 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::util::error::Error;
  8use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
  9use crate::ns;
 10use jid::Jid;
 11use minidom::Element;
 12use std::convert::TryFrom;
 13
 14generate_empty_element!(
 15    /// The element requesting the blocklist, the result iq will contain a
 16    /// [BlocklistResult].
 17    BlocklistRequest,
 18    "blocklist",
 19    BLOCKING
 20);
 21
 22impl IqGetPayload for BlocklistRequest {}
 23
 24macro_rules! generate_blocking_element {
 25    ($(#[$meta:meta])* $elem:ident, $name:tt) => (
 26        $(#[$meta])*
 27        #[derive(Debug, Clone)]
 28        pub struct $elem {
 29            /// List of JIDs affected by this command.
 30            pub items: Vec<Jid>,
 31        }
 32
 33        impl TryFrom<Element> for $elem {
 34            type Error = Error;
 35
 36            fn try_from(elem: Element) -> Result<$elem, Error> {
 37                check_self!(elem, $name, BLOCKING);
 38                check_no_attributes!(elem, $name);
 39                let mut items = vec!();
 40                for child in elem.children() {
 41                    check_self!(child, "item", BLOCKING);
 42                    check_no_unknown_attributes!(child, "item", ["jid"]);
 43                    check_no_children!(child, "item");
 44                    items.push(get_attr!(child, "jid", Required));
 45                }
 46                Ok($elem { items })
 47            }
 48        }
 49
 50        impl From<$elem> for Element {
 51            fn from(elem: $elem) -> Element {
 52                Element::builder($name)
 53                        .ns(ns::BLOCKING)
 54                        .append_all(elem.items.into_iter().map(|jid| {
 55                             Element::builder("item")
 56                                     .ns(ns::BLOCKING)
 57                                     .attr("jid", jid)
 58                                     .build()
 59                         }))
 60                        .build()
 61            }
 62        }
 63    );
 64}
 65
 66generate_blocking_element!(
 67    /// The element containing the current blocklist, as a reply from
 68    /// [BlocklistRequest].
 69    BlocklistResult,
 70    "blocklist"
 71);
 72
 73impl IqResultPayload for BlocklistResult {}
 74
 75// TODO: Prevent zero elements from being allowed.
 76generate_blocking_element!(
 77    /// A query to block one or more JIDs.
 78    Block,
 79    "block"
 80);
 81
 82impl IqSetPayload for Block {}
 83
 84generate_blocking_element!(
 85    /// A query to unblock one or more JIDs, or all of them.
 86    ///
 87    /// Warning: not putting any JID there means clearing out the blocklist.
 88    Unblock,
 89    "unblock"
 90);
 91
 92impl IqSetPayload for Unblock {}
 93
 94generate_empty_element!(
 95    /// The application-specific error condition when a message is blocked.
 96    Blocked,
 97    "blocked",
 98    BLOCKING_ERRORS
 99);
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use jid::BareJid;
105
106    #[cfg(target_pointer_width = "32")]
107    #[test]
108    fn test_size() {
109        assert_size!(BlocklistRequest, 0);
110        assert_size!(BlocklistResult, 12);
111        assert_size!(Block, 12);
112        assert_size!(Unblock, 12);
113    }
114
115    #[cfg(target_pointer_width = "64")]
116    #[test]
117    fn test_size() {
118        assert_size!(BlocklistRequest, 0);
119        assert_size!(BlocklistResult, 24);
120        assert_size!(Block, 24);
121        assert_size!(Unblock, 24);
122    }
123
124    #[test]
125    fn test_simple() {
126        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'/>".parse().unwrap();
127        let request_elem = elem.clone();
128        BlocklistRequest::try_from(request_elem).unwrap();
129
130        let result_elem = elem.clone();
131        let result = BlocklistResult::try_from(result_elem).unwrap();
132        assert_eq!(result.items, vec!());
133
134        let elem: Element = "<block xmlns='urn:xmpp:blocking'/>".parse().unwrap();
135        let block = Block::try_from(elem).unwrap();
136        assert_eq!(block.items, vec!());
137
138        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'/>".parse().unwrap();
139        let unblock = Unblock::try_from(elem).unwrap();
140        assert_eq!(unblock.items, vec!());
141    }
142
143    #[test]
144    fn test_items() {
145        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
146        let two_items = vec![
147            Jid::Bare(BareJid {
148                node: Some(String::from("coucou")),
149                domain: String::from("coucou"),
150            }),
151            Jid::Bare(BareJid {
152                node: None,
153                domain: String::from("domain"),
154            }),
155        ];
156
157        let result_elem = elem.clone();
158        let result = BlocklistResult::try_from(result_elem).unwrap();
159        assert_eq!(result.items, two_items);
160
161        let elem: Element = "<block xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></block>".parse().unwrap();
162        let block = Block::try_from(elem).unwrap();
163        assert_eq!(block.items, two_items);
164
165        let elem: Element = "<unblock xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></unblock>".parse().unwrap();
166        let unblock = Unblock::try_from(elem).unwrap();
167        assert_eq!(unblock.items, two_items);
168    }
169
170    #[cfg(not(feature = "disable-validation"))]
171    #[test]
172    fn test_invalid() {
173        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"
174            .parse()
175            .unwrap();
176        let request_elem = elem.clone();
177        let error = BlocklistRequest::try_from(request_elem).unwrap_err();
178        let message = match error {
179            Error::ParseError(string) => string,
180            _ => panic!(),
181        };
182        assert_eq!(message, "Unknown attribute in blocklist element.");
183
184        let result_elem = elem.clone();
185        let error = BlocklistResult::try_from(result_elem).unwrap_err();
186        let message = match error {
187            Error::ParseError(string) => string,
188            _ => panic!(),
189        };
190        assert_eq!(message, "Unknown attribute in blocklist element.");
191
192        let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
193            .parse()
194            .unwrap();
195        let error = Block::try_from(elem).unwrap_err();
196        let message = match error {
197            Error::ParseError(string) => string,
198            _ => panic!(),
199        };
200        assert_eq!(message, "Unknown attribute in block element.");
201
202        let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
203            .parse()
204            .unwrap();
205        let error = Unblock::try_from(elem).unwrap_err();
206        let message = match error {
207            Error::ParseError(string) => string,
208            _ => panic!(),
209        };
210        assert_eq!(message, "Unknown attribute in unblock element.");
211    }
212
213    #[cfg(not(feature = "disable-validation"))]
214    #[test]
215    fn test_non_empty_blocklist_request() {
216        let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
217        let error = BlocklistRequest::try_from(elem).unwrap_err();
218        let message = match error {
219            Error::ParseError(string) => string,
220            _ => panic!(),
221        };
222        assert_eq!(message, "Unknown child in blocklist element.");
223    }
224}