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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//! Out of band exchange messages.
//!
//! Support for the RFC8183 out-of-band setup requests and responses
//! used to exchange identity and configuration between CAs and their
//! parent CA and/or RPKI Publication Servers.
use std::convert::TryFrom;
use std::path::PathBuf;
use std::str::{from_utf8_unchecked, FromStr};
use std::{fmt, io};

use base64::DecodeError;
use bcder::decode;
use bytes::Bytes;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use rpki::uri;
use rpki::x509;

use crate::commons::api::{Handle, PublisherHandle, RepoInfo};
use crate::commons::remote::id::IdCert;
use crate::commons::util::file;
use crate::commons::util::xml::{AttributesError, XmlReader, XmlReaderErr, XmlWriter};

pub const VERSION: &str = "1";
pub const NS: &str = "http://www.hactrn.net/uris/rpki/rpki-setup/";

//------------ ChildRequest --------------------------------------------------

/// Type representing a <child_request /> defined in section 5.2.1 of
/// RFC8183.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ChildRequest {
    /// The optional 'tag' identifier used like a session identifier
    tag: Option<String>,

    /// The handle the child wants to use for itself. This may not be honored
    /// by the parent.
    child_handle: Handle,

    /// The self-signed IdCert containing the child's public key.
    id_cert: IdCert,
}

/// # Data Access
///
impl ChildRequest {
    pub fn new(child_handle: Handle, id_cert: IdCert) -> Self {
        ChildRequest {
            tag: None,
            child_handle,
            id_cert,
        }
    }

    pub fn unwrap(self) -> (Option<String>, Handle, IdCert) {
        (self.tag, self.child_handle, self.id_cert)
    }

    pub fn tag(&self) -> Option<&String> {
        self.tag.as_ref()
    }
    pub fn child_handle(&self) -> &Handle {
        &self.child_handle
    }
    pub fn id_cert(&self) -> &IdCert {
        &self.id_cert
    }
}

/// # Validation
///
impl ChildRequest {
    /// Parses a <child_request /> message, and validates the
    /// embedded certificate. MUST be a validly signed TA cert.
    pub fn validate<R>(reader: R) -> Result<Self, Error>
    where
        R: io::Read,
    {
        Self::validate_at(reader, x509::Time::now())
    }

    /// Parses a <child_request /> message.
    fn validate_at<R>(reader: R, now: x509::Time) -> Result<Self, Error>
    where
        R: io::Read,
    {
        XmlReader::decode(reader, |r| {
            r.take_named_element("child_request", |mut a, r| {
                if a.take_req("version")? != VERSION {
                    return Err(Error::InvalidVersion);
                }

                let tag = a.take_opt("tag");
                let child_handle = Handle::from_str(&a.take_req("child_handle")?)
                    .map_err(|_| Error::InvalidHandle)?;

                if a.take_opt("valid_until").is_some() {
                    warn!(
                        "Found deprecated attribute 'valid_until' used by \
                         old rpkid implementations. Ignoring this, but other \
                         things may break."
                    )
                }

                a.exhausted()?;

                let bytes = r.take_named_element("child_bpki_ta", |a, r| {
                    a.exhausted()?;
                    r.take_bytes_std()
                })?;
                let id_cert = IdCert::decode(bytes)?;
                id_cert.validate_ta_at(now)?;

                Ok(ChildRequest {
                    child_handle,
                    tag,
                    id_cert,
                })
            })
        })
    }
}

/// # Encoding
///
impl ChildRequest {
    /// Encodes the <child_request/> to a Vec
    pub fn encode_vec(&self) -> Vec<u8> {
        XmlWriter::encode_vec(|w| {
            let mut a = vec![
                ("xmlns", NS),
                ("version", VERSION),
                ("child_handle", self.child_handle.as_ref()),
            ];

            if let Some(ref t) = self.tag {
                a.push(("tag", t.as_ref()));
            }

            w.put_element("child_request", Some(a.as_ref()), |w| {
                w.put_element("child_bpki_ta", None, |w| {
                    w.put_base64_std(&self.id_cert.to_bytes())
                })
            })
        })
    }

