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
23 import java.io.IOException;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.junit.Before;
30 import org.junit.Test;
38 public class ChecksummedFileTest
39 extends AbstractChecksumTestCase
41 /** SHA1 checksum from www.ibiblio.org/maven2, incuding file path */
42 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
44 private static final String REMOTE_METADATA_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
46 private static final String REMOTE_METADATA_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
50 public void cleanTestDir()
54 FileUtils.deleteDirectory(getTestOutputDir());
55 } catch (IOException ex) {
56 Logger.getLogger(ChecksummedFileTest.class.getName()).log(Level.SEVERE, null, ex);
60 private File createTestableJar( String filename )
63 File srcFile = getTestResource( filename );
64 File destFile = new File( getTestOutputDir(), srcFile.getName() );
65 FileUtils.copyFile( srcFile, destFile );
69 private File createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
72 File srcFile = getTestResource( filename );
73 File jarFile = new File( getTestOutputDir(), srcFile.getName() );
74 FileUtils.copyFile( srcFile, jarFile );
78 File srcSha1 = new File( srcFile.getAbsolutePath() + ".sha1" );
79 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
81 FileUtils.copyFile( srcSha1, sha1File );
86 File srcMd5 = new File( srcFile.getAbsolutePath() + ".md5" );
87 File md5File = new File( jarFile.getAbsolutePath() + ".md5" );
89 FileUtils.copyFile( srcMd5, md5File );
96 public void testCalculateChecksumMd5()
99 File testfile = getTestResource( "examples/redback-authz-open.jar" );
100 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
101 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
102 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
103 assertEquals( expectedChecksum, actualChecksum );
107 public void testCalculateChecksumSha1()
110 File testfile = getTestResource( "examples/redback-authz-open.jar" );
111 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
112 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
113 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
114 assertEquals( expectedChecksum, actualChecksum );
118 public void testCreateChecksum()
121 File testableJar = createTestableJar( "examples/redback-authz-open.jar" );
122 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
123 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
124 File hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
125 assertTrue( "ChecksumAlgorithm file should exist.", hashFile.exists() );
126 String hashContents = FileUtils.readFileToString( hashFile );
127 hashContents = StringUtils.trim( hashContents );
128 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
132 public void testFixChecksum()
135 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
136 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
138 // A typical scenario seen in the wild.
139 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
141 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
142 assertFalse( "ChecksummedFile.isValid(SHA1) == false", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
144 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
145 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
147 assertTrue( "ChecksummedFile.isValid(SHA1) == true", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
151 public void testGetChecksumFile()
153 ChecksummedFile checksummedFile = new ChecksummedFile( new File( "test.jar" ) );
154 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getName() );
158 public void testIsValidChecksum()
161 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
163 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
164 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
168 public void testIsValidChecksumInvalidSha1Format()
171 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
172 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
174 // A typical scenario seen in the wild.
175 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
177 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
178 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
183 public void testIsValidChecksumNoChecksumFiles()
186 File jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
188 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
189 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
190 ChecksumAlgorithm.SHA1,
191 ChecksumAlgorithm.MD5 } ) );
196 public void testIsValidChecksumSha1AndMd5()
199 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
201 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
202 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
203 ChecksumAlgorithm.SHA1,
204 ChecksumAlgorithm.MD5 } ) );
208 public void testIsValidChecksumSha1NoMd5()
211 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
213 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
214 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
215 ChecksumAlgorithm.SHA1,
216 ChecksumAlgorithm.MD5 } ) );
221 public void testParseChecksum()
224 String expected = SERVLETAPI_SHA1
225 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom";
227 File testfile = getTestResource( "examples/redback-authz-open.jar" );
228 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
229 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
230 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
231 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
236 public void testParseChecksumAltDash1()
239 String expected = SERVLETAPI_SHA1 + " -";
240 File testfile = getTestResource( "examples/redback-authz-open.jar" );
241 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
242 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
243 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
244 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
248 public void testParseChecksumAltDash2()
251 String expected = "SHA1(-)=" + SERVLETAPI_SHA1;
252 File testfile = getTestResource( "examples/redback-authz-open.jar" );
253 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
254 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
255 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
256 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
260 public void testRemoteMetadataChecksumFilePathSha1()
263 String expected = REMOTE_METADATA_SHA1 + " /home/test/repository/examples/metadata/maven-metadata.xml";
264 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
265 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
269 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
270 "maven-metadata-remote.xml" );
271 assertEquals( "Checksum doesn't match", REMOTE_METADATA_SHA1, s );
273 catch ( IOException e )
276 fail( "IOException should not occur." );
281 public void testRemoteMetadataChecksumFilePathMd5()
284 String expected = REMOTE_METADATA_MD5 + " ./examples/metadata/maven-metadata.xml";
285 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
286 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
290 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.MD5,
291 "maven-metadata-remote.xml" );
292 assertEquals( "Checksum doesn't match", REMOTE_METADATA_MD5, s );
294 catch ( IOException e )
297 fail( "IOException should not occur." );