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.test.utils.ArchivaSpringJUnit4ClassRunner;
23 import org.apache.commons.io.FileUtils;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.TestName;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import java.io.BufferedReader;
33 import java.io.FileReader;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Properties;
39 import static org.junit.Assert.*;
46 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
48 public class ChecksumPolicyTest
50 private static final String GOOD = "good";
52 private static final String BAD = "bad";
55 @Named( value = "postDownloadPolicy#checksum" )
56 PostDownloadPolicy downloadPolicy;
59 public TestName name = new TestName();
61 private PostDownloadPolicy lookupPolicy()
64 return downloadPolicy;
68 public void testFailOnFileOnly()
71 assertFailSetting( false, null, null );
75 public void testFailOnFileWithBadMd5AndBadSha1()
78 assertFailSetting( false, BAD, BAD );
82 public void testFailOnFileWithBadMd5AndGoodSha1()
85 assertFailSetting( false, BAD, GOOD );
89 public void testFailOnFileWithBadMd5Only()
92 assertFailSetting( false, BAD, null );
96 public void testFailOnFileWithBadSha1Only()
99 assertFailSetting( false, null, BAD );
103 public void testFailOnFileWithGoodMd5AndBadSha1()
106 assertFailSetting( false, GOOD, BAD );
110 public void testFailOnFileWithGoodMd5AndGoodSha1()
113 assertFailSetting( true, GOOD, GOOD );
117 public void testFailOnFileWithGoodMd5Only()
120 assertFailSetting( true, GOOD, null );
124 public void testFailOnFileWithGoodSha1Only()
127 assertFailSetting( true, null, GOOD );
131 public void testFixOnFileOnly()
134 assertFixSetting( true, null, null );
138 public void testFixOnFileWithBadMd5AndBadSha1()
141 assertFixSetting( true, BAD, BAD );
145 public void testFixOnFileWithBadMd5AndGoodSha1()
148 assertFixSetting( true, BAD, GOOD );
152 public void testFixOnFileWithBadMd5Only()
155 assertFixSetting( true, BAD, null );
159 public void testFixOnFileWithBadSha1Only()
162 assertFixSetting( true, null, BAD );
166 public void testFixOnFileWithGoodMd5AndBadSha1()
169 assertFixSetting( true, GOOD, BAD );
173 public void testFixOnFileWithGoodMd5AndGoodSha1()
176 assertFixSetting( true, GOOD, GOOD );
180 public void testFixOnFileWithGoodMd5Only()
183 assertFixSetting( true, GOOD, null );
187 public void testFixOnFileWithGoodSha1Only()
190 assertFixSetting( true, null, GOOD );
194 public void testIgnore()
197 PostDownloadPolicy policy = lookupPolicy();
198 Path localFile = createTestableFiles( null, null );
199 Properties request = createRequest();
201 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
204 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
207 PostDownloadPolicy policy = lookupPolicy();
208 Path localFile = createTestableFiles( md5State, sha1State );
209 Properties request = createRequest();
211 boolean actualResult;
215 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
218 catch ( PolicyViolationException e )
220 actualResult = false;
221 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
223 assertFalse( msg + " local file should not exist:", Files.exists(localFile) );
224 Path md5File = localFile.toAbsolutePath().resolveSibling( localFile.getFileName() + ".sha1" );
225 Path sha1File = localFile.toAbsolutePath().resolveSibling( localFile.getFileName() + ".md5" );
226 assertFalse( msg + " local md5 file should not exist:", Files.exists(md5File) );
227 assertFalse( msg + " local sha1 file should not exist:", Files.exists(sha1File) );
230 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
233 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
236 PostDownloadPolicy policy = lookupPolicy();
237 Path localFile = createTestableFiles( md5State, sha1State );
238 Properties request = createRequest();
240 boolean actualResult;
244 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
247 catch ( PolicyViolationException e )
249 actualResult = false;
252 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
254 // End result should be legitimate SHA1 and MD5 files.
255 Path md5File = localFile.toAbsolutePath().resolveSibling( localFile.getFileName() + ".md5" );
256 Path sha1File = localFile.toAbsolutePath().resolveSibling( localFile.getFileName() + ".sha1" );
258 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", Files.exists(md5File) && Files.isRegularFile(md5File) );
259 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", Files.exists(sha1File) && Files.isRegularFile(sha1File) );
261 String actualMd5Contents = readChecksumFile( md5File );
262 String actualSha1Contents = readChecksumFile( sha1File );
264 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
265 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
267 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
268 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
272 * Read the first line from the checksum file, and return it (trimmed).
274 private String readChecksumFile( Path checksumFile )
277 FileReader freader = null;
278 BufferedReader buf = null;
282 freader = new FileReader( checksumFile.toFile() );
283 buf = new BufferedReader( freader );
284 return buf.readLine();
293 if ( freader != null )
300 private String createMessage( String settingType, String md5State, String sha1State )
302 StringBuilder msg = new StringBuilder();
303 msg.append( "Expected result of ChecksumPolicy.apply(" );
304 msg.append( settingType.toUpperCase() );
305 msg.append( ") when working with " );
306 if ( md5State == null )
312 msg.append( "a " ).append( md5State.toUpperCase() );
315 msg.append( " MD5 and " );
317 if ( sha1State == null )
323 msg.append( "a " ).append( sha1State.toUpperCase() );
325 msg.append( " SHA1:" );
327 return msg.toString();
330 private Properties createRequest()
332 Properties request = new Properties();
334 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
339 private Path createTestableFiles( String md5State, String sha1State )
342 Path sourceDir = getTestFile( "src/test/resources/checksums/" );
343 Path destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
345 FileUtils.copyFileToDirectory( sourceDir.resolve("artifact.jar" ).toFile(), destDir.toFile() );
347 if ( md5State != null )
349 Path md5File = sourceDir.resolve("artifact.jar.md5-" + md5State );
350 assertTrue( "Testable file exists: " + md5File.getFileName() + ":", Files.exists(md5File) && Files.isRegularFile(md5File) );
351 Path destFile = destDir.resolve("artifact.jar.md5" );
352 FileUtils.copyFile( md5File.toFile(), destFile.toFile() );
355 if ( sha1State != null )
357 Path sha1File = sourceDir.resolve("artifact.jar.sha1-" + sha1State );
358 assertTrue( "Testable file exists: " + sha1File.getFileName() + ":", Files.exists(sha1File) && Files.isRegularFile(sha1File) );
359 Path destFile = destDir.resolve("artifact.jar.sha1" );
360 FileUtils.copyFile( sha1File.toFile(), destFile.toFile() );
363 Path localFile = destDir.resolve("artifact.jar" );
367 public static Path getTestFile( String path )
369 return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );