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