package test.hash; // ---------------------------------------------------------------------------- // $Id: $ // // Copyright (C) 2002, Free Software Foundation, Inc. // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License or (at your option) any // later version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details. // // You should have received a copy of the GNU General Public License along with // this program; see the file COPYING. If not, write to the // // Free Software Foundation Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307 // USA // // As a special exception, if you link this library with other files to produce // an executable, this library does not by itself cause the resulting // executable to be covered by the GNU General Public License. This exception // does not however invalidate any other reasons why the executable file might // be covered by the GNU General Public License. // ---------------------------------------------------------------------------- import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner; import gnu.crypto.hash.IMessageDigest; import gnu.crypto.hash.MD2; import gnu.crypto.util.Util; /** *

Conformance tests for the MD2 implementation.

* * @version $Revision: $ */ public class TestOfMD2 extends TestCase { // Constants and variables // ------------------------------------------------------------------------- /** The local reference to an MD2. */ private IMessageDigest algorithm; // Constructor(s) // ------------------------------------------------------------------------- public TestOfMD2(String name) { super(name); } // Class methods // ------------------------------------------------------------------------- public static void main(String[] args) { TestRunner.run(suite()); } public static Test suite() { return new TestSuite(TestOfMD2.class); } // Instance methods // ------------------------------------------------------------------------- public void testSelfTest() { try { assertTrue("selfTest()", algorithm.selfTest()); } catch (Exception x) { fail("selfTest(): "+String.valueOf(x)); } } public void testA() { try { algorithm.update("a".getBytes(), 0, 1); byte[] md = algorithm.digest(); String exp = "32EC01EC4A6DAC72C0AB96FB34C0B5D1"; assertTrue("testA()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testA(): "+String.valueOf(x)); } } public void testABC() { try { algorithm.update("abc".getBytes(), 0, 3); byte[] md = algorithm.digest(); String exp = "DA853B0D3F88D99B30283A69E6DED6BB"; assertTrue("testABC()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testABC(): "+String.valueOf(x)); } } public void testMessageDigest() { try { algorithm.update("message digest".getBytes(), 0, 14); byte[] md = algorithm.digest(); String exp = "AB4F496BFB2A530B219FF33031FE06B0"; assertTrue("testMessageDigest()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testMessageDigest(): "+String.valueOf(x)); } } public void testAlphabet() { try { algorithm.update("abcdefghijklmnopqrstuvwxyz".getBytes(), 0, 26); byte[] md = algorithm.digest(); String exp = "4E8DDFF3650292AB5A4108C3AA47940B"; assertTrue("testAlphabet()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testAlphabet(): "+String.valueOf(x)); } } public void testAsciiSubset() { try { algorithm.update("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".getBytes(), 0, 62); byte[] md = algorithm.digest(); String exp = "DA33DEF2A42DF13975352846C30338CD"; assertTrue("testAsciiSubset()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testAsciiSubset(): "+String.valueOf(x)); } } public void testEightyNumerics() { try { algorithm.update("12345678901234567890123456789012345678901234567890123456789012345678901234567890".getBytes(), 0, 80); byte[] md = algorithm.digest(); String exp = "D5976F79D83D3A0DC9806C3C66F3EFD8"; assertTrue("testEightyNumerics()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testEightyNumerics(): "+String.valueOf(x)); } } public void testCloning() { try { algorithm.update("a".getBytes(), 0, 1); IMessageDigest clone = (IMessageDigest) algorithm.clone(); byte[] md = algorithm.digest(); String exp = "32EC01EC4A6DAC72C0AB96FB34C0B5D1"; assertTrue("testCloning()", exp.equals(Util.toString(md))); clone.update("bc".getBytes(), 0, 2); md = clone.digest(); exp = "DA853B0D3F88D99B30283A69E6DED6BB"; assertTrue("testABC()", exp.equals(Util.toString(md))); } catch (Exception x) { fail("testCloning(): "+String.valueOf(x)); } } // over-ridden methods from junit.framework.TestCase ----------------------- protected void setUp() throws Exception { algorithm = new MD2(); } }