1 /* md5test.cc: test cases for the MD5 code
2  *
3  * Copyright (C) 2006 Olly Betts
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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <cstdlib>
23 #include <iostream>
24 #include <string>
25 
26 #include <cstdio>
27 
28 #include "md5wrap.h"
29 
30 using namespace std;
31 
32 struct testcase {
33     const char * string;
34     const char * hash;
35 };
36 
37 static testcase md5_testcases[] = {
38     { "", "d41d8cd98f00b204e9800998ecf8427e" },
39     { "test", "098f6bcd4621d373cade4e832627b4f6" },
40     { "\x80\x81\x82", "b385760a988b494d3f9df43456928176" },
41     { NULL, NULL }
42 };
43 
main()44 int main() {
45     string md5;
46     for (testcase * t = md5_testcases; t->string; ++t) {
47 	string hexhash;
48 	md5_string(t->string, md5);
49 	for (size_t i = 0; i < md5.size(); ++i) {
50 	    char buf[16];
51 	    sprintf(buf, "%02x", (unsigned char)md5[i]);
52 	    hexhash += buf;
53 	}
54 	if (hexhash != t->hash) {
55 	    cerr << "md5 of \"" << t->string << "\" should be \"" << t->hash << "\" not \"" << hexhash << "\"" << endl;
56 	    exit(1);
57 	}
58     }
59 }
60