01: /*
02: * Copyright (C) 2004-2007 Stephen Ostermiller
03: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
04: *
05: * This program is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: *
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: * See COPYING.TXT for details.
16: */
17: package com.Ostermiller.util;
18:
19: /**
20: * MD5 regression test.
21: */
22: class MD5Tests {
23:
24: private static class TestCase {
25: private String md5;
26: private byte[] bytes;
27:
28: /**
29: * Test case
30: * @param md5 desired test result
31: * @param bytes bytes to which apply md5 and compare to the test result
32: */
33: private TestCase(String md5, byte[] bytes) {
34: this .md5 = md5;
35: this .bytes = bytes;
36: }
37:
38: private void test() throws Exception {
39: String hashString = MD5.getHashString(bytes);
40: if (!md5.equals(hashString)) {
41: throw new Exception("Failed test. Should be " + md5
42: + " was " + hashString + ".");
43: }
44: }
45: }
46:
47: private static final TestCase[] testCases = new TestCase[] {
48: new TestCase("d41d8cd98f00b204e9800998ecf8427e",
49: new byte[] {}),
50: new TestCase("0cc175b9c0f1b6a831c399e269772661",
51: new byte[] { 'a' }),
52: new TestCase("900150983cd24fb0d6963f7d28e17f72",
53: new byte[] { 'a', 'b', 'c' }),
54: new TestCase("f96b697d7cb7938d525a2f31aaf161d0",
55: new byte[] { 'm', 'e', 's', 's', 'a', 'g', 'e',
56: ' ', 'd', 'i', 'g', 'e', 's', 't' }),
57: new TestCase("c3fcd3d76192e4007dfb496cca67e13b",
58: new byte[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
59: 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
60: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
61: 'x', 'y', 'z' }),
62: new TestCase("d174ab98d277d9f5a5611c2c9f419d9f",
63: new byte[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
64: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
65: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
66: 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e',
67: 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
68: 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
69: 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
70: '3', '4', '5', '6', '7', '8', '9' }),
71: new TestCase(
72: "57edf4a22be3c955ac49da2e2107b67a",
73: new byte[] { '1', '2', '3', '4', '5', '6', '7',
74: '8', '9', '0', '1', '2', '3', '4', '5',
75: '6', '7', '8', '9', '0', '1', '2', '3',
76: '4', '5', '6', '7', '8', '9', '0', '1',
77: '2', '3', '4', '5', '6', '7', '8', '9',
78: '0', '1', '2', '3', '4', '5', '6', '7',
79: '8', '9', '0', '1', '2', '3', '4', '5',
80: '6', '7', '8', '9', '0', '1', '2', '3',
81: '4', '5', '6', '7', '8', '9', '0', '1',
82: '2', '3', '4', '5', '6', '7', '8', '9', '0' }), };
83:
84: /**
85: * Main method for regression test
86: * @param args command line arguments (ignored)
87: */
88: public static void main(String[] args) {
89: try {
90: for (int i = 0; i < testCases.length; i++) {
91: testCases[i].test();
92: }
93: } catch (Exception x) {
94: x.printStackTrace(System.err);
95: System.exit(1);
96: }
97: System.exit(0);
98: }
99: }
|