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;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang.StringUtils;
31 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
34 public class ChecksummedFileTest
35 extends AbstractChecksumTestCase
37 /** SHA1 checksum from www.ibiblio.org/maven2, incuding file path */
38 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
40 private File createTestableJar( String filename )
43 File srcFile = getTestResource( filename );
44 File destFile = new File( getTestOutputDir(), srcFile.getName() );
45 FileUtils.copyFile( srcFile, destFile );
49 private File createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
52 File srcFile = getTestResource( filename );
53 File jarFile = new File( getTestOutputDir(), srcFile.getName() );
54 FileUtils.copyFile( srcFile, jarFile );
58 File srcSha1 = new File( srcFile.getAbsolutePath() + ".sha1" );
59 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
61 FileUtils.copyFile( srcSha1, sha1File );
66 File srcMd5 = new File( srcFile.getAbsolutePath() + ".md5" );
67 File md5File = new File( jarFile.getAbsolutePath() + ".md5" );
69 FileUtils.copyFile( srcMd5, md5File );
75 public void testCalculateChecksumMd5()
78 File testfile = getTestResource( "examples/redback-authz-open.jar" );
79 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
80 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
81 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
82 assertEquals( expectedChecksum, actualChecksum );
85 public void testCalculateChecksumSha1()
88 File testfile = getTestResource( "examples/redback-authz-open.jar" );
89 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
90 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
91 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
92 assertEquals( expectedChecksum, actualChecksum );
95 public void testCreateChecksum()
98 File testableJar = createTestableJar( "examples/redback-authz-open.jar" );
99 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
100 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
101 File hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
102 assertTrue( "ChecksumAlgorithm file should exist.", hashFile.exists() );
103 String hashContents = FileUtils.readFileToString( hashFile );
104 hashContents = StringUtils.trim( hashContents );
105 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
108 public void testFixChecksum()
111 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
112 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
114 // A typical scenario seen in the wild.
115 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
117 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
118 assertFalse( "ChecksummedFile.isValid(SHA1) == false", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
120 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
121 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
123 assertTrue( "ChecksummedFile.isValid(SHA1) == true", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
126 public void testGetChecksumFile()
128 ChecksummedFile checksummedFile = new ChecksummedFile( new File( "test.jar" ) );
129 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getName() );
132 public void testIsValidChecksum()
135 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
137 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
138 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
141 public void testIsValidChecksumInvalidSha1Format()
144 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
145 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
147 // A typical scenario seen in the wild.
148 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
150 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
151 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
155 public void testIsValidChecksumNoChecksumFiles()
158 File jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
160 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
161 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
162 ChecksumAlgorithm.SHA1,
163 ChecksumAlgorithm.MD5 } ) );
167 public void testIsValidChecksumSha1AndMd5()
170 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
172 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
173 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
174 ChecksumAlgorithm.SHA1,
175 ChecksumAlgorithm.MD5 } ) );
178 public void testIsValidChecksumSha1NoMd5()
181 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
183 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
184 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
185 ChecksumAlgorithm.SHA1,
186 ChecksumAlgorithm.MD5 } ) );
190 public void testParseChecksum()
193 String expected = SERVLETAPI_SHA1
194 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom";
196 File testfile = getTestResource( "examples/redback-authz-open.jar" );
197 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
198 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
199 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
200 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
204 public void testParseChecksumAltDash1()
207 String expected = SERVLETAPI_SHA1 + " -";
208 File testfile = getTestResource( "examples/redback-authz-open.jar" );
209 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
210 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
211 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
212 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
215 public void testParseChecksumAltDash2()
218 String expected = "SHA1(-)=" + SERVLETAPI_SHA1;
219 File testfile = getTestResource( "examples/redback-authz-open.jar" );
220 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
221 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
222 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
223 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );