1 package org.apache.maven.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.codehaus.plexus.PlexusTestCase;
23 import org.codehaus.plexus.util.FileUtils;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.util.Properties;
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public class ChecksumPolicyTest
37 extends PlexusTestCase
39 private static final String GOOD = "good";
41 private static final String BAD = "bad";
43 public void testFailOnFileOnly()
46 assertFailSetting( false, null, null );
49 public void testFailOnFileWithBadMd5AndBadSha1()
52 assertFailSetting( false, BAD, BAD );
55 public void testFailOnFileWithBadMd5AndGoodSha1()
58 assertFailSetting( false, BAD, GOOD );
61 public void testFailOnFileWithBadMd5Only()
64 assertFailSetting( false, BAD, null );
67 public void testFailOnFileWithBadSha1Only()
70 assertFailSetting( false, null, BAD );
73 public void testFailOnFileWithGoodMd5AndBadSha1()
76 assertFailSetting( false, GOOD, BAD );
79 public void testFailOnFileWithGoodMd5AndGoodSha1()
82 assertFailSetting( true, GOOD, GOOD );
85 public void testFailOnFileWithGoodMd5Only()
88 assertFailSetting( true, GOOD, null );
91 public void testFailOnFileWithGoodSha1Only()
94 assertFailSetting( true, null, GOOD );
97 public void testFixOnFileOnly()
100 assertFixSetting( true, null, null );
103 public void testFixOnFileWithBadMd5AndBadSha1()
106 assertFixSetting( true, BAD, BAD );
109 public void testFixOnFileWithBadMd5AndGoodSha1()
112 assertFixSetting( true, BAD, GOOD );
115 public void testFixOnFileWithBadMd5Only()
118 assertFixSetting( true, BAD, null );
121 public void testFixOnFileWithBadSha1Only()
124 assertFixSetting( true, null, BAD );
127 public void testFixOnFileWithGoodMd5AndBadSha1()
130 assertFixSetting( true, GOOD, BAD );
133 public void testFixOnFileWithGoodMd5AndGoodSha1()
136 assertFixSetting( true, GOOD, GOOD );
139 public void testFixOnFileWithGoodMd5Only()
142 assertFixSetting( true, GOOD, null );
145 public void testFixOnFileWithGoodSha1Only()
148 assertFixSetting( true, null, GOOD );
151 public void testIgnore()
154 PostDownloadPolicy policy = lookupPolicy();
155 File localFile = createTestableFiles( null, null );
156 Properties request = createRequest();
158 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
161 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
164 PostDownloadPolicy policy = lookupPolicy();
165 File localFile = createTestableFiles( md5State, sha1State );
166 Properties request = createRequest();
168 boolean actualResult;
172 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
175 catch ( PolicyViolationException e )
177 actualResult = false;
178 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
180 assertFalse( msg + " local file should not exist:", localFile.exists() );
181 File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
182 File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
183 assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
184 assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
187 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
190 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
193 PostDownloadPolicy policy = lookupPolicy();
194 File localFile = createTestableFiles( md5State, sha1State );
195 Properties request = createRequest();
197 boolean actualResult;
201 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
204 catch ( PolicyViolationException e )
206 actualResult = false;
209 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
211 // End result should be legitimate SHA1 and MD5 files.
212 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
213 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
215 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
216 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
218 String actualMd5Contents = readChecksumFile( md5File );
219 String actualSha1Contents = readChecksumFile( sha1File );
221 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
222 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
224 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
225 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
229 * Read the first line from the checksum file, and return it (trimmed).
231 private String readChecksumFile( File checksumFile )
234 FileReader freader = null;
235 BufferedReader buf = null;
239 freader = new FileReader( checksumFile );
240 buf = new BufferedReader( freader );
241 return buf.readLine();
250 if ( freader != null )
257 private String createMessage( String settingType, String md5State, String sha1State )
259 StringBuffer msg = new StringBuffer();
260 msg.append( "Expected result of ChecksumPolicy.apply(" );
261 msg.append( settingType.toUpperCase() );
262 msg.append( ") when working with " );
263 if ( md5State == null )
269 msg.append( "a " ).append( md5State.toUpperCase() );
272 msg.append( " MD5 and " );
274 if ( sha1State == null )
280 msg.append( "a " ).append( sha1State.toUpperCase() );
282 msg.append( " SHA1:" );
284 return msg.toString();
287 private Properties createRequest()
289 Properties request = new Properties();
291 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
296 private File createTestableFiles( String md5State, String sha1State )
299 File sourceDir = getTestFile( "src/test/resources/checksums/" );
300 File destDir = getTestFile( "target/checksum-tests/" + getName() + "/" );
302 FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
304 if ( md5State != null )
306 File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
307 assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
308 File destFile = new File( destDir, "artifact.jar.md5" );
309 FileUtils.copyFile( md5File, destFile );
312 if ( sha1State != null )
314 File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
315 assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
316 File destFile = new File( destDir, "artifact.jar.sha1" );
317 FileUtils.copyFile( sha1File, destFile );
320 File localFile = new File( destDir, "artifact.jar" );
324 private PostDownloadPolicy lookupPolicy()
327 PostDownloadPolicy policy = (PostDownloadPolicy) lookup( PostDownloadPolicy.class.getName(), "checksum" );
328 assertNotNull( policy );