krysalis-update/src/test/java/org/apache/depot/update/util/security HashCodeManagerTest.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:52 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/test/java/org/apache/depot/update/util/security
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/test/java/org/apache/depot/update/util/security

Added Files:
	HashCodeManagerTest.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: HashCodeManagerTest.java ---
/*
 * Copyright  2000-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.depot.update.util.security;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import junit.framework.TestCase;


/**
 * Tests for the HashHelper class
 *
 * @author mmay
 * @author $LastChangedBy$
 * @version $Rev$
 */
public class HashCodeManagerTest extends TestCase {
    private MessageDigest messageDigest;
    
    private int readBufferSize = (8 * 1024);
    
    /**
     * Creates a new HashHelperTest object.
     *
     * @param name Name des Tests
     */
    public HashCodeManagerTest(String name) {
        super(name);
    }

    /**
     * TODO Add JavaDoc
     *
     * @throws Exception TODO
     */
    public void setUp() throws Exception {
		this.messageDigest = MessageDigest.getInstance("MD5");
    }

    /**
     * TODO Add JavaDoc
     *
     * @throws Exception TODO
     */
    public void testGetDigest() throws Exception {
        File hashFile = new File("src/artifacts/test/org/apache/depot/update/util/security/test.txt");
        File metaFile = new File("src/artifacts/test/org/apache/depot/update/util/security/test.txt.md5");
                
        byte[] firstByteArray = HashCodeManager.getDigest(hashFile,  this.messageDigest, this.readBufferSize);
        byte[] sameByteArray = HashCodeManager.getDigest(hashFile, this.messageDigest, this.readBufferSize);
        byte[] otherByteArray = HashCodeManager.getDigest(metaFile, this.messageDigest, this.readBufferSize);

        /* ByteArrays from the same file should be equal */
        assertTrue(HashCodeManager.compareHashes(firstByteArray, sameByteArray));
        assertFalse(HashCodeManager.compareHashes(firstByteArray, otherByteArray));
    }
    
	public void testCompare() throws Exception {
		String first = "Hello World!";
		String snd =   "Hello again!";
		String third = "Hello again World";
		
		assertTrue("Hashes over same", HashCodeManager.compareHashes(first.getBytes(), first.getBytes()));
		assertFalse("Hashes over different", HashCodeManager.compareHashes(first.getBytes(), snd.getBytes()));
		assertFalse("Hashes over inserted", HashCodeManager.compareHashes(first.getBytes(), third.getBytes()));		
	}
	
	public void testCreateDigester() throws Exception {
		MessageDigest md = null;
		
		try {
			md = HashCodeManager.createDigester(null, null);						
		} catch (NoSuchProviderException nspex) {
			assertTrue("Can't create a null propvider", true);
		}
		
		assertEquals(HashCodeManager.DEFAULT_ALGORITHM, md.getAlgorithm());
		md = HashCodeManager.createDigester("SHA", null);
		assertEquals("Asked for SHA got SHA", "SHA", md.getAlgorithm());
		
		boolean success = false;

		try {
			md = HashCodeManager.createDigester("XYZ", null);			
	        success = false;	
		} catch (NoSuchProviderException nspex) {
			success = false;
		} catch (NoSuchAlgorithmException nsaex) {
			success = true;
		}
		
		assertTrue("Can't create an XYZ digest", success);		
		success = false;
		
		try {
			md = HashCodeManager.createDigester("MD5", "SomeProvider");			
	        success = false;	
		} catch (NoSuchProviderException nspex) {
			success = true;
		} catch (NoSuchAlgorithmException nsaex) {
			success = false;
		}
		
		assertTrue("Can't create an MD5 with some bogus provider", success);		
	}
			
	public void testReadHashFromFile() throws Exception {
        File metaFile = new File("src/artifacts/test/org/apache/depot/update/util/security/test.txt.md5");

		byte[] digest = HashCodeManager.readHashFromFile(metaFile);
		assertNotNull(digest);

		String digStr = HashCodeManager.createDigestString(digest);

		String someDigestString = new String("eee7ec3c1c7f6d63370e7f84511b316e"); 
		
		assertEquals(digStr, someDigestString);
	}
	
	/**
	 * For this test there are some prerequisites. We need one file (small) for which we can process 
	 * the test (generate the HashCode). Then we need the HashCode generated with a different Tool. 
	 * 
	 * It would be best to first test the reading of a HashCode from a file. We could use the same file
	 * for this. 
	 *
	 * @throws Exception
	 */
	/*public void testGenerateHash() throws Exception {
		 get classloader to load needed artifacts - should be done in a static way (utils class) 
		ClassLoader loader = this.getClass().getClassLoader();
		InputStream stream = loader.getResourceAsStream("test.xml");

		
		
//		GenerateHash(file, digest)
		
	}*/
	
	/**	
     *
     * @param args TODO	
     */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(HashCodeManagerTest.class);
    }
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/