import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
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.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader;
import org.apache.maven.repository.converter.transaction.FileTransaction;
import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DigesterException;
import org.apache.maven.repository.reporting.ArtifactReporter;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.FileUtils;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
-import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
{
boolean result;
- try
- {
- result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
- "failure.incorrect.md5" );
- result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
- "failure.incorrect.sha1" );
- }
- catch ( NoSuchAlgorithmException e )
- {
- throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
- }
+ result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact,
+ "failure.incorrect.md5" );
+ result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
+ "failure.incorrect.sha1" );
return result;
}
ArtifactReporter reporter,
Artifact artifact,
String key )
- throws IOException, NoSuchAlgorithmException
+ throws IOException
{
boolean result = true;
if ( md5.exists() )
{
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 ) );
result = false;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DigesterException;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 );
}
- catch ( NoSuchAlgorithmException e )
+ catch ( DigesterException 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
{
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
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.IOUtil;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.Collections;
+import java.util.Enumeration;
import java.util.Iterator;
+import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
{
md5 = digester.createChecksum( artifactFile, "MD5" );
}
- catch ( FileNotFoundException e )
- {
- throw new RepositoryIndexException( "Unable to compute checksum.", e );
- }
- catch ( NoSuchAlgorithmException e )
- {
- throw new RepositoryIndexException( "Unable to compute checksum.", e );
- }
- catch ( IOException e )
+ catch ( DigesterException e )
{
throw new RepositoryIndexException( "Unable to compute checksum.", e );
}
import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
{
return digester.createChecksum( new File( file ), algorithm );
}
- catch ( FileNotFoundException e )
+ catch ( DigesterException e )
{
- throw new RepositoryIndexException( e.getMessage(), e );
- }
- catch ( IOException e )
- {
- throw new RepositoryIndexException( e.getMessage(), e );
- }
- catch ( NoSuchAlgorithmException e )
- {
- throw new RepositoryIndexException( e.getMessage(), e );
+ throw new RepositoryIndexException( "Failed to create checksum", e );
}
}
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
/**
* This class reports invalid and mismatched checksums of artifacts and metadata files.
{
try
{
- if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) )
- {
- reporter.addSuccess( artifact );
- }
- else
- {
- reporter.addFailure( artifact, checksumAlgorithm + " checksum does not match." );
- }
+ digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
+
+ reporter.addSuccess( artifact );
}
- catch ( NoSuchAlgorithmException e )
+ catch ( DigesterException e )
{
- reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
+ reporter.addFailure( artifact, e.getMessage() );
}
catch ( IOException e )
{
- reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
+ reporter.addFailure( artifact, "Read file error: " + e.getMessage() );
}
}
else
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DigesterException;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
/**
* This class reports invalid and mismatched checksums of artifacts and metadata files.
{
try
{
- if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) )
- {
- reporter.addSuccess( metadata );
- }
- else
- {
- reporter.addFailure( metadata, checksumAlgorithm + " checksum does not match." );
- }
+ digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
+
+ reporter.addSuccess( metadata );
}
- catch ( NoSuchAlgorithmException e )
+ catch ( DigesterException e )
{
- reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
+ reporter.addFailure( metadata, e.getMessage() );
}
catch ( IOException e )
{
- reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() );
+ reporter.addFailure( metadata, "Read file error: " + e.getMessage() );
}
}
else
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
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.RepositoryIndexException;
import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import java.io.File;
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
{
checksum = digester.createChecksum( artifact.getFile(), algorithm );
}
- catch ( IOException e )
- {
- throw new ReportProcessorException( "Failed to generate checksum", e );
- }
- catch ( NoSuchAlgorithmException e )
+ catch ( DigesterException e )
{
throw new ReportProcessorException( "Failed to generate checksum", e );
}
*/
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.IOUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.security.NoSuchAlgorithmException;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
* @param type The type of checksum file to be created.
*/
protected void createChecksumFile( String type )
- throws NoSuchAlgorithmException, IOException
+ throws DigesterException, IOException
{
//loop through the valid artifact names..
if ( "VALID".equals( type ) )
* @param type The type of checksum to be created. (Valid or invalid)
*/
protected void createMetadataFile( String type )
- throws NoSuchAlgorithmException, IOException
+ throws DigesterException, IOException
{
//loop through the valid artifact names..
if ( "VALID".equals( type ) )
* @param isValid Indicates whether the checksum to be created is valid or not.
*/
private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid )
- throws IOException, NoSuchAlgorithmException
+ throws IOException, DigesterException
{
//Initialize variables for creating jar files
String repoUrl = repository.getBasedir();
* @param isValid Indicates whether the checksum to be created is valid or not.
*/
private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid )
- throws IOException, NoSuchAlgorithmException
+ throws IOException, DigesterException
{
//create checksum for the metadata file..
String repoUrl = repository.getBasedir();
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.repository.digest.DigesterException;
import java.io.File;
import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
/**
* Test the ChecksumArtifactReporter when the checksum files are valid.
*/
public void testChecksumArtifactReporterSuccess()
- throws ReportProcessorException, NoSuchAlgorithmException, IOException
+ throws ReportProcessorException, IOException, DigesterException
{
createChecksumFile( "VALID" );
createChecksumFile( "INVALID" );
* The reporter should report 2 success validation.
*/
public void testChecksumMetadataReporterSuccess()
- throws ReportProcessorException, NoSuchAlgorithmException, IOException
+ throws ReportProcessorException, DigesterException, IOException
{
createMetadataFile( "VALID" );
createMetadataFile( "INVALID" );
* Test the conditional when the checksum files of the artifact & metadata do not exist.
*/
public void testChecksumFilesDoNotExist()
- throws ReportProcessorException, NoSuchAlgorithmException, IOException
+ throws ReportProcessorException, DigesterException, IOException
{
createChecksumFile( "VALID" );
createMetadataFile( "VALID" );
*/
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
private static final int BYTE_MASK = 0xFF;
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
{
byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
}
while ( numRead != -1 );
}
+ catch( IOException e )
+ {
+ throw new DigesterException( "Failed to read from file: " + file.getAbsolutePath(), e );
+ }
finally
{
IOUtil.close( fis );
return byteArrayToHexStr( digest.digest() );
}
- public boolean verifyChecksum( File file, String checksum, String algorithm )
- throws NoSuchAlgorithmException, IOException
+ public void verifyChecksum( File file, String checksum, String algorithm )
+ throws DigesterException
{
- boolean result = true;
-
String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
// Free-BSD / openssl
Matcher m =
String filename = m.group( 1 );
if ( !filename.equals( file.getName() ) )
{
- // TODO: provide better warning
- result = false;
+ throw new DigesterException( "Supplied checksum does not match checksum pattern" );
}
trimmedChecksum = m.group( 2 );
}
String filename = m.group( 2 );
if ( !filename.equals( file.getName() ) )
{
- // TODO: provide better warning
- result = false;
+ throw new DigesterException( "Supplied checksum does not match checksum pattern" );
}
trimmedChecksum = m.group( 1 );
}
}
- if ( result )
+ //Create checksum for jar file
+ String sum = createChecksum( file, algorithm );
+ if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
{
- //Create checksum for jar file
- String sum = createChecksum( file, algorithm );
- result = trimmedChecksum.toUpperCase().equals( sum.toUpperCase() );
+ throw new DigesterException( "Checksum failed" );
}
- return result;
}
/**
*/
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
/**
* Create a digest for a file.
String MD5 = "MD5";
String createChecksum( File file, String algorithm )
- throws FileNotFoundException, IOException, NoSuchAlgorithmException;
+ throws DigesterException;
- boolean verifyChecksum( File file, String checksum, String algorithm )
- throws NoSuchAlgorithmException, IOException;
+ void verifyChecksum( File file, String checksum, String algorithm )
+ throws DigesterException;
}
--- /dev/null
+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 );
+ }
+}
private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2";
public void testBareDigestFormat()
- throws NoSuchAlgorithmException, IOException
+ throws DigesterException, IOException
{
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()
- throws NoSuchAlgorithmException, IOException
+ throws IOException
{
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
- assertTrue( "test openssl format MD5",
- digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 ) );
- assertTrue( "test openssl format SHA1",
- digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 ) );
-
- assertTrue( "test freebsd format MD5",
- 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 ) );
-
- assertFalse( "test wrong filename", digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 ) );
- assertFalse( "test wrong sha1",
- digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 ) );
+ try
+ {
+ digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 );
+ }
+ catch ( DigesterException e )
+ {
+ fail( "OpenSSL MD5 format must not cause exception" );
+ }
+
+ try
+ {
+ digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 );
+ }
+ catch ( DigesterException e )
+ {
+ fail( "OpenSSL SHA1 format must not cause exception" );
+ }
+
+ try
+ {
+ 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()
throws NoSuchAlgorithmException, IOException
{
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
- assertTrue( "test GNU format MD5", digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 ) );
- assertTrue( "test GNU format SHA1", digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 ) );
-
- assertTrue( "test GNU text format MD5", digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 ) );
- assertTrue( "test GNU text format SHA1",
- digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 ) );
-
- assertFalse( "test wrong filename", digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 ) );
- assertFalse( "test wrong sha1", digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 ) );
+ try
+ {
+ digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 );
+ }
+ catch ( DigesterException e )
+ {
+ fail( "GNU format MD5 must not cause exception" );
+ }
+
+ try
+ {
+ digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 );
+ }
+ catch ( DigesterException e )
+ {
+ fail( "GNU format SHA1 must not cause exception" );
+ }
+
+ try
+ {
+ 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()
throws NoSuchAlgorithmException, IOException
{
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
- assertTrue( "test untrimmed GNU format SHA1",
- digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 ) );
+ try
+ {
+ digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 );
+ }
+ catch ( DigesterException e )
+ {
+ fail( "GNU untrimmed SHA1 must not cause exception" );
+ }
}
}