]> source.dussan.org Git - archiva.git/commitdiff
[MRM-138] add checksum tests and bugfixes, refactor digest utils out
authorBrett Porter <brett@apache.org>
Mon, 14 Aug 2006 00:52:42 +0000 (00:52 +0000)
committerBrett Porter <brett@apache.org>
Mon, 14 Aug 2006 00:52:42 +0000 (00:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@431275 13f79535-47bb-0310-9956-ffa450edef68

26 files changed:
maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyRequestHandler.java
maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/ProxyRequestHandlerTest.java
maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/WagonDelegate.java
maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-removed-from-proxies/1.0/get-removed-from-proxies-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar.sha1 [new file with mode: 0644]
maven-repository-utils/src/main/java/org/apache/maven/repository/digest/AbstractDigester.java
maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigestUtils.java [new file with mode: 0644]

index e9bcbce0e40119da8d795a83c021819d41d49765..d620788ae59ddf89ad17f97c6e1adb765e49bbfc 100644 (file)
@@ -19,6 +19,8 @@ package org.apache.maven.repository.proxy;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.repository.digest.DigestUtils;
+import org.apache.maven.repository.digest.DigesterException;
 import org.apache.maven.repository.discovery.ArtifactDiscoverer;
 import org.apache.maven.repository.discovery.DiscovererException;
 import org.apache.maven.wagon.ConnectionException;
@@ -47,6 +49,7 @@ import java.util.Map;
  *
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @plexus.component
+ * @todo use wagonManager for cache use file:// as URL
  * @todo this currently duplicates a lot of the wagon manager, and doesn't do things like snapshot resolution, etc.
  * The checksum handling is inconsistent with that of the wagon manager.
  * Should we have a more artifact based one? This will merge metadata so should behave correctly, and it is able to
@@ -72,6 +75,11 @@ public class DefaultProxyRequestHandler
      */
     private Map/*<String,Wagon>*/ wagons;
 
+    /**
+     * @plexus.requirement role="org.apache.maven.repository.digest.Digester"
+     */
+    private Map/*<String,Digester>*/ digesters;
+
     public File get( String path, List proxiedRepositories, ArtifactRepository managedRepository )
         throws ProxyException, ResourceDoesNotExistException
     {
@@ -81,17 +89,9 @@ public class DefaultProxyRequestHandler
     public File get( String path, List proxiedRepositories, ArtifactRepository managedRepository, ProxyInfo wagonProxy )
         throws ProxyException, ResourceDoesNotExistException
     {
-        // TODO! this will prove wrong for metadata and snapshots, let tests bring it out
-        //@todo use wagonManager for cache use file:// as URL
-        File cachedFile = new File( managedRepository.getBasedir(), path );
-        if ( !cachedFile.exists() )
-        {
-            cachedFile = getAlways( path, proxiedRepositories, managedRepository, wagonProxy );
-        }
-        return cachedFile;
+        return get( managedRepository, path, proxiedRepositories, wagonProxy, false );
     }
 
-
     public File getAlways( String path, List proxiedRepositories, ArtifactRepository managedRepository )
         throws ProxyException, ResourceDoesNotExistException
     {
@@ -101,6 +101,13 @@ public class DefaultProxyRequestHandler
     public File getAlways( String path, List proxiedRepositories, ArtifactRepository managedRepository,
                            ProxyInfo wagonProxy )
         throws ResourceDoesNotExistException, ProxyException
+    {
+        return get( managedRepository, path, proxiedRepositories, wagonProxy, true );
+    }
+
+    private File get( ArtifactRepository managedRepository, String path, List proxiedRepositories, ProxyInfo wagonProxy,
+                      boolean force )
+        throws ProxyException, ResourceDoesNotExistException
     {
         File target = new File( managedRepository.getBasedir(), path );
 
@@ -108,13 +115,13 @@ public class DefaultProxyRequestHandler
         {
             ProxiedArtifactRepository repository = (ProxiedArtifactRepository) i.next();
 
-            if ( repository.isCachedFailure( path ) )
+            if ( !force && repository.isCachedFailure( path ) )
             {
                 processCachedRepositoryFailure( repository, "Cached failure found for: " + path );
             }
             else
             {
-                get( path, target, repository, managedRepository, wagonProxy );
+                get( path, target, repository, managedRepository, wagonProxy, force );
             }
         }
 
@@ -127,10 +134,10 @@ public class DefaultProxyRequestHandler
     }
 
     private void get( String path, File target, ProxiedArtifactRepository repository,
-                      ArtifactRepository managedRepository, ProxyInfo wagonProxy )
+                      ArtifactRepository managedRepository, ProxyInfo wagonProxy, boolean force )
         throws ProxyException
     {
-        ArtifactRepositoryPolicy policy = null;
+        ArtifactRepositoryPolicy policy;
 
         if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) )
         {
@@ -138,12 +145,12 @@ public class DefaultProxyRequestHandler
         }
         else if ( path.endsWith( "maven-metadata.xml" ) )
         {
-            // TODO: this is not "always" as this method expects!
             // TODO: merge the metadata!
             policy = repository.getRepository().getReleases();
-            if ( !target.exists() || isOutOfDate( policy, target ) )
+            if ( force || !target.exists() || isOutOfDate( policy, target ) )
             {
-                getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target, policy );
+                getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target, policy,
+                                       force );
             }
         }
         else
