Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviv committed Jul 10, 2024
1 parent 38297da commit eaf4b57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/cryptotester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,32 @@ void CryptoTester::testAesEncryption() {
CPPUNIT_ASSERT(data2 == decrypted2);
}

void CryptoTester::testAesEncryptionWithMultipleKeySizes() {
auto data = std::vector<uint8_t>(rand(), rand());

// Valid key sizes
for (auto key_length : {16, 24, 32}) {
auto key = std::vector<uint8_t>(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<uint8_t>(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() {

Expand Down
2 changes: 2 additions & 0 deletions tests/cryptotester.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -69,6 +70,7 @@ class CryptoTester : public CppUnit::TestFixture {
* Test key streching and aes encryption/decryption
*/
void testAesEncryption();
void testAesEncryptionWithMultipleKeySizes();
};

} // namespace test

0 comments on commit eaf4b57

Please sign in to comment.