2018-02-20 17:43:19 +01:00
|
|
|
|
// Copyright (c) 2018 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-08-04 21:06:45 +02:00
|
|
|
|
use xso::{AsXml, FromXml};
|
|
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
use crate::iq::{IqResultPayload, IqSetPayload};
|
2018-12-18 15:27:30 +01:00
|
|
|
|
use crate::ns;
|
2019-10-23 01:32:41 +02:00
|
|
|
|
use jid::{FullJid, Jid};
|
2018-02-20 17:43:19 +01:00
|
|
|
|
|
2024-08-06 16:00:52 +02:00
|
|
|
|
/// The bind feature exposed in stream:features.
|
|
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
|
|
#[xml(namespace = ns::BIND, name = "bind")]
|
|
|
|
|
|
pub struct BindFeature {
|
|
|
|
|
|
/// Present if bind is required.
|
|
|
|
|
|
#[xml(child(default))]
|
|
|
|
|
|
required: Option<Required>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Notes that bind is required.
|
|
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
|
|
#[xml(namespace = ns::BIND, name = "required")]
|
|
|
|
|
|
pub struct Required;
|
|
|
|
|
|
|
2018-08-08 19:44:55 +02:00
|
|
|
|
/// The request for resource binding, which is the process by which a client
|
|
|
|
|
|
/// can obtain a full JID and start exchanging on the XMPP network.
|
|
|
|
|
|
///
|
2022-12-27 16:31:58 +01:00
|
|
|
|
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
2024-08-04 21:06:45 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
|
|
#[xml(namespace = ns::BIND, name = "bind")]
|
2019-07-31 13:52:08 +02:00
|
|
|
|
pub struct BindQuery {
|
2018-08-08 19:44:55 +02:00
|
|
|
|
/// Requests this resource, the server may associate another one though.
|
2019-07-30 21:25:27 +02:00
|
|
|
|
///
|
|
|
|
|
|
/// If this is None, we request no particular resource, and a random one
|
|
|
|
|
|
/// will be affected by the server.
|
2024-08-04 21:06:45 +02:00
|
|
|
|
#[xml(extract(default, fields(text(type_ = String))))]
|
2019-07-30 21:25:27 +02:00
|
|
|
|
resource: Option<String>,
|
2018-02-20 17:43:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-31 13:52:08 +02:00
|
|
|
|
impl BindQuery {
|
2018-08-08 19:44:55 +02:00
|
|
|
|
/// Creates a resource binding request.
|
2019-07-31 13:52:08 +02:00
|
|
|
|
pub fn new(resource: Option<String>) -> BindQuery {
|
|
|
|
|
|
BindQuery { resource }
|
2018-02-20 17:43:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-31 13:52:08 +02:00
|
|
|
|
impl IqSetPayload for BindQuery {}
|
2018-05-16 15:08:17 +02:00
|
|
|
|
|
2019-07-30 21:25:27 +02:00
|
|
|
|
/// The response for resource binding, containing the client’s full JID.
|
|
|
|
|
|
///
|
2022-12-27 16:31:58 +01:00
|
|
|
|
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
2024-08-04 21:06:45 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
|
|
|
|
|
#[xml(namespace = ns::BIND, name = "bind")]
|
2019-07-30 21:25:27 +02:00
|
|
|
|
pub struct BindResponse {
|
|
|
|
|
|
/// The full JID returned by the server for this client.
|
2024-08-04 21:06:45 +02:00
|
|
|
|
#[xml(extract(fields(text(type_ = FullJid))))]
|
2019-07-30 21:25:27 +02:00
|
|
|
|
jid: FullJid,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl IqResultPayload for BindResponse {}
|
|
|
|
|
|
|
2019-09-06 11:45:04 +02:00
|
|
|
|
impl From<BindResponse> for FullJid {
|
|
|
|
|
|
fn from(bind: BindResponse) -> FullJid {
|
|
|
|
|
|
bind.jid
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<BindResponse> for Jid {
|
|
|
|
|
|
fn from(bind: BindResponse) -> Jid {
|
2024-04-15 17:03:57 +02:00
|
|
|
|
Jid::from(bind.jid)
|
2019-09-06 11:45:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-20 17:43:19 +01:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
2024-08-04 21:06:45 +02:00
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
use xso::error::{Error, FromElementError};
|
2018-02-20 17:43:19 +01:00
|
|
|
|
|
2018-10-28 13:10:48 +01:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
2024-08-06 16:00:52 +02:00
|
|
|
|
assert_size!(BindFeature, 1);
|
|
|
|
|
|
assert_size!(Required, 0);
|
2019-07-31 13:52:08 +02:00
|
|
|
|
assert_size!(BindQuery, 12);
|
2023-06-20 14:14:15 +02:00
|
|
|
|
assert_size!(BindResponse, 16);
|
2018-10-28 13:10:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 14:26:16 +02:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
2024-08-06 16:00:52 +02:00
|
|
|
|
assert_size!(BindFeature, 1);
|
|
|
|
|
|
assert_size!(Required, 0);
|
2019-07-31 13:52:08 +02:00
|
|
|
|
assert_size!(BindQuery, 24);
|
2023-06-20 14:08:58 +02:00
|
|
|
|
assert_size!(BindResponse, 32);
|
2018-10-26 14:26:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-20 17:43:19 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_simple() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2019-07-31 13:52:08 +02:00
|
|
|
|
let bind = BindQuery::try_from(elem).unwrap();
|
2019-07-30 21:25:27 +02:00
|
|
|
|
assert_eq!(bind.resource, None);
|
|
|
|
|
|
|
2019-10-23 01:32:41 +02:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Hello™</resource></bind>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2019-07-31 13:52:08 +02:00
|
|
|
|
let bind = BindQuery::try_from(elem).unwrap();
|
2019-07-30 21:25:27 +02:00
|
|
|
|
// FIXME: “™” should be resourceprep’d into “TM” here…
|
|
|
|
|
|
//assert_eq!(bind.resource.unwrap(), "HelloTM");
|
|
|
|
|
|
assert_eq!(bind.resource.unwrap(), "Hello™");
|
|
|
|
|
|
|
2023-06-20 14:07:50 +02:00
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/Hello™</jid></bind>"
|
2019-07-30 21:25:27 +02:00
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
let bind = BindResponse::try_from(elem).unwrap();
|
2023-06-20 14:07:50 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
bind.jid,
|
|
|
|
|
|
FullJid::new("coucou@linkmauve.fr/HelloTM").unwrap()
|
|
|
|
|
|
);
|
2018-02-20 17:43:19 +01:00
|
|
|
|
}
|
2018-12-18 16:00:25 +01:00
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2018-12-18 16:00:25 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_resource() {
|
|
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2019-07-31 13:52:08 +02:00
|
|
|
|
let error = BindQuery::try_from(elem).unwrap_err();
|
2018-12-18 16:00:25 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2018-12-18 16:00:25 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-04 21:06:45 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Unknown attribute in extraction for field 'resource' in BindQuery element."
|
|
|
|
|
|
);
|
2018-12-18 16:00:25 +01:00
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2019-07-31 13:52:08 +02:00
|
|
|
|
let error = BindQuery::try_from(elem).unwrap_err();
|
2018-12-18 16:00:25 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2018-12-18 16:00:25 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-04 21:06:45 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Unknown child in extraction for field 'resource' in BindQuery element."
|
|
|
|
|
|
);
|
2018-12-18 16:00:25 +01:00
|
|
|
|
}
|
2018-02-20 17:43:19 +01:00
|
|
|
|
}
|