diff --git a/tests/cryptotester.cpp b/tests/cryptotester.cpp index c33a038ff..c187d4254 100644 --- a/tests/cryptotester.cpp +++ b/tests/cryptotester.cpp @@ -239,6 +239,32 @@ void CryptoTester::testAesEncryption() { CPPUNIT_ASSERT(data2 == decrypted2); } +void CryptoTester::testAesEncryptionWithMultipleKeySizes() { + auto data = std::vector(rand(), rand()); + + // Valid key sizes + for (auto key_length : {16, 24, 32}) { + auto key = std::vector(key_length, rand()); + + auto encrypted_data = dht::crypto::aesEncrypt(data, key); + auto decrypted_data = dht::crypto::aesDecrypt(encrypted_data, key); + + CPPUNIT_ASSERT(data == decrypted_data); + } + + // Invalid key sizes + for (auto key_length : {12, 28, 36}) { + auto key = std::vector(key_length, rand()); + + try { + dht::crypto::aesEncrypt(data, key); + CPPUNIT_FAIL(std::to_string(key_length) + " should be an invalid key size."); + } catch (const dht::crypto::DecryptError&) { + // Do nothing - this is expected + } + } +} + void CryptoTester::tearDown() { diff --git a/tests/cryptotester.h b/tests/cryptotester.h index 890190571..6cd552c40 100644 --- a/tests/cryptotester.h +++ b/tests/cryptotester.h @@ -34,6 +34,7 @@ class CryptoTester : public CppUnit::TestFixture { CPPUNIT_TEST(testCertificateSerialNumber); CPPUNIT_TEST(testOcsp); CPPUNIT_TEST(testAesEncryption); + CPPUNIT_TEST(testAesEncryptionWithMultipleKeySizes); CPPUNIT_TEST_SUITE_END(); public: @@ -69,6 +70,7 @@ class CryptoTester : public CppUnit::TestFixture { * Test key streching and aes encryption/decryption */ void testAesEncryption(); + void testAesEncryptionWithMultipleKeySizes(); }; } // namespace test