Debian Codegraph @

md5/tests/md5_tests.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Descent Developers
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <gtest/gtest.h>
20
21 #include <sstream>
22 #include <iomanip>
23
24 #include "md5.h"
25
26 namespace {
27
28 std::string hexdigest_after_n_rounds_of_fuzz(int n) {
29 MD5 md5;
30 for (int i = 0; i < n; ++i) {
31 md5.update((float)1.0f);
32 md5.update((int)2);
33 md5.update((int16_t)3);
34 md5.update((uint32_t)4U);
35 md5.update((uint8_t)5);
36 }
37
38 std::ostringstream hexdigest;
39 hexdigest << std::hex;
40 auto digest = md5.digest();
41
42 for (int i = 0; i < 16; ++i)
43 hexdigest << std::setw(2) << std::setfill('0') << std::nouppercase << static_cast<uint32_t>(digest[i]);
44
45 return hexdigest.str();
46 }
47
48 }; // namespace
49
50 TEST(D3, MD5) {
51 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(0), "d41d8cd98f00b204e9800998ecf8427e");
52 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1), "c56c005504f5b9b17df9d83f1106e9b2");
53 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(2), "98327a3f4ee311c9bdc8613508922c95");
54 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(4), "f2343ce526646c087df92d708e883675");
55 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(8), "73396b338a0a769c4e6ae5b7b0bc187e");
56 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(100), "99b30c1dcc42d97c3d914c26a14bb1d7");
57 EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1999), "82eeff8c7d574c8232b0ca6ca2c9dd40");
58 }