@@ -182,23 +189,21 @@ public class DefaultProxyRequestHandler
                 else
                 {
                     // Don't use releases policy, we don't want to perform updates on them (only metadata, as used earlier)
-                    // TODO: this is not "always" as this method expects!
-                    if ( !target.exists() || isOutOfDate( policy, target ) )
+                    if ( force || !target.exists() || isOutOfDate( policy, target ) )
                     {
                         getFileFromRepository( artifactRepository.pathOf( artifact ), repository,
-                                               managedRepository.getBasedir(), wagonProxy, target, policy );
+                                               managedRepository.getBasedir(), wagonProxy, target, policy, force );
                     }
                 }
             }
             else
             {
                 // Some other unknown file in the repository, proxy as is
-                // TODO: this is not "always" as this method expects!
-                if ( !target.exists() )
+                if ( force || !target.exists() )
                 {
                     policy = repository.getRepository().getReleases();
-                    getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target,
-                                           policy );
+                    getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target, policy,
+                                           force );
                 }
             }
         }
@@ -211,13 +216,17 @@ public class DefaultProxyRequestHandler
     }
 
     private void getFileFromRepository( String path, ProxiedArtifactRepository repository, String repositoryCachePath,
-                                        ProxyInfo httpProxy, File target, ArtifactRepositoryPolicy policy )
+                                        ProxyInfo httpProxy, File target, ArtifactRepositoryPolicy policy,
+                                        boolean force )
         throws ProxyException
     {
         boolean connected = false;
         Map checksums = null;
         Wagon wagon = null;
 
+        File temp = new File( target.getAbsolutePath() + ".tmp" );
+        temp.deleteOnExit();
+
         try
         {
             String protocol = repository.getRepository().getProtocol();
@@ -234,9 +243,6 @@ public class DefaultProxyRequestHandler
             connected = connectToRepository( wagon, repository, httpProxy );
             if ( connected )
             {
-                File temp = new File( target.getAbsolutePath() + ".tmp" );
-                temp.deleteOnExit();
-
                 int tries = 0;
                 boolean success;
 
@@ -246,7 +252,7 @@ public class DefaultProxyRequestHandler
 
                     getLogger().debug( "Trying " + path + " from " + repository.getName() + "..." );
 
-                    if ( !target.exists() )
+                    if ( force || !target.exists() )
                     {
                         wagon.get( path, temp );
                     }
@@ -263,14 +269,14 @@ public class DefaultProxyRequestHandler
                                                   path, policy );
                         return;
                     }
-
-                    // temp won't exist if we called getIfNewer and it was older, but its still a successful return
-                    if ( temp.exists() )
-                    {
-                        moveTempToTarget( temp, target );
-                    }
                 }
                 while ( !success );
+
+                // temp won't exist if we called getIfNewer and it was older, but its still a successful return
+                if ( temp.exists() )
+                {
+                    moveTempToTarget( temp, target );
+                }
             }
             //try next repository
         }
