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

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