| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | TEST(TestShaEncryptUtils, TestAesEncryptDecrypt) { |
| 23 | // 16 bytes key |
| 24 | auto* key = "12345678abcdefgh"; |
| 25 | auto* to_encrypt = "some test string"; |
| 26 | |
| 27 | auto key_len = static_cast<int32_t>(strlen(reinterpret_cast<const char*>(key))); |
| 28 | auto to_encrypt_len = |
| 29 | static_cast<int32_t>(strlen(reinterpret_cast<const char*>(to_encrypt))); |
| 30 | unsigned char cipher_1[64]; |
| 31 | |
| 32 | int32_t cipher_1_len = |
| 33 | gandiva::aes_encrypt(to_encrypt, to_encrypt_len, key, key_len, cipher_1); |
| 34 | |
| 35 | unsigned char decrypted_1[64]; |
| 36 | int32_t decrypted_1_len = gandiva::aes_decrypt(reinterpret_cast<const char*>(cipher_1), |
| 37 | cipher_1_len, key, key_len, decrypted_1); |
| 38 | |
| 39 | EXPECT_EQ(std::string(reinterpret_cast<const char*>(to_encrypt), to_encrypt_len), |
| 40 | std::string(reinterpret_cast<const char*>(decrypted_1), decrypted_1_len)); |
| 41 | |
| 42 | // 24 bytes key |
| 43 | key = "12345678abcdefgh12345678"; |
| 44 | to_encrypt = "some\ntest\nstring"; |
| 45 | |
| 46 | key_len = static_cast<int32_t>(strlen(reinterpret_cast<const char*>(key))); |
| 47 | to_encrypt_len = |
| 48 | static_cast<int32_t>(strlen(reinterpret_cast<const char*>(to_encrypt))); |
| 49 | unsigned char cipher_2[64]; |
| 50 | |
| 51 | int32_t cipher_2_len = |
| 52 | gandiva::aes_encrypt(to_encrypt, to_encrypt_len, key, key_len, cipher_2); |
| 53 | |
| 54 | unsigned char decrypted_2[64]; |
| 55 | int32_t decrypted_2_len = gandiva::aes_decrypt(reinterpret_cast<const char*>(cipher_2), |
| 56 | cipher_2_len, key, key_len, decrypted_2); |
| 57 | |
| 58 | EXPECT_EQ(std::string(reinterpret_cast<const char*>(to_encrypt), to_encrypt_len), |
| 59 | std::string(reinterpret_cast<const char*>(decrypted_2), decrypted_2_len)); |
| 60 | |
| 61 | // 32 bytes key |
| 62 | key = "12345678abcdefgh12345678abcdefgh"; |
| 63 | to_encrypt = "New\ntest\nstring"; |
| 64 | |
| 65 | key_len = static_cast<int32_t>(strlen(reinterpret_cast<const char*>(key))); |
| 66 | to_encrypt_len = |
| 67 | static_cast<int32_t>(strlen(reinterpret_cast<const char*>(to_encrypt))); |
| 68 | unsigned char cipher_3[64]; |
| 69 | |
| 70 | int32_t cipher_3_len = |
| 71 | gandiva::aes_encrypt(to_encrypt, to_encrypt_len, key, key_len, cipher_3); |
| 72 | |
| 73 | unsigned char decrypted_3[64]; |
| 74 | int32_t decrypted_3_len = gandiva::aes_decrypt(reinterpret_cast<const char*>(cipher_3), |
| 75 | cipher_3_len, key, key_len, decrypted_3); |
| 76 | |
| 77 | EXPECT_EQ(std::string(reinterpret_cast<const char*>(to_encrypt), to_encrypt_len), |
| 78 | std::string(reinterpret_cast<const char*>(decrypted_3), decrypted_3_len)); |
| 79 |
nothing calls this directly
no test coverage detected