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 11KB

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