2023-02-27 02:58:45 +00:00
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::expect_used)]
|
|
|
|
|
|
|
|
use crate::netmask;
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::time::{Duration, SystemTime};
|
|
|
|
use ipnet::Ipv4Net;
|
|
|
|
use crate::cert::{deserialize_nebula_certificate, NebulaCertificate, NebulaCertificateDetails};
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn certificate_serialization() {
|
|
|
|
let before = SystemTime::now() - Duration::from_secs(60);
|
|
|
|
let after = SystemTime::now() + Duration::from_secs(60);
|
|
|
|
let pub_key = b"1234567890abcedfghij1234567890ab";
|
|
|
|
|
|
|
|
let cert = NebulaCertificate {
|
|
|
|
details: NebulaCertificateDetails {
|
|
|
|
name: "testing".to_string(),
|
|
|
|
ips: vec![
|
|
|
|
netmask!("10.1.1.1", "255.255.255.0"),
|
|
|
|
netmask!("10.1.1.2", "255.255.0.0"),
|
|
|
|
netmask!("10.1.1.3", "255.0.255.0")
|
|
|
|
],
|
|
|
|
subnets: vec![
|
|
|
|
netmask!("9.1.1.1", "255.0.255.0"),
|
|
|
|
netmask!("9.1.1.2", "255.255.255.0"),
|
|
|
|
netmask!("9.1.1.3", "255.255.0.0")
|
|
|
|
],
|
|
|
|
groups: vec!["test-group1".to_string(), "test-group2".to_string(), "test-group3".to_string()],
|
|
|
|
not_before: before,
|
|
|
|
not_after: after,
|
|
|
|
public_key: *pub_key,
|
|
|
|
is_ca: false,
|
|
|
|
issuer: "1234567890abcedfghij1234567890ab".to_string(),
|
|
|
|
},
|
2023-02-27 02:59:46 +00:00
|
|
|
signature: b"1234567890abcedfghij1234567890ab".to_vec(),
|
2023-02-27 02:58:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let bytes = cert.serialize().unwrap();
|
|
|
|
|
|
|
|
let deserialized = deserialize_nebula_certificate(&bytes).unwrap();
|
|
|
|
/*
|
|
|
|
assert.Equal(t, nc.Details.Name, nc2.Details.Name)
|
|
|
|
assert.Equal(t, nc.Details.NotBefore, nc2.Details.NotBefore)
|
|
|
|
assert.Equal(t, nc.Details.NotAfter, nc2.Details.NotAfter)
|
|
|
|
assert.Equal(t, nc.Details.PublicKey, nc2.Details.PublicKey)
|
|
|
|
assert.Equal(t, nc.Details.IsCA, nc2.Details.IsCA)
|
|
|
|
*/
|
|
|
|
assert_eq!(cert.signature, deserialized.signature);
|
|
|
|
assert_eq!(cert.details.name, deserialized.details.name);
|
|
|
|
assert_eq!(cert.details.not_before, deserialized.details.not_after);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! netmask {
|
|
|
|
($ip:expr,$mask:expr) => {
|
|
|
|
Ipv4Net::with_netmask(Ipv4Addr::from_str($ip).unwrap(), Ipv4Addr::from_str($mask).unwrap()).unwrap()
|
|
|
|
};
|
|
|
|
}
|