Author: jdcasey Date: Mon Apr 17 07:33:16 2006 New Revision: 394678 URL: http://svn.apache.org/viewcvs?rev=394678&view=rev Log: [MJAR-32] Applying fix for jar-signer mojo verifying the wrong jar file after signing.
Modified: maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignVerifyMojoTest.java Modified: maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java?rev=394678&r1=394677&r2=394678&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java (original) +++ maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java Mon Apr 17 07:33:16 2006 @@ -117,7 +117,7 @@ /** * See <a href="http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jarsigner.html#Options">options</a>. - * + * <p/> * Not specifying this argument will sign the jar in-place (your original jar is going to be overwritten). * * @parameter expression="${signedjar}" @@ -186,23 +186,29 @@ getLog().info( "Skipping JAR signing for file: " + getJarFile().getAbsolutePath() ); } + // we use this mojo to check if there's a need to sign. + // If we sign and if we need to verify, we reuse it to check the signature JarSignVerifyMojo verifyMojo = createJarSignVerifyMojo(); + verifyMojo.setWorkingDir( workingDirectory ); + + verifyMojo.setBasedir( basedir ); + File signedJarFile = signedjar != null ? signedjar : getJarFile(); + verifyMojo.setVerbose( verbose ); + + verifyMojo.setJarPath( signedJarFile ); + if ( signedJarFile.exists() ) { - verifyMojo.setWorkingDir( workingDirectory ); - verifyMojo.setBasedir( basedir ); - verifyMojo.setJarPath( signedJarFile ); - verifyMojo.setVerbose( verbose ); verifyMojo.setErrorWhenNotSigned( false ); verifyMojo.execute(); } if ( verifyMojo.isSigned() ) { - getLog().info( "JAR " + getJarFile().getAbsoluteFile() + " is already signed. Skipping."); + getLog().info( "JAR " + signedJarFile.getAbsoluteFile() + " is already signed. Skipping." ); return; } Modified: maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java?rev=394678&r1=394677&r2=394678&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java (original) +++ maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java Mon Apr 17 07:33:16 2006 @@ -104,7 +104,7 @@ project.setArtifact( mockArtifact ); mojo.setProject( project ); - new File(getNullJar()).delete(); + new File( getDummyNonSignedJarPath() ).delete(); } public void tearDown() @@ -117,12 +117,65 @@ public void testRunOK() throws MojoExecutionException { + JarSignVerifyMojoTest.MockJarSignVerifyMojo mockJarSignVerifyMojo = + new JarSignVerifyMojoTest.MockJarSignVerifyMojo(); + mojo.verifyMojo = mockJarSignVerifyMojo; + + mojo.execute(); + + String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", + "/tmp/signed/file-version.jar", getDummyNonSignedJarPath(), "alias"}; + + checkMojo( expectedArguments ); + + assertEquals( "sign operation wasn't verified", 0, mockJarSignVerifyMojo.commandLines.size() ); + } + + /** + */ + public void testVerifyJarGeneratedBySignOperation() + throws MojoExecutionException + { + JarSignVerifyMojoTest.MockJarSignVerifyMojo mockJarSignVerifyMojo = + new JarSignVerifyMojoTest.MockJarSignVerifyMojo(); + mojo.verifyMojo = mockJarSignVerifyMojo; + mojo.setVerify( true ); + mockJarSignVerifyMojo.lastOutLine = "jar verified."; + mojo.execute(); String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", - "/tmp/signed/file-version.jar", getNullJar(), "alias"}; + "/tmp/signed/file-version.jar", getDummyNonSignedJarPath(), "alias"}; checkMojo( expectedArguments ); + + String[] expectedVerifyArguments = {"-verify", "/tmp/signed/file-version.jar"}; + + JarSignVerifyMojoTest.checkMojo( mockJarSignVerifyMojo, expectedVerifyArguments ); + } + + /** + */ + public void testVerifyInPlaceSignedJar() + throws MojoExecutionException + { + JarSignVerifyMojoTest.MockJarSignVerifyMojo mockJarSignVerifyMojo = + new JarSignVerifyMojoTest.MockJarSignVerifyMojo(); + mojo.verifyMojo = mockJarSignVerifyMojo; + mojo.setSignedJar( null ); + mojo.setVerify( true ); + mockJarSignVerifyMojo.lastOutLine = "jar verified."; + + mojo.execute(); + + String[] expectedArguments = + {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", getDummyNonSignedJarPath(), "alias"}; + + checkMojo( expectedArguments ); + + String[] expectedVerifyArguments = {"-verify", getDummyNonSignedJarPath()}; + + JarSignVerifyMojoTest.checkMojo( mockJarSignVerifyMojo, expectedVerifyArguments ); } /** @@ -153,12 +206,12 @@ mojo.verifyMojo = new MyJarSignVerifyMojo(); - new File(getNullJar()).createNewFile(); + new File( getDummyNonSignedJarPath() ).createNewFile(); mojo.execute(); String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", - "/tmp/signed/file-version.jar", getNullJar(), "alias"}; + "/tmp/signed/file-version.jar", getDummyNonSignedJarPath(), "alias"}; checkMojo( expectedArguments ); } @@ -167,6 +220,10 @@ */ public void testRunFailure() { + JarSignVerifyMojoTest.MockJarSignVerifyMojo mockJarSignVerifyMojo = + new JarSignVerifyMojoTest.MockJarSignVerifyMojo(); + mojo.verifyMojo = mockJarSignVerifyMojo; + mojo.executeResult = 1; // any missing argument should produce this. Let's simulate a missing alias @@ -183,12 +240,14 @@ } String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", - "/tmp/signed/file-version.jar", getNullJar()}; + "/tmp/signed/file-version.jar", getDummyNonSignedJarPath()}; checkMojo( expectedArguments ); + + assertEquals( "sign operation wasn't verified", 0, mockJarSignVerifyMojo.commandLines.size() ); } - private String getNullJar() + private String getDummyNonSignedJarPath() { String value = System.getProperty( "java.io.tmpdir" ); if ( !value.endsWith( "\\" ) && !value.endsWith( "/" ) ) @@ -216,7 +275,7 @@ } String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", - "/tmp/signed/file-version.jar", getNullJar(), "alias"}; + "/tmp/signed/file-version.jar", getDummyNonSignedJarPath(), "alias"}; checkMojo( expectedArguments ); } Modified: maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignVerifyMojoTest.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignVerifyMojoTest.java?rev=394678&r1=394677&r2=394678&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignVerifyMojoTest.java (original) +++ maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignVerifyMojoTest.java Mon Apr 17 07:33:16 2006 @@ -23,9 +23,7 @@ import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.StreamConsumer; -import junit.framework.Test; import junit.framework.TestCase; -import junit.framework.TestSuite; import java.io.File; import java.io.InputStream; @@ -35,83 +33,97 @@ import java.util.HashMap; import java.util.List; import java.util.ArrayList; -import java.util.Arrays; /** * These unit tests only check whether the generated command lines are correct. * Really running the command would mean checking the results, which is too painful and not really a unit test. * It would probably require to 'jarsigner -verify' the resulting signed jar and I believe it would make the code * too complex with very few benefits. - * + * * @author Jerome Lacoste <[EMAIL PROTECTED]> * @version $Id$ */ -public class JarSignVerifyMojoTest extends TestCase +public class JarSignVerifyMojoTest + extends TestCase { private MockJarSignVerifyMojo mojo; - static class MockJarSignVerifyMojo extends JarSignVerifyMojo { + static class MockJarSignVerifyMojo + extends JarSignVerifyMojo + { public int executeResult; + public List commandLines = new ArrayList(); + public String failureMsg; + public Map systemProperties = new HashMap(); + public String lastOutLine; - protected int executeCommandLine( Commandline commandLine, InputStream inputStream, - StreamConsumer systemOut, StreamConsumer systemErr ) - throws CommandLineException + protected int executeCommandLine( Commandline commandLine, InputStream inputStream, StreamConsumer systemOut, + StreamConsumer systemErr ) + throws CommandLineException { commandLines.add( commandLine ); - if ( failureMsg != null ) { - throw new CommandLineException( failureMsg ) ; + if ( failureMsg != null ) + { + throw new CommandLineException( failureMsg ); } - if ( lastOutLine != null ) { - systemOut.consumeLine( lastOutLine ); + if ( lastOutLine != null ) + { + systemOut.consumeLine( lastOutLine ); } return executeResult; } - protected String getSystemProperty( String key ) { - return (String) systemProperties.get( key ); + protected String getSystemProperty( String key ) + { + return (String) systemProperties.get( key ); } } - public void setUp() throws IOException { + public void setUp() + throws IOException + { mojo = new MockJarSignVerifyMojo(); mojo.executeResult = 0; // it doesn't really matter if the paths are not cross-platform, we don't execute the command lines anyway - File basedir = new File( System.getProperty( "java.io.tmpdir" ) ) ; + File basedir = new File( System.getProperty( "java.io.tmpdir" ) ); mojo.setBasedir( basedir ); mojo.setWorkingDir( basedir ); mojo.setJarPath( new File( "/tmp/signed/file-version.jar" ) ); } - public void tearDown() { + public void tearDown() + { mojo = null; } - public void testPleaseMaven() { + public void testPleaseMaven() + { assertTrue( true ); } /** */ - public void testRunOK() throws MojoExecutionException + public void testRunOK() + throws MojoExecutionException { mojo.lastOutLine = "jar verified."; mojo.execute(); - String[] expectedArguments = - { "-verify", "/tmp/signed/file-version.jar" }; + String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"}; checkMojo( expectedArguments ); } /** */ - public void testRunOKAllArguments() throws MojoExecutionException + public void testRunOKAllArguments() + throws MojoExecutionException { mojo.lastOutLine = "jar verified."; @@ -120,8 +132,7 @@ mojo.execute(); - String[] expectedArguments = - { "-verify", "-verbose", "-certs", "/tmp/signed/file-version.jar" }; + String[] expectedArguments = {"-verify", "-verbose", "-certs", "/tmp/signed/file-version.jar"}; checkMojo( expectedArguments ); } @@ -132,15 +143,17 @@ { mojo.executeResult = 1; - try { - mojo.execute(); - fail( "expected failure" ); - } catch ( MojoExecutionException e ) { + try + { + mojo.execute(); + fail( "expected failure" ); + } + catch ( MojoExecutionException e ) + { assertTrue( e.getMessage().startsWith( "Result of " ) ); } - String[] expectedArguments = - { "-verify", "/tmp/signed/file-version.jar" }; + String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"}; checkMojo( expectedArguments ); } @@ -151,15 +164,17 @@ { mojo.lastOutLine = "jar is unsigned."; - try { - mojo.execute(); - fail( "expected failure" ); - } catch ( MojoExecutionException e ) { + try + { + mojo.execute(); + fail( "expected failure" ); + } + catch ( MojoExecutionException e ) + { assertTrue( e.getMessage().startsWith( "Verify failed: jar is unsigned." ) ); } - String[] expectedArguments = - { "-verify", "/tmp/signed/file-version.jar" }; + String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"}; checkMojo( expectedArguments ); } @@ -170,22 +185,30 @@ { mojo.failureMsg = "simulated failure"; - try { + try + { mojo.execute(); fail( "expected failure" ); - } catch ( MojoExecutionException e ) { + } + catch ( MojoExecutionException e ) + { assertEquals( "command execution failed", e.getMessage() ); } - String[] expectedArguments = - { "-verify", "/tmp/signed/file-version.jar" }; + String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"}; checkMojo( expectedArguments ); } - private void checkMojo( String[] expectedCommandLineArguments ) { + private void checkMojo( String[] expectedCommandLineArguments ) + { + checkMojo( mojo, expectedCommandLineArguments ); + } + + static void checkMojo( MockJarSignVerifyMojo mojo, String[] expectedCommandLineArguments ) + { assertEquals( 1, mojo.commandLines.size() ); - Commandline commandline = (Commandline) mojo.commandLines.get(0); + Commandline commandline = (Commandline) mojo.commandLines.get( 0 ); String[] arguments = commandline.getArguments(); // isn't there an assertEquals for arrays? /* @@ -193,12 +216,12 @@ System.out.println( arguments[ i ] ); } */ - assertEquals( "Differing number of arguments", - expectedCommandLineArguments.length, - arguments.length ); - for (int i = 0; i < arguments.length; i++ ) { - expectedCommandLineArguments[ i ] = StringUtils.replace( expectedCommandLineArguments[ i ], "/", File.separator ); - assertEquals( expectedCommandLineArguments[ i ], arguments[ i ] ); + assertEquals( "Differing number of arguments", expectedCommandLineArguments.length, arguments.length ); + for ( int i = 0; i < arguments.length; i++ ) + { + expectedCommandLineArguments[i] = + StringUtils.replace( expectedCommandLineArguments[i], "/", File.separator ); + assertEquals( expectedCommandLineArguments[i], expectedCommandLineArguments[i], arguments[i] ); } } }