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;
33 public class ChecksummedFileTest
34 extends AbstractChecksumTestCase
36 /** SHA1 checksum from www.ibiblio.org/maven2, incuding file path */
37 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
39 private static final String REMOTE_METADATA_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
41 private static final String REMOTE_METADATA_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
43 private File createTestableJar( String filename )
46 File srcFile = getTestResource( filename );
47 File destFile = new File( getTestOutputDir(), srcFile.getName() );
48 FileUtils.copyFile( srcFile, destFile );
52 private File createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
55 File srcFile = getTestResource( filename );
56 File jarFile = new File( getTestOutputDir(), srcFile.getName() );
57 FileUtils.copyFile( srcFile, jarFile );
61 File srcSha1 = new File( srcFile.getAbsolutePath() + ".sha1" );
62 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
64 FileUtils.copyFile( srcSha1, sha1File );
69 File srcMd5 = new File( srcFile.getAbsolutePath() + ".md5" );
70 File md5File = new File( jarFile.getAbsolutePath() + ".md5" );
72 FileUtils.copyFile( srcMd5, md5File );
78 public void testCalculateChecksumMd5()
81 File testfile = getTestResource( "examples/redback-authz-open.jar" );
82 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
83 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
84 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
85 assertEquals( expectedChecksum, actualChecksum );
88 public void testCalculateChecksumSha1()
91 File testfile = getTestResource( "examples/redback-authz-open.jar" );
92 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
93 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
94 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
95 assertEquals( expectedChecksum, actualChecksum );
98 public void testCreateChecksum()
101 File testableJar = createTestableJar( "examples/redback-authz-open.jar" );
102 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
103 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
104 File hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
105 assertTrue( "ChecksumAlgorithm file should exist.", hashFile.exists() );
106 String hashContents = FileUtils.readFileToString( hashFile );
107 hashContents = StringUtils.trim( hashContents );
108 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
111 public void testFixChecksum()
114 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
115 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
117 // A typical scenario seen in the wild.
118 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
120 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
121 assertFalse( "ChecksummedFile.isValid(SHA1) == false", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
123 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
124 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
126 assertTrue( "ChecksummedFile.isValid(SHA1) == true", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
129 public void testGetChecksumFile()
131 ChecksummedFile checksummedFile = new ChecksummedFile( new File( "test.jar" ) );
132 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getName() );
135 public void testIsValidChecksum()
138 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
140 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
141 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
144 public void testIsValidChecksumInvalidSha1Format()
147 File jarFile = createTestableJar( "examples/redback-authz-open.jar" );
148 File sha1File = new File( jarFile.getAbsolutePath() + ".sha1" );
150 // A typical scenario seen in the wild.
151 FileUtils.writeStringToFile( sha1File, "sha1sum: redback-authz-open.jar: No such file or directory" );
153 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
154 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
158 public void testIsValidChecksumNoChecksumFiles()
161 File jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
163 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
164 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
165 ChecksumAlgorithm.SHA1,
166 ChecksumAlgorithm.MD5 } ) );
170 public void testIsValidChecksumSha1AndMd5()
173 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
175 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
176 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
177 ChecksumAlgorithm.SHA1,
178 ChecksumAlgorithm.MD5 } ) );
181 public void testIsValidChecksumSha1NoMd5()
184 File jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
186 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
187 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums( new ChecksumAlgorithm[] {
188 ChecksumAlgorithm.SHA1,
189 ChecksumAlgorithm.MD5 } ) );
193 public void testParseChecksum()
196 String expected = SERVLETAPI_SHA1
197 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom";
199 File testfile = getTestResource( "examples/redback-authz-open.jar" );
200 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
201 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
202 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
203 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
207 public void testParseChecksumAltDash1()
210 String expected = SERVLETAPI_SHA1 + " -";
211 File testfile = getTestResource( "examples/redback-authz-open.jar" );
212 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
213 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
214 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
215 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
218 public void testParseChecksumAltDash2()
221 String expected = "SHA1(-)=" + SERVLETAPI_SHA1;
222 File testfile = getTestResource( "examples/redback-authz-open.jar" );
223 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
224 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
225 "servletapi/servletapi/2.4/servletapi-2.4.pom" );
226 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
229 public void testRemoteMetadataChecksumFilePathSha1()
232 String expected = REMOTE_METADATA_SHA1 + " /home/test/repository/examples/metadata/maven-metadata.xml";
233 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
234 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
238 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.SHA1,
239 "maven-metadata-remote.xml" );
240 assertEquals( "Checksum doesn't match", REMOTE_METADATA_SHA1, s );
242 catch ( IOException e )
245 fail( "IOException should not occur." );
249 public void testRemoteMetadataChecksumFilePathMd5()
252 String expected = REMOTE_METADATA_MD5 + " ./examples/metadata/maven-metadata.xml";
253 File testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
254 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
258 String s = checksummedFile.parseChecksum( expected, ChecksumAlgorithm.MD5,
259 "maven-metadata-remote.xml" );
260 assertEquals( "Checksum doesn't match", REMOTE_METADATA_MD5, s );
262 catch ( IOException e )
265 fail( "IOException should not occur." );