Path: blob/master/modules/mbedtls/tests/test_crypto_mbedtls.cpp
10278 views
/**************************************************************************/1/* test_crypto_mbedtls.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "test_crypto_mbedtls.h"3132#include "../crypto_mbedtls.h"3334#include "tests/test_macros.h"35#include "tests/test_utils.h"3637namespace TestCryptoMbedTLS {3839void hmac_digest_test(HashingContext::HashType ht, String expected_hex) {40CryptoMbedTLS crypto;41PackedByteArray key = String("supersecretkey").to_utf8_buffer();42PackedByteArray msg = String("Return of the MAC!").to_utf8_buffer();43PackedByteArray digest = crypto.hmac_digest(ht, key, msg);44String hex = String::hex_encode_buffer(digest.ptr(), digest.size());45CHECK(hex == expected_hex);46}4748void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex) {49HMACContextMbedTLS ctx;50PackedByteArray key = String("supersecretkey").to_utf8_buffer();51PackedByteArray msg1 = String("Return of ").to_utf8_buffer();52PackedByteArray msg2 = String("the MAC!").to_utf8_buffer();53Error err = ctx.start(ht, key);54CHECK(err == OK);55err = ctx.update(msg1);56CHECK(err == OK);57err = ctx.update(msg2);58CHECK(err == OK);59PackedByteArray digest = ctx.finish();60String hex = String::hex_encode_buffer(digest.ptr(), digest.size());61CHECK(hex == expected_hex);62}6364Ref<CryptoKey> create_crypto_key(const String &p_key_path, bool p_public_only) {65Ref<CryptoKey> crypto_key = Ref<CryptoKey>(CryptoKey::create());66crypto_key->load(p_key_path, p_public_only);67return crypto_key;68}6970String read_file_s(const String &p_file_path) {71Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ);72REQUIRE(file_access.is_valid());73return file_access->get_as_utf8_string();74}7576bool files_equal(const String &p_in_path, const String &p_out_path) {77const String s_in = read_file_s(p_in_path);78const String s_out = read_file_s(p_out_path);79return s_in == s_out;80}8182void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) {83Ref<CryptoKey> crypto_key = create_crypto_key(p_key_path, p_public_only);84bool is_equal = crypto_key->is_public_only() == p_public_only;85CHECK(is_equal);86}8788void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) {89Ref<CryptoKey> crypto_key = create_crypto_key(p_in_path, p_public_only);90crypto_key->save(p_out_path, p_public_only);91bool is_equal = files_equal(p_in_path, p_out_path);92CHECK(is_equal);93}9495void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) {96Ref<CryptoKey> crypto_key = create_crypto_key(p_in_priv_path, false);97crypto_key->save(p_out_path, true);98bool is_equal = files_equal(p_in_pub_path, p_out_path);99CHECK(is_equal);100}101} // namespace TestCryptoMbedTLS102103104