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.archiva.common.utils.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;
30 import java.nio.charset.Charset;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.nio.file.StandardCopyOption;
42 public class ChecksummedFileTest
43 extends AbstractChecksumTestCase
46 * SHA1 checksum from www.ibiblio.org/maven2, incuding file path
48 private static final String SERVLETAPI_SHA1 = "bcc82975c0f9c681fcb01cc38504c992553e93ba";
50 private static final String REMOTE_METADATA_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
52 private static final String REMOTE_METADATA_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
54 private static final Charset FILE_ENCODING = Charset.forName( "UTF-8" );
58 public void cleanTestDir()
62 FileUtils.deleteDirectory( getTestOutputDir() );
64 catch ( IOException ex )
66 LoggerFactory.getLogger( ChecksummedFileTest.class ).warn( ex.getMessage(), ex );
70 private Path createTestableJar( String filename )
73 Path srcFile = getTestResource( filename );
74 Path destFile = getTestOutputDir( ).resolve( srcFile.getFileName());
75 Files.copy( srcFile, destFile, StandardCopyOption.REPLACE_EXISTING );
79 private Path createTestableJar( String filename, boolean copySha1, boolean copyMd5 )
82 Path srcFile = getTestResource( filename );
83 Path jarFile = getTestOutputDir().resolve(srcFile.getFileName() );
84 Files.copy( srcFile, jarFile, StandardCopyOption.REPLACE_EXISTING );
88 Path srcSha1 = srcFile.resolveSibling( srcFile.getFileName() + ".sha1" );
89 Path sha1File = jarFile.resolveSibling( jarFile.getFileName() + ".sha1" );
91 Files.copy( srcSha1, sha1File, StandardCopyOption.REPLACE_EXISTING );
96 Path srcMd5 = srcFile.resolveSibling( srcFile.getFileName() + ".md5" );
97 Path md5File = jarFile.resolveSibling( jarFile.getFileName() + ".md5" );
99 Files.copy( srcMd5, md5File, StandardCopyOption.REPLACE_EXISTING );
106 public void testCalculateChecksumMd5()
109 Path testfile = getTestResource( "examples/redback-authz-open.jar" );
110 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
111 String expectedChecksum = "f42047fe2e177ac04d0df7aa44d408be";
112 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 );
113 assertEquals( expectedChecksum, actualChecksum );
117 public void testCalculateChecksumSha1()
120 Path testfile = getTestResource( "examples/redback-authz-open.jar" );
121 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
122 String expectedChecksum = "2bb14b388973351b0a4dfe11d171965f59cc61a1";
123 String actualChecksum = checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 );
124 assertEquals( expectedChecksum, actualChecksum );
128 public void testCreateChecksum()
131 Path testableJar = createTestableJar( "examples/redback-authz-open.jar" );
132 ChecksummedFile checksummedFile = new ChecksummedFile( testableJar );
133 checksummedFile.createChecksum( ChecksumAlgorithm.SHA1 );
134 Path hashFile = checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 );
135 assertTrue( "ChecksumAlgorithm file should exist.", Files.exists(hashFile) );
136 String hashContents = org.apache.commons.io.FileUtils.readFileToString( hashFile.toFile() );
137 hashContents = StringUtils.trim( hashContents );
138 assertEquals( "2bb14b388973351b0a4dfe11d171965f59cc61a1 redback-authz-open.jar", hashContents );
142 public void testFixChecksum()
143 throws IOException, ChecksumValidationException
145 Path jarFile = createTestableJar( "examples/redback-authz-open.jar" );
146 Path sha1File = jarFile.resolveSibling( jarFile.getFileName()+ ".sha1" );
148 // A typical scenario seen in the wild.
149 org.apache.commons.io.FileUtils.writeStringToFile( sha1File.toFile(), "sha1sum: redback-authz-open.jar: No such file or directory" );
151 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
152 assertFalse( "ChecksummedFile.isValid(SHA1) == false",
153 checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
155 boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
156 assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
158 assertTrue( "ChecksummedFile.isValid(SHA1) == true",
159 checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
163 public void testGetChecksumFile()
165 ChecksummedFile checksummedFile = new ChecksummedFile( Paths.get( "test.jar" ) );
166 assertEquals( "test.jar.sha1", checksummedFile.getChecksumFile( ChecksumAlgorithm.SHA1 ).getFileName().toString() );
170 public void testIsValidChecksum()
171 throws IOException, ChecksumValidationException
173 Path jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
175 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
176 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
180 public void testIsValidChecksumInvalidSha1Format()
181 throws IOException, ChecksumValidationException
183 Path jarFile = createTestableJar( "examples/redback-authz-open.jar" );
184 Path sha1File = jarFile.resolveSibling( jarFile.getFileName() + ".sha1" );
186 // A typical scenario seen in the wild.
187 FileUtils.writeStringToFile( sha1File, FILE_ENCODING, "sha1sum: redback-authz-open.jar: No such file or directory" );
189 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
190 assertFalse( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
195 public void testIsValidChecksumNoChecksumFiles()
196 throws IOException, ChecksumValidationException
198 Path jarFile = createTestableJar( "examples/redback-authz-open.jar", false, false );
200 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
201 assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
202 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
207 public void testIsValidChecksumSha1AndMd5()
208 throws IOException, ChecksumValidationException
210 Path jarFile = createTestableJar( "examples/redback-authz-open.jar", true, true );
212 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
213 assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
214 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
218 public void testIsValidChecksumSha1NoMd5()
219 throws IOException, ChecksumValidationException
221 Path jarFile = createTestableJar( "examples/redback-authz-open.jar", true, false );
223 ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
224 assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums(
225 new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
230 public void testParseChecksum()
231 throws IOException, ChecksumValidationException
233 Path expectedFile = getTestOutputDir().resolve("servletapi-2.4.pom.sha1");
234 FileUtils.writeStringToFile( expectedFile, FILE_ENCODING, SERVLETAPI_SHA1
235 + " /home/projects/maven/repository-staging/to-ibiblio/maven2/servletapi/servletapi/2.4/servletapi-2.4.pom");
237 Path testfile = getTestResource( "examples/redback-authz-open.jar" );
238 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
239 String s = checksummedFile.parseChecksum( expectedFile, ChecksumAlgorithm.SHA1,
240 "servletapi/servletapi/2.4/servletapi-2.4.pom", FILE_ENCODING);
241 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
246 public void testParseChecksumAltDash1()
247 throws IOException, ChecksumValidationException
249 Path expectedFile = getTestOutputDir().resolve("redback-authz-open.jar.sha1");
250 FileUtils.writeStringToFile( expectedFile, FILE_ENCODING, SERVLETAPI_SHA1 + " -");
251 Path testfile = getTestResource( "examples/redback-authz-open.jar" );
252 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
253 String s = checksummedFile.parseChecksum( expectedFile, ChecksumAlgorithm.SHA1,
254 "servletapi/servletapi/2.4/servletapi-2.4.pom", FILE_ENCODING );
255 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
259 public void testParseChecksumAltDash2()
260 throws IOException, ChecksumValidationException
262 Path expectedFile = getTestOutputDir().resolve("redback-authz-open.jar.sha1");
263 FileUtils.writeStringToFile( expectedFile, FILE_ENCODING, "SHA1(-)=" + SERVLETAPI_SHA1);
264 Path testfile = getTestResource( "examples/redback-authz-open.jar" );
265 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
266 String s = checksummedFile.parseChecksum( expectedFile, ChecksumAlgorithm.SHA1,
267 "servletapi/servletapi/2.4/servletapi-2.4.pom" , FILE_ENCODING);
268 assertEquals( "Checksum doesn't match", SERVLETAPI_SHA1, s );
272 public void testRemoteMetadataChecksumFilePathSha1()
275 Path expectedFile = getTestOutputDir().resolve("maven-metadata-remote.xml.sha1");
276 FileUtils.writeStringToFile( expectedFile, FILE_ENCODING, REMOTE_METADATA_SHA1 + " /home/test/repository/examples/metadata/maven-metadata.xml");
277 Path testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
278 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
282 String s = checksummedFile.parseChecksum( expectedFile, ChecksumAlgorithm.SHA1, "maven-metadata-remote.xml", FILE_ENCODING );
283 assertEquals( "Checksum doesn't match", REMOTE_METADATA_SHA1, s );
285 catch ( ChecksumValidationException e )
288 fail( "IOException should not occur." );
293 public void testRemoteMetadataChecksumFilePathMd5()
296 Path expectedFile = getTestOutputDir().resolve( "maven-metadata.xml.md5" );
297 FileUtils.writeStringToFile( expectedFile, FILE_ENCODING, REMOTE_METADATA_MD5 + " ./examples/metadata/maven-metadata.xml");
298 Path testfile = getTestResource( "examples/metadata/maven-metadata-remote.xml" );
299 ChecksummedFile checksummedFile = new ChecksummedFile( testfile );
303 String s = checksummedFile.parseChecksum( expectedFile, ChecksumAlgorithm.MD5, "maven-metadata-remote.xml", FILE_ENCODING );
304 assertEquals( "Checksum doesn't match", REMOTE_METADATA_MD5, s );
306 catch ( ChecksumValidationException e )
309 fail( "IOException should not occur." );