use super::*;
use crate::hash::{sha1, sha256};

#[derive(Debug)]
struct TestVector {
    password: &'static str,
    salt: &'static str,
    iter: u32,
    output: &'static [u8],
}

// Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070
static SHA1_TEST_VECTORS: &[TestVector] = &[
    TestVector {
        password: "password",
        salt: "salt",
        iter: 1,
        output: &[
            0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60,
            0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6,
        ],
    },
    TestVector {
        password: "password",
        salt: "salt",
        iter: 2,
        output: &[
            0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d,
            0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57,
        ],
    },
    TestVector {
        password: "password",
        salt: "salt",
        iter: 4096,
        output: &[
            0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7,
            0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1,
        ],
    },
    // // This one takes too long
    // TestVector {
    //     password: "password",
    //     salt: "salt",
    //     iter: 16777216,
    //     output: &[
    //         0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
    //         0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
    //         0x26, 0x34, 0xe9, 0x84,
    //     ],
    // },
    TestVector {
        password: "passwordPASSWORDpassword",
        salt: "saltSALTsaltSALTsaltSALTsaltSALTsalt",
        iter: 4096,
        output: &[
            0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0,
            0xe4, 0x4a, 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, 0x38,
        ],
    },
    TestVector {
        password: "pass\0word",
        salt: "sa\0lt",
        iter: 4096,
        output: &[
            0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25,
            0xe0, 0xc3,
        ],
    },
];

// Test vectors from
// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
static SHA256_TEST_VECTORS: &[TestVector] = &[
    TestVector {
        password: "password",
        salt: "salt",
        iter: 1,
        output: &[
            0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4,
            0xf8, 0x37, 0xa8, 0x65, 0x48, 0xc9,
        ],
    },
    TestVector {
        password: "password",
        salt: "salt",
        iter: 2,
        output: &[
            0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0,
            0x6d, 0xd0, 0x2a, 0x30, 0x3f, 0x8e,
        ],
    },
    TestVector {
        password: "password",
        salt: "salt",
        iter: 4096,
        output: &[
            0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c,
            0x4c, 0x8d, 0x96, 0x28, 0x93, 0xa0,
        ],
    },
    TestVector {
        password: "passwordPASSWORDpassword",
        salt: "saltSALTsaltSALTsaltSALTsaltSALTsalt",
        iter: 4096,
        output: &[
            0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e,
            0x84, 0xcf, 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, 0x1c,
        ],
    },
    TestVector {
        password: "pass\0word",
        salt: "sa\0lt",
        iter: 4096,
        output: &[
            0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a,
            0x86, 0x87,
        ],
    },
];

fn test_hash<const N: usize, H, F>(hash_name: &str, vectors: &[TestVector], hash_fn: F)
where
    H: crate::hash::Hash<N> + crate::mac::hmac::Marshalable,
    F: Fn() -> H,
{
    for (i, v) in vectors.iter().enumerate() {
        let output = key(
            v.password.as_bytes(),
            v.salt.as_bytes(),
            v.iter,
            v.output.len(),
            &hash_fn,
        );
        assert_eq!(
            output, v.output,
            "{} {}: expected {:02x?}, got {:02x?}",
            hash_name, i, v.output, output
        );
    }
}

#[test]
fn test_with_hmac_sha1() {
    test_hash("SHA1", SHA1_TEST_VECTORS, sha1::new);
}

#[test]
fn test_with_hmac_sha256() {
    test_hash("SHA256", SHA256_TEST_VECTORS, sha256::new256);
}
