test_hashing_context.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* test_hashing_context.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/crypto/hashing_context.h"
  32. #include "tests/test_macros.h"
  33. namespace TestHashingContext {
  34. TEST_CASE("[HashingContext] Default - MD5/SHA1/SHA256") {
  35. HashingContext ctx;
  36. static const uint8_t md5_expected[] = {
  37. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
  38. };
  39. static const uint8_t sha1_expected[] = {
  40. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
  41. 0xaf, 0xd8, 0x07, 0x09
  42. };
  43. static const uint8_t sha256_expected[] = {
  44. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
  45. 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  46. };
  47. CHECK(ctx.start(HashingContext::HASH_MD5) == OK);
  48. PackedByteArray result = ctx.finish();
  49. REQUIRE(result.size() == 16);
  50. CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);
  51. CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);
  52. result = ctx.finish();
  53. REQUIRE(result.size() == 20);
  54. CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);
  55. CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);
  56. result = ctx.finish();
  57. REQUIRE(result.size() == 32);
  58. CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);
  59. }
  60. TEST_CASE("[HashingContext] Multiple updates - MD5/SHA1/SHA256") {
  61. HashingContext ctx;
  62. const String s = "xyz";
  63. const PackedByteArray s_byte_parts[] = {
  64. String("x").to_ascii_buffer(),
  65. String("y").to_ascii_buffer(),
  66. String("z").to_ascii_buffer()
  67. };
  68. static const uint8_t md5_expected[] = {
  69. 0xd1, 0x6f, 0xb3, 0x6f, 0x09, 0x11, 0xf8, 0x78, 0x99, 0x8c, 0x13, 0x61, 0x91, 0xaf, 0x70, 0x5e
  70. };
  71. static const uint8_t sha1_expected[] = {
  72. 0x66, 0xb2, 0x74, 0x17, 0xd3, 0x7e, 0x02, 0x4c, 0x46, 0x52, 0x6c, 0x2f, 0x6d, 0x35, 0x8a, 0x75,
  73. 0x4f, 0xc5, 0x52, 0xf3
  74. };
  75. static const uint8_t sha256_expected[] = {
  76. 0x36, 0x08, 0xbc, 0xa1, 0xe4, 0x4e, 0xa6, 0xc4, 0xd2, 0x68, 0xeb, 0x6d, 0xb0, 0x22, 0x60, 0x26,
  77. 0x98, 0x92, 0xc0, 0xb4, 0x2b, 0x86, 0xbb, 0xf1, 0xe7, 0x7a, 0x6f, 0xa1, 0x6c, 0x3c, 0x92, 0x82
  78. };
  79. CHECK(ctx.start(HashingContext::HASH_MD5) == OK);
  80. CHECK(ctx.update(s_byte_parts[0]) == OK);
  81. CHECK(ctx.update(s_byte_parts[1]) == OK);
  82. CHECK(ctx.update(s_byte_parts[2]) == OK);
  83. PackedByteArray result = ctx.finish();
  84. REQUIRE(result.size() == 16);
  85. CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);
  86. CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);
  87. CHECK(ctx.update(s_byte_parts[0]) == OK);
  88. CHECK(ctx.update(s_byte_parts[1]) == OK);
  89. CHECK(ctx.update(s_byte_parts[2]) == OK);
  90. result = ctx.finish();
  91. REQUIRE(result.size() == 20);
  92. CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);
  93. CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);
  94. CHECK(ctx.update(s_byte_parts[0]) == OK);
  95. CHECK(ctx.update(s_byte_parts[1]) == OK);
  96. CHECK(ctx.update(s_byte_parts[2]) == OK);
  97. result = ctx.finish();
  98. REQUIRE(result.size() == 32);
  99. CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);
  100. }
  101. TEST_CASE("[HashingContext] Invalid use of start") {
  102. HashingContext ctx;
  103. ERR_PRINT_OFF;
  104. CHECK_MESSAGE(
  105. ctx.start(static_cast<HashingContext::HashType>(-1)) == ERR_UNAVAILABLE,
  106. "Using invalid hash types should fail.");
  107. ERR_PRINT_ON;
  108. REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);
  109. ERR_PRINT_OFF;
  110. CHECK_MESSAGE(
  111. ctx.start(HashingContext::HASH_MD5) == ERR_ALREADY_IN_USE,
  112. "Calling 'start' twice before 'finish' should fail.");
  113. ERR_PRINT_ON;
  114. }
  115. TEST_CASE("[HashingContext] Invalid use of update") {
  116. HashingContext ctx;
  117. ERR_PRINT_OFF;
  118. CHECK_MESSAGE(
  119. ctx.update(PackedByteArray()) == ERR_UNCONFIGURED,
  120. "Calling 'update' before 'start' should fail.");
  121. ERR_PRINT_ON;
  122. REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);
  123. ERR_PRINT_OFF;
  124. CHECK_MESSAGE(
  125. ctx.update(PackedByteArray()) == FAILED,
  126. "Calling 'update' with an empty byte array should fail.");
  127. ERR_PRINT_ON;
  128. }
  129. TEST_CASE("[HashingContext] Invalid use of finish") {
  130. HashingContext ctx;
  131. ERR_PRINT_OFF;
  132. CHECK_MESSAGE(
  133. ctx.finish() == PackedByteArray(),
  134. "Calling 'finish' before 'start' should return an empty byte array.");
  135. ERR_PRINT_ON;
  136. }
  137. } // namespace TestHashingContext