Add unit tests for CryptoKey

This commit is contained in:
Edward Moulsdale 2024-03-01 07:17:59 +00:00
parent 7d2ca2d8ac
commit e461496ecb
6 changed files with 139 additions and 0 deletions

1
.gitignore vendored
View File

@ -43,6 +43,7 @@ logs/
# Generated by unit tests # Generated by unit tests
tests/data/*.translation tests/data/*.translation
tests/data/crypto/out*
############################ ############################
### General build output ### ### General build output ###

View File

@ -121,6 +121,9 @@ if env["tests"]:
env_mbed_tls.Append(CPPDEFINES=["TESTS_ENABLED"]) env_mbed_tls.Append(CPPDEFINES=["TESTS_ENABLED"])
env_mbed_tls.add_source_files(module_obj, "./tests/*.cpp") env_mbed_tls.add_source_files(module_obj, "./tests/*.cpp")
if env["disable_exceptions"]:
env_mbed_tls.Append(CPPDEFINES=["DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS"])
env.modules_sources += module_obj env.modules_sources += module_obj
# Needed to force rebuilding the module files when the thirdparty library is updated. # Needed to force rebuilding the module files when the thirdparty library is updated.

View File

@ -33,6 +33,7 @@
#include "../crypto_mbedtls.h" #include "../crypto_mbedtls.h"
#include "tests/test_macros.h" #include "tests/test_macros.h"
#include "tests/test_utils.h"
namespace TestCryptoMbedTLS { namespace TestCryptoMbedTLS {
@ -60,4 +61,42 @@ void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex)
String hex = String::hex_encode_buffer(digest.ptr(), digest.size()); String hex = String::hex_encode_buffer(digest.ptr(), digest.size());
CHECK(hex == expected_hex); CHECK(hex == expected_hex);
} }
Ref<CryptoKey> create_crypto_key(const String &p_key_path, bool p_public_only) {
Ref<CryptoKey> crypto_key = Ref<CryptoKey>(CryptoKey::create());
crypto_key->load(p_key_path, p_public_only);
return crypto_key;
}
String read_file_s(const String &p_file_path) {
Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ);
REQUIRE(file_access.is_valid());
return file_access->get_as_utf8_string();
}
bool files_equal(const String &p_in_path, const String &p_out_path) {
const String s_in = read_file_s(p_in_path);
const String s_out = read_file_s(p_out_path);
return s_in == s_out;
}
void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) {
Ref<CryptoKey> crypto_key = create_crypto_key(p_key_path, p_public_only);
bool is_equal = crypto_key->is_public_only() == p_public_only;
CHECK(is_equal);
}
void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) {
Ref<CryptoKey> crypto_key = create_crypto_key(p_in_path, p_public_only);
crypto_key->save(p_out_path, p_public_only);
bool is_equal = files_equal(p_in_path, p_out_path);
CHECK(is_equal);
}
void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) {
Ref<CryptoKey> crypto_key = create_crypto_key(p_in_priv_path, false);
crypto_key->save(p_out_path, true);
bool is_equal = files_equal(p_in_pub_path, p_out_path);
CHECK(is_equal);
}
} // namespace TestCryptoMbedTLS } // namespace TestCryptoMbedTLS

View File

@ -31,9 +31,11 @@
#ifndef TEST_CRYPTO_MBEDTLS_H #ifndef TEST_CRYPTO_MBEDTLS_H
#define TEST_CRYPTO_MBEDTLS_H #define TEST_CRYPTO_MBEDTLS_H
#include "core/crypto/crypto.h"
#include "core/crypto/hashing_context.h" #include "core/crypto/hashing_context.h"
#include "tests/test_macros.h" #include "tests/test_macros.h"
#include "tests/test_utils.h"
namespace TestCryptoMbedTLS { namespace TestCryptoMbedTLS {
@ -56,6 +58,35 @@ TEST_CASE("[HMACContext] HMAC digest") {
// SHA-1 // SHA-1
hmac_context_digest_test(HashingContext::HashType::HASH_SHA1, "a0ac4cd68a2f4812c355983d94e8d025afe7dddf"); hmac_context_digest_test(HashingContext::HashType::HASH_SHA1, "a0ac4cd68a2f4812c355983d94e8d025afe7dddf");
} }
void crypto_key_public_only_test(const String &p_key_path, bool public_only);
TEST_CASE("[Crypto] CryptoKey is_public_only") {
crypto_key_public_only_test(TestUtils::get_data_path("crypto/in.key"), false);
crypto_key_public_only_test(TestUtils::get_data_path("crypto/in.pub"), true);
}
void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool public_only);
TEST_CASE("[Crypto] CryptoKey save") {
const String in_priv_path = TestUtils::get_data_path("crypto/in.key");
const String out_priv_path = TestUtils::get_data_path("crypto/out.key");
crypto_key_save_test(in_priv_path, out_priv_path, false);
const String in_pub_path = TestUtils::get_data_path("crypto/in.pub");
const String out_pub_path = TestUtils::get_data_path("crypto/out.pub");
crypto_key_save_test(in_pub_path, out_pub_path, true);
}
void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path);
TEST_CASE("[Crypto] CryptoKey save public_only") {
const String in_priv_path = TestUtils::get_data_path("crypto/in.key");
const String in_pub_path = TestUtils::get_data_path("crypto/in.pub");
const String out_path = TestUtils::get_data_path("crypto/out_public_only.pub");
crypto_key_save_public_only_test(in_priv_path, in_pub_path, out_path);
}
} // namespace TestCryptoMbedTLS } // namespace TestCryptoMbedTLS
#endif // TEST_CRYPTO_MBEDTLS_H #endif // TEST_CRYPTO_MBEDTLS_H

51
tests/data/crypto/in.key Normal file
View File

@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEAp+sWMepqN+dXhmZkS45W0FTLX2f0zRb+kUhXngfiEprgA2Uu
ZP5gGv7/EjOBDniYalGZqYPJum08Px++17P6Hyopr05aPhZ6Ocnt+LAU/WRAFspN
3xXTZglqnmGMsdNcqBl3loVNmC1H/zX53oBeP/L/i4lva3DjCRIpHtGjfy3ufSn7
fTrlBqKIEHYgQZyzos9dxbk1NJcWVxlNmvFRtgPW6DQ/P3g0ahLJeq/hO3ykUFfi
KdhHkS+s9dC9qjZwSxCCEF1o6zkEO6ynxk0sxjOpPTJFw7fQ+2KeJWJQfdZeJ6u+
DpbwK4g9tMUMNxf3QMk1KhAnwwUrKXfZcviS3zweD8Tl5zEj45H1ghoUQfuW7Y3w
gwHMu8lF8iGA87cf/fhqr0V9piCcwWkfVP/athpMoUfyj9Sa3ag0dvDSo9PAet0u
rXmdKTyhMg4lQL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0V9piCcwWkfVP/a
thpMoUfyj9Sa3ag0dvDSo9PAet0urXmdKTyhMg4lQL6f9BmMuuB/KwWzCuG/5VY9
ONxno3OVX6juHTpng5UglbkiDsD3tivl1gCbvIryoGdt+xI0JmAC5eXfg79Nio/B
ayDR9Npm1m460p3GeRaawyYysBo/L5/YZ/S3bYBRoJ7lq6GkTA+22lWAb04IgtS8
wxO4Ma8EOtKD+AoR3C+WLivcp9LNTxbQOMKGL+8imQGBEz3XTR4lrE02QDQy0DIB
Ky7p7dhlyBdwhTmBX3P2mx0CAwEAAQ==
-----END PUBLIC KEY-----