    /// Encode to XML string
    pub fn to_xml(&self) -> String {
        let s = self.encode_vec();
        let s = unsafe { from_utf8_unchecked(s.as_slice()) };
        s.to_string()
    }
}

impl fmt::Display for ChildRequest {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "handle '{}' id (key) '{}'",
            self.child_handle,
            self.id_cert.ski_hex()
        )
    }
}

//------------ ParentResponse ------------------------------------------------

/// Type representing a <parent_response /> defined in section 5.2.2 of
/// RFC8183.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ParentResponse {
    /// The optional 'tag' identifier used like a session identifier
    tag: Option<String>,

    /// The parent CA's IdCert
    id_cert: IdCert,

    /// The handle of the parent CA.
    parent_handle: Handle,

    /// The handle chosen for the child CA. Note that this may not be the
    /// same as the handle the CA asked for.
    child_handle: Handle,

    /// The URI where the CA needs to send its RFC6492 messages
    service_uri: ServiceUri,
}

/// # Construct and Data Access
///
impl ParentResponse {
    pub fn new(
        tag: Option<String>,
        id_cert: IdCert,
        parent_handle: Handle,
        child_handle: Handle,
        service_uri: ServiceUri,
    ) -> Self {
        ParentResponse {
            tag,
            id_cert,
            parent_handle,
            child_handle,
            service_uri,
        }
    }

    pub fn tag(&self) -> Option<&String> {
        self.tag.as_ref()
    }
    pub fn id_cert(&self) -> &IdCert {
        &self.id_cert
    }
    pub fn parent_handle(&self) -> &Handle {
        &self.parent_handle
    }
    pub fn child_handle(&self) -> &Handle {
        &self.child_handle
    }
    pub fn service_uri(&self) -> &ServiceUri {
        &self.service_uri
    }
}

/// # Validation
///
impl ParentResponse {
    pub fn validate<R>(reader: R) -> Result<Self, Error>
    where
        R: io::Read,
    {
        Self::validate_at(reader, x509::Time::now())
    }

    fn validate_at<R>(reader: R, now: x509::Time) -> Result<Self, Error>
    where
        R: io::Read,
    {
        XmlReader::decode(reader, |r| {
            r.take_named_element("parent_response", |mut a, r| {
                if a.take_req("version")? != VERSION {
                    return Err(Error::InvalidVersion);
                }

                let tag = a.take_opt("tag");
                let parent_handle = Handle::from_str(&a.take_req("parent_handle")?)
                    .map_err(|_| Error::InvalidHandle)?;

                let child_handle = Handle::from_str(&a.take_req("child_handle")?)
                    .map_err(|_| Error::InvalidHandle)?;
                let service_uri = ServiceUri::try_from(a.take_req("service_uri")?)?;

                if a.take_opt("valid_until").is_some() {
                    warn!(
                        "Found deprecated attribute 'valid_until' used by \
                         old rpkid implementations. Ignoring this, but other \
                         things may break."
                    )
                }

                a.exhausted()?;

                let bytes = r.take_named_element("parent_bpki_ta", |a, r| {
                    a.exhausted()?;
                    r.take_bytes_std()
                })?;
                let id_cert = IdCert::decode(bytes)?;
                id_cert.validate_ta_at(now)?;

                /// We don't do anything with <offer/> or <referral /> in
                /// responses given to us, and we do not include them in
                /// our own responses given to children.
                ///
                /// I.e. the child needs to set up their publication server
                /// exchange separately, and explicitly.
                fn ignore_offer_or_referral<R>(r: &mut XmlReader<R>) -> Result<(), Error>
                where
                    R: io::Read,
                {
                    r.take_opt_element(|tag, _a, r| {
                        match tag.name.as_str() {
                            "offer" => {
                                r.take_empty()?;
                                Ok(None)
                            }
                            "referral" => {
                                let chars = r.take_chars()?;
                                Ok(Some(chars)) // help return type inference.
                            }
                            _ => Err(Error::InvalidXml),
                        }
                    })?;
                    Ok(())
                }

                while r.next_start_name().is_some() {
                    ignore_offer_or_referral(r)?;
                }

                Ok(ParentResponse {
                    tag,
                    id_cert,
                    parent_handle,
                    child_handle,
                    service_uri,
                })
            })
        })
    }
}

