You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumPolicyTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.io.FileUtils;
  21. import org.apache.archiva.common.utils.FileUtil;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.TestName;
  25. import org.junit.runner.RunWith;
  26. import org.springframework.test.context.ContextConfiguration;
  27. import java.io.BufferedReader;
  28. import java.io.File;
  29. import java.io.FileReader;
  30. import java.util.Properties;
  31. import javax.inject.Inject;
  32. import javax.inject.Named;
  33. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  34. import static org.junit.Assert.*;
  35. /**
  36. * ChecksumPolicyTest
  37. *
  38. *
  39. */
  40. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  41. @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml"} )
  42. public class ChecksumPolicyTest
  43. {
  44. private static final String GOOD = "good";
  45. private static final String BAD = "bad";
  46. @Inject
  47. @Named( value = "postDownloadPolicy#checksum" )
  48. PostDownloadPolicy downloadPolicy;
  49. @Rule
  50. public TestName name = new TestName();
  51. private PostDownloadPolicy lookupPolicy()
  52. throws Exception
  53. {
  54. return downloadPolicy;
  55. }
  56. @Test
  57. public void testFailOnFileOnly()
  58. throws Exception
  59. {
  60. assertFailSetting( false, null, null );
  61. }
  62. @Test
  63. public void testFailOnFileWithBadMd5AndBadSha1()
  64. throws Exception
  65. {
  66. assertFailSetting( false, BAD, BAD );
  67. }
  68. @Test
  69. public void testFailOnFileWithBadMd5AndGoodSha1()
  70. throws Exception
  71. {
  72. assertFailSetting( false, BAD, GOOD );
  73. }
  74. @Test
  75. public void testFailOnFileWithBadMd5Only()
  76. throws Exception
  77. {
  78. assertFailSetting( false, BAD, null );
  79. }
  80. @Test
  81. public void testFailOnFileWithBadSha1Only()
  82. throws Exception
  83. {
  84. assertFailSetting( false, null, BAD );
  85. }
  86. @Test
  87. public void testFailOnFileWithGoodMd5AndBadSha1()
  88. throws Exception
  89. {
  90. assertFailSetting( false, GOOD, BAD );
  91. }
  92. @Test
  93. public void testFailOnFileWithGoodMd5AndGoodSha1()
  94. throws Exception
  95. {
  96. assertFailSetting( true, GOOD, GOOD );
  97. }
  98. @Test
  99. public void testFailOnFileWithGoodMd5Only()
  100. throws Exception
  101. {
  102. assertFailSetting( true, GOOD, null );
  103. }
  104. @Test
  105. public void testFailOnFileWithGoodSha1Only()
  106. throws Exception
  107. {
  108. assertFailSetting( true, null, GOOD );
  109. }
  110. @Test
  111. public void testFixOnFileOnly()
  112. throws Exception
  113. {
  114. assertFixSetting( true, null, null );
  115. }
  116. @Test
  117. public void testFixOnFileWithBadMd5AndBadSha1()
  118. throws Exception
  119. {
  120. assertFixSetting( true, BAD, BAD );
  121. }
  122. @Test
  123. public void testFixOnFileWithBadMd5AndGoodSha1()
  124. throws Exception
  125. {
  126. assertFixSetting( true, BAD, GOOD );
  127. }
  128. @Test
  129. public void testFixOnFileWithBadMd5Only()
  130. throws Exception
  131. {
  132. assertFixSetting( true, BAD, null );
  133. }
  134. @Test
  135. public void testFixOnFileWithBadSha1Only()
  136. throws Exception
  137. {
  138. assertFixSetting( true, null, BAD );
  139. }
  140. @Test
  141. public void testFixOnFileWithGoodMd5AndBadSha1()
  142. throws Exception
  143. {
  144. assertFixSetting( true, GOOD, BAD );
  145. }
  146. @Test
  147. public void testFixOnFileWithGoodMd5AndGoodSha1()
  148. throws Exception
  149. {
  150. assertFixSetting( true, GOOD, GOOD );
  151. }
  152. @Test
  153. public void testFixOnFileWithGoodMd5Only()
  154. throws Exception
  155. {
  156. assertFixSetting( true, GOOD, null );
  157. }
  158. @Test
  159. public void testFixOnFileWithGoodSha1Only()
  160. throws Exception
  161. {
  162. assertFixSetting( true, null, GOOD );
  163. }
  164. @Test
  165. public void testIgnore()
  166. throws Exception
  167. {
  168. PostDownloadPolicy policy = lookupPolicy();
  169. File localFile = createTestableFiles( null, null );
  170. Properties request = createRequest();
  171. policy.applyPolicy( ChecksumPolicy.IGNORE, request, localFile );
  172. }
  173. private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
  174. throws Exception
  175. {
  176. PostDownloadPolicy policy = lookupPolicy();
  177. File localFile = createTestableFiles( md5State, sha1State );
  178. Properties request = createRequest();
  179. boolean actualResult;
  180. try
  181. {
  182. policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
  183. actualResult = true;
  184. }
  185. catch ( PolicyViolationException e )
  186. {
  187. actualResult = false;
  188. String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
  189. assertFalse( msg + " local file should not exist:", localFile.exists() );
  190. File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
  191. File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
  192. assertFalse( msg + " local md5 file should not exist:", md5File.exists() );
  193. assertFalse( msg + " local sha1 file should not exist:", sha1File.exists() );
  194. }
  195. assertEquals( createMessage( ChecksumPolicy.FAIL, md5State, sha1State ), expectedResult, actualResult );
  196. }
  197. private void assertFixSetting( boolean expectedResult, String md5State, String sha1State )
  198. throws Exception
  199. {
  200. PostDownloadPolicy policy = lookupPolicy();
  201. File localFile = createTestableFiles( md5State, sha1State );
  202. Properties request = createRequest();
  203. boolean actualResult;
  204. try
  205. {
  206. policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
  207. actualResult = true;
  208. }
  209. catch ( PolicyViolationException e )
  210. {
  211. actualResult = false;
  212. }
  213. assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
  214. // End result should be legitimate SHA1 and MD5 files.
  215. File md5File = new File( localFile.getAbsolutePath() + ".md5" );
  216. File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
  217. assertTrue( "ChecksumPolicy.apply(FIX) md5 should exist.", md5File.exists() && md5File.isFile() );
  218. assertTrue( "ChecksumPolicy.apply(FIX) sha1 should exist.", sha1File.exists() && sha1File.isFile() );
  219. String actualMd5Contents = readChecksumFile( md5File );
  220. String actualSha1Contents = readChecksumFile( sha1File );
  221. String expectedMd5Contents = "360ccd01d8a0a2d94b86f9802c2fc548 artifact.jar";
  222. String expectedSha1Contents = "7dd8929150664f182db60ad15f20359d875f059f artifact.jar";
  223. assertEquals( "ChecksumPolicy.apply(FIX) md5 contents:", expectedMd5Contents, actualMd5Contents );
  224. assertEquals( "ChecksumPolicy.apply(FIX) sha1 contents:", expectedSha1Contents, actualSha1Contents );
  225. }
  226. /**
  227. * Read the first line from the checksum file, and return it (trimmed).
  228. */
  229. private String readChecksumFile( File checksumFile )
  230. throws Exception
  231. {
  232. FileReader freader = null;
  233. BufferedReader buf = null;
  234. try
  235. {
  236. freader = new FileReader( checksumFile );
  237. buf = new BufferedReader( freader );
  238. return buf.readLine();
  239. }
  240. finally
  241. {
  242. if ( buf != null )
  243. {
  244. buf.close();
  245. }
  246. if ( freader != null )
  247. {
  248. freader.close();
  249. }
  250. }
  251. }
  252. private String createMessage( String settingType, String md5State, String sha1State )
  253. {
  254. StringBuffer msg = new StringBuffer();
  255. msg.append( "Expected result of ChecksumPolicy.apply(" );
  256. msg.append( settingType.toUpperCase() );
  257. msg.append( ") when working with " );
  258. if ( md5State == null )
  259. {
  260. msg.append( "NO" );
  261. }
  262. else
  263. {
  264. msg.append( "a " ).append( md5State.toUpperCase() );
  265. }
  266. msg.append( " MD5 and " );
  267. if ( sha1State == null )
  268. {
  269. msg.append( "NO" );
  270. }
  271. else
  272. {
  273. msg.append( "a " ).append( sha1State.toUpperCase() );
  274. }
  275. msg.append( " SHA1:" );
  276. return msg.toString();
  277. }
  278. private Properties createRequest()
  279. {
  280. Properties request = new Properties();
  281. request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
  282. return request;
  283. }
  284. private File createTestableFiles( String md5State, String sha1State )
  285. throws Exception
  286. {
  287. File sourceDir = getTestFile( "src/test/resources/checksums/" );
  288. File destDir = getTestFile( "target/checksum-tests/" + name.getMethodName() + "/" );
  289. FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );
  290. if ( md5State != null )
  291. {
  292. File md5File = new File( sourceDir, "artifact.jar.md5-" + md5State );
  293. assertTrue( "Testable file exists: " + md5File.getName() + ":", md5File.exists() && md5File.isFile() );
  294. File destFile = new File( destDir, "artifact.jar.md5" );
  295. FileUtils.copyFile( md5File, destFile );
  296. }
  297. if ( sha1State != null )
  298. {
  299. File sha1File = new File( sourceDir, "artifact.jar.sha1-" + sha1State );
  300. assertTrue( "Testable file exists: " + sha1File.getName() + ":", sha1File.exists() && sha1File.isFile() );
  301. File destFile = new File( destDir, "artifact.jar.sha1" );
  302. FileUtils.copyFile( sha1File, destFile );
  303. }
  304. File localFile = new File( destDir, "artifact.jar" );
  305. return localFile;
  306. }
  307. public static File getTestFile( String path )
  308. {
  309. return new File( FileUtil.getBasedir(), path );
  310. }
  311. }