use crate::date::DateTime;
use crate::pubsub::PubSubPayload;
use crate::util::text_node_codecs::{Base64, Codec};
generate_element!(
PubKeyData, "data", OX,
text: (
data: Base64
)
);
generate_element!(
PubKey, "pubkey", OX,
attributes: [
date: Option<DateTime> = "date"
],
children: [
data: Required<PubKeyData> = ("data", OX) => PubKeyData
]
);
impl PubSubPayload for PubKey {}
generate_element!(
PubKeyMeta, "pubkey-metadata", OX,
attributes: [
v4fingerprint: Required<String> = "v4-fingerprint",
date: Required<DateTime> = "date",
]
);
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);
}
}