/// # Encoding
///
impl ParentResponse {
    /// Encodes the <parent_response/> to a Vec
    pub fn encode_vec(&self) -> Vec<u8> {
        XmlWriter::encode_vec(|w| {
            let service_uri = self.service_uri.to_string();

            let mut a = vec![
                ("xmlns", NS),
                ("version", VERSION),
                ("service_uri", service_uri.as_ref()),
                ("child_handle", self.child_handle.as_ref()),
                ("parent_handle", self.parent_handle.as_ref()),
            ];

            if let Some(ref t) = self.tag {
                a.push(("tag", t.as_ref()));
            }

            w.put_element("parent_response", Some(a.as_ref()), |w| {
                w.put_element("parent_bpki_ta", None, |w| {
                    w.put_base64_std(&self.id_cert.to_bytes())
                })
            })
        })
    }
}

//------------ PublisherRequest ----------------------------------------------

/// Type representing a <publisher_request/>
///
/// This is the XML message with identity information that a CA sends to a
/// Publication Server.
///
/// For more info, see: https://tools.ietf.org/html/rfc8183#section-5.2.3
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PublisherRequest {
    /// The optional 'tag' identifier used like a session identifier
    tag: Option<String>,

    /// The name the publishing CA likes to call itself by
    publisher_handle: PublisherHandle,

    /// The self-signed IdCert containing the publisher's public key.
    id_cert: IdCert,
}

/// # Construct and Data Access
///
impl PublisherRequest {
    pub fn new(tag: Option<String>, publisher_handle: PublisherHandle, id_cert: IdCert) -> Self {
        PublisherRequest {
            tag,
            publisher_handle,
            id_cert,
        }
    }

    pub fn id_cert(&self) -> &IdCert {
        &self.id_cert
    }

    pub fn publisher_handle(&self) -> &Handle {
        &self.publisher_handle
    }

    pub fn unpack(self) -> (Option<String>, PublisherHandle, IdCert) {
        (self.tag, self.publisher_handle, self.id_cert)
    }
}

/// # Validation
///
impl PublisherRequest {
    pub fn validate<R>(reader: R) -> Result<Self, Error>
    where
        R: io::Read,
    {
        Self::validate_at(reader, x509::Time::now())
    }

    /// Parses a <publisher_request /> message.
    fn validate_at<R>(reader: R, now: x509::Time) -> Result<Self, Error>
    where
        R: io::Read,
    {
        XmlReader::decode(reader, |r| {
            r.take_named_element("publisher_request", |mut a, r| {
                if a.take_req("version")? != "1" {
                    return Err(Error::InvalidVersion);
                }

                let tag = a.take_opt("tag");
                let publisher_handle = a.take_req("publisher_handle")?;
                let publisher_handle =
                    Handle::from_str(&publisher_handle).map_err(|_| Error::InvalidHandle)?;
                a.exhausted()?;

                let bytes = r.take_named_element("publisher_bpki_ta", |a, r| {
                    a.exhausted()?;
                    r.take_bytes_std()
                })?;

                let id_cert = IdCert::decode(bytes)?;
                id_cert.validate_ta_at(now)?;

                Ok(PublisherRequest {
                    tag,
                    publisher_handle,
                    id_cert,
                })
            })
        })
    }
}

/// Encoding
///
impl PublisherRequest {
    /// Encodes a <publisher_request> to a Vec
    pub fn encode_vec(&self) -> Vec<u8> {
        XmlWriter::encode_vec(|w| {
            let mut a = vec![
                ("xmlns", NS),
                ("version", VERSION),
                ("publisher_handle", self.publisher_handle.as_ref()),
            ];

            if let Some(ref t) = self.tag {
                a.push(("tag", t.as_ref()));
            }

            w.put_element("publisher_request", Some(a.as_ref()), |w| {
                w.put_element("publisher_bpki_ta", None, |w| {
                    w.put_base64_std(&self.id_cert.to_bytes())
                })
            })
        })
    }

    /// Saves this as an XML file
    pub fn save(&self, full_path: &PathBuf) -> Result<(), io::Error> {
        let xml = self.encode_vec();
        file::save(&Bytes::from(xml), full_path)
    }
}

impl Into<IdCert> for PublisherRequest {
    fn into(self) -> IdCert {
        self.id_cert
    }
}

//------------ RepositoryResponse --------------------------------------------

/// Type representing a <repository_response/>
///
/// This is the response sent to a CA by the publication server. It contains
/// the details needed by the CA to send publication messages to the server.
///
/// See https://tools.ietf.org/html/rfc8183#section-5.2.4
#[derive(Clone, Debug, Deserialize, Eq, Serialize, PartialEq)]
pub struct RepositoryResponse {
    /// The optional 'tag' identifier used like a session identifier
    tag: Option<String>,

    /// The name the publication server decided to call the CA by.
    /// Note that this may not be the same as the handle the CA asked for.
    publisher_handle: Handle,

    /// The Publication Server Identity Certificate
    id_cert: IdCert,

    /// The URI where the CA needs to send its RFC8181 messages
    service_uri: ServiceUri,

    /// Contains the rsync base (sia_base) and rrdp notification uri
    repo_info: RepoInfo,
}

/// # Construct and Data Access
///
impl RepositoryResponse {
    /// Creates a new response.
    pub fn new(
        tag: Option<String>,
        publisher_handle: Handle,
        id_cert: IdCert,
        service_uri: ServiceUri,
        repo_info: RepoInfo,
    ) -> Self {
        RepositoryResponse {
            tag,
            publisher_handle,
            id_cert,
            service_uri,
            repo_info,
        }
    }

    pub fn tag(&self) -> &Option<String> {
        &self.tag
    }

    pub fn publisher_handle(&self) -> &Handle {
        &self.publisher_handle
    }

    pub fn id_cert(&self) -> &IdCert {
        &self.id_cert
    }

    pub fn service_uri(&self) -> &ServiceUri {
        &self.service_uri
    }

    pub fn repo_info(&self) -> &RepoInfo {
        &self.repo_info
    }
}

/// # Validation
///
impl RepositoryResponse {
    /// Parses a <repository_response /> message.
    pub fn validate<R>(reader: R) -> Result<Self, Error>
    where
        R: io::Read,
    {
        Self::validate_at(reader, x509::Time::now())
    }

    fn validate_at<R>(reader: R, now: x509::Time) -> Result<Self, Error>
    where
        R: io::Read,
    {
        XmlReader::decode(reader, |r| {
            r.take_named_element("repository_response", |mut a, r| {
                if a.take_req("version")? != VERSION {
                    return Err(Error::InvalidVersion);
                }

                let tag = a.take_opt("tag");
                let publisher_handle = a.take_req("publisher_handle")?;
                let publisher_handle =
                    Handle::from_str(&publisher_handle).map_err(|_| Error::InvalidHandle)?;

                let service_uri = ServiceUri::try_from(a.take_req("service_uri")?)?;
                let sia_base = uri::Rsync::from_string(a.take_req("sia_base")?)?;
                let rrdp_notification_uri =
                    uri::Https::from_string(a.take_req("rrdp_notification_uri")?)?;

                a.exhausted()?;

                let id_cert = r.take_named_element("repository_bpki_ta", |a, r| {
                    a.exhausted()?;
                    r.take_bytes_std()
                })?;

                let id_cert = IdCert::decode(id_cert)?;
                id_cert.validate_ta_at(now)?;

                let repo_info = RepoInfo::new(sia_base, rrdp_notification_uri);

                Ok(RepositoryResponse {
                    tag,
                    publisher_handle,
                    id_cert,
                    service_uri,
                    repo_info,
                })
            })
        })
    }
}

