View Javadoc
1   package org.apache.archiva.policies;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
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.utils.ArchivaSpringJUnit4ClassRunner;
37  
38  import static org.junit.Assert.*;
39  
40  /**
41   * ChecksumPolicyTest
42   *
43   *
44   */
45  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
46  @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
47  public class ChecksumPolicyTest
48  {
49      private static final String GOOD = "good";
50  
51      private static final String BAD = "bad";
52  
53      @Inject
54      @Named( value = "postDownloadPolicy#checksum" )
55      PostDownloadPolicy downloadPolicy;
56  
57      @Rule
58      public TestName name = new TestName();
59  
60      private PostDownloadPolicy lookupPolicy()
61          throws Exception
62      {
63          return downloadPolicy;
64      }
65  
66      @Test
67      public void testFailOnFileOnly()
68          throws Exception
69      {
70          assertFailSetting( false, null, null );
71      }
72  
73      @Test
74      public void testFailOnFileWithBadMd5AndBadSha1()
75          throws Exception
76      {
77          assertFailSetting( false, BAD, BAD );
78      }
79  
80      @Test
81      public void testFailOnFileWithBadMd5AndGoodSha1()
82          throws Exception
83      {
84          assertFailSetting( false, BAD, GOOD );
85      }
86  
87      @Test
88      public void testFailOnFileWithBadMd5Only()
89          throws Exception
90      {
91          assertFailSetting( false, BAD, null );
92      }
93  
94      @Test
95      public void testFailOnFileWithBadSha1Only()
96          throws Exception
97      {
98          assertFailSetting( false, null, BAD );
99      }
100 
101     @Test
102     public void testFailOnFileWithGoodMd5AndBadSha1()
103         throws Exception
104     {
105         assertFailSetting( false, GOOD, BAD );
106     }
107 
108     @Test
109     public void testFailOnFileWithGoodMd5AndGoodSha1()
110         throws Exception
111     {
112         assertFailSetting( true, GOOD, GOOD );
113     }
114 
115     @Test
116     public void testFailOnFileWithGoodMd5Only()
117         throws Exception
118     {
119         assertFailSetting( true, GOOD, null );
120     }
121 
122     @Test
123     public void testFailOnFileWithGoodSha1Only()
124         throws Exception
125     {
126         assertFailSetting( true, null, GOOD );
127     }
128 
129     @Test
130     public void testFixOnFileOnly()
131         throws Exception
132     {
133         assertFixSetting( true, null, null );
134     }
135 
136     @Test
137     public void testFixOnFileWithBadMd5AndBadSha1()
138         throws Exception
139     {
140         assertFixSetting( true, BAD, BAD );
141     }
142 
143     @Test
144     public void testFixOnFileWithBadMd5AndGoodSha1()
145         throws Exception
146     {
147         assertFixSetting( true, BAD, GOOD );
148     }
149 
150     @Test
151     public void testFixOnFileWithBadMd5Only()
152         throws Exception
153     {
154         assertFixSetting( true, BAD, null );
155     }
156 
157     @Test
158     public void testFixOnFileWithBadSha1Only()
159         throws Exception
160     {
161         assertFixSetting( true, null, BAD );
162     }
163 
164     @Test
165     public void testFixOnFileWithGoodMd5AndBadSha1()
166         throws Exception
167     {
168         assertFixSetting( true, GOOD, BAD );
169     }
170 
171     @Test
172     public void testFixOnFileWithGoodMd5AndGoodSha1()
173         throws Exception
174     {
175         assertFixSetting( true, GOOD, GOOD );
176     }
177 
178     @Test
179     public void testFixOnFileWithGoodMd5Only()
180         throws Exception
181     {
182         assertFixSetting( true, GOOD, null );
183     }
184 
185     @Test
186     public void testFixOnFileWithGoodSha1Only()
187         throws Exception
188     {
189         assertFixSetting( true, null, GOOD );
190     }
191 
192     @Test
193     public void testIgnore()
194         throws Exception
195     {
196         PostDownloadPolicy policy = lookupPolicy();
197         File localFile = createTestableFiles( null, null );
198         Properties request = createRequest();
199 
200         policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
201     }
202 
203     private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
204         throws Exception
205     {
206         PostDownloadPolicy policy = lookupPolicy();
207         File localFile = createTestableFiles( md5State, sha1State );
208         Properties request = createRequest();
209 
210         boolean actualResult;
211 
212         try
213         {
214             policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
215             actualResult = true;
216         }
217         catch ( PolicyViolationException e )
218         {
219             actualResult = false;
220             String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
221 
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() );
227         }
228 
229         assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
230     }
231 
232     private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
233         throws Exception
234     {
235         PostDownloadPolicy policy = lookupPolicy();
236         File localFile = createTestableFiles( md5State, sha1State );
237         Properties request = createRequest();
238 
239         boolean actualResult;
240 
241         try
242         {
243             policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
244             actualResult = true;
245         }
246         catch ( PolicyViolationException e )
247         {
248             actualResult = false;
249         }
250 
251         assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
252 
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" );
256 
257         assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
258         assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
259 
260         String actualMd5Contents = readChecksumFile( md5File );
261         String actualSha1Contents = readChecksumFile( sha1File );
262 
263         String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548  artifact.jar";
264         String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f  artifact.jar";
265 
266         assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
267         assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
268     }
269 
270     /**
271      * Read the first line from the checksum file, and return it (trimmed).
272      */
273     private String readChecksumFile( File checksumFile )
274         throws Exception
275     {
276         FileReader freader = null;
277         BufferedReader buf = null;
278 
279         try
280         {
281             freader = new FileReader( checksumFile );
282             buf = new BufferedReader( freader );
283             return buf.readLine();
284         }
285         finally
286         {
287             if ( buf != null )
288             {
289                 buf.close();
290             }
291 
292             if ( freader != null )
293             {
294                 freader.close();
295             }
296         }
297     }
298 
299     private String createMessage( String settingType, String md5State, String sha1State )
300     {
301         StringBuilder msg = new StringBuilder();
302         msg.append( "Expected result of ChecksumPolicy.apply(" );
303         msg.append( settingType.toUpperCase() );
304         msg.append( ") when working with " );
305         if ( md5State == null )
306         {
307             msg.append( "NO" );
308         }
309         else
310         {
311             msg.append( "a " ).append( md5State.toUpperCase() );
312         }
313 
314         msg.append( " MD5 and " );
315 
316         if ( sha1State == null )
317         {
318             msg.append( "NO" );
319         }
320         else
321         {
322             msg.append( "a " ).append( sha1State.toUpperCase() );
323         }
324         msg.append( " SHA1:" );
325 
326         return msg.toString();
327     }
328 
329     private Properties createRequest()
330     {
331         Properties request = new Properties();
332 
333         request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
334 
335         return request;
336     }
337 
338     private File createTestableFiles( String md5State, String sha1State )
339         throws Exception
340     {
341         File sourceDir = getTestFile( "src/test/resources/checksums/" );
342         File destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
343 
344         FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
345 
346         if ( md5State != null )
347         {
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 );
352         }
353 
354         if ( sha1State != null )
355         {
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 );
360         }
361 
362         File localFile = new File( destDir, "artifact.jar" );
363         return localFile;
364     }
365 
366     public static File getTestFile( String path )
367     {
368         return new File( FileUtil.getBasedir(), path );
369     }
370 
371 }