use rpki::data_model::common::Asn1TimeEncoding;
use rpki::data_model::rc::{ResourceCertificate, ResourceCertificateProfileError};

#[test]
fn rc_validity_time_encoding_is_validated() {
    let der = std::fs::read(
        "tests/fixtures/repository/rpki.apnic.net/repository/B527EF581D6611E2BB468F7C72FD1FF2/BfycW4hQb3wNP4YsiJW-1n6fjro.cer",
    )
    .expect("read RC fixture");

    let mut parsed = ResourceCertificate::parse_der(&der).expect("parse RC fixture");
    parsed.validity_not_before.encoding = match parsed.validity_not_before.encoding {
        Asn1TimeEncoding::UtcTime => Asn1TimeEncoding::GeneralizedTime,
        Asn1TimeEncoding::GeneralizedTime => Asn1TimeEncoding::UtcTime,
    };

    match parsed.validate_profile() {
        Ok(_rc) => panic!("expected time-encoding validation error"),
        Err(ResourceCertificateProfileError::InvalidTimeEncoding(e)) => {
            assert_eq!(e.field, "notBefore");
        }
        Err(e) => panic!("unexpected error: {e}"),
    }
}
