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.commons.io.FileUtils;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.TestName;
26 import org.junit.runner.RunWith;
27 import org.springframework.test.context.ContextConfiguration;
29 import java.io.BufferedReader;
31 import java.io.FileReader;
32 import java.util.Properties;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import static org.junit.Assert.*;
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
46 public class ChecksumPolicyTest
48 private static final String GOOD = "good";
50 private static final String BAD = "bad";
53 @Named( value = "postDownloadPolicy#checksum" )
54 PostDownloadPolicy downloadPolicy;
57 public TestName name = new TestName();
59 private PostDownloadPolicy lookupPolicy()
62 return downloadPolicy;
66 public void testFailOnFileOnly()
69 assertFailSetting( false, null, null );
73 public void testFailOnFileWithBadMd5AndBadSha1()
76 assertFailSetting( false, BAD, BAD );
80 public void testFailOnFileWithBadMd5AndGoodSha1()
83 assertFailSetting( false, BAD, GOOD );
87 public void testFailOnFileWithBadMd5Only()
90 assertFailSetting( false, BAD, null );
94 public void testFailOnFileWithBadSha1Only()
97 assertFailSetting( false, null, BAD );
101 public void testFailOnFileWithGoodMd5AndBadSha1()
104 assertFailSetting( false, GOOD, BAD );
108 public void testFailOnFileWithGoodMd5AndGoodSha1()
111 assertFailSetting( true, GOOD, GOOD );
115 public void testFailOnFileWithGoodMd5Only()
118 assertFailSetting( true, GOOD, null );
122 public void testFailOnFileWithGoodSha1Only()
125 assertFailSetting( true, null, GOOD );
129 public void testFixOnFileOnly()
132 assertFixSetting( true, null, null );
136 public void testFixOnFileWithBadMd5AndBadSha1()
139 assertFixSetting( true, BAD, BAD );
143 public void testFixOnFileWithBadMd5AndGoodSha1()
146 assertFixSetting( true, BAD, GOOD );
150 public void testFixOnFileWithBadMd5Only()
153 assertFixSetting( true, BAD, null );
157 public void testFixOnFileWithBadSha1Only()
160 assertFixSetting( true, null, BAD );
164 public void testFixOnFileWithGoodMd5AndBadSha1()
167 assertFixSetting( true, GOOD, BAD );
171 public void testFixOnFileWithGoodMd5AndGoodSha1()
174 assertFixSetting( true, GOOD, GOOD );
178 public void testFixOnFileWithGoodMd5Only()
181 assertFixSetting( true, GOOD, null );
185 public void testFixOnFileWithGoodSha1Only()
188 assertFixSetting( true, null, GOOD );
192 public void testIgnore()
195 PostDownloadPolicy policy = lookupPolicy();
196 File localFile = createTestableFiles( null, null );
197 Properties request = createRequest();
199 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
202 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
205 PostDownloadPolicy policy = lookupPolicy();
206 File localFile = createTestableFiles( md5State, sha1State );
207 Properties request = createRequest();
209 boolean actualResult;
213 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
216 catch ( PolicyViolationException e )
218 actualResult = false;
219 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
221 assertFalse( msg + " local file should not exist:", localFile.exists() );
222 File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
223 File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
224 assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
225 assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
228 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
231 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
234 PostDownloadPolicy policy = lookupPolicy();
235 File localFile = createTestableFiles( md5State, sha1State );
236 Properties request = createRequest();
238 boolean actualResult;
242 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
245 catch ( PolicyViolationException e )
247 actualResult = false;
250 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
252 // End result should be legitimate SHA1 and MD5 files.
253 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
254 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
256 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
257 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
259 String actualMd5Contents = readChecksumFile( md5File );
260 String actualSha1Contents = readChecksumFile( sha1File );
262 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
263 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
265 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
266 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
270 * Read the first line from the checksum file, and return it (trimmed).
272 private String readChecksumFile( File checksumFile )
275 FileReader freader = null;
276 BufferedReader buf = null;
280 freader = new FileReader( checksumFile );
281 buf = new BufferedReader( freader );
282 return buf.readLine();
291 if ( freader != null )
298 private String createMessage( String settingType, String md5State, String sha1State )
300 StringBuilder msg = new StringBuilder();
301 msg.append( "Expected result of ChecksumPolicy.apply(" );
302 msg.append( settingType.toUpperCase() );
303 msg.append( ") when working with " );
304 if ( md5State == null )
310 msg.append( "a " ).append( md5State.toUpperCase() );
313 msg.append( " MD5 and " );
315 if ( sha1State == null )
321 msg.append( "a " ).append( sha1State.toUpperCase() );
323 msg.append( " SHA1:" );
325 return msg.toString();
328 private Properties createRequest()
330 Properties request = new Properties();
332 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
337 private File createTestableFiles( String md5State, String sha1State )
340 File sourceDir = getTestFile( "src/test/resources/checksums/" );
341 File destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
343 FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
345 if ( md5State != null )
347 File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
348 assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
349 File destFile = new File( destDir, "artifact.jar.md5" );
350 FileUtils.copyFile( md5File, destFile );
353 if ( sha1State != null )
355 File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
356 assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
357 File destFile = new File( destDir, "artifact.jar.sha1" );
358 FileUtils.copyFile( sha1File, destFile );
361 File localFile = new File( destDir, "artifact.jar" );
365 public static File getTestFile( String path )
367 return new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), path );