Path: blob/master/tests/core/test_hashing_context.cpp
23449 views
/**************************************************************************/1/* test_hashing_context.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 "tests/test_macros.h"3132TEST_FORCE_LINK(test_hashing_context)3334#include "core/crypto/hashing_context.h"3536namespace TestHashingContext {3738TEST_CASE("[HashingContext] Default - MD5/SHA1/SHA256") {39HashingContext ctx;4041static const uint8_t md5_expected[] = {420xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e43};44static const uint8_t sha1_expected[] = {450xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,460xaf, 0xd8, 0x07, 0x0947};48static const uint8_t sha256_expected[] = {490xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,500x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x5551};5253CHECK(ctx.start(HashingContext::HASH_MD5) == OK);54PackedByteArray result = ctx.finish();55REQUIRE(result.size() == 16);56CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);5758CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);59result = ctx.finish();60REQUIRE(result.size() == 20);61CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);6263CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);64result = ctx.finish();65REQUIRE(result.size() == 32);66CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);67}6869TEST_CASE("[HashingContext] Multiple updates - MD5/SHA1/SHA256") {70HashingContext ctx;71const String s = "xyz";7273const PackedByteArray s_byte_parts[] = {74String("x").to_ascii_buffer(),75String("y").to_ascii_buffer(),76String("z").to_ascii_buffer()77};7879static const uint8_t md5_expected[] = {800xd1, 0x6f, 0xb3, 0x6f, 0x09, 0x11, 0xf8, 0x78, 0x99, 0x8c, 0x13, 0x61, 0x91, 0xaf, 0x70, 0x5e81};82static const uint8_t sha1_expected[] = {830x66, 0xb2, 0x74, 0x17, 0xd3, 0x7e, 0x02, 0x4c, 0x46, 0x52, 0x6c, 0x2f, 0x6d, 0x35, 0x8a, 0x75,840x4f, 0xc5, 0x52, 0xf385};86static const uint8_t sha256_expected[] = {870x36, 0x08, 0xbc, 0xa1, 0xe4, 0x4e, 0xa6, 0xc4, 0xd2, 0x68, 0xeb, 0x6d, 0xb0, 0x22, 0x60, 0x26,880x98, 0x92, 0xc0, 0xb4, 0x2b, 0x86, 0xbb, 0xf1, 0xe7, 0x7a, 0x6f, 0xa1, 0x6c, 0x3c, 0x92, 0x8289};9091CHECK(ctx.start(HashingContext::HASH_MD5) == OK);92CHECK(ctx.update(s_byte_parts[0]) == OK);93CHECK(ctx.update(s_byte_parts[1]) == OK);94CHECK(ctx.update(s_byte_parts[2]) == OK);95PackedByteArray result = ctx.finish();96REQUIRE(result.size() == 16);97CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);9899CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);100CHECK(ctx.update(s_byte_parts[0]) == OK);101CHECK(ctx.update(s_byte_parts[1]) == OK);102CHECK(ctx.update(s_byte_parts[2]) == OK);103result = ctx.finish();104REQUIRE(result.size() == 20);105CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);106107CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);108CHECK(ctx.update(s_byte_parts[0]) == OK);109CHECK(ctx.update(s_byte_parts[1]) == OK);110CHECK(ctx.update(s_byte_parts[2]) == OK);111result = ctx.finish();112REQUIRE(result.size() == 32);113CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);114}115116TEST_CASE("[HashingContext] Invalid use of start") {117HashingContext ctx;118119ERR_PRINT_OFF;120CHECK_MESSAGE(121ctx.start(static_cast<HashingContext::HashType>(-1)) == ERR_UNAVAILABLE,122"Using invalid hash types should fail.");123ERR_PRINT_ON;124125REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);126127ERR_PRINT_OFF;128CHECK_MESSAGE(129ctx.start(HashingContext::HASH_MD5) == ERR_ALREADY_IN_USE,130"Calling 'start' twice before 'finish' should fail.");131ERR_PRINT_ON;132}133134TEST_CASE("[HashingContext] Invalid use of update") {135HashingContext ctx;136137ERR_PRINT_OFF;138CHECK_MESSAGE(139ctx.update(PackedByteArray()) == ERR_UNCONFIGURED,140"Calling 'update' before 'start' should fail.");141ERR_PRINT_ON;142143REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);144145ERR_PRINT_OFF;146CHECK_MESSAGE(147ctx.update(PackedByteArray()) == FAILED,148"Calling 'update' with an empty byte array should fail.");149ERR_PRINT_ON;150}151152TEST_CASE("[HashingContext] Invalid use of finish") {153HashingContext ctx;154155ERR_PRINT_OFF;156CHECK_MESSAGE(157ctx.finish() == PackedByteArray(),158"Calling 'finish' before 'start' should return an empty byte array.");159ERR_PRINT_ON;160}161162} // namespace TestHashingContext163164165