Author: krosenvold Date: Wed Jul 31 05:36:36 2013 New Revision: 1508730 URL: http://svn.apache.org/r1508730 Log: Added missing files
Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java?rev=1508730&r1=1508729&r2=1508730&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java Wed Jul 31 05:36:36 2013 @@ -187,7 +187,7 @@ public abstract class AbstractInstallMoj { String checksum; getLog().debug( "Calculating " + digester.getAlgorithm() + " checksum for " + originalFile ); - checksum = digester.calc(originalFile); + checksum = digester.calculate( originalFile ); File checksumFile = new File( installedFile.getAbsolutePath() + ext ); getLog().debug( "Installing checksum to " + checksumFile ); Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java?rev=1508730&r1=1508729&r2=1508730&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java Wed Jul 31 05:36:36 2013 @@ -22,10 +22,13 @@ package org.apache.maven.plugin.install; import org.apache.commons.codec.binary.Hex; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author Kristian Rosenvold @@ -59,7 +62,7 @@ public class SimpleDigester { return messageDigest.getAlgorithm(); } - public String calc( File file ) throws MojoExecutionException { + public String calculate( File file ) throws MojoExecutionException { FileInputStream fis = null; BufferedInputStream bis = null; @@ -93,4 +96,54 @@ public class SimpleDigester { size = is.read( buffer, 0, bufsize ); } } + + public void verify( File file, String checksum ) + throws MojoExecutionException + { + String trimmed = cleanChecksum( checksum, messageDigest.getAlgorithm(), file.getName() ); + String sum = calculate( file ); + if ( !StringUtils.equalsIgnoreCase( trimmed, sum ) ) + { + throw new RuntimeException( "Checksum failed (expected=" + trimmed + ", actual=" + sum + ")" ); + } + } + + private static String cleanChecksum( String checksum, String algorithm, String path ) + { + String trimmed = checksum.replace( '\n', ' ' ).trim(); + + // Free-BSD / openssl + String regex = algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-fA-F0-9]+)"; + Matcher m = Pattern.compile( regex ).matcher( trimmed ); + if ( m.matches() ) + { + String filename = m.group( 1 ); + if ( !isValidChecksumPattern( filename, path ) ) + { + throw new RuntimeException( "Supplied checksum does not match checksum pattern" ); + } + trimmed = m.group( 2 ); + } + else + { + // GNU tools + m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmed ); + if ( m.matches() ) + { + String filename = m.group( 2 ); + if ( !isValidChecksumPattern( filename, path ) ) + { + throw new RuntimeException( "Supplied checksum does not match checksum pattern" ); + } + trimmed = m.group( 1 ); + } + } + return trimmed; + } + + private static boolean isValidChecksumPattern( String filename, String path ) + { + return filename.endsWith( path ) || filename.equals("-"); + } + } Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java?rev=1508730&r1=1508729&r2=1508730&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java Wed Jul 31 05:36:36 2013 @@ -249,8 +249,8 @@ public class InstallFileMojoTest mojo.execute(); //get the actual checksum of the artifact - String actualMd5Sum = mojo.md5Digester.calc( file ); - String actualSha1Sum = mojo.sha1Digester.calc( file ); + String actualMd5Sum = mojo.md5Digester.calculate( file ); + String actualSha1Sum = mojo.sha1Digester.calculate( file ); String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version; Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java?rev=1508730&r1=1508729&r2=1508730&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java Wed Jul 31 05:36:36 2013 @@ -29,7 +29,6 @@ import org.apache.maven.project.MavenPro import org.apache.maven.shared.utils.io.FileUtils; import java.io.File; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -249,12 +248,12 @@ public class InstallMojoTest assertTrue( pom.exists() ); //get the actual checksum of the pom - String actualPomMd5Sum = mojo.md5Digester.calc( pom ); - String actualPomSha1Sum = mojo.sha1Digester.calc( pom ); + String actualPomMd5Sum = mojo.md5Digester.calculate( pom ); + String actualPomSha1Sum = mojo.sha1Digester.calculate( pom ); //get the actual checksum of the artifact - String actualMd5Sum = mojo.md5Digester.calc( file ); - String actualSha1Sum = mojo.sha1Digester.calc( file ); + String actualMd5Sum = mojo.md5Digester.calculate( file ); + String actualSha1Sum = mojo.sha1Digester.calculate( file ); String groupId = dotToSlashReplacer( artifact.getGroupId() );