use xso::{text::Base64, FromXml, IntoXml};
use crate::date::DateTime;
use crate::ns;
use crate::pubsub::PubSubPayload;
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::OX, name = "data")]
pub struct PubKeyData {
#[xml(text = Base64)]
pub data: Vec<u8>,
}
generate_element!(
PubKey, "pubkey", OX,
attributes: [
date: Option<DateTime> = "date"
],
children: [
data: Required<PubKeyData> = ("data", OX) => PubKeyData
]
);
impl PubSubPayload for PubKey {}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::OX, name = "pubkey-metadata")]
pub struct PubKeyMeta {
#[xml(attribute = "v4-fingerprint")]
pub v4fingerprint: String,
#[xml(attribute = "date")]
pub date: DateTime,
}
generate_element!(
PubKeysMeta, "public-key-list", OX,
children: [
pubkeys: Vec<PubKeyMeta> = ("pubkey-metadata", OX) => PubKeyMeta
]
);
impl PubSubPayload for PubKeysMeta {}
#[cfg(test)]
mod tests {
use super::*;
use crate::ns;
use crate::pubsub::{
pubsub::{Item as PubSubItem, Publish},
Item, NodeName,
};
use crate::Element;
use std::str::FromStr;
#[test]
fn pubsub_publish_pubkey_data() {
let pubkey = PubKey {
date: None,
data: PubKeyData {
data: (&"Foo").as_bytes().to_vec(),
},
};
println!("Foo1: {:?}", pubkey);
let pubsub = Publish {
node: NodeName(format!("{}:{}", ns::OX_PUBKEYS, "some-fingerprint")),
items: vec![PubSubItem(Item::new(None, None, Some(pubkey)))],
};
println!("Foo2: {:?}", pubsub);
}
#[test]
fn pubsub_publish_pubkey_meta() {
let pubkeymeta = PubKeysMeta {
pubkeys: vec![PubKeyMeta {
v4fingerprint: "some-fingerprint".to_owned(),
date: DateTime::from_str("2019-03-30T18:30:25Z").unwrap(),
}],
};
println!("Foo1: {:?}", pubkeymeta);
let pubsub = Publish {
node: NodeName("foo".to_owned()),
items: vec![PubSubItem(Item::new(None, None, Some(pubkeymeta)))],
};
println!("Foo2: {:?}", pubsub);
}
#[test]
fn test_serialize_pubkey() {
let reference: Element = "<pubkey xmlns='urn:xmpp:openpgp:0'><data>AAAA</data></pubkey>"
.parse()
.unwrap();
let pubkey = PubKey {
date: None,
data: PubKeyData {
data: b"\0\0\0".to_vec(),
},
};
let serialized: Element = pubkey.into();
assert_eq!(serialized, reference);
}
}