extern crate keybob;

mod derivation {
    use keybob::{Key, KeyType};

    #[test]
    fn basic_128() {
        let k1 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
        let k2 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");

        assert_eq!(k1, k2);
    }

    #[test]
    fn basic_256() {
        let k1 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");
        let k2 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");

        assert_eq!(k1, k2);
    }

    #[test]
    fn colliding_pw_128() {
        let k1 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
        let k2 = Key::from_pw(KeyType::Aes128, "123456789", "bob");

        assert!(k1 != k2);
    }

    #[test]
    fn colliding_pw_256() {
        let k1 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");
        let k2 = Key::from_pw(KeyType::Aes256, "123456789", "bob");

        assert!(k1 != k2);
    }
}

mod workflow {
    use keybob::{Key, KeyType};

    #[test]
    fn miscreant() {
        use aes_siv::aead::generic_array::GenericArray;
        use aes_siv::aead::{Aead, NewAead};
        use aes_siv::Aes256SivAead;

        let key = Key::new(KeyType::Aes256);
        let aes = Aes256SivAead::new_varkey(key.as_slice()).expect("incorrect key length");

        /* Technically just random data, you can also use a "key" as nonce */
        let nonce = GenericArray::clone_from_slice(&Key::new(KeyType::Aes256).as_slice()[..16]);
        let data_in = "This is a message!";

        let encrypted = aes
            .encrypt(&nonce, data_in.as_bytes())
            .expect("encryption failed");
        let decrypted = aes.decrypt(&nonce, encrypted.as_slice()).unwrap();
        let data_out = ::std::str::from_utf8(&decrypted.as_slice()).unwrap();

        assert_eq!(data_in, data_out);
    }

    #[test]
    fn printers() {
        let k1 = Key::from_pw(KeyType::Aes128, "192837465", "jane");
        assert_eq!(&format!("{:?}", k1), "Key: type: Aes128 – \"4HaDrA==\"");
    }
}