@@ -289,6 +295,8 @@ public class DefaultProxyRequestHandler
         }
         finally
         {
+            temp.delete();
+
             if ( wagon != null && checksums != null )
             {
                 releaseChecksumListeners( wagon, checksums );
@@ -374,70 +382,92 @@ public class DefaultProxyRequestHandler
         throws ProxyException
     {
         releaseChecksumListeners( wagon, checksumMap );
-        for ( Iterator checksums = checksumMap.keySet().iterator(); checksums.hasNext(); )
+
+        boolean correctChecksum = false;
+
+        boolean allNotFound = true;
+
+        for ( Iterator i = checksumMap.keySet().iterator(); i.hasNext() && !correctChecksum; )
         {
-            String checksumExt = (String) checksums.next();
+            String checksumExt = (String) i.next();
             ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt );
             String checksumPath = path + "." + checksumExt;
             File checksumFile = new File( repositoryCachePath, checksumPath );
 
+            File tempChecksumFile = new File( checksumFile.getAbsolutePath() + ".tmp" );
+            tempChecksumFile.deleteOnExit();
+
             try
             {
-                File tempChecksumFile = new File( checksumFile.getAbsolutePath() + ".tmp" );
-
                 wagon.get( checksumPath, tempChecksumFile );
 
-                String remoteChecksum = FileUtils.fileRead( tempChecksumFile ).trim();
-                if ( remoteChecksum.indexOf( ' ' ) > 0 )
-                {
-                    remoteChecksum = remoteChecksum.substring( 0, remoteChecksum.indexOf( ' ' ) );
-                }
+                allNotFound = false;
+
+                String remoteChecksum = DigestUtils.cleanChecksum( FileUtils.fileRead( tempChecksumFile ),
+                                                                   checksumExt.toUpperCase(),
+                                                                   path.substring( path.lastIndexOf( '/' ) ) );
 
                 String actualChecksum = checksum.getActualChecksum().toUpperCase();
                 remoteChecksum = remoteChecksum.toUpperCase();
 
-                boolean checksumCheck;
                 if ( remoteChecksum.equals( actualChecksum ) )
                 {
                     moveTempToTarget( tempChecksumFile, checksumFile );
 
-                    checksumCheck = true;
+                    correctChecksum = true;
                 }
                 else
                 {
                     getLogger().warn(
                         "The checksum '" + actualChecksum + "' did not match the remote value: " + remoteChecksum );
-                    checksumCheck = false;
                 }
-                return checksumCheck;
             }
             catch ( TransferFailedException e )
             {
                 getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage(),
                                   e );
                 // do nothing try the next checksum
+
+                allNotFound = false;
             }
             catch ( ResourceDoesNotExistException e )
             {
                 getLogger().debug( "The checksum did not exist: " + checksumPath, e );
                 // do nothing try the next checksum
+                // remove it if it is present locally in case there is an old incorrect one
+                if ( checksumFile.exists() )
+                {
+                    checksumFile.delete();
+                }
             }
             catch ( AuthorizationException e )
             {
                 getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage(),
                                   e );
                 // do nothing try the next checksum
+
+                allNotFound = false;
             }
             catch ( IOException e )
             {
                 getLogger().warn( "An error occurred while reading the temporary checksum file.", e );
-                return false;
-            }
-        }
+                // do nothing try the next checksum
 
