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