use dope_quic::hp::HpKey;
use dope_quic::qkdf::{InitialSecrets, PacketKeys};
use shin::hash::HashAlg;
use shin::kdf::Hkdf;

const DCID: [u8; 8] = [0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08];

const CLIENT_SECRET: [u8; 32] = [
    0xc0, 0x0c, 0xf1, 0x51, 0xca, 0x5b, 0xe0, 0x75, 0xed, 0x0e, 0xbf, 0xb5, 0xc8, 0x03, 0x23, 0xc4,
    0x2d, 0x6b, 0x7d, 0xb6, 0x78, 0x81, 0x28, 0x9a, 0xf4, 0x00, 0x8f, 0x1f, 0x6c, 0x35, 0x7a, 0xea,
];

const INITIAL_SECRET: [u8; 32] = [
    0x7d, 0xb5, 0xdf, 0x06, 0xe7, 0xa6, 0x9e, 0x43, 0x24, 0x96, 0xad, 0xed, 0xb0, 0x08, 0x51, 0x92,
    0x35, 0x95, 0x22, 0x15, 0x96, 0xae, 0x2a, 0xe9, 0xfb, 0x81, 0x15, 0xc1, 0xe9, 0xed, 0x0a, 0x44,
];

const SERVER_SECRET: [u8; 32] = [
    0x3c, 0x19, 0x98, 0x28, 0xfd, 0x13, 0x9e, 0xfd, 0x21, 0x6c, 0x15, 0x5a, 0xd8, 0x44, 0xcc, 0x81,
    0xfb, 0x82, 0xfa, 0x8d, 0x74, 0x46, 0xfa, 0x7d, 0x78, 0xbe, 0x80, 0x3a, 0xcd, 0xda, 0x95, 0x1b,
];

const CLIENT_KEY: [u8; 16] = [
    0x1f, 0x36, 0x96, 0x13, 0xdd, 0x76, 0xd5, 0x46, 0x77, 0x30, 0xef, 0xcb, 0xe3, 0xb1, 0xa2, 0x2d,
];
const CLIENT_IV: [u8; 12] = [
    0xfa, 0x04, 0x4b, 0x2f, 0x42, 0xa3, 0xfd, 0x3b, 0x46, 0xfb, 0x25, 0x5c,
];
const CLIENT_HP: [u8; 16] = [
    0x9f, 0x50, 0x44, 0x9e, 0x04, 0xa0, 0xe8, 0x10, 0x28, 0x3a, 0x1e, 0x99, 0x33, 0xad, 0xed, 0xd2,
];

const SERVER_KEY: [u8; 16] = [
    0xcf, 0x3a, 0x53, 0x31, 0x65, 0x3c, 0x36, 0x4c, 0x88, 0xf0, 0xf3, 0x79, 0xb6, 0x06, 0x7e, 0x37,
];
const SERVER_IV: [u8; 12] = [
    0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3e,
];
const SERVER_HP: [u8; 16] = [
    0xc2, 0x06, 0xb8, 0xd9, 0xb9, 0xf0, 0xf3, 0x76, 0x44, 0x43, 0x0b, 0x49, 0x0e, 0xea, 0xa3, 0x14,
];

#[test]
fn initial_secrets_match_rfc9001() {
    let init = Hkdf::extract(HashAlg::Sha256, &dope_quic::qkdf::INITIAL_SALT_V1, &DCID);
    assert_eq!(init.as_slice(), &INITIAL_SECRET[..]);
    let s = InitialSecrets::from_dcid(&DCID);
    assert_eq!(s.client, CLIENT_SECRET);
    assert_eq!(s.server, SERVER_SECRET);
}

#[test]
fn rfc5869_a1_hkdf_extract_test_vector() {
    let ikm = [0x0bu8; 22];
    let salt: [u8; 13] = [
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
    ];
    let expected: [u8; 32] = [
        0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba,
        0x63, 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2,
        0xb3, 0xe5,
    ];
    let prk = Hkdf::extract(HashAlg::Sha256, &salt, &ikm);
    eprintln!("computed = {:02x?}", prk.as_slice());
    eprintln!("expected = {:02x?}", expected);
    assert_eq!(prk.as_slice(), &expected[..]);
}

#[test]
fn client_packet_keys_match_rfc9001() {
    let pk = PacketKeys::aes_128(&CLIENT_SECRET);
    assert_eq!(pk.key, CLIENT_KEY);
    assert_eq!(pk.iv, CLIENT_IV);
    assert_eq!(pk.hp, CLIENT_HP);
}

#[test]
fn server_packet_keys_match_rfc9001() {
    let pk = PacketKeys::aes_128(&SERVER_SECRET);
    assert_eq!(pk.key, SERVER_KEY);
    assert_eq!(pk.iv, SERVER_IV);
    assert_eq!(pk.hp, SERVER_HP);
}

#[test]
fn hp_mask_for_client_initial_sample() {
    const SAMPLE: [u8; 16] = [
        0xd1, 0xb1, 0xc9, 0x8d, 0xd7, 0x68, 0x9f, 0xb8, 0xec, 0x11, 0xd2, 0x42, 0xb1, 0x23, 0xdc,
        0x9b,
    ];
    const MASK: [u8; 5] = [0x43, 0x7b, 0x9a, 0xec, 0x36];

    let hp = HpKey::aes_128(&CLIENT_HP);
    assert_eq!(hp.mask(&SAMPLE).unwrap(), MASK);
}
