001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.util; 017 018import static org.hamcrest.core.Is.is; 019import static org.hamcrest.core.IsNull.notNullValue; 020import static org.junit.Assert.assertThat; 021import java.io.IOException; 022import java.io.InputStream; 023import java.security.NoSuchAlgorithmException; 024import org.junit.Test; 025import org.modeshape.common.util.SecureHash.Algorithm; 026import org.modeshape.common.util.SecureHash.HashingInputStream; 027 028public class SecureHashTest { 029 030 @Test 031 public void shouldCorrectlyComputeSecureHashUsingMD2() throws Exception { 032 assertCorrectlyComputeSecureHashUsing(Algorithm.MD2); 033 } 034 035 @Test 036 public void shouldCorrectlyComputeSecureHashUsingMD5() throws Exception { 037 assertCorrectlyComputeSecureHashUsing(Algorithm.MD5); 038 } 039 040 @Test 041 public void shouldCorrectlyComputeSecureHashUsingSHA1() throws Exception { 042 assertCorrectlyComputeSecureHashUsing(Algorithm.SHA_1); 043 } 044 045 @Test 046 public void shouldCorrectlyComputeSecureHashUsingSHA256() throws Exception { 047 assertCorrectlyComputeSecureHashUsing(Algorithm.SHA_256); 048 } 049 050 @Test 051 public void shouldCorrectlyComputeSecureHashUsingSHA384() throws Exception { 052 assertCorrectlyComputeSecureHashUsing(Algorithm.SHA_384); 053 054 } 055 056 protected void assertCorrectlyComputeSecureHashUsing( Algorithm algorithm ) throws Exception { 057 assertSecureHashStreamWorks(algorithm, "/org/modeshape/common/i18n/I18nTest$TestI18n_en.properties"); 058 assertSecureHashStreamWorks(algorithm, "/org/modeshape/common/i18n/I18nTest$TestI18n_fr.properties"); 059 assertSecureHashStreamWorks(algorithm, "/org/modeshape/common/i18n/MockI18n.properties"); 060 assertSecureHashStreamWorks(algorithm, "/org/modeshape/common/util/additionalmime.types"); 061 assertSecureHashStreamWorks(algorithm, "/org/modeshape/common/logging/LoggerTest.properties"); 062 assertSecureHashStreamWorks(algorithm, "/log4j.properties"); 063 assertSecureHashStreamWorks(algorithm, "/maven-metadata-repository.jboss.org.xml"); 064 } 065 066 protected void assertSecureHashStreamWorks( Algorithm algorithm, 067 String resourceName ) throws IOException, NoSuchAlgorithmException { 068 // Find the content of the file ... 069 InputStream stream = getClass().getResourceAsStream(resourceName); 070 assertThat(stream, is(notNullValue())); 071 byte[] bytesThruStream = IoUtil.readBytes(stream); 072 073 // Find the secure hash of the file ... 074 stream = getClass().getResourceAsStream(resourceName); 075 assertThat(stream, is(notNullValue())); 076 byte[] hashThruStream = null; 077 try { 078 hashThruStream = SecureHash.getHash(algorithm, stream); 079 } finally { 080 stream.close(); 081 } 082 083 // Now try reading the stream using a hash stream ... 084 stream = getClass().getResourceAsStream(resourceName); 085 assertThat(stream, is(notNullValue())); 086 HashingInputStream hashingStream = SecureHash.createHashingStream(algorithm, stream); 087 byte[] bytesThruHashingStream = IoUtil.readBytes(hashingStream); // closes stream 088 byte[] hashThruHashingStream = hashingStream.getHash(); 089 090 // The content should be the same .. 091 assertThat(bytesThruHashingStream, is(bytesThruStream)); 092 093 // The hash should also be the same ... 094 assertThat(hashThruHashingStream, is(hashThruStream)); 095 096 // System.out.println(algorithm.digestName() + "---> " + hashingStream.getHashAsHexString() + " of " + resourceName); 097 } 098 099}