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.apache.archiva.common.utils.FileUtil;
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 java.io.BufferedReader;
32 import java.io.FileReader;
33 import java.util.Properties;
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
38 import static org.junit.Assert.*;
45 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
47 public class ChecksumPolicyTest
49 private static final String GOOD = "good";
51 private static final String BAD = "bad";
54 @Named( value = "postDownloadPolicy#checksum" )
55 PostDownloadPolicy downloadPolicy;
58 public TestName name = new TestName();
60 private PostDownloadPolicy lookupPolicy()
63 return downloadPolicy;
67 public void testFailOnFileOnly()
70 assertFailSetting( false, null, null );
74 public void testFailOnFileWithBadMd5AndBadSha1()
77 assertFailSetting( false, BAD, BAD );
81 public void testFailOnFileWithBadMd5AndGoodSha1()
84 assertFailSetting( false, BAD, GOOD );
88 public void testFailOnFileWithBadMd5Only()
91 assertFailSetting( false, BAD, null );
95 public void testFailOnFileWithBadSha1Only()
98 assertFailSetting( false, null, BAD );
102 public void testFailOnFileWithGoodMd5AndBadSha1()
105 assertFailSetting( false, GOOD, BAD );
109 public void testFailOnFileWithGoodMd5AndGoodSha1()
112 assertFailSetting( true, GOOD, GOOD );
116 public void testFailOnFileWithGoodMd5Only()
119 assertFailSetting( true, GOOD, null );
123 public void testFailOnFileWithGoodSha1Only()
126 assertFailSetting( true, null, GOOD );
130 public void testFixOnFileOnly()
133 assertFixSetting( true, null, null );
137 public void testFixOnFileWithBadMd5AndBadSha1()
140 assertFixSetting( true, BAD, BAD );
144 public void testFixOnFileWithBadMd5AndGoodSha1()
147 assertFixSetting( true, BAD, GOOD );
151 public void testFixOnFileWithBadMd5Only()
154 assertFixSetting( true, BAD, null );
158 public void testFixOnFileWithBadSha1Only()
161 assertFixSetting( true, null, BAD );
165 public void testFixOnFileWithGoodMd5AndBadSha1()
168 assertFixSetting( true, GOOD, BAD );
172 public void testFixOnFileWithGoodMd5AndGoodSha1()
175 assertFixSetting( true, GOOD, GOOD );
179 public void testFixOnFileWithGoodMd5Only()
182 assertFixSetting( true, GOOD, null );
186 public void testFixOnFileWithGoodSha1Only()
189 assertFixSetting( true, null, GOOD );
193 public void testIgnore()
196 PostDownloadPolicy policy = lookupPolicy();
197 File localFile = createTestableFiles( null, null );
198 Properties request = createRequest();
200 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
203 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
206 PostDownloadPolicy policy = lookupPolicy();
207 File localFile = createTestableFiles( md5State, sha1State );
208 Properties request = createRequest();
210 boolean actualResult;
214 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
217 catch ( PolicyViolationException e )
219 actualResult = false;
220 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
222 assertFalse( msg + " local file should not exist:", localFile.exists() );
223 File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
224 File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
225 assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
226 assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
229 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
232 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
235 PostDownloadPolicy policy = lookupPolicy();
236 File localFile = createTestableFiles( md5State, sha1State );
237 Properties request = createRequest();
239 boolean actualResult;
243 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
246 catch ( PolicyViolationException e )
248 actualResult = false;
251 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
253 // End result should be legitimate SHA1 and MD5 files.
254 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
255 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
257 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
258 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
260 String actualMd5Contents = readChecksumFile( md5File );
261 String actualSha1Contents = readChecksumFile( sha1File );
263 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
264 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
266 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
267 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
271 * Read the first line from the checksum file, and return it (trimmed).
273 private String readChecksumFile( File checksumFile )
276 FileReader freader = null;
277 BufferedReader buf = null;
281 freader = new FileReader( checksumFile );
282 buf = new BufferedReader( freader );
283 return buf.readLine();
292 if ( freader != null )
299 private String createMessage( String settingType, String md5State, String sha1State )
301 StringBuffer msg = new StringBuffer();
302 msg.append( "Expected result of ChecksumPolicy.apply(" );
303 msg.append( settingType.toUpperCase() );
304 msg.append( ") when working with " );
305 if ( md5State == null )
311 msg.append( "a " ).append( md5State.toUpperCase() );
314 msg.append( " MD5 and " );
316 if ( sha1State == null )
322 msg.append( "a " ).append( sha1State.toUpperCase() );
324 msg.append( " SHA1:" );
326 return msg.toString();
329 private Properties createRequest()
331 Properties request = new Properties();
333 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
338 private File createTestableFiles( String md5State, String sha1State )
341 File sourceDir = getTestFile( "src/test/resources/checksums/" );
342 File destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
344 FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
346 if ( md5State != null )
348 File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
349 assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
350 File destFile = new File( destDir, "artifact.jar.md5" );
351 FileUtils.copyFile( md5File, destFile );
354 if ( sha1State != null )
356 File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
357 assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
358 File destFile = new File( destDir, "artifact.jar.sha1" );
359 FileUtils.copyFile( sha1File, destFile );
362 File localFile = new File( destDir, "artifact.jar" );
366 public static File getTestFile( String path )
368 return new File( FileUtil.getBasedir(), path );