-        getLogger().debug( "No remote checksums available." );
+                allNotFound = false;
+            }
+            catch ( DigesterException e )
+            {
+                getLogger().warn( "The checksum was invalid: " + checksumPath + ": " + e.getMessage(), e );
+                // do nothing try the next checksum
 
-        return true;
+                allNotFound = false;
+            }
+            finally
+            {
+                tempChecksumFile.delete();
+            }
+        }
+        return correctChecksum || allNotFound;
     }
 
     /**
index 4698671fbf381194dc47cc870ef64db91b1975ee..ee34366445a459eb5c1e2af1a4c5f04f20bfd2c2 100644 (file)
@@ -38,27 +38,27 @@ import java.util.List;
 /**
  * @author Brett Porter
  * @todo! tests to do vvv
- * @todo test get always
- * @todo test get always when resource is present locally but not in any proxied repos (should fail)
- * @todo test get always ignores cached failures
- * @todo test when managed repo is m1 layout (proxy is m2), including metadata
- * @todo test when one proxied repo is m1 layout (managed is m2), including metadata
- * @todo test when one proxied repo is m1 layout (managed is m1), including metadata
+ * @todo test checksum request from proxy gets from managed repo when present
+ * @todo test checksum request from proxy doesn't go to proxy when not in managed repo
  * @todo test metadata - general
  * @todo test metadata - multiple repos are merged
  * @todo test metadata - update interval
  * @todo test metadata - looking for an update and file has been removed remotely
  * @todo test snapshots - general
- * @todo test snapshots - newer version on repo2 is pulled down
- * @todo test snapshots - older version on repo2 is skipped
- * @todo test snapshots - update interval
+ * @todo test snapshots - newer version on repo1 (than local), timestamp driven
+ * @todo test snapshots - older version on repo1 skipped (than local), timestamp driven
+ * @todo test snapshots - newer version on repo2 is pulled down (no local), timestamp driven
+ * @todo test snapshots - older version on repo2 is skipped  (no local), timestamp driven
+ * @todo test snapshots - update interval (not updated if within period), timestamp driven
+ * @todo test snapshots - newer version on repo1 (than local), metadata driven
+ * @todo test snapshots - older version on repo1 skipped (than local), metadata driven
+ * @todo test snapshots - newer version on repo2 is pulled down (no local), metadata driven
+ * @todo test snapshots - older version on repo2 is skipped  (no local), metadata driven
+ * @todo test snapshots - update interval (not updated if within period), metadata driven
  * @todo test snapshots - when failure is cached but cache period is over (and check failure is cleared)
- * @todo test remote checksum only md5
- * @todo test remote checksum only sha1
- * @todo test remote checksum missing
- * @todo test remote checksum present and correct
- * @todo test remote checksum present and incorrect
- * @todo test remote checksum transfer failed
+ * @todo test when managed repo is m1 layout (proxy is m2), including metadata
+ * @todo test when one proxied repo is m1 layout (managed is m2), including metadata
+ * @todo test when one proxied repo is m1 layout (managed is m1), including metadata
  */
 public class ProxyRequestHandlerTest
     extends PlexusTestCase
@@ -453,6 +453,397 @@ public class ProxyRequestHandlerTest
         assertFalse( "Check failure", proxiedArtifactRepository.isCachedFailure( path ) );
     }
 