/// # Encoding
///
impl RepositoryResponse {
    /// Encodes the <repository_response/> to a Vec
    pub fn encode_vec(&self) -> Vec<u8> {
        XmlWriter::encode_vec(|w| {
            let service_uri = self.service_uri.to_string();
            let sia_base = self.repo_info.base_uri().to_string();
            let rrdp_notification_uri = self.repo_info.rpki_notify().to_string();

            let mut a = vec![
                ("xmlns", NS),
                ("version", VERSION),
                ("publisher_handle", self.publisher_handle.as_ref()),
                ("service_uri", service_uri.as_ref()),
                ("sia_base", sia_base.as_ref()),
                ("rrdp_notification_uri", rrdp_notification_uri.as_ref()),
            ];

            if let Some(ref t) = self.tag {
                a.push(("tag", t.as_ref()));
            }

            w.put_element("repository_response", Some(&a), |w| {
                w.put_element("repository_bpki_ta", None, |w| {
                    w.put_base64_std(&self.id_cert.to_bytes())
                })
            })
        })
    }

    /// Saves this as an XML file
    pub fn save(&self, full_path: &PathBuf) -> Result<(), io::Error> {
        let xml = self.encode_vec();
        file::save(&Bytes::from(xml), full_path)
    }
}

//------------ ServiceUri ----------------------------------------------------

/// The service URI where a child or publisher needs to send its
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ServiceUri {
    Https(uri::Https),
    Http(String),
}

impl TryFrom<String> for ServiceUri {
    type Error = Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if value.to_lowercase().starts_with("http://") {
            Ok(ServiceUri::Http(value))
        } else {
            Ok(ServiceUri::Https(uri::Https::from_str(&value)?))
        }
    }
}

impl fmt::Display for ServiceUri {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ServiceUri::Http(string) => string.fmt(f),
            ServiceUri::Https(https) => https.fmt(f),
        }
    }
}

impl Serialize for ServiceUri {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ServiceUri {
    fn deserialize<D>(deserializer: D) -> Result<ServiceUri, D::Error>
    where
        D: Deserializer<'de>,
    {
        let string = String::deserialize(deserializer)?;
        ServiceUri::try_from(string).map_err(de::Error::custom)
    }
}

//------------ Error ---------------------------------------------------------

#[derive(Debug, Display)]
pub enum Error {
    #[display(fmt = "Invalid XML")]
    InvalidXml,

    #[display(fmt = "Invalid version")]
    InvalidVersion,

    #[display(fmt = "Invalid XML file: {}", _0)]
    XmlReadError(XmlReaderErr),

    #[display(fmt = "Invalid XML file: {}", _0)]
    XmlAttributesError(AttributesError),

    #[display(fmt = "Invalid base64: {}", _0)]
    Base64Error(DecodeError),

    #[display(fmt = "Invalid handle in XML, MUST be [-_A-Za-z0-9/]{{1,255}}")]
    InvalidHandle,

    #[display(fmt = "Cannot parse identity certificate: {}", _0)]
    CannotParseIdCert(decode::Error),

    #[display(fmt = "Invalid identity certificate: {}", _0)]
    InvalidIdCert(x509::ValidationError),

    #[display(fmt = "{}", _0)]
    Uri(uri::Error),
}

impl From<XmlReaderErr> for Error {
    fn from(e: XmlReaderErr) -> Error {
        Error::XmlReadError(e)
    }
}

impl From<AttributesError> for Error {
    fn from(e: AttributesError) -> Error {
        Error::XmlAttributesError(e)
    }
}

impl From<DecodeError> for Error {
    fn from(e: DecodeError) -> Error {
        Error::Base64Error(e)
    }
}

impl From<decode::Error> for Error {
    fn from(e: decode::Error) -> Error {
        Error::CannotParseIdCert(e)
    }
}

impl From<x509::ValidationError> for Error {
    fn from(e: x509::ValidationError) -> Error {
        Error::InvalidIdCert(e)
    }
}

impl From<uri::Error> for Error {
    fn from(e: uri::Error) -> Self {
        Error::Uri(e)
    }
}

//------------ Tests ---------------------------------------------------------

#[cfg(test)]
mod tests {
    use rpki::x509::Time;

