use rpki::data_model::roa::{RoaDecodeError, RoaObject, RoaProfileError};
use rpki::data_model::signed_object::RpkiSignedObject;

#[test]
fn decode_roa_fixture_smoke() {
    let der = std::fs::read("tests/fixtures/repository/rpki.cernet.net/repo/cernet/0/AS4538.roa")
        .expect("read ROA fixture");
    let roa = RoaObject::decode_der(&der).expect("decode roa");
    roa.validate_profile().expect("validate ROA profile");
    assert_eq!(
        roa.econtent_type,
        rpki::data_model::oid::OID_CT_ROUTE_ORIGIN_AUTHZ
    );
    assert_eq!(roa.roa.version, 0);
    assert_eq!(roa.roa.as_id, 4538);
    assert!(!roa.roa.ip_addr_blocks.is_empty());
    assert!(
        roa.roa
            .ip_addr_blocks
            .iter()
            .all(|f| !f.addresses.is_empty())
    );
    println!("{roa:#?}");
}

#[test]
fn from_signed_object_accepts_roa_fixture() {
    let der = std::fs::read("tests/fixtures/repository/rpki.cernet.net/repo/cernet/0/AS4538.roa")
        .expect("read ROA fixture");
    let so = RpkiSignedObject::decode_der(&der).expect("decode signed object");
    let roa = RoaObject::from_signed_object(so).expect("from_signed_object");
    roa.validate_profile().expect("validate ROA profile");
}

#[test]
fn decode_rejects_non_roa_econtent_type() {
    let der = std::fs::read(
        "tests/fixtures/repository/rpki.cernet.net/repo/cernet/0/05FC9C5B88506F7C0D3F862C8895BED67E9F8EBA.mft",
    )
    .expect("read MFT fixture");
    let err = RoaObject::decode_der(&der).unwrap_err();
    assert!(matches!(
        err,
        RoaDecodeError::Validate(RoaProfileError::InvalidEContentType(_))
    ));
}
