Path: blob/master/modules/mbedtls/tests/test_crypto_mbedtls.cpp
11353 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 "core/io/file_access.h"35#include "tests/test_macros.h"36#include "tests/test_utils.h"3738namespace TestCryptoMbedTLS {3940void hmac_digest_test(HashingContext::HashType ht, String expected_hex) {41CryptoMbedTLS crypto;42PackedByteArray key = String("supersecretkey").to_utf8_buffer();43PackedByteArray msg = String("Return of the MAC!").to_utf8_buffer();44PackedByteArray digest = crypto.hmac_digest(ht, key, msg);45String hex = String::hex_encode_buffer(digest.ptr(), digest.size());46CHECK(hex == expected_hex);47}4849void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex) {50HMACContextMbedTLS ctx;51PackedByteArray key = String("supersecretkey").to_utf8_buffer();52PackedByteArray msg1 = String("Return of ").to_utf8_buffer();53PackedByteArray msg2 = String("the MAC!").to_utf8_buffer();54Error err = ctx.start(ht, key);55CHECK(err == OK);56err = ctx.update(msg1);57CHECK(err == OK);58err = ctx.update(msg2);59CHECK(err == OK);60PackedByteArray digest = ctx.finish();61String hex = String::hex_encode_buffer(digest.ptr(), digest.size());62CHECK(hex == expected_hex);63}6465Ref<CryptoKey> create_crypto_key(const String &p_key_path, bool p_public_only) {66Ref<CryptoKey> crypto_key = Ref<CryptoKey>(CryptoKey::create());67crypto_key->load(p_key_path, p_public_only);68return crypto_key;69}7071String read_file_s(const String &p_file_path) {72Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ);73REQUIRE(file_access.is_valid());74return file_access->get_as_utf8_string();75}7677bool files_equal(const String &p_in_path, const String &p_out_path) {78const String s_in = read_file_s(p_in_path);79const String s_out = read_file_s(p_out_path);80return s_in == s_out;81}8283void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) {84Ref<CryptoKey> crypto_key = create_crypto_key(p_key_path, p_public_only);85bool is_equal = crypto_key->is_public_only() == p_public_only;86CHECK(is_equal);87}8889void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) {90Ref<CryptoKey> crypto_key = create_crypto_key(p_in_path, p_public_only);91crypto_key->save(p_out_path, p_public_only);92bool is_equal = files_equal(p_in_path, p_out_path);93CHECK(is_equal);94}9596void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) {97Ref<CryptoKey> crypto_key = create_crypto_key(p_in_priv_path, false);98crypto_key->save(p_out_path, true);99bool is_equal = files_equal(p_in_pub_path, p_out_path);100CHECK(is_equal);101}102} // namespace TestCryptoMbedTLS103104105