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

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