1 | // |
---|---|
2 | // MD5EngineTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "MD5EngineTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/MD5Engine.h" |
15 | |
16 | |
17 | using Poco::MD5Engine; |
18 | using Poco::DigestEngine; |
19 | |
20 | |
21 | MD5EngineTest::MD5EngineTest(const std::string& rName): CppUnit::TestCase(rName) |
22 | { |
23 | } |
24 | |
25 | |
26 | MD5EngineTest::~MD5EngineTest() |
27 | { |
28 | } |
29 | |
30 | |
31 | void MD5EngineTest::testMD5() |
32 | { |
33 | MD5Engine engine; |
34 | |
35 | // test vectors from RFC 1321 |
36 | |
37 | engine.update(""); |
38 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "d41d8cd98f00b204e9800998ecf8427e"); |
39 | |
40 | engine.update("a"); |
41 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "0cc175b9c0f1b6a831c399e269772661"); |
42 | |
43 | engine.update("abc"); |
44 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "900150983cd24fb0d6963f7d28e17f72"); |
45 | |
46 | engine.update("message digest"); |
47 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "f96b697d7cb7938d525a2f31aaf161d0"); |
48 | |
49 | engine.update("abcdefghijklmnopqrstuvwxyz"); |
50 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "c3fcd3d76192e4007dfb496cca67e13b"); |
51 | |
52 | engine.update("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
53 | engine.update("abcdefghijklmnopqrstuvwxyz0123456789"); |
54 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "d174ab98d277d9f5a5611c2c9f419d9f"); |
55 | |
56 | engine.update("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); |
57 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "57edf4a22be3c955ac49da2e2107b67a"); |
58 | } |
59 | |
60 | |
61 | void MD5EngineTest::testConstantTimeEquals() |
62 | { |
63 | DigestEngine::Digest d1 = DigestEngine::digestFromHex("d41d8cd98f00b204e9800998ecf8427e"); |
64 | DigestEngine::Digest d2 = DigestEngine::digestFromHex("d41d8cd98f00b204e9800998ecf8427e"); |
65 | DigestEngine::Digest d3 = DigestEngine::digestFromHex("0cc175b9c0f1b6a831c399e269772661"); |
66 | |
67 | assertTrue (DigestEngine::constantTimeEquals(d1, d2)); |
68 | assertTrue (!DigestEngine::constantTimeEquals(d1, d3)); |
69 | } |
70 | |
71 | |
72 | void MD5EngineTest::setUp() |
73 | { |
74 | } |
75 | |
76 | |
77 | void MD5EngineTest::tearDown() |
78 | { |
79 | } |
80 | |
81 | |
82 | CppUnit::Test* MD5EngineTest::suite() |
83 | { |
84 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MD5EngineTest"); |
85 | |
86 | CppUnit_addTest(pSuite, MD5EngineTest, testMD5); |
87 | CppUnit_addTest(pSuite, MD5EngineTest, testConstantTimeEquals); |
88 | |
89 | return pSuite; |
90 | } |
91 |