1 package org.apache.archiva.policies;
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.filelock.DefaultFileLockManager;
23 import org.apache.archiva.repository.storage.FilesystemStorage;
24 import org.apache.archiva.repository.storage.StorageAsset;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TestName;
30 import org.junit.runner.RunWith;
31 import org.springframework.test.context.ContextConfiguration;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.io.BufferedReader;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.Properties;
43 import static org.junit.Assert.*;
50 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
51 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
52 public class ChecksumPolicyTest
54 private static final String GOOD = "good";
56 private static final String BAD = "bad";
58 private static FilesystemStorage filesystemStorage;
61 @Named( value = "postDownloadPolicy#checksum" )
62 PostDownloadPolicy downloadPolicy;
65 public TestName name = new TestName();
67 private PostDownloadPolicy lookupPolicy()
70 return downloadPolicy;
74 public void testFailOnFileOnly()
77 assertFailSetting( false, null, null );
81 public void testFailOnFileWithBadMd5AndBadSha1()
84 assertFailSetting( false, BAD, BAD );
88 public void testFailOnFileWithBadMd5AndGoodSha1()
91 assertFailSetting( false, BAD, GOOD );
95 public void testFailOnFileWithBadMd5Only()
98 assertFailSetting( false, BAD, null );
102 public void testFailOnFileWithBadSha1Only()
105 assertFailSetting( false, null, BAD );
109 public void testFailOnFileWithGoodMd5AndBadSha1()
112 assertFailSetting( false, GOOD, BAD );
116 public void testFailOnFileWithGoodMd5AndGoodSha1()
119 assertFailSetting( true, GOOD, GOOD );
123 public void testFailOnFileWithGoodMd5Only()
126 assertFailSetting( true, GOOD, null );
130 public void testFailOnFileWithGoodSha1Only()
133 assertFailSetting( true, null, GOOD );
137 public void testFixOnFileOnly()
140 assertFixSetting( true, null, null );
144 public void testFixOnFileWithBadMd5AndBadSha1()
147 assertFixSetting( true, BAD, BAD );
151 public void testFixOnFileWithBadMd5AndGoodSha1()
154 assertFixSetting( true, BAD, GOOD );
158 public void testFixOnFileWithBadMd5Only()
161 assertFixSetting( true, BAD, null );
165 public void testFixOnFileWithBadSha1Only()
168 assertFixSetting( true, null, BAD );
172 public void testFixOnFileWithGoodMd5AndBadSha1()
175 assertFixSetting( true, GOOD, BAD );
179 public void testFixOnFileWithGoodMd5AndGoodSha1()
182 assertFixSetting( true, GOOD, GOOD );
186 public void testFixOnFileWithGoodMd5Only()
189 assertFixSetting( true, GOOD, null );
193 public void testFixOnFileWithGoodSha1Only()
196 assertFixSetting( true, null, GOOD );
200 public void testIgnore()
203 PostDownloadPolicy policy = lookupPolicy();
204 StorageAsset localFile = createTestableFiles( null, null );
205 Properties request = createRequest();
207 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
210 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
213 PostDownloadPolicy policy = lookupPolicy();
214 StorageAsset localFile = createTestableFiles( md5State, sha1State );
215 Properties request = createRequest();
217 boolean actualResult;
221 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
224 catch ( PolicyViolationException e )
226 actualResult = false;
227 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
229 assertFalse( msg + " local file should not exist:", localFile.exists() );
230 Path md5File = localFile.getFilePath().toAbsolutePath().resolveSibling( localFile.getName() + ".sha1" );
231 Path sha1File = localFile.getFilePath().toAbsolutePath().resolveSibling( localFile.getName() + ".md5" );
232 assertFalse( msg + " local md5 file should not exist:", Files.exists(md5File) );
233 assertFalse( msg + " local sha1 file should not exist:", Files.exists(sha1File) );
236 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
239 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
242 PostDownloadPolicy policy = lookupPolicy();
243 StorageAsset localFile = createTestableFiles( md5State, sha1State );
244 Properties request = createRequest();
246 boolean actualResult;
250 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
253 catch ( PolicyViolationException e )
255 actualResult = false;
258 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
260 // End result should be legitimate SHA1 and MD5 files.
261 Path md5File = localFile.getFilePath().toAbsolutePath().resolveSibling( localFile.getName() + ".md5" );
262 Path sha1File = localFile.getFilePath().toAbsolutePath().resolveSibling( localFile.getName() + ".sha1" );
264 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", Files.exists(md5File) && Files.isRegularFile(md5File) );
265 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", Files.exists(sha1File) && Files.isRegularFile(sha1File) );
267 String actualMd5Contents = readChecksumFile( md5File );
268 String actualSha1Contents = readChecksumFile( sha1File );
270 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
271 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
273 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
274 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
278 * Read the first line from the checksum file, and return it (trimmed).
280 private String readChecksumFile( Path checksumFile )
283 FileReader freader = null;
284 BufferedReader buf = null;
288 freader = new FileReader( checksumFile.toFile() );
289 buf = new BufferedReader( freader );
290 return buf.readLine();
299 if ( freader != null )
306 private String createMessage( String settingType, String md5State, String sha1State )
308 StringBuilder msg = new StringBuilder();
309 msg.append( "Expected result of ChecksumPolicy.apply(" );
310 msg.append( settingType.toUpperCase() );
311 msg.append( ") when working with " );
312 if ( md5State == null )
318 msg.append( "a " ).append( md5State.toUpperCase() );
321 msg.append( " MD5 and " );
323 if ( sha1State == null )
329 msg.append( "a " ).append( sha1State.toUpperCase() );
331 msg.append( " SHA1:" );
333 return msg.toString();
336 private Properties createRequest()
338 Properties request = new Properties();
340 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
345 private StorageAsset createTestableFiles(String md5State, String sha1State )
348 FilesystemStorage fs = new FilesystemStorage(Paths.get("target/checksum-tests"), new DefaultFileLockManager());
349 StorageAsset sourceDir = getTestFile( "src/test/resources/checksums/" );
350 StorageAsset destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
352 FileUtils.copyFileToDirectory( sourceDir.getFilePath().resolve("artifact.jar" ).toFile(), destDir.getFilePath().toFile() );
354 if ( md5State != null )
356 Path md5File = sourceDir.getFilePath().resolve("artifact.jar.md5-" + md5State );
357 assertTrue( "Testable file exists: " + md5File.getFileName() + ":", Files.exists(md5File) && Files.isRegularFile(md5File) );
358 Path destFile = destDir.getFilePath().resolve("artifact.jar.md5" );
359 FileUtils.copyFile( md5File.toFile(), destFile.toFile() );
362 if ( sha1State != null )
364 Path sha1File = sourceDir.getFilePath().resolve("artifact.jar.sha1-" + sha1State );
365 assertTrue( "Testable file exists: " + sha1File.getFileName() + ":", Files.exists(sha1File) && Files.isRegularFile(sha1File) );
366 Path destFile = destDir.getFilePath().resolve("artifact.jar.sha1" );
367 FileUtils.copyFile( sha1File.toFile(), destFile.toFile() );
371 StorageAsset localAsset = destDir.resolve("artifact.jar");
375 public static StorageAsset getTestFile( String path ) throws IOException {
376 if (filesystemStorage==null) {
377 filesystemStorage = new FilesystemStorage(Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()), new DefaultFileLockManager());
379 return filesystemStorage.getAsset( path );