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;
27 import org.junit.Test;
35 public class ChecksummedFileTest
36 extends AbstractChecksumTestCase
38 /** SHA1 checksum from www.ibiblio.org/maven2, incuding file path */
39 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
41 private static final String REMOTE_METADATA_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
43 private static final String REMOTE_METADATA_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
45 private File createTestableJar( String filename )
48 File srcFile = getTestResource( filename );
49 File destFile = new File( getTestOutputDir(), srcFile.getName() );
50 FileUtils.copyFile( srcFile, destFile );
54 private File createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
57 File srcFile = getTestResource( filename );
58 File jarFile = new File( getTestOutputDir(), srcFile.getName() );
59 FileUtils.copyFile( srcFile, jarFile );
63 File srcSha1 = new File( srcFile.getAbsolutePath() + ".sha1" );
64 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
66 FileUtils.copyFile( srcSha1, sha1File );
71 File srcMd5 = new File( srcFile.getAbsolutePath() + ".md5" );
72 File md5File = new File( jarFile.getAbsolutePath() + ".md5" );
74 FileUtils.copyFile( srcMd5, md5File );
81 public void testCalculateChecksumMd5()
84 File testfile = getTestResource( "examples/redback-authz-open.jar" );
85 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
86 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
87 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
88 assertEquals( expectedChecksum, actualChecksum );
92 public void testCalculateChecksumSha1()
95 File testfile = getTestResource( "examples/redback-authz-open.jar" );
96 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
97 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
98 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
99 assertEquals( expectedChecksum, actualChecksum );
103 public void testCreateChecksum()
106 File testableJar = createTestableJar( "examples/redback-authz-open.jar" );
107 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
108 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
109 File hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
110 assertTrue( "ChecksumAlgorithm file should exist.", hashFile.exists() );
111 String hashContents = FileUtils.readFileToString( hashFile );
112 hashContents = StringUtils.trim( hashContents );
113 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
117 public void testFixChecksum()
120 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
121 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
123 // A typical scenario seen in the wild.
124 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
126 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
127 assertFalse( "ChecksummedFile.isValid(SHA1) == false", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
129 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
130 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
132 assertTrue( "ChecksummedFile.isValid(SHA1) == true", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
136 public void testGetChecksumFile()
138 ChecksummedFile checksummedFile = new ChecksummedFile( new File( "test.jar" ) );
139 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getName() );
143 public void testIsValidChecksum()
146 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
148 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
149 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
153 public void testIsValidChecksumInvalidSha1Format()
156 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
157 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
159 // A typical scenario seen in the wild.
160 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
162 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
163 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
168 public void testIsValidChecksumNoChecksumFiles()
171 File jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
173 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
174 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
175 ChecksumAlgorithm.SHA1,
176 ChecksumAlgorithm.MD5 } ) );
181 public void testIsValidChecksumSha1AndMd5()
184 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
186 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
187 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
188 ChecksumAlgorithm.SHA1,
189 ChecksumAlgorithm.MD5 } ) );
193 public void testIsValidChecksumSha1NoMd5()
196 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
198 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
199 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
200 ChecksumAlgorithm.SHA1,
201 ChecksumAlgorithm.MD5 } ) );
206 public void testParseChecksum()
209 String expected = SERVLETAPI_SHA1
210 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom";
212 File testfile = getTestResource( "examples/redback-authz-open.jar" );
213 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
214 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
215 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
216 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
221 public void testParseChecksumAltDash1()
224 String expected = SERVLETAPI_SHA1 + " -";
225 File testfile = getTestResource( "examples/redback-authz-open.jar" );
226 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
227 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
228 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
229 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
233 public void testParseChecksumAltDash2()
236 String expected = "SHA1(-)=" + SERVLETAPI_SHA1;
237 File testfile = getTestResource( "examples/redback-authz-open.jar" );
238 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
239 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
240 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
241 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
245 public void testRemoteMetadataChecksumFilePathSha1()
248 String expected = REMOTE_METADATA_SHA1 + " /home/test/repository/examples/metadata/maven-metadata.xml";
249 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
250 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
254 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
255 "maven-metadata-remote.xml" );
256 assertEquals( "Checksum doesn't match", REMOTE_METADATA_SHA1, s );
258 catch ( IOException e )
261 fail( "IOException should not occur." );
266 public void testRemoteMetadataChecksumFilePathMd5()
269 String expected = REMOTE_METADATA_MD5 + " ./examples/metadata/maven-metadata.xml";
270 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
271 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
275 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.MD5,
276 "maven-metadata-remote.xml" );
277 assertEquals( "Checksum doesn't match", REMOTE_METADATA_MD5, s );
279 catch ( IOException e )
282 fail( "IOException should not occur." );