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