Changed boolean Digester methods to throw an exception instead for a more informative failure reason

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@414745 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2006-06-16 06:01:50 +00:00
parent 565f3f2844
commit 1e5e969496
13 changed files with 260 additions and 140 deletions

View File

@ -19,8 +19,6 @@ package org.apache.maven.repository.converter;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
@ -39,6 +37,7 @@ import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
import org.apache.maven.repository.converter.transaction.FileTransaction; import org.apache.maven.repository.converter.transaction.FileTransaction;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.apache.maven.repository.reporting.ArtifactReporter; import org.apache.maven.repository.reporting.ArtifactReporter;
import org.codehaus.plexus.i18n.I18N; import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
@ -51,7 +50,6 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -612,17 +610,10 @@ public class DefaultRepositoryConverter
{ {
boolean result; boolean result;
try
{
result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact, result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
"failure.incorrect.md5" ); "failure.incorrect.md5" );
result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact, result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
"failure.incorrect.sha1" ); "failure.incorrect.sha1" );
}
catch ( NoSuchAlgorithmException e )
{
throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
}
return result; return result;
} }
@ -632,7 +623,7 @@ public class DefaultRepositoryConverter
ArtifactReporter reporter, ArtifactReporter reporter,
Artifact artifact, Artifact artifact,
String key ) String key )
throws IOException, NoSuchAlgorithmException throws IOException
{ {
boolean result = true; boolean result = true;
@ -640,7 +631,11 @@ public class DefaultRepositoryConverter
if ( md5.exists() ) if ( md5.exists() )
{ {
String checksum = FileUtils.fileRead( md5 ); String checksum = FileUtils.fileRead( md5 );
if ( !digester.verifyChecksum( file, checksum, algorithm ) ) try
{
digester.verifyChecksum( file, checksum, algorithm );
}
catch ( DigesterException e )
{ {
reporter.addFailure( artifact, getI18NString( key ) ); reporter.addFailure( artifact, getI18NString( key ) );
result = false; result = false;

View File

@ -22,11 +22,10 @@ import org.apache.lucene.index.Term;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
@ -158,18 +157,10 @@ public class ArtifactRepositoryIndex
sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 ); md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 );
} }
catch ( NoSuchAlgorithmException e ) catch ( DigesterException e )
{ {
throw new RepositoryIndexException( "Unable to create a checksum", e ); throw new RepositoryIndexException( "Unable to create a checksum", e );
} }
catch ( FileNotFoundException e )
{
throw new RepositoryIndexException( "Error reading from artifact file", e );
}
catch ( IOException e )
{
throw new RepositoryIndexException( "Error reading from artifact file", e );
}
try try
{ {

View File

@ -26,21 +26,20 @@ import org.apache.lucene.document.Field;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException; import java.util.zip.ZipException;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -150,15 +149,7 @@ public class EclipseRepositoryIndex
{ {
md5 = digester.createChecksum( artifactFile, "MD5" ); md5 = digester.createChecksum( artifactFile, "MD5" );
} }
catch ( FileNotFoundException e ) catch ( DigesterException e )
{
throw new RepositoryIndexException( "Unable to compute checksum.", e );
}
catch ( NoSuchAlgorithmException e )
{
throw new RepositoryIndexException( "Unable to compute checksum.", e );
}
catch ( IOException e )
{ {
throw new RepositoryIndexException( "Unable to compute checksum.", e ); throw new RepositoryIndexException( "Unable to compute checksum.", e );
} }

View File

@ -28,12 +28,11 @@ import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin; import org.apache.maven.model.ReportPlugin;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
@ -310,17 +309,9 @@ public class PomRepositoryIndex
{ {
return digester.createChecksum( new File( file ), algorithm ); return digester.createChecksum( new File( file ), algorithm );
} }
catch ( FileNotFoundException e ) catch ( DigesterException e )
{ {
throw new RepositoryIndexException( e.getMessage(), e ); throw new RepositoryIndexException( "Failed to create checksum", e );
}
catch ( IOException e )
{
throw new RepositoryIndexException( e.getMessage(), e );
}
catch ( NoSuchAlgorithmException e )
{
throw new RepositoryIndexException( e.getMessage(), e );
} }
} }

View File

@ -20,11 +20,11 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
/** /**
* This class reports invalid and mismatched checksums of artifacts and metadata files. * This class reports invalid and mismatched checksums of artifacts and metadata files.
@ -74,22 +74,17 @@ public class ChecksumArtifactReporter
{ {
try try
{ {
if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) ) digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
{
reporter.addSuccess( artifact ); reporter.addSuccess( artifact );
} }
else catch ( DigesterException e )
{ {
reporter.addFailure( artifact, checksumAlgorithm + " checksum does not match." ); reporter.addFailure( artifact, e.getMessage() );
}
}
catch ( NoSuchAlgorithmException e )
{
reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
} }
catch ( IOException e ) catch ( IOException e )
{ {
reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); reporter.addFailure( artifact, "Read file error: " + e.getMessage() );
} }
} }
else else

View File

@ -19,11 +19,11 @@ package org.apache.maven.repository.reporting;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
/** /**
* This class reports invalid and mismatched checksums of artifacts and metadata files. * This class reports invalid and mismatched checksums of artifacts and metadata files.
@ -69,22 +69,17 @@ public class ChecksumMetadataReporter
{ {
try try
{ {
if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) ) digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
{
reporter.addSuccess( metadata ); reporter.addSuccess( metadata );
} }
else catch ( DigesterException e )
{ {
reporter.addFailure( metadata, checksumAlgorithm + " checksum does not match." ); reporter.addFailure( metadata, e.getMessage() );
}
}
catch ( NoSuchAlgorithmException e )
{
reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
} }
catch ( IOException e ) catch ( IOException e )
{ {
reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); reporter.addFailure( metadata, "Read file error: " + e.getMessage() );
} }
} }
else else

View File

@ -20,6 +20,7 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.apache.maven.repository.indexing.RepositoryIndex; import org.apache.maven.repository.indexing.RepositoryIndex;
import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchException; import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
@ -30,8 +31,6 @@ import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -88,11 +87,7 @@ public class DuplicateArtifactFileReportProcessor
{ {
checksum = digester.createChecksum( artifact.getFile(), algorithm ); checksum = digester.createChecksum( artifact.getFile(), algorithm );
} }
catch ( IOException e ) catch ( DigesterException e )
{
throw new ReportProcessorException( "Failed to generate checksum", e );
}
catch ( NoSuchAlgorithmException e )
{ {
throw new ReportProcessorException( "Failed to generate checksum", e ); throw new ReportProcessorException( "Failed to generate checksum", e );
} }

View File

@ -17,6 +17,7 @@ package org.apache.maven.repository.reporting;
*/ */
import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
@ -28,7 +29,6 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.security.NoSuchAlgorithmException;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
@ -61,7 +61,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase
* @param type The type of checksum file to be created. * @param type The type of checksum file to be created.
*/ */
protected void createChecksumFile( String type ) protected void createChecksumFile( String type )
throws NoSuchAlgorithmException, IOException throws DigesterException, IOException
{ {
//loop through the valid artifact names.. //loop through the valid artifact names..
if ( "VALID".equals( type ) ) if ( "VALID".equals( type ) )
@ -86,7 +86,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase
* @param type The type of checksum to be created. (Valid or invalid) * @param type The type of checksum to be created. (Valid or invalid)
*/ */
protected void createMetadataFile( String type ) protected void createMetadataFile( String type )
throws NoSuchAlgorithmException, IOException throws DigesterException, IOException
{ {
//loop through the valid artifact names.. //loop through the valid artifact names..
if ( "VALID".equals( type ) ) if ( "VALID".equals( type ) )
@ -110,7 +110,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase
* @param isValid Indicates whether the checksum to be created is valid or not. * @param isValid Indicates whether the checksum to be created is valid or not.
*/ */
private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid ) private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid )
throws IOException, NoSuchAlgorithmException throws IOException, DigesterException
{ {
//Initialize variables for creating jar files //Initialize variables for creating jar files
String repoUrl = repository.getBasedir(); String repoUrl = repository.getBasedir();
@ -177,7 +177,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase
* @param isValid Indicates whether the checksum to be created is valid or not. * @param isValid Indicates whether the checksum to be created is valid or not.
*/ */
private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid ) private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid )
throws IOException, NoSuchAlgorithmException throws IOException, DigesterException
{ {
//create checksum for the metadata file.. //create checksum for the metadata file..
String repoUrl = repository.getBasedir(); String repoUrl = repository.getBasedir();

View File

@ -25,10 +25,10 @@ import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.repository.digest.DigesterException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator; import java.util.Iterator;
/** /**
@ -56,7 +56,7 @@ public class ChecksumArtifactReporterTest
* Test the ChecksumArtifactReporter when the checksum files are valid. * Test the ChecksumArtifactReporter when the checksum files are valid.
*/ */
public void testChecksumArtifactReporterSuccess() public void testChecksumArtifactReporterSuccess()
throws ReportProcessorException, NoSuchAlgorithmException, IOException throws ReportProcessorException, IOException, DigesterException
{ {
createChecksumFile( "VALID" ); createChecksumFile( "VALID" );
createChecksumFile( "INVALID" ); createChecksumFile( "INVALID" );
@ -90,7 +90,7 @@ public class ChecksumArtifactReporterTest
* The reporter should report 2 success validation. * The reporter should report 2 success validation.
*/ */
public void testChecksumMetadataReporterSuccess() public void testChecksumMetadataReporterSuccess()
throws ReportProcessorException, NoSuchAlgorithmException, IOException throws ReportProcessorException, DigesterException, IOException
{ {
createMetadataFile( "VALID" ); createMetadataFile( "VALID" );
createMetadataFile( "INVALID" ); createMetadataFile( "INVALID" );
@ -195,7 +195,7 @@ public class ChecksumArtifactReporterTest
* Test the conditional when the checksum files of the artifact & metadata do not exist. * Test the conditional when the checksum files of the artifact & metadata do not exist.
*/ */
public void testChecksumFilesDoNotExist() public void testChecksumFilesDoNotExist()
throws ReportProcessorException, NoSuchAlgorithmException, IOException throws ReportProcessorException, DigesterException, IOException
{ {
createChecksumFile( "VALID" ); createChecksumFile( "VALID" );
createMetadataFile( "VALID" ); createMetadataFile( "VALID" );

View File

@ -17,11 +17,13 @@ package org.apache.maven.repository.digest;
*/ */
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -41,11 +43,28 @@ public class DefaultDigester
private static final int BYTE_MASK = 0xFF; private static final int BYTE_MASK = 0xFF;
public String createChecksum( File file, String algorithm ) public String createChecksum( File file, String algorithm )
throws IOException, NoSuchAlgorithmException throws DigesterException
{ {
MessageDigest digest = MessageDigest.getInstance( algorithm ); MessageDigest digest;
try
{
digest = MessageDigest.getInstance( algorithm );
}
catch ( NoSuchAlgorithmException e )
{
throw new DigesterException( "Specified algorithm not found: " + algorithm, e );
}
InputStream fis = null;
try
{
fis = new FileInputStream( file );
}
catch ( FileNotFoundException e )
{
throw new DigesterException( "Specified file not found: " + file.getAbsolutePath(), e );
}
InputStream fis = new FileInputStream( file );
try try
{ {
byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE]; byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
@ -60,6 +79,10 @@ public class DefaultDigester
} }
while ( numRead != -1 ); while ( numRead != -1 );
} }
catch( IOException e )
{
throw new DigesterException( "Failed to read from file: " + file.getAbsolutePath(), e );
}
finally finally
{ {
IOUtil.close( fis ); IOUtil.close( fis );
@ -68,11 +91,9 @@ public class DefaultDigester
return byteArrayToHexStr( digest.digest() ); return byteArrayToHexStr( digest.digest() );
} }
public boolean verifyChecksum( File file, String checksum, String algorithm ) public void verifyChecksum( File file, String checksum, String algorithm )
throws NoSuchAlgorithmException, IOException throws DigesterException
{ {
boolean result = true;
String trimmedChecksum = checksum.replace( '\n', ' ' ).trim(); String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
// Free-BSD / openssl // Free-BSD / openssl
Matcher m = Matcher m =
@ -83,8 +104,7 @@ public class DefaultDigester
String filename = m.group( 1 ); String filename = m.group( 1 );
if ( !filename.equals( file.getName() ) ) if ( !filename.equals( file.getName() ) )
{ {
// TODO: provide better warning throw new DigesterException( "Supplied checksum does not match checksum pattern" );
result = false;
} }
trimmedChecksum = m.group( 2 ); trimmedChecksum = m.group( 2 );
} }
@ -97,20 +117,18 @@ public class DefaultDigester
String filename = m.group( 2 ); String filename = m.group( 2 );
if ( !filename.equals( file.getName() ) ) if ( !filename.equals( file.getName() ) )
{ {
// TODO: provide better warning throw new DigesterException( "Supplied checksum does not match checksum pattern" );
result = false;
} }
trimmedChecksum = m.group( 1 ); trimmedChecksum = m.group( 1 );
} }
} }
if ( result )
{
//Create checksum for jar file //Create checksum for jar file
String sum = createChecksum( file, algorithm ); String sum = createChecksum( file, algorithm );
result = trimmedChecksum.toUpperCase().equals( sum.toUpperCase() ); if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
{
throw new DigesterException( "Checksum failed" );
} }
return result;
} }
/** /**

View File

@ -17,9 +17,6 @@ package org.apache.maven.repository.digest;
*/ */
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
/** /**
* Create a digest for a file. * Create a digest for a file.
@ -35,8 +32,8 @@ public interface Digester
String MD5 = "MD5"; String MD5 = "MD5";
String createChecksum( File file, String algorithm ) String createChecksum( File file, String algorithm )
throws FileNotFoundException, IOException, NoSuchAlgorithmException; throws DigesterException;
boolean verifyChecksum( File file, String checksum, String algorithm ) void verifyChecksum( File file, String checksum, String algorithm )
throws NoSuchAlgorithmException, IOException; throws DigesterException;
} }

View File

@ -0,0 +1,34 @@
package org.apache.maven.repository.digest;
/*
* Copyright 2005-2006 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.
*/
/**
* @author Edwin Punzalan
*/
public class DigesterException
extends Exception
{
public DigesterException( String message )
{
super( message );
}
public DigesterException( String message, Throwable cause )
{
super( message, cause );
}
}

View File

@ -39,54 +39,172 @@ public class DigesterTest
private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2"; private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2";
public void testBareDigestFormat() public void testBareDigestFormat()
throws NoSuchAlgorithmException, IOException throws DigesterException, IOException
{ {
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
assertTrue( "test bare format MD5", digester.verifyChecksum( file, MD5, Digester.MD5 ) );
assertTrue( "test bare format SHA1", digester.verifyChecksum( file, SHA1, Digester.SHA1 ) );
assertFalse( "test wrong sha1", digester.verifyChecksum( file, WRONG_SHA1, Digester.SHA1 ) ); try
{
digester.verifyChecksum( file, MD5, Digester.MD5 );
}
catch ( DigesterException e )
{
fail( "Bare format MD5 must not throw exception" );
}
try
{
digester.verifyChecksum( file, SHA1, Digester.SHA1 );
}
catch ( DigesterException e )
{
fail( "Bare format SHA1 must not throw exception" );
}
try
{
digester.verifyChecksum( file, WRONG_SHA1, Digester.SHA1 );
fail( "wrong checksum must throw an exception" );
}
catch ( DigesterException e )
{
//expected
}
} }
public void testOpensslDigestFormat() public void testOpensslDigestFormat()
throws NoSuchAlgorithmException, IOException throws IOException
{ {
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
assertTrue( "test openssl format MD5", try
digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 ) ); {
assertTrue( "test openssl format SHA1", digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 );
digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 ) ); }
catch ( DigesterException e )
{
fail( "OpenSSL MD5 format must not cause exception" );
}
assertTrue( "test freebsd format MD5", try
digester.verifyChecksum( file, "MD5 (test-file.txt) = " + MD5, Digester.MD5 ) ); {
assertTrue( "test freebsd format SHA1", digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 );
digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + SHA1, Digester.SHA1 ) ); }
catch ( DigesterException e )
{
fail( "OpenSSL SHA1 format must not cause exception" );
}
assertFalse( "test wrong filename", digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 ) ); try
assertFalse( "test wrong sha1", {
digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 ) ); digester.verifyChecksum( file, "MD5 (test-file.txt) = " + MD5, Digester.MD5 );
}
catch ( DigesterException e )
{
fail( "FreeBSD MD5 format must not cause exception" );
}
try
{
digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + SHA1, Digester.SHA1 );
}
catch ( DigesterException e )
{
fail( "FreeBSD SHA1 format must not cause exception" );
}
try
{
digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 );
fail( "Wrong filename should cause an exception" );
}
catch ( DigesterException e )
{
//expected
}
try
{
digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 );
fail( "Wrong sha1 should cause an exception" );
}
catch ( DigesterException e )
{
//expected
}
} }
public void testGnuDigestFormat() public void testGnuDigestFormat()
throws NoSuchAlgorithmException, IOException throws NoSuchAlgorithmException, IOException
{ {
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
assertTrue( "test GNU format MD5", digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 ) ); try
assertTrue( "test GNU format SHA1", digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 ) ); {
digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 );
}
catch ( DigesterException e )
{
fail( "GNU format MD5 must not cause exception" );
}
assertTrue( "test GNU text format MD5", digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 ) ); try
assertTrue( "test GNU text format SHA1", {
digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 ) ); digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 );
}
catch ( DigesterException e )
{
fail( "GNU format SHA1 must not cause exception" );
}
assertFalse( "test wrong filename", digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 ) ); try
assertFalse( "test wrong sha1", digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 ) ); {
digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 );
}
catch ( DigesterException e )
{
fail( "GNU text format MD5 must not cause exception" );
}
try
{
digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 );
}
catch ( DigesterException e )
{
fail( "GNU text format SHA1 must not cause exception" );
}
try
{
digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 );
fail( "Wrong filename cause an exception" );
}
catch ( DigesterException e )
{
//expected
}
try
{
digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 );
fail( "Wrong SHA1 cause an exception" );
}
catch ( DigesterException e )
{
//expected
}
} }
public void testUntrimmedContent() public void testUntrimmedContent()
throws NoSuchAlgorithmException, IOException throws NoSuchAlgorithmException, IOException
{ {
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
assertTrue( "test untrimmed GNU format SHA1", try
digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 ) ); {
digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 );
}
catch ( DigesterException e )
{
fail( "GNU untrimmed SHA1 must not cause exception" );
}
} }
} }