test_crypto_mbedtls.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* test_crypto_mbedtls.cpp */
  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. #include "test_crypto_mbedtls.h"
  31. #include "../crypto_mbedtls.h"
  32. #include "core/io/file_access.h"
  33. #include "tests/test_macros.h"
  34. #include "tests/test_utils.h"
  35. namespace TestCryptoMbedTLS {
  36. void hmac_digest_test(HashingContext::HashType ht, String expected_hex) {
  37. CryptoMbedTLS crypto;
  38. PackedByteArray key = String("supersecretkey").to_utf8_buffer();
  39. PackedByteArray msg = String("Return of the MAC!").to_utf8_buffer();
  40. PackedByteArray digest = crypto.hmac_digest(ht, key, msg);
  41. String hex = String::hex_encode_buffer(digest.ptr(), digest.size());
  42. CHECK(hex == expected_hex);
  43. }
  44. void hmac_context_digest_test(HashingContext::HashType ht, String expected_hex) {
  45. HMACContextMbedTLS ctx;
  46. PackedByteArray key = String("supersecretkey").to_utf8_buffer();
  47. PackedByteArray msg1 = String("Return of ").to_utf8_buffer();
  48. PackedByteArray msg2 = String("the MAC!").to_utf8_buffer();
  49. Error err = ctx.start(ht, key);
  50. CHECK(err == OK);
  51. err = ctx.update(msg1);
  52. CHECK(err == OK);
  53. err = ctx.update(msg2);
  54. CHECK(err == OK);
  55. PackedByteArray digest = ctx.finish();
  56. String hex = String::hex_encode_buffer(digest.ptr(), digest.size());
  57. CHECK(hex == expected_hex);
  58. }
  59. Ref<CryptoKey> create_crypto_key(const String &p_key_path, bool p_public_only) {
  60. Ref<CryptoKey> crypto_key = Ref<CryptoKey>(CryptoKey::create());
  61. crypto_key->load(p_key_path, p_public_only);
  62. return crypto_key;
  63. }
  64. String read_file_s(const String &p_file_path) {
  65. Ref<FileAccess> file_access = FileAccess::open(p_file_path, FileAccess::READ);
  66. REQUIRE(file_access.is_valid());
  67. return file_access->get_as_utf8_string();
  68. }
  69. bool files_equal(const String &p_in_path, const String &p_out_path) {
  70. const String s_in = read_file_s(p_in_path);
  71. const String s_out = read_file_s(p_out_path);
  72. return s_in == s_out;
  73. }
  74. void crypto_key_public_only_test(const String &p_key_path, bool p_public_only) {
  75. Ref<CryptoKey> crypto_key = create_crypto_key(p_key_path, p_public_only);
  76. bool is_equal = crypto_key->is_public_only() == p_public_only;
  77. CHECK(is_equal);
  78. }
  79. void crypto_key_save_test(const String &p_in_path, const String &p_out_path, bool p_public_only) {
  80. Ref<CryptoKey> crypto_key = create_crypto_key(p_in_path, p_public_only);
  81. crypto_key->save(p_out_path, p_public_only);
  82. bool is_equal = files_equal(p_in_path, p_out_path);
  83. CHECK(is_equal);
  84. }
  85. void crypto_key_save_public_only_test(const String &p_in_priv_path, const String &p_in_pub_path, const String &p_out_path) {
  86. Ref<CryptoKey> crypto_key = create_crypto_key(p_in_priv_path, false);
  87. crypto_key->save(p_out_path, true);
  88. bool is_equal = files_equal(p_in_pub_path, p_out_path);
  89. CHECK(is_equal);
  90. }
  91. } // namespace TestCryptoMbedTLS