1 package org.apache.archiva.checksum;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.slf4j.LoggerFactory;
29 import java.io.IOException;
37 public class ChecksummedFileTest
38 extends AbstractChecksumTestCase
41 * SHA1 checksum from www.ibiblio.org/maven2, incuding file path
43 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
45 private static final String REMOTE_METADATA_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
47 private static final String REMOTE_METADATA_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
51 public void cleanTestDir()
55 FileUtils.deleteDirectory( getTestOutputDir() );
57 catch ( IOException ex )
59 LoggerFactory.getLogger( ChecksummedFileTest.class ).warn( ex.getMessage(), ex );
63 private File createTestableJar( String filename )
66 File srcFile = getTestResource( filename );
67 File destFile = new File( getTestOutputDir(), srcFile.getName() );
68 FileUtils.copyFile( srcFile, destFile );
72 private File createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
75 File srcFile = getTestResource( filename );
76 File jarFile = new File( getTestOutputDir(), srcFile.getName() );
77 FileUtils.copyFile( srcFile, jarFile );
81 File srcSha1 = new File( srcFile.getAbsolutePath() + ".sha1" );
82 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
84 FileUtils.copyFile( srcSha1, sha1File );
89 File srcMd5 = new File( srcFile.getAbsolutePath() + ".md5" );
90 File md5File = new File( jarFile.getAbsolutePath() + ".md5" );
92 FileUtils.copyFile( srcMd5, md5File );
99 public void testCalculateChecksumMd5()
102 File testfile = getTestResource( "examples/redback-authz-open.jar" );
103 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
104 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
105 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
106 assertEquals( expectedChecksum, actualChecksum );
110 public void testCalculateChecksumSha1()
113 File testfile = getTestResource( "examples/redback-authz-open.jar" );
114 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
115 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
116 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
117 assertEquals( expectedChecksum, actualChecksum );
121 public void testCreateChecksum()
124 File testableJar = createTestableJar( "examples/redback-authz-open.jar" );
125 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
126 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
127 File hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
128 assertTrue( "ChecksumAlgorithm file should exist.", hashFile.exists() );
129 String hashContents = FileUtils.readFileToString( hashFile );
130 hashContents = StringUtils.trim( hashContents );
131 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
135 public void testFixChecksum()
138 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
139 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
141 // A typical scenario seen in the wild.
142 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
144 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
145 assertFalse( "ChecksummedFile.isValid(SHA1) == false",
146 checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
148 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
149 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
151 assertTrue( "ChecksummedFile.isValid(SHA1) == true",
152 checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
156 public void testGetChecksumFile()
158 ChecksummedFile checksummedFile = new ChecksummedFile( new File( "test.jar" ) );
159 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getName() );
163 public void testIsValidChecksum()
166 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
168 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
169 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
173 public void testIsValidChecksumInvalidSha1Format()
176 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
177 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
179 // A typical scenario seen in the wild.
180 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
182 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
183 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
188 public void testIsValidChecksumNoChecksumFiles()
191 File jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
193 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
194 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
195 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
200 public void testIsValidChecksumSha1AndMd5()
203 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
205 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
206 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
207 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
211 public void testIsValidChecksumSha1NoMd5()
214 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
216 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
217 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums(
218 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
223 public void testParseChecksum()
226 String expected = SERVLETAPI_SHA1
227 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom";
229 File testfile = getTestResource( "examples/redback-authz-open.jar" );
230 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
231 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
232 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
233 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
238 public void testParseChecksumAltDash1()
241 String expected = SERVLETAPI_SHA1 + " -";
242 File testfile = getTestResource( "examples/redback-authz-open.jar" );
243 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
244 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
245 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
246 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
250 public void testParseChecksumAltDash2()
253 String expected = "SHA1(-)=" + SERVLETAPI_SHA1;
254 File testfile = getTestResource( "examples/redback-authz-open.jar" );
255 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
256 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
257 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
258 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
262 public void testRemoteMetadataChecksumFilePathSha1()
265 String expected = REMOTE_METADATA_SHA1 + " /home/test/repository/examples/metadata/maven-metadata.xml";
266 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
267 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
271 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1, "maven-metadata-remote.xml" );
272 assertEquals( "Checksum doesn't match", REMOTE_METADATA_SHA1, s );
274 catch ( IOException e )
277 fail( "IOException should not occur." );
282 public void testRemoteMetadataChecksumFilePathMd5()
285 String expected = REMOTE_METADATA_MD5 + " ./examples/metadata/maven-metadata.xml";
286 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
287 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
291 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.MD5, "maven-metadata-remote.xml" );
292 assertEquals( "Checksum doesn't match", REMOTE_METADATA_MD5, s );
294 catch ( IOException e )
297 fail( "IOException should not occur." );