    use crate::commons::remote::id::tests::test_id_certificate;
    use crate::commons::util::test;

    use super::*;

    fn rpkid_time() -> Time {
        Time::utc(2012, 1, 1, 0, 0, 0)
    }

    fn example_rrdp_uri() -> uri::Https {
        test::https("https://rpki.example/rrdp/notify.xml")
    }

    fn example_sia_base() -> uri::Rsync {
        test::rsync("rsync://a.example/rpki/Alice/Bob-42/")
    }

    fn example_service_uri() -> ServiceUri {
        ServiceUri::Https(test::https("https://a.example/publication/Alice/Bob-42"))
    }

    #[test]
    fn validate_rpkid_publisher_request() {
        let xml = include_str!("../../../test-resources/oob/publisher_request.xml");
        let pr = PublisherRequest::validate_at(xml.as_bytes(), rpkid_time()).unwrap();
        assert_eq!(Handle::from_str_unsafe("Bob"), pr.publisher_handle);
        assert_eq!(Some("A0001".to_string()), pr.tag);
    }

    #[test]
    fn validate_rpkid_repository_response() {
        let xml = include_str!("../../../test-resources/oob/repository_response.xml");
        let rr = RepositoryResponse::validate_at(xml.as_bytes(), rpkid_time()).unwrap();
        assert_eq!(Some("A0001".to_string()), rr.tag);
        assert_eq!(Handle::from_str_unsafe("Alice/Bob-42"), rr.publisher_handle);
        assert_eq!(example_service_uri(), rr.service_uri);
        assert_eq!(example_rrdp_uri(), rr.repo_info().rpki_notify());
        assert_eq!(&example_sia_base(), rr.repo_info().base_uri());
    }

    #[test]
    fn publisher_request() {
        let cert = test_id_certificate();

        let pr = PublisherRequest {
            tag: Some("tag".to_string()),
            publisher_handle: Handle::from_str_unsafe("tim"),
            id_cert: cert,
        };

        let enc = pr.encode_vec();

        PublisherRequest::validate_at(enc.as_slice(), rpkid_time()).unwrap();
    }

    #[test]
    fn repository_response() {
        let cert = test_id_certificate();

        let repo_info = RepoInfo::new(example_sia_base(), example_rrdp_uri());

        let pr = RepositoryResponse {
            tag: Some("tag".to_string()),
            publisher_handle: Handle::from_str_unsafe("tim"),
            repo_info,
            service_uri: example_service_uri(),
            id_cert: cert,
        };

        let enc = pr.encode_vec();

        RepositoryResponse::validate_at(enc.as_slice(), rpkid_time()).unwrap();
    }

    #[test]
    fn child_request() {
        let xml = include_str!("../../../test-resources/remote/rpkid-child-id.xml");
        let req = ChildRequest::validate_at(xml.as_bytes(), rpkid_time()).unwrap();

        assert_eq!(&Handle::from_str_unsafe("Carol"), req.child_handle());
        assert_eq!(None, req.tag());

        let encoded = req.encode_vec();
        let decoded = ChildRequest::validate_at(encoded.as_slice(), rpkid_time()).unwrap();

        assert_eq!(req, decoded);
    }

    #[test]
    fn validate_rpkid_parent_response_referral() {
        let xml = include_str!("../../../test-resources/remote/rpkid-parent-response-referral.xml");
        let _res = ParentResponse::validate_at(xml.as_bytes(), rpkid_time()).unwrap();
    }

    #[test]
    fn parent_response() {
        let xml = include_str!("../../../test-resources/remote/rpkid-parent-response-offer.xml");
        let res = ParentResponse::validate_at(xml.as_bytes(), rpkid_time()).unwrap();

        let encoded = res.encode_vec();
        let decoded = ParentResponse::validate_at(encoded.as_slice(), rpkid_time()).unwrap();

        assert_eq!(res, decoded);
    }
}