+    public void testGetAlwaysAlreadyPresent()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+        String unexpectedContents = FileUtils.fileRead( expectedFile );
+
+        assertTrue( expectedFile.exists() );
+
+        File file = requestHandler.getAlways( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        File proxiedFile = new File( proxiedRepository1.getBasedir(), path );
+        String expectedContents = FileUtils.fileRead( proxiedFile );
+        assertEquals( "Check file contents", expectedContents, FileUtils.fileRead( file ) );
+        assertFalse( "Check file contents", unexpectedContents.equals( FileUtils.fileRead( file ) ) );
+    }
+
+    public void testGetAlwaysAlreadyPresentRemovedFromProxies()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-removed-from-proxies/1.0/get-removed-from-proxies-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+        String expectedContents = FileUtils.fileRead( expectedFile );
+
+        assertTrue( expectedFile.exists() );
+
+        File file = requestHandler.getAlways( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        assertEquals( "Check file contents", expectedContents, FileUtils.fileRead( file ) );
+
+        // TODO: is this the correct behaviour, or should it be considered removed too?
+    }
+
+    public void testGetAlwaysWithCachedFailure()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+        String unexpectedContents = FileUtils.fileRead( expectedFile );
+
+        assertTrue( expectedFile.exists() );
+
+        proxiedRepositories.clear();
+        ProxiedArtifactRepository proxiedArtifactRepository = createProxiedRepository( proxiedRepository1 );
+        proxiedArtifactRepository.addFailure( path, DEFAULT_POLICY );
+        proxiedRepositories.add( proxiedArtifactRepository );
+        proxiedRepositories.add( createProxiedRepository( proxiedRepository2 ) );
+        File file = requestHandler.getAlways( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        File proxiedFile = new File( proxiedRepository1.getBasedir(), path );
+        String expectedContents = FileUtils.fileRead( proxiedFile );
+        assertEquals( "Check file contents", expectedContents, FileUtils.fileRead( file ) );
+        assertFalse( "Check file contents", unexpectedContents.equals( FileUtils.fileRead( file ) ) );
+    }
+
+    public void testGetRemovesTemporaryFileOnSuccess()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        File tempFile = new File( file.getParentFile(), file.getName() + ".tmp" );
+        assertFalse( "Check temporary file removed", tempFile.exists() );
+    }
+
+    public void testGetRemovesTemporaryFileOnError()
+        throws ResourceDoesNotExistException, ProxyException, IOException, TransferFailedException,
+        AuthorizationException
+    {
+        String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        proxiedRepository1 = createRepository( "proxied1", "test://..." );
+        proxiedRepositories.clear();
+        ProxiedArtifactRepository proxiedArtifactRepository1 = createProxiedRepository( proxiedRepository1 );
+        proxiedRepositories.add( proxiedArtifactRepository1 );
+
+        wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+        wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
+
+        wagonMockControl.replay();
+
+        try
+        {
+            File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+            fail( "Found file: " + file + "; but was expecting a failure" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            // as expected
+            wagonMockControl.verify();
+
+            File tempFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
+            assertFalse( "Check temporary file removed", tempFile.exists() );
+        }
+    }
+
+    public void testGetRemovesTemporaryChecksumFileOnSuccess()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        File tempFile = new File( file.getParentFile(), file.getName() + ".sha1.tmp" );
+        assertFalse( "Check temporary file removed", tempFile.exists() );
+    }
+
+    public void testGetRemovesTemporaryChecksumFileOnError()
+        throws ResourceDoesNotExistException, ProxyException, IOException, TransferFailedException,
+        AuthorizationException
+    {
+        String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        proxiedRepository1 = createRepository( "proxied1", "test://..." );
+        proxiedRepositories.clear();
+        ProxiedArtifactRepository proxiedArtifactRepository1 = createProxiedRepository( proxiedRepository1 );
+        proxiedRepositories.add( proxiedArtifactRepository1 );
+
+        wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+
+        mockFailedChecksums( path, expectedFile );
+
+        wagonMockControl.replay();
+
+        try
+        {
+            File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+            fail( "Found file: " + file + "; but was expecting a failure" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            // as expected
+            wagonMockControl.verify();
+
+            File tempFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
+            assertFalse( "Check temporary file removed", tempFile.exists() );
+
+            tempFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".sha1.tmp" );
+            assertFalse( "Check temporary file removed", tempFile.exists() );
+        }
+    }
+
+    public void testGetChecksumBothCorrect()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        File checksumFile = getChecksumFile( file, "sha1" );
+        assertTrue( "Check file created", checksumFile.exists() );
+        assertEquals( "Check checksum", "066d76e459f7782c312c31e8a11b3c0f1e3e43a7 *get-checksum-both-right-1.0.jar",
+                      FileUtils.fileRead( checksumFile ).trim() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "md5" ).exists() );
+    }
+
+    public void testGetCorrectSha1NoMd5()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        File checksumFile = getChecksumFile( file, "sha1" );
+        assertTrue( "Check file created", checksumFile.exists() );
+        assertEquals( "Check checksum", "748a3a013bf5eacf2bbb40a2ac7d37889b728837 *get-checksum-sha1-only-1.0.jar",
+                      FileUtils.fileRead( checksumFile ).trim() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "md5" ).exists() );
+    }
+
+    public void testGetCorrectSha1BadMd5()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        File checksumFile = getChecksumFile( file, "sha1" );
+        assertTrue( "Check file created", checksumFile.exists() );
+        assertEquals( "Check checksum", "3dd1a3a57b807d3ef3fbc6013d926c891cbb8670 *get-checksum-sha1-bad-md5-1.0.jar",
+                      FileUtils.fileRead( checksumFile ).trim() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "md5" ).exists() );
+    }
+
+    public void testGetCorrectMd5NoSha1()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        File checksumFile = getChecksumFile( file, "md5" );
+        assertTrue( "Check file created", checksumFile.exists() );
+        assertEquals( "Check checksum", "f3af5201bf8da801da37db8842846e1c *get-checksum-md5-only-1.0.jar",
+                      FileUtils.fileRead( checksumFile ).trim() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "sha1" ).exists() );
+    }
+
+    public void testGetCorrectMd5BadSha1()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        File checksumFile = getChecksumFile( file, "md5" );
+        assertTrue( "Check file created", checksumFile.exists() );
+        assertEquals( "Check checksum", "8a02aa67549d27b2a03cd4547439c6d3 *get-checksum-md5-bad-sha1-1.0.jar",
+                      FileUtils.fileRead( checksumFile ).trim() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "sha1" ).exists() );
+    }
+
+    public void testGetWithNoChecksums()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+
+        assertFalse( "Check file not created", getChecksumFile( file, "md5" ).exists() );
+        assertFalse( "Check file not created", getChecksumFile( file, "sha1" ).exists() );
+    }
+
+    public void testGetBadMd5BadSha1()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        try
+        {
+            File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+            fail( "Found file: " + file + "; but was expecting a failure" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            // expect a failure
+            assertFalse( "Check file not created", expectedFile.exists() );
+
+            assertFalse( "Check file not created", getChecksumFile( expectedFile, "md5" ).exists() );
+            assertFalse( "Check file not created", getChecksumFile( expectedFile, "sha1" ).exists() );
+        }
+    }
+
+    public void testGetChecksumTransferFailed()
+        throws ResourceDoesNotExistException, ProxyException, IOException, TransferFailedException,
+        AuthorizationException
+    {
+        String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+
+        assertFalse( expectedFile.exists() );
+
+        proxiedRepository1 = createRepository( "proxied1", "test://..." );
+        proxiedRepositories.clear();
+        ProxiedArtifactRepository proxiedArtifactRepository1 = createProxiedRepository( proxiedRepository1 );
+        proxiedRepositories.add( proxiedArtifactRepository1 );
+
+        wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+
+        mockFailedChecksums( path, expectedFile );
+
+        wagonMockControl.replay();
+
+        try
+        {
+            File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+            fail( "Found file: " + file + "; but was expecting a failure" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            // as expected
+            wagonMockControl.verify();
+
+            assertFalse( "Check file not created", expectedFile.exists() );
+
+            assertFalse( "Check file not created", getChecksumFile( expectedFile, "md5" ).exists() );
+            assertFalse( "Check file not created", getChecksumFile( expectedFile, "sha1" ).exists() );
+        }
+    }
+
+    private void mockFailedChecksums( String path, File expectedFile )
+        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
+    {
+        // must do it twice as it will re-attempt it
+        wagonMock.get( path + ".sha1", new File( expectedFile.getParentFile(), expectedFile.getName() + ".sha1.tmp" ) );
+        wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
+
+        wagonMock.get( path + ".md5", new File( expectedFile.getParentFile(), expectedFile.getName() + ".md5.tmp" ) );
+        wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
+
+        wagonMock.get( path + ".sha1", new File( expectedFile.getParentFile(), expectedFile.getName() + ".sha1.tmp" ) );
+        wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
+
+        wagonMock.get( path + ".md5", new File( expectedFile.getParentFile(), expectedFile.getName() + ".md5.tmp" ) );
+        wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
+    }
+
+    public void testGetAlwaysBadChecksumPresentLocallyAbsentRemote()
+        throws ResourceDoesNotExistException, ProxyException, IOException
+    {
+        String path = "org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar";
+        File expectedFile = new File( defaultManagedRepository.getBasedir(), path );
+        String unexpectedContents = FileUtils.fileRead( expectedFile );
+
+        assertTrue( expectedFile.exists() );
+
+        File file = requestHandler.getAlways( path, proxiedRepositories, defaultManagedRepository );
+
+        assertEquals( "Check file matches", expectedFile, file );
+        assertTrue( "Check file created", file.exists() );
+        File proxiedFile = new File( proxiedRepository1.getBasedir(), path );
+        String expectedContents = FileUtils.fileRead( proxiedFile );
+        assertEquals( "Check file contents", expectedContents, FileUtils.fileRead( file ) );
+        assertFalse( "Check file contents", unexpectedContents.equals( FileUtils.fileRead( file ) ) );
+
+        assertFalse( "Check checksum removed", new File( file.getParentFile(), file.getName() + ".sha1" ).exists() );
+        assertFalse( "Check checksum removed", new File( file.getParentFile(), file.getName() + ".md5" ).exists() );
+    }
+
+    private File getChecksumFile( File file, String algorithm )
+    {
+        return new File( file.getParentFile(), file.getName() + "." + algorithm );
+    }
+
     /**
      * A faster recursive copy that omits .svn directories.
      *
index 904d6b869b57e021ceaa6bec5b232228873a061c..023195e4dcbac77f011d47ecb5368000bc7b5b3d 100644 (file)
@@ -16,19 +16,21 @@ package org.apache.maven.repository.proxy;
  * limitations under the License.
  */
 
-import org.apache.maven.wagon.Wagon;
-import org.apache.maven.wagon.TransferFailedException;
-import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.authorization.AuthorizationException;
 import org.apache.maven.wagon.events.SessionListener;
 import org.apache.maven.wagon.events.TransferListener;
 import org.apache.maven.wagon.proxy.ProxyInfo;
-import org.apache.maven.wagon.authentication.AuthenticationException;
-import org.apache.maven.wagon.authentication.AuthenticationInfo;
 import org.apache.maven.wagon.repository.Repository;
-import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.codehaus.plexus.util.FileUtils;
 
 import java.io.File;
+import java.io.IOException;
 
 /**
  * A dummy wagon implementation
@@ -40,16 +42,21 @@ public class WagonDelegate
 {
     private Wagon delegate;
 
+    private String contentToGet;
+
     public void get( String resourceName, File destination )
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
     {
         delegate.get( resourceName, destination );
+        create( destination );
     }
 
     public boolean getIfNewer( String resourceName, File destination, long timestamp )
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
     {
-        return delegate.getIfNewer( resourceName, destination, timestamp );
+        boolean result = delegate.getIfNewer( resourceName, destination, timestamp );
+        createIfMissing( destination );
+        return result;
     }
 
     public void put( File source, String destination )
@@ -154,4 +161,38 @@ public class WagonDelegate
     {
         this.delegate = delegate;
     }
+
+    void setContentToGet( String content )
+    {
+        contentToGet = content;
+    }
+
+    private void createIfMissing( File destination )
+    {
+        // since the mock won't actually copy a file, create an empty one to simulate file existence
+        if ( !destination.exists() )
+        {
+            create( destination );
+        }
+    }
+
+    private void create( File destination )
+    {
+        try
+        {
+            destination.getParentFile().mkdirs();
+            if ( contentToGet == null )
+            {
+                destination.createNewFile();
+            }
+            else
+            {
+                FileUtils.fileWrite( destination.getAbsolutePath(), contentToGet );
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new RuntimeException( e.getMessage(), e );
+        }
+    }
 }
diff --git a/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar b/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar
new file mode 100644 (file)
index 0000000..62a1e1c
--- /dev/null
@@ -0,0 +1,3 @@
+get-bad-local-checksum-1.0.jar\r
+(managed)\r
+\r
diff --git a/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.md5
new file mode 100644 (file)
index 0000000..5fd0ae2
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..5fd0ae2
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-removed-from-proxies/1.0/get-removed-from-proxies-1.0.jar b/maven-repository-proxy/src/test/repositories/managed/org/apache/maven/test/get-removed-from-proxies/1.0/get-removed-from-proxies-1.0.jar
new file mode 100644 (file)
index 0000000..54dc5ee
--- /dev/null
@@ -0,0 +1,3 @@
+get-removed-from-proxies-1.0.jar
+(managed)
+
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-bad-local-checksum/1.0/get-bad-local-checksum-1.0.jar
new file mode 100644 (file)
index 0000000..b5d8045
--- /dev/null
@@ -0,0 +1,3 @@
+get-bad-local-checksum-1.0.jar\r
+(proxied 1)\r
+\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar
new file mode 100644 (file)
index 0000000..98fae80
--- /dev/null
@@ -0,0 +1,2 @@
+get-checksum-both-bad-1.0.jar\r
+\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.md5
new file mode 100644 (file)
index 0000000..5fd0ae2
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-bad/1.0/get-checksum-both-bad-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..a2d3f29
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar
new file mode 100644 (file)
index 0000000..7fa9ec4
--- /dev/null
@@ -0,0 +1 @@
+get-checksum-both-right-1.0.jar\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.md5
new file mode 100644 (file)
index 0000000..9b9e337
--- /dev/null
@@ -0,0 +1 @@
+e58f30c6a150a2e843552438d18e15cb *get-checksum-both-right-1.0.jar
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-both-right/1.0/get-checksum-both-right-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..cdd9a33
--- /dev/null
@@ -0,0 +1 @@
+066d76e459f7782c312c31e8a11b3c0f1e3e43a7 *get-checksum-both-right-1.0.jar\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar
new file mode 100644 (file)
index 0000000..68e3480
--- /dev/null
@@ -0,0 +1,2 @@
+get-checksum-md5-bad-sha1-1.0.jar\r
+\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.md5
new file mode 100644 (file)
index 0000000..d785caa
--- /dev/null
@@ -0,0 +1 @@
+8a02aa67549d27b2a03cd4547439c6d3 *get-checksum-md5-bad-sha1-1.0.jar
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-bad-sha1/1.0/get-checksum-md5-bad-sha1-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..a2d3f29
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar
new file mode 100644 (file)
index 0000000..915323d
--- /dev/null
@@ -0,0 +1,2 @@
+get-checksum-md5-only-1.0.jar
+
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-md5-only/1.0/get-checksum-md5-only-1.0.jar.md5
new file mode 100644 (file)
index 0000000..f5ecac3
--- /dev/null
@@ -0,0 +1 @@
+f3af5201bf8da801da37db8842846e1c *get-checksum-md5-only-1.0.jar\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar
new file mode 100644 (file)
index 0000000..f02c918
--- /dev/null
@@ -0,0 +1,2 @@
+get-checksum-sha1-bad-md5-1.0.jar
+
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.md5 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.md5
new file mode 100644 (file)
index 0000000..a2d3f29
--- /dev/null
@@ -0,0 +1 @@
+invalid checksum file\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-bad-md5/1.0/get-checksum-sha1-bad-md5-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..4256235
--- /dev/null
@@ -0,0 +1 @@
+3dd1a3a57b807d3ef3fbc6013d926c891cbb8670 *get-checksum-sha1-bad-md5-1.0.jar\r
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar
new file mode 100644 (file)
index 0000000..efd9ed0
--- /dev/null
@@ -0,0 +1,2 @@
+get-checksum-sha1-only-1.0.jar
+
diff --git a/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar.sha1 b/maven-repository-proxy/src/test/repositories/proxied1/org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar.sha1
new file mode 100644 (file)
index 0000000..e64dccf
--- /dev/null
@@ -0,0 +1 @@
+748a3a013bf5eacf2bbb40a2ac7d37889b728837 *get-checksum-sha1-only-1.0.jar
index cbc95c0e5f960849826e05b439cf730e5325062c..c4c8cc7b1ac23da64854c6e3c37c421886d6c7c8 100644 (file)
@@ -23,8 +23,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.security.NoSuchAlgorithmException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * Create a digest for a file.
@@ -72,36 +70,8 @@ public abstract class AbstractDigester
     public void verify( File file, String checksum )
         throws DigesterException
     {
-        String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
-
-        String algorithm = streamingDigester.getAlgorithm();
-
-        // Free-BSD / openssl
-        Matcher m = Pattern.compile( algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-zA-Z0-9]+)" )
-            .matcher( trimmedChecksum );
-        if ( m.matches() )
-        {
-            String filename = m.group( 1 );
-            if ( !filename.equals( file.getName() ) )
-            {
-                throw new DigesterException( "Supplied checksum does not match checksum pattern" );
-            }
-            trimmedChecksum = m.group( 2 );
-        }
-        else
-        {
-            // GNU tools
-            m = Pattern.compile( "([a-zA-Z0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum );
-            if ( m.matches() )
-            {
-                String filename = m.group( 2 );
-                if ( !filename.equals( file.getName() ) )
-                {
-                    throw new DigesterException( "Supplied checksum does not match checksum pattern" );
-                }
-                trimmedChecksum = m.group( 1 );
-            }
-        }
+        String trimmedChecksum =
+            DigestUtils.cleanChecksum( checksum, streamingDigester.getAlgorithm(), file.getName() );
 
         //Create checksum for jar file
         String sum = calc( file );
diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigestUtils.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigestUtils.java
new file mode 100644 (file)
index 0000000..8bc76b8
--- /dev/null
@@ -0,0 +1,67 @@
+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.
+ */
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Parse files from checksum file formats.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public class DigestUtils
+{
+    private DigestUtils()
+    {
+        // don't create this class
+    }
+
+    public static String cleanChecksum( String checksum, String algorithm, String path )
+        throws DigesterException
+    {
+        String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
+
+        // Free-BSD / openssl
+        String regex = algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-fA-F0-9]+)";
+        Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
+        if ( m.matches() )
+        {
+            String filename = m.group( 1 );
+            if ( !path.endsWith( filename ) )
+            {
+                throw new DigesterException( "Supplied checksum does not match checksum pattern" );
+            }
+            trimmedChecksum = m.group( 2 );
+        }
+        else
+        {
+            // GNU tools
+            m = Pattern.compile( "([a-fA-F0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum );
+            if ( m.matches() )
+            {
+                String filename = m.group( 2 );
+                if ( !path.endsWith( filename ) )
+                {
+                    throw new DigesterException( "Supplied checksum does not match checksum pattern" );
+                }
+                trimmedChecksum = m.group( 1 );
+            }
+        }
+        return trimmedChecksum;
+    }
+}