test_hashing_context.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef TEST_HASHING_CONTEXT_H
  31. #define TEST_HASHING_CONTEXT_H
  32. #include "core/crypto/hashing_context.h"
  33. #include "tests/test_macros.h"
  34. namespace TestHashingContext {
  35. TEST_CASE("[HashingContext] Default - MD5/SHA1/SHA256") {
  36. HashingContext ctx;
  37. static const uint8_t md5_expected[] = {
  38. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
  39. };
  40. static const uint8_t sha1_expected[] = {
  41. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
  42. 0xaf, 0xd8, 0x07, 0x09
  43. };
  44. static const uint8_t sha256_expected[] = {
  45. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
  46. 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  47. };
  48. CHECK(ctx.start(HashingContext::HASH_MD5) == OK);
  49. PackedByteArray result = ctx.finish();
  50. REQUIRE(result.size() == 16);
  51. CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);
  52. CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);
  53. result = ctx.finish();
  54. REQUIRE(result.size() == 20);
  55. CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);
  56. CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);
  57. result = ctx.finish();
  58. REQUIRE(result.size() == 32);
  59. CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);
  60. }
  61. TEST_CASE("[HashingContext] Multiple updates - MD5/SHA1/SHA256") {
  62. HashingContext ctx;
  63. const String s = "xyz";
  64. const PackedByteArray s_byte_parts[] = {
  65. String("x").to_ascii_buffer(),
  66. String("y").to_ascii_buffer(),
  67. String("z").to_ascii_buffer()
  68. };
  69. static const uint8_t md5_expected[] = {
  70. 0xd1, 0x6f, 0xb3, 0x6f, 0x09, 0x11, 0xf8, 0x78, 0x99, 0x8c, 0x13, 0x61, 0x91, 0xaf, 0x70, 0x5e
  71. };
  72. static const uint8_t sha1_expected[] = {
  73. 0x66, 0xb2, 0x74, 0x17, 0xd3, 0x7e, 0x02, 0x4c, 0x46, 0x52, 0x6c, 0x2f, 0x6d, 0x35, 0x8a, 0x75,
  74. 0x4f, 0xc5, 0x52, 0xf3
  75. };
  76. static const uint8_t sha256_expected[] = {
  77. 0x36, 0x08, 0xbc, 0xa1, 0xe4, 0x4e, 0xa6, 0xc4, 0xd2, 0x68, 0xeb, 0x6d, 0xb0, 0x22, 0x60, 0x26,
  78. 0x98, 0x92, 0xc0, 0xb4, 0x2b, 0x86, 0xbb, 0xf1, 0xe7, 0x7a, 0x6f, 0xa1, 0x6c, 0x3c, 0x92, 0x82
  79. };
  80. CHECK(ctx.start(HashingContext::HASH_MD5) == OK);
  81. CHECK(ctx.update(s_byte_parts[0]) == OK);
  82. CHECK(ctx.update(s_byte_parts[1]) == OK);
  83. CHECK(ctx.update(s_byte_parts[2]) == OK);
  84. PackedByteArray result = ctx.finish();
  85. REQUIRE(result.size() == 16);
  86. CHECK(memcmp(result.ptr(), md5_expected, 16) == 0);
  87. CHECK(ctx.start(HashingContext::HASH_SHA1) == OK);
  88. CHECK(ctx.update(s_byte_parts[0]) == OK);
  89. CHECK(ctx.update(s_byte_parts[1]) == OK);
  90. CHECK(ctx.update(s_byte_parts[2]) == OK);
  91. result = ctx.finish();
  92. REQUIRE(result.size() == 20);
  93. CHECK(memcmp(result.ptr(), sha1_expected, 20) == 0);
  94. CHECK(ctx.start(HashingContext::HASH_SHA256) == OK);
  95. CHECK(ctx.update(s_byte_parts[0]) == OK);
  96. CHECK(ctx.update(s_byte_parts[1]) == OK);
  97. CHECK(ctx.update(s_byte_parts[2]) == OK);
  98. result = ctx.finish();
  99. REQUIRE(result.size() == 32);
  100. CHECK(memcmp(result.ptr(), sha256_expected, 32) == 0);
  101. }
  102. TEST_CASE("[HashingContext] Invalid use of start") {
  103. HashingContext ctx;
  104. ERR_PRINT_OFF;
  105. CHECK_MESSAGE(
  106. ctx.start(static_cast<HashingContext::HashType>(-1)) == ERR_UNAVAILABLE,
  107. "Using invalid hash types should fail.");
  108. ERR_PRINT_ON;
  109. REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);
  110. ERR_PRINT_OFF;
  111. CHECK_MESSAGE(
  112. ctx.start(HashingContext::HASH_MD5) == ERR_ALREADY_IN_USE,
  113. "Calling 'start' twice before 'finish' should fail.");
  114. ERR_PRINT_ON;
  115. }
  116. TEST_CASE("[HashingContext] Invalid use of update") {
  117. HashingContext ctx;
  118. ERR_PRINT_OFF;
  119. CHECK_MESSAGE(
  120. ctx.update(PackedByteArray()) == ERR_UNCONFIGURED,
  121. "Calling 'update' before 'start' should fail.");
  122. ERR_PRINT_ON;
  123. REQUIRE(ctx.start(HashingContext::HASH_MD5) == OK);
  124. ERR_PRINT_OFF;
  125. CHECK_MESSAGE(
  126. ctx.update(PackedByteArray()) == FAILED,
  127. "Calling 'update' with an empty byte array should fail.");
  128. ERR_PRINT_ON;
  129. }
  130. TEST_CASE("[HashingContext] Invalid use of finish") {
  131. HashingContext ctx;
  132. ERR_PRINT_OFF;
  133. CHECK_MESSAGE(
  134. ctx.finish() == PackedByteArray(),
  135. "Calling 'finish' before 'start' should return an empty byte array.");
  136. ERR_PRINT_ON;
  137. }
  138. } // namespace TestHashingContext
  139. #endif // TEST_HASHING_CONTEXT_H