mirror of https://github.com/apache/maven.git
o Added creation of SHA1 digests alongside MD5's
o Modified digest-file creation to be text files, not binaries. PR: MNG-311, MNG-287 git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163975 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d7e66db253
commit
8d19eb5e90
|
@ -18,7 +18,8 @@ package org.apache.maven.tools.repoclean.digest;
|
|||
*/
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.tools.repoclean.report.FileReporter;
|
||||
import org.apache.maven.tools.repoclean.report.ReportWriteException;
|
||||
import org.apache.maven.tools.repoclean.report.Reporter;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -33,22 +34,36 @@ public class ArtifactDigestVerifier
|
|||
public static final String ROLE = ArtifactDigestVerifier.class.getName();
|
||||
|
||||
private ArtifactDigestor artifactDigestor;
|
||||
|
||||
public void setArtifactDigestor(ArtifactDigestor artifactDigestor)
|
||||
{
|
||||
this.artifactDigestor = artifactDigestor;
|
||||
}
|
||||
|
||||
public void verifyDigest( Artifact artifact, File artifactTarget, FileReporter reporter, boolean reportOnly ) throws Exception
|
||||
public void verifyDigest( Artifact artifact, File artifactTarget, Reporter reporter, boolean reportOnly )
|
||||
throws ArtifactDigestException, ReportWriteException, IOException
|
||||
{
|
||||
verifyDigestFile( artifact, artifactTarget, reporter, reportOnly, ".md5", ArtifactDigestor.MD5 );
|
||||
|
||||
verifyDigestFile( artifact, artifactTarget, reporter, reportOnly, ".sha1", ArtifactDigestor.SHA );
|
||||
}
|
||||
|
||||
private void verifyDigestFile( Artifact artifact, File artifactTarget, Reporter reporter, boolean reportOnly,
|
||||
String digestExt, String digestAlgorithm )
|
||||
throws ArtifactDigestException, ReportWriteException, IOException
|
||||
{
|
||||
// create the digest source file from which to copy/verify.
|
||||
File digestSourceFile = new File( artifact.getFile() + ".md5" );
|
||||
File digestSourceFile = new File( artifact.getFile() + digestExt );
|
||||
|
||||
// create the digest target file from which to copy/create.
|
||||
File digestTargetFile = new File( artifactTarget + ".md5" );
|
||||
|
||||
File digestTargetFile = new File( artifactTarget + digestExt );
|
||||
|
||||
boolean verified = false;
|
||||
|
||||
// if the digest source file exists, then verify it.
|
||||
if ( digestSourceFile.exists() )
|
||||
{
|
||||
verified = artifactDigestor.verifyArtifactDigest( artifactTarget, digestTargetFile,
|
||||
ArtifactDigestor.MD5 );
|
||||
verified = artifactDigestor.verifyArtifactDigest( artifactTarget, digestTargetFile, digestAlgorithm );
|
||||
|
||||
if ( verified )
|
||||
{
|
||||
|
@ -61,7 +76,7 @@ public class ArtifactDigestVerifier
|
|||
catch ( IOException e )
|
||||
{
|
||||
reporter.error( "Cannot copy digest file for artifact[" + artifact.getId()
|
||||
+ "] from source to target.", e );
|
||||
+ "] from source to target for digest algorithm: \'" + digestAlgorithm + "\'.", e );
|
||||
|
||||
throw e;
|
||||
}
|
||||
|
@ -69,12 +84,12 @@ public class ArtifactDigestVerifier
|
|||
}
|
||||
else
|
||||
{
|
||||
reporter.warn( ".md5 for artifact[" + artifact.getId() + "] in target repository is wrong." );
|
||||
reporter.warn( digestExt + " for artifact[" + artifact.getId() + "] in target repository is wrong." );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
reporter.warn( ".md5 for artifact[" + artifact.getId() + "] is missing in source repository." );
|
||||
reporter.warn( digestExt + " for artifact[" + artifact.getId() + "] is missing in source repository." );
|
||||
}
|
||||
|
||||
// if the .md5 was missing or did not verify correctly, create a new one
|
||||
|
@ -83,7 +98,7 @@ public class ArtifactDigestVerifier
|
|||
{
|
||||
if ( !reportOnly )
|
||||
{
|
||||
artifactDigestor.createArtifactDigest( artifactTarget, digestTargetFile, ArtifactDigestor.MD5 );
|
||||
artifactDigestor.createArtifactDigest( artifactTarget, digestTargetFile, digestAlgorithm );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@ import org.codehaus.plexus.util.IOUtil;
|
|||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
@ -43,24 +43,24 @@ public class ArtifactDigestor
|
|||
public boolean verifyArtifactDigest( File artifactFile, File digestFile, String algorithm )
|
||||
throws ArtifactDigestException
|
||||
{
|
||||
if(artifactFile.exists() && digestFile.exists())
|
||||
if ( artifactFile.exists() && digestFile.exists() )
|
||||
{
|
||||
byte[] generatedDigest = generateArtifactDigest( artifactFile, algorithm );
|
||||
|
||||
|
||||
InputStream in = null;
|
||||
try
|
||||
{
|
||||
in = new FileInputStream( artifactFile );
|
||||
|
||||
|
||||
int digestLen = generatedDigest.length;
|
||||
int currentIdx = 0;
|
||||
|
||||
|
||||
boolean matched = true;
|
||||
|
||||
|
||||
int read = -1;
|
||||
while ( ( read = in.read() ) > -1 )
|
||||
{
|
||||
if(currentIdx >= digestLen || read != generatedDigest[currentIdx])
|
||||
if ( currentIdx >= digestLen || read != generatedDigest[currentIdx] )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -68,23 +68,25 @@ public class ArtifactDigestor
|
|||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new ArtifactDigestException("Cannot verify digest for artifact file: \'" + artifactFile + "\' against digest file: \'" + digestFile + "\' using algorithm: \'" + algorithm + "\'", e);
|
||||
throw new ArtifactDigestException( "Cannot verify digest for artifact file: \'" + artifactFile
|
||||
+ "\' against digest file: \'" + digestFile + "\' using algorithm: \'" + algorithm + "\'", e );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( in );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private byte[] generateArtifactDigest( File artifactFile, String algorithm ) throws ArtifactDigestException
|
||||
|
||||
public byte[] generateArtifactDigest( File artifactFile, String algorithm )
|
||||
throws ArtifactDigestException
|
||||
{
|
||||
MessageDigest digest = null;
|
||||
try
|
||||
|
@ -105,7 +107,7 @@ public class ArtifactDigestor
|
|||
int read = -1;
|
||||
while ( ( read = in.read( buffer ) ) > -1 )
|
||||
{
|
||||
digest.update(buffer, 0, read);
|
||||
digest.update( buffer, 0, read );
|
||||
}
|
||||
}
|
||||
catch ( IOException e )
|
||||
|
@ -120,14 +122,17 @@ public class ArtifactDigestor
|
|||
return digest.digest();
|
||||
}
|
||||
|
||||
private void writeDigestFile( File digestFile, byte[] digestData ) throws IOException
|
||||
private void writeDigestFile( File digestFile, byte[] digestData )
|
||||
throws IOException
|
||||
{
|
||||
OutputStream out = null;
|
||||
Writer out = null;
|
||||
try
|
||||
{
|
||||
out = new FileOutputStream( digestFile );
|
||||
out.write( digestData );
|
||||
out.flush();
|
||||
out = new FileWriter( digestFile );
|
||||
for ( int i = 0; i < digestData.length; i++ )
|
||||
{
|
||||
out.write( Integer.toHexString( digestData[i] ) );
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
@ -21,39 +21,63 @@ import java.net.URL;
|
|||
|
||||
public final class TestSupport
|
||||
{
|
||||
|
||||
|
||||
private static final String REPO_SUBDIR = "repo/";
|
||||
|
||||
private static final String REPO_MARKER = "repo-marker.txt";
|
||||
private static final int MY_PACKAGE_TRIM = TestSupport.class.getPackage().getName().length() + 1;
|
||||
|
||||
|
||||
private static final int PACKAGE_TRIM = TestSupport.class.getPackage().getName().length() + 1;
|
||||
|
||||
private TestSupport()
|
||||
{
|
||||
}
|
||||
|
||||
public static String getMyRepositoryPath(Object testInstance)
|
||||
|
||||
public static String getMyRepositoryPath( Object testInstance )
|
||||
{
|
||||
Class testClass = testInstance.getClass();
|
||||
|
||||
String myRepo = testClass.getName().substring(MY_PACKAGE_TRIM);
|
||||
|
||||
return getRepositoryPath(myRepo);
|
||||
|
||||
String myRepo = testClass.getName().substring( PACKAGE_TRIM );
|
||||
|
||||
return getRepositoryPath( myRepo );
|
||||
}
|
||||
|
||||
public static File getMyResource( Object testInstance, String relativePath )
|
||||
{
|
||||
Class testClass = testInstance.getClass();
|
||||
|
||||
String myPath = testClass.getName().substring( PACKAGE_TRIM );
|
||||
|
||||
String resource = myPath.replace( '.', '/' );
|
||||
|
||||
if ( !relativePath.startsWith( "/" ) )
|
||||
{
|
||||
resource += "/";
|
||||
}
|
||||
|
||||
resource += relativePath;
|
||||
|
||||
return getResource( resource );
|
||||
}
|
||||
|
||||
public static String getRepositoryPath( String relativePath )
|
||||
{
|
||||
String base = relativePath.replace('.', '/');
|
||||
|
||||
if(!base.endsWith("/"))
|
||||
String base = relativePath.replace( '.', '/' );
|
||||
|
||||
if ( !base.endsWith( "/" ) )
|
||||
{
|
||||
base += "/";
|
||||
}
|
||||
|
||||
|
||||
return getResource( base + REPO_SUBDIR + REPO_MARKER ).getParentFile().getAbsolutePath();
|
||||
}
|
||||
|
||||
public static File getResource( String relativePath )
|
||||
{
|
||||
ClassLoader cloader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
URL repoMarkerResource = cloader.getResource(base + REPO_MARKER);
|
||||
|
||||
File repoMarker = new File(repoMarkerResource.getPath()).getAbsoluteFile();
|
||||
|
||||
return repoMarker.getParentFile().getPath();
|
||||
URL resource = cloader.getResource( relativePath );
|
||||
|
||||
return new File( resource.getPath() ).getAbsoluteFile();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package org.apache.maven.tools.repoclean.digest;
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.DefaultArtifact;
|
||||
import org.apache.maven.tools.repoclean.TestSupport;
|
||||
import org.apache.maven.tools.repoclean.report.DummyReporter;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/*
|
||||
* Copyright 2001-2005 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.
|
||||
*/
|
||||
|
||||
public class ArtifactDigestVerifierTest
|
||||
extends PlexusTestCase
|
||||
{
|
||||
|
||||
public void testShouldWriteBothMD5AndSHA1DigestFiles() throws Exception
|
||||
{
|
||||
ArtifactDigestVerifier verifier = (ArtifactDigestVerifier) lookup( ArtifactDigestVerifier.ROLE );
|
||||
|
||||
Artifact artifact = new DefaultArtifact("testGroup", "testArtifact", "1.0", "jar");
|
||||
|
||||
File artifactFile = TestSupport.getResource("digest/ArtifactDigestorTest/digestFormatVerifyArtifact.jar");
|
||||
|
||||
artifact.setFile(artifactFile);
|
||||
|
||||
File tempFile = File.createTempFile("artifactDigestFileVerifyBase", "jar");
|
||||
|
||||
File md5 = new File(tempFile + ".md5");
|
||||
File sha1 = new File(tempFile + ".sha1");
|
||||
|
||||
System.out.println("[INFO] We expect warnings for missing source digest files here:");
|
||||
verifier.verifyDigest(artifact, tempFile, new DummyReporter(), false);
|
||||
System.out.println("[INFO] Target digest files should have been created.");
|
||||
|
||||
assertTrue(md5.exists());
|
||||
assertTrue(sha1.exists());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package org.apache.maven.tools.repoclean.digest;
|
||||
|
||||
import org.apache.maven.tools.repoclean.TestSupport;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.util.Arrays;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/*
|
||||
* Copyright 2001-2005 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.
|
||||
*/
|
||||
|
||||
public class ArtifactDigestorTest
|
||||
extends TestCase
|
||||
{
|
||||
|
||||
private static final String DIGEST_FORMAT_VERIFY_ARTIFACT = "digestFormatVerifyArtifact.jar";
|
||||
|
||||
public void testShouldWriteDigestFileInHexNotBinary() throws Exception
|
||||
{
|
||||
ArtifactDigestor digestor = new ArtifactDigestor();
|
||||
|
||||
File artifact = TestSupport.getMyResource(this, DIGEST_FORMAT_VERIFY_ARTIFACT);
|
||||
|
||||
byte[] rawDigest = digestor.generateArtifactDigest( artifact, ArtifactDigestor.MD5 );
|
||||
|
||||
StringBuffer rawConverted = new StringBuffer(rawDigest.length * 2);
|
||||
for ( int i = 0; i < rawDigest.length; i++ )
|
||||
{
|
||||
rawConverted.append(Integer.toHexString(rawDigest[i]));
|
||||
}
|
||||
|
||||
File digestFile = File.createTempFile("repoclean-artifactDigest-formatTest", ".md5");
|
||||
|
||||
digestor.createArtifactDigest( artifact, digestFile, ArtifactDigestor.MD5 );
|
||||
|
||||
FileReader reader = new FileReader(digestFile);
|
||||
StringBuffer written = new StringBuffer(rawDigest.length * 2);
|
||||
|
||||
char[] cbuf = new char[rawDigest.length * 2];
|
||||
int read = -1;
|
||||
|
||||
while((read = reader.read(cbuf)) > -1)
|
||||
{
|
||||
written.append(cbuf, 0, read);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
assertEquals(rawConverted.length(), written.length());
|
||||
|
||||
cbuf = new char[written.length()];
|
||||
char[] cbuf2 = new char[cbuf.length];
|
||||
|
||||
written.getChars(0, cbuf.length, cbuf, 0);
|
||||
rawConverted.getChars(0, cbuf2.length, cbuf2, 0);
|
||||
|
||||
assertTrue(Arrays.equals(cbuf, cbuf2));
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue