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