PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/deployment-scanner/src/test/java/org/jboss/as/server/deployment/scanner/ZipCompletionScannerUnitTestCase.java

#
Java | 282 lines | 175 code | 78 blank | 29 comment | 2 complexity | 584446e789f76131ed61fb20566d5e41 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.server.deployment.scanner;
  23. import static org.jboss.as.server.deployment.scanner.AutoDeployTestSupport.getByteBuffer;
  24. import static org.jboss.as.server.deployment.scanner.AutoDeployTestSupport.putUnsignedShort;
  25. import java.io.File;
  26. import java.nio.ByteBuffer;
  27. import java.nio.channels.FileChannel;
  28. import junit.framework.Assert;
  29. import org.jboss.as.server.deployment.scanner.ZipCompletionScanner.NonScannableZipException;
  30. import org.junit.After;
  31. import org.junit.AfterClass;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34. /**
  35. * Unit tests for {@link ZipCompletionScanner}
  36. *
  37. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  38. */
  39. public class ZipCompletionScannerUnitTestCase {
  40. private static AutoDeployTestSupport testSupport;
  41. @BeforeClass
  42. public static void setupClass() {
  43. testSupport = new AutoDeployTestSupport(ZipCompletionScannerUnitTestCase.class.getSimpleName());
  44. }
  45. @AfterClass
  46. public static void teardownClass() {
  47. if (testSupport != null) {
  48. testSupport.cleanupFiles();
  49. }
  50. }
  51. @After
  52. public void tearDown() {
  53. testSupport.cleanupChannels();
  54. }
  55. @Test
  56. public void testBasicWar() throws Exception {
  57. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(testSupport.getBasicWar()));
  58. }
  59. @Test
  60. public void testEmptyFile() throws Exception {
  61. File empty = testSupport.getFile("empty.jar");
  62. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(empty));
  63. }
  64. @Test
  65. public void testMaxScan() throws Exception {
  66. int size = (1 << 16) + 22;
  67. File maxscan = testSupport.getFile("maxscan.jar");
  68. FileChannel ch = testSupport.getChannel(maxscan, false);
  69. ByteBuffer bb = ByteBuffer.allocate(1);
  70. bb.put((byte)0);
  71. bb.flip();
  72. ch.write(bb, size -1);
  73. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscan));
  74. File maxscanplus = testSupport.getFile("maxscanplus.jar");
  75. ch = testSupport.getChannel(maxscanplus, false);
  76. bb.flip();
  77. ch.write(bb, size);
  78. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus));
  79. File maxscanminus = testSupport.getFile("maxscanminus.jar");
  80. ch = testSupport.getChannel(maxscanminus, false);
  81. bb.flip();
  82. ch.write(bb, size - 2);
  83. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus));
  84. }
  85. @Test
  86. public void testLeadingBytes() throws Exception {
  87. File leading = testSupport.getFile("leadingbyte.jar");
  88. testSupport.createZip(leading, 1, false, false, false, false);
  89. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(leading));
  90. }
  91. @Test
  92. public void testTrailingByte() throws Exception {
  93. File trailing = testSupport.getFile("trailingbyte.jar");
  94. FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
  95. FileChannel out = testSupport.getChannel(trailing, false);
  96. ByteBuffer bb = getByteBuffer((int) in.size());
  97. in.read(bb);
  98. putUnsignedShort(bb, 1, (int) in.size() - 2);
  99. bb.flip();
  100. out.write(bb);
  101. long size = out.size();
  102. bb = ByteBuffer.allocate(1);
  103. bb.put((byte)0);
  104. bb.flip();
  105. out.write(bb, size);
  106. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(trailing));
  107. }
  108. @Test
  109. public void testTrailingBytes() throws Exception {
  110. File trailing = testSupport.getFile("trailingbytes.jar");
  111. FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
  112. FileChannel out = testSupport.getChannel(trailing, false);
  113. int maxShort = (1 << 16) -1;
  114. ByteBuffer bb = getByteBuffer((int) in.size());
  115. in.read(bb);
  116. putUnsignedShort(bb, maxShort , (int) in.size() - 2);
  117. bb.flip();
  118. out.write(bb);
  119. long size = out.size();
  120. bb = ByteBuffer.allocate(1);
  121. bb.put((byte)0);
  122. bb.flip();
  123. out.write(bb, size + maxShort -1);
  124. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(trailing));
  125. }
  126. @Test
  127. public void testLeadingAndTrailingBytes() throws Exception {
  128. File file = testSupport.getFile("leadingtrailing.jar");
  129. testSupport.createZip(file, 1, true, false, false, false);
  130. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(file));
  131. }
  132. @Test
  133. public void testTruncatedEndRecord() throws Exception {
  134. File truncated = testSupport.getFile("truncated1.jar");
  135. FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
  136. FileChannel out = testSupport.getChannel(truncated, false);
  137. // Write out all but 1 byte
  138. ByteBuffer bb = getByteBuffer((int) in.size() - 1);
  139. in.read(bb);
  140. bb.flip();
  141. out.write(bb);
  142. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(truncated));
  143. truncated = testSupport.getFile("truncated2.jar");
  144. out = testSupport.getChannel(truncated, false);
  145. // Write out just past the end of central dir record signature
  146. bb = getByteBuffer((int) in.size() - 18);
  147. in.read(bb, 0);
  148. bb.flip();
  149. out.write(bb);
  150. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(truncated));
  151. }
  152. @Test
  153. public void testExtendedDataDescriptor() throws Exception {
  154. File file = testSupport.getFile("extdata.jar");
  155. testSupport.createZip(file, 0, false, false, true, false);
  156. Assert.assertTrue(ZipCompletionScanner.isCompleteZip(file));
  157. }
  158. @Test
  159. public void testFindNestedCentralDir() throws Exception {
  160. File file = testSupport.getFile("findnested.jar");
  161. testSupport.createZip(file, 0, false, true, false, false);
  162. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(file));
  163. }
  164. @Test
  165. public void testFindNestedCentralDirWithExt() throws Exception {
  166. File file = testSupport.getFile("findnestedext.jar");
  167. testSupport.createZip(file, 0, false, true, true, false);
  168. Assert.assertFalse(ZipCompletionScanner.isCompleteZip(file));
  169. }
  170. @Test
  171. public void testFindNestedCentralDirWithLeadingBytes() throws Exception {
  172. File file = testSupport.getFile("findnestedleading.jar");
  173. testSupport.createZip(file, 1, false, true, false, false);
  174. try {
  175. ZipCompletionScanner.isCompleteZip(file);
  176. Assert.fail("Scan of jar with nested content and leading bytes did not fail");
  177. }
  178. catch (NonScannableZipException good) {
  179. }
  180. }
  181. @Test
  182. public void testFindNestedCentralDirWithExtAndLeadingBytes() throws Exception {
  183. File file = testSupport.getFile("findnestedleadingext.jar");
  184. testSupport.createZip(file, 1, false, true, true, false);
  185. try {
  186. ZipCompletionScanner.isCompleteZip(file);
  187. Assert.fail("Scan of jar with nested content and leading bytes did not fail");
  188. }
  189. catch (NonScannableZipException good) {
  190. }
  191. }
  192. // This test should be reworked if Zip64 support is added
  193. @Test
  194. public void testZip64() throws Exception {
  195. File zip = testSupport.getFile("leadingbyte.jar");
  196. testSupport.createZip(zip, 0, false, false, false, true);
  197. try {
  198. ZipCompletionScanner.isCompleteZip(zip);
  199. Assert.fail("Scan of Zip64 file did not fail");
  200. }
  201. catch (NonScannableZipException good) {
  202. }
  203. }
  204. }