xmpp_parsers/
starttls.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2024 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/.

use xso::{AsXml, FromXml};

use crate::ns;

/// Request to start TLS.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "starttls")]
pub struct Request;

/// Information that TLS may now commence.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "proceed")]
pub struct Proceed;

/// Stream feature for StartTLS
///
/// Used in [`crate::stream_features::StreamFeatures`].
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "starttls")]
pub struct StartTls {
    /// Marker for mandatory StartTLS.
    // TODO: replace with `#[xml(flag)]` once we have it
    #[xml(child(default))]
    pub required: Option<RequiredStartTls>,
}

/// Marker for mandatory StartTLS.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::TLS, name = "required")]
pub struct RequiredStartTls;

/// Enum which allows parsing/serialising any STARTTLS element.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml()]
pub enum Nonza {
    /// Request to start TLS
    #[xml(transparent)]
    Request(Request),

    /// Information that TLS may now commence
    #[xml(transparent)]
    Proceed(Proceed),
}

#[cfg(test)]
mod tests {
    use super::*;
    use minidom::Element;

    #[test]
    fn test_size() {
        assert_size!(Request, 0);
        assert_size!(Proceed, 0);
        assert_size!(StartTls, 1);
        assert_size!(RequiredStartTls, 0);
        assert_size!(Nonza, 1);
    }

    #[test]
    fn test_parsers() {
        let elem: Element = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
            .parse()
            .unwrap();
        let request = Request::try_from(elem.clone()).unwrap();
        let elem2 = Element::from(request);
        assert_eq!(elem, elem2);

        let elem: Element = "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
            .parse()
            .unwrap();
        let proceed = Proceed::try_from(elem.clone()).unwrap();
        let elem2 = Element::from(proceed);
        assert_eq!(elem, elem2);

        let elem: Element = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
            .parse()
            .unwrap();
        let starttls = StartTls::try_from(elem.clone()).unwrap();
        assert_eq!(starttls.required, None);
        let elem2 = Element::from(starttls);
        assert_eq!(elem, elem2);

        let elem: Element =
            "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls>"
                .parse()
                .unwrap();
        let starttls = StartTls::try_from(elem.clone()).unwrap();
        assert_eq!(starttls.required, Some(RequiredStartTls));
        let elem2 = Element::from(starttls);
        assert_eq!(elem, elem2);

        let elem: Element = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
            .parse()
            .unwrap();
        let nonza = Nonza::try_from(elem.clone()).unwrap();
        assert_eq!(nonza, Nonza::Request(Request));
        let elem2 = Element::from(nonza);
        assert_eq!(elem, elem2);

        let elem: Element = "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
            .parse()
            .unwrap();
        let nonza = Nonza::try_from(elem.clone()).unwrap();
        assert_eq!(nonza, Nonza::Proceed(Proceed));
        let elem2 = Element::from(nonza);
        assert_eq!(elem, elem2);
    }
}