1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
mod traits;
mod redis_data_vault;
mod config;
pub mod encryption;
pub mod tokenizer;

pub use traits::DataVault;
pub use redis_data_vault::RedisDataVault;


#[cfg(test)]
mod tests {
    use credit_card::CreditCard;
    use crate::traits::DataVault;
    use crate::redis_data_vault::RedisDataVault;
    use crate::encryption::Aes128CbcEncryption;

    #[tokio::test]
    async fn store_retrieve() {
        let vault = RedisDataVault::new();

        let cc = CreditCard {
            number: "4111111111111111".to_string(),
            cardholder_name: "Graydon Hoare".to_string(),
            expiration_month: "01".to_string(),
            expiration_year: "2023".to_string(),
            brand: None,
            security_code: None
        };

        let token = vault.store_credit_card(&cc).await;
        let credit_card = vault.retrieve_credit_card(&token.to_string()).await;
        assert_eq!(credit_card.number, cc.number)
    }

    #[test]
    fn test_encrypt_string() {
        let plaintext = "Hello world!".to_string();
        let x = hex::decode("1b7a4c403124ae2fb52bedc534d82fa8").unwrap();
        let expected_ciphertext = x.as_slice();

        let enc = Aes128CbcEncryption::new();
        let ciphertext = enc.encrypt_string(&plaintext);

        assert_eq!(ciphertext, expected_ciphertext);
    }

    #[test]
    fn test_decrypt() {
        let plaintext = "Hello world!".to_string();
        let x = hex::decode("1b7a4c403124ae2fb52bedc534d82fa8").unwrap();
        let ciphertext = x.as_slice();

        let enc = Aes128CbcEncryption::new();
        let decrypted_ciphertext = enc.decrypt(&ciphertext);

        assert_eq!(decrypted_ciphertext, plaintext);
    }

    #[test]
    fn test_encrypt_decrypt() {
        let plaintext = "Hello world!".to_string();
        let enc = Aes128CbcEncryption::new();
        let ciphertext = enc.encrypt_string(&plaintext);
        let decrypted_ciphertext = enc.decrypt_vec(ciphertext);
        assert_eq!(decrypted_ciphertext, plaintext);
    }
}