1 package org.apache.maven.archiva.policies;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.util.Properties;
26
27 import org.apache.commons.io.FileUtils;
28 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
29
30
31
32
33
34
35 public class ChecksumPolicyTest
36 extends PlexusInSpringTestCase
37 {
38 private static final String GOOD = "good";
39
40 private static final String BAD = "bad";
41
42 public void testFailOnFileOnly()
43 throws Exception
44 {
45 assertFailSetting( false, null, null );
46 }
47
48 public void testFailOnFileWithBadMd5AndBadSha1()
49 throws Exception
50 {
51 assertFailSetting( false, BAD, BAD );
52 }
53
54 public void testFailOnFileWithBadMd5AndGoodSha1()
55 throws Exception
56 {
57 assertFailSetting( false, BAD, GOOD );
58 }
59
60 public void testFailOnFileWithBadMd5Only()
61 throws Exception
62 {
63 assertFailSetting( false, BAD, null );
64 }
65
66 public void testFailOnFileWithBadSha1Only()
67 throws Exception
68 {
69 assertFailSetting( false, null, BAD );
70 }
71
72 public void testFailOnFileWithGoodMd5AndBadSha1()
73 throws Exception
74 {
75 assertFailSetting( false, GOOD, BAD );
76 }
77
78 public void testFailOnFileWithGoodMd5AndGoodSha1()
79 throws Exception
80 {
81 assertFailSetting( true, GOOD, GOOD );
82 }
83
84 public void testFailOnFileWithGoodMd5Only()
85 throws Exception
86 {
87 assertFailSetting( true, GOOD, null );
88 }
89
90 public void testFailOnFileWithGoodSha1Only()
91 throws Exception
92 {
93 assertFailSetting( true, null, GOOD );
94 }
95
96 public void testFixOnFileOnly()
97 throws Exception
98 {
99 assertFixSetting( true, null, null );
100 }
101
102 public void testFixOnFileWithBadMd5AndBadSha1()
103 throws Exception
104 {
105 assertFixSetting( true, BAD, BAD );
106 }
107
108 public void testFixOnFileWithBadMd5AndGoodSha1()
109 throws Exception
110 {
111 assertFixSetting( true, BAD, GOOD );
112 }
113
114 public void testFixOnFileWithBadMd5Only()
115 throws Exception
116 {
117 assertFixSetting( true, BAD, null );
118 }
119
120 public void testFixOnFileWithBadSha1Only()
121 throws Exception
122 {
123 assertFixSetting( true, null, BAD );
124 }
125
126 public void testFixOnFileWithGoodMd5AndBadSha1()
127 throws Exception
128 {
129 assertFixSetting( true, GOOD, BAD );
130 }
131
132 public void testFixOnFileWithGoodMd5AndGoodSha1()
133 throws Exception
134 {
135 assertFixSetting( true, GOOD, GOOD );
136 }
137
138 public void testFixOnFileWithGoodMd5Only()
139 throws Exception
140 {
141 assertFixSetting( true, GOOD, null );
142 }
143
144 public void testFixOnFileWithGoodSha1Only()
145 throws Exception
146 {
147 assertFixSetting( true, null, GOOD );
148 }
149
150 public void testIgnore()
151 throws Exception
152 {
153 PostDownloadPolicy policy = lookupPolicy();
154 File localFile = createTestableFiles( null, null );
155 Properties request = createRequest();
156
157 policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
158 }
159
160 private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
161 throws Exception
162 {
163 PostDownloadPolicy policy = lookupPolicy();
164 File localFile = createTestableFiles( md5State, sha1State );
165 Properties request = createRequest();
166
167 boolean actualResult;
168
169 try
170 {
171 policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
172 actualResult = true;
173 }
174 catch ( PolicyViolationException e )
175 {
176 actualResult = false;
177 String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
178
179 assertFalse( msg + " local file should not exist:", localFile.exists() );
180 File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
181 File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
182 assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
183 assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
184 }
185
186 assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
187 }
188
189 private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
190 throws Exception
191 {
192 PostDownloadPolicy policy = lookupPolicy();
193 File localFile = createTestableFiles( md5State, sha1State );
194 Properties request = createRequest();
195
196 boolean actualResult;
197
198 try
199 {
200 policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
201 actualResult = true;
202 }
203 catch ( PolicyViolationException e )
204 {
205 actualResult = false;
206 }
207
208 assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
209
210
211 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
212 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
213
214 assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
215 assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
216
217 String actualMd5Contents = readChecksumFile( md5File );
218 String actualSha1Contents = readChecksumFile( sha1File );
219
220 String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
221 String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
222
223 assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
224 assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
225 }
226
227
228
229
230 private String readChecksumFile( File checksumFile )
231 throws Exception
232 {
233 FileReader freader = null;
234 BufferedReader buf = null;
235
236 try
237 {
238 freader = new FileReader( checksumFile );
239 buf = new BufferedReader( freader );
240 return buf.readLine();
241 }
242 finally
243 {
244 if ( buf != null )
245 {
246 buf.close();
247 }
248
249 if ( freader != null )
250 {
251 freader.close();
252 }
253 }
254 }
255
256 private String createMessage( String settingType, String md5State, String sha1State )
257 {
258 StringBuffer msg = new StringBuffer();
259 msg.append( "Expected result of ChecksumPolicy.apply(" );
260 msg.append( settingType.toUpperCase() );
261 msg.append( ") when working with " );
262 if ( md5State == null )
263 {
264 msg.append( "NO" );
265 }
266 else
267 {
268 msg.append( "a " ).append( md5State.toUpperCase() );
269 }
270
271 msg.append( " MD5 and " );
272
273 if ( sha1State == null )
274 {
275 msg.append( "NO" );
276 }
277 else
278 {
279 msg.append( "a " ).append( sha1State.toUpperCase() );
280 }
281 msg.append( " SHA1:" );
282
283 return msg.toString();
284 }
285
286 private Properties createRequest()
287 {
288 Properties request = new Properties();
289
290 request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
291
292 return request;
293 }
294
295 private File createTestableFiles( String md5State, String sha1State )
296 throws Exception
297 {
298 File sourceDir = getTestFile( "src/test/resources/checksums/" );
299 File destDir = getTestFile( "target/checksum-tests/" + getName() + "/" );
300
301 FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
302
303 if ( md5State != null )
304 {
305 File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
306 assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
307 File destFile = new File( destDir, "artifact.jar.md5" );
308 FileUtils.copyFile( md5File, destFile );
309 }
310
311 if ( sha1State != null )
312 {
313 File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
314 assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
315 File destFile = new File( destDir, "artifact.jar.sha1" );
316 FileUtils.copyFile( sha1File, destFile );
317 }
318
319 File localFile = new File( destDir, "artifact.jar" );
320 return localFile;
321 }
322
323 private PostDownloadPolicy lookupPolicy()
324 throws Exception
325 {
326 PostDownloadPolicy policy = (PostDownloadPolicy) lookup( PostDownloadPolicy.class.getName(), "checksum" );
327 assertNotNull( policy );
328 return policy;
329 }
330
331 }