Initial version, with MD5
[libs/crypto.git] / tests / md5.cpp
1 #include <msp/crypto/md5.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class MD5Tests: public Test::RegisteredTest<MD5Tests>
8 {
9 public:
10         MD5Tests();
11
12         static const char *get_name() { return "MD5"; }
13
14 private:
15         void empty_input();
16         void known_strings();
17         void size_corner_cases();
18         void large_input();
19 };
20
21 MD5Tests::MD5Tests()
22 {
23         add(&MD5Tests::empty_input,       "empty input");
24         add(&MD5Tests::known_strings,     "known strings");
25         add(&MD5Tests::size_corner_cases, "input size corner cases");
26         add(&MD5Tests::large_input,       "large input");
27 }
28
29 void MD5Tests::empty_input()
30 {
31         string digest = Crypto::MD5().get_hexdigest();
32         EXPECT_EQUAL(digest, "d41d8cd98f00b204e9800998ecf8427e");
33 }
34
35 void MD5Tests::known_strings()
36 {
37         // Test suite from RFC 1321
38         const char *test_suite[] =
39         {
40                 "a", "0cc175b9c0f1b6a831c399e269772661",
41                 "abc", "900150983cd24fb0d6963f7d28e17f72",
42                 "message digest", "f96b697d7cb7938d525a2f31aaf161d0",
43                 "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
44                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f",
45                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a",
46                 0
47         };
48
49         for(unsigned i=0; test_suite[i]; i+=2)
50         {
51                 string digest = Crypto::MD5(test_suite[i]).get_hexdigest();
52                 expect_equal(digest, digest==test_suite[i+1], string("digest == \"")+test_suite[i+1]+"\"");
53         }
54
55         // This appears in Wikipedia
56         string digest = Crypto::MD5("The quick brown fox jumps over the lazy dog").get_hexdigest();
57         EXPECT_EQUAL(digest, "9e107d9d372bb6826bd81d3542a419d6");
58 }
59
60 void MD5Tests::size_corner_cases()
61 {
62         // 55 bytes; shortest possible padding
63         string digest = Crypto::MD5("0123456789abcdef0123456789abcdef0123456789abcdef0123456").get_hexdigest();
64         EXPECT_EQUAL(digest, "d8ea71eb4d2af27f59a5316c971065e6");
65
66         // 56 bytes; longest possible padding
67         digest = Crypto::MD5("0123456789abcdef0123456789abcdef0123456789abcdef01234567").get_hexdigest();
68         EXPECT_EQUAL(digest, "a68f061e81239660f6305195739ba7f0");
69
70         // 64 bytes; exactly one block
71         digest = Crypto::MD5("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef").get_hexdigest();
72         EXPECT_EQUAL(digest, "fe3a1ff59f3b89b2ad3d33f08984874b");
73 }
74
75 void MD5Tests::large_input()
76 {
77         static const char lorem_ipsum[] = "Lorem ipsum dolor sit amet, consectetur "
78                 "adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore "
79                 "magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
80                 "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute "
81                 "irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
82                 "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, "
83                 "sunt in culpa qui officia deserunt mollit anim id est laborum.";
84
85         string digest = Crypto::MD5(lorem_ipsum).get_hexdigest();
86         EXPECT_EQUAL(digest, "db89bb5ceab87f9c0fcc2ab36c189c2c");
87 }