PageRenderTime 39ms CodeModel.GetById 20ms app.highlight 17ms RepoModel.GetById 0ms 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 */
 22package org.jboss.as.server.deployment.scanner;
 23
 24import static org.jboss.as.server.deployment.scanner.AutoDeployTestSupport.getByteBuffer;
 25import static org.jboss.as.server.deployment.scanner.AutoDeployTestSupport.putUnsignedShort;
 26
 27import java.io.File;
 28import java.nio.ByteBuffer;
 29import java.nio.channels.FileChannel;
 30
 31import junit.framework.Assert;
 32
 33import org.jboss.as.server.deployment.scanner.ZipCompletionScanner.NonScannableZipException;
 34import org.junit.After;
 35import org.junit.AfterClass;
 36import org.junit.BeforeClass;
 37import org.junit.Test;
 38
 39/**
 40 * Unit tests for {@link ZipCompletionScanner}
 41 *
 42 * @author Brian Stansberry (c) 2011 Red Hat Inc.
 43 */
 44public class ZipCompletionScannerUnitTestCase {
 45
 46    private static AutoDeployTestSupport testSupport;
 47
 48    @BeforeClass
 49    public static void setupClass() {
 50
 51        testSupport = new AutoDeployTestSupport(ZipCompletionScannerUnitTestCase.class.getSimpleName());
 52    }
 53
 54    @AfterClass
 55    public static void teardownClass() {
 56        if (testSupport != null) {
 57            testSupport.cleanupFiles();
 58        }
 59    }
 60
 61    @After
 62    public void tearDown() {
 63        testSupport.cleanupChannels();
 64    }
 65
 66    @Test
 67    public void testBasicWar() throws Exception {
 68        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(testSupport.getBasicWar()));
 69    }
 70
 71    @Test
 72    public void testEmptyFile() throws Exception {
 73        File empty = testSupport.getFile("empty.jar");
 74        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(empty));
 75    }
 76
 77    @Test
 78    public void testMaxScan() throws Exception {
 79        int size = (1 << 16) + 22;
 80        File maxscan = testSupport.getFile("maxscan.jar");
 81        FileChannel ch = testSupport.getChannel(maxscan, false);
 82        ByteBuffer bb = ByteBuffer.allocate(1);
 83        bb.put((byte)0);
 84        bb.flip();
 85        ch.write(bb, size -1);
 86
 87        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscan));
 88
 89        File maxscanplus = testSupport.getFile("maxscanplus.jar");
 90        ch = testSupport.getChannel(maxscanplus, false);
 91        bb.flip();
 92        ch.write(bb, size);
 93
 94        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus));
 95
 96        File maxscanminus = testSupport.getFile("maxscanminus.jar");
 97        ch = testSupport.getChannel(maxscanminus, false);
 98        bb.flip();
 99        ch.write(bb, size - 2);
100
101        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(maxscanplus));
102    }
103
104    @Test
105    public void testLeadingBytes() throws Exception {
106
107        File leading = testSupport.getFile("leadingbyte.jar");
108
109        testSupport.createZip(leading, 1, false, false, false, false);
110
111        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(leading));
112    }
113
114    @Test
115    public void testTrailingByte() throws Exception {
116
117        File trailing = testSupport.getFile("trailingbyte.jar");
118
119        FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
120
121        FileChannel out = testSupport.getChannel(trailing, false);
122
123        ByteBuffer bb = getByteBuffer((int) in.size());
124        in.read(bb);
125        putUnsignedShort(bb, 1, (int) in.size() - 2);
126        bb.flip();
127        out.write(bb);
128
129        long size = out.size();
130
131        bb = ByteBuffer.allocate(1);
132        bb.put((byte)0);
133        bb.flip();
134        out.write(bb, size);
135
136        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(trailing));
137    }
138
139    @Test
140    public void testTrailingBytes() throws Exception {
141
142        File trailing = testSupport.getFile("trailingbytes.jar");
143
144        FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
145
146        FileChannel out = testSupport.getChannel(trailing, false);
147
148        int maxShort = (1 << 16) -1;
149        ByteBuffer bb = getByteBuffer((int) in.size());
150        in.read(bb);
151        putUnsignedShort(bb, maxShort , (int) in.size() - 2);
152        bb.flip();
153        out.write(bb);
154
155        long size = out.size();
156
157        bb = ByteBuffer.allocate(1);
158        bb.put((byte)0);
159        bb.flip();
160        out.write(bb, size + maxShort -1);
161
162        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(trailing));
163    }
164
165    @Test
166    public void testLeadingAndTrailingBytes() throws Exception {
167
168        File file = testSupport.getFile("leadingtrailing.jar");
169
170        testSupport.createZip(file, 1, true, false, false, false);
171
172        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(file));
173    }
174
175    @Test
176    public void testTruncatedEndRecord() throws Exception {
177
178        File truncated = testSupport.getFile("truncated1.jar");
179
180        FileChannel in = testSupport.getChannel(testSupport.getBasicWar(), true);
181
182        FileChannel out = testSupport.getChannel(truncated, false);
183
184        // Write out all but 1 byte
185        ByteBuffer bb = getByteBuffer((int) in.size() - 1);
186        in.read(bb);
187        bb.flip();
188        out.write(bb);
189
190        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(truncated));
191
192        truncated = testSupport.getFile("truncated2.jar");
193
194        out = testSupport.getChannel(truncated, false);
195
196        // Write out just past the end of central dir record signature
197        bb = getByteBuffer((int) in.size() - 18);
198        in.read(bb, 0);
199        bb.flip();
200        out.write(bb);
201
202        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(truncated));
203
204    }
205
206    @Test
207    public void testExtendedDataDescriptor() throws Exception {
208
209        File file = testSupport.getFile("extdata.jar");
210
211        testSupport.createZip(file, 0, false, false, true, false);
212
213        Assert.assertTrue(ZipCompletionScanner.isCompleteZip(file));
214    }
215
216    @Test
217    public void testFindNestedCentralDir() throws Exception {
218
219        File file = testSupport.getFile("findnested.jar");
220
221        testSupport.createZip(file, 0, false, true, false, false);
222
223        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(file));
224    }
225
226    @Test
227    public void testFindNestedCentralDirWithExt() throws Exception {
228
229        File file = testSupport.getFile("findnestedext.jar");
230
231        testSupport.createZip(file, 0, false, true, true, false);
232
233        Assert.assertFalse(ZipCompletionScanner.isCompleteZip(file));
234    }
235
236    @Test
237    public void testFindNestedCentralDirWithLeadingBytes() throws Exception {
238
239        File file = testSupport.getFile("findnestedleading.jar");
240
241        testSupport.createZip(file, 1, false, true, false, false);
242
243        try {
244            ZipCompletionScanner.isCompleteZip(file);
245            Assert.fail("Scan of jar with nested content and leading bytes did not fail");
246        }
247        catch (NonScannableZipException good) {
248        }
249    }
250
251    @Test
252    public void testFindNestedCentralDirWithExtAndLeadingBytes() throws Exception {
253
254        File file = testSupport.getFile("findnestedleadingext.jar");
255
256        testSupport.createZip(file, 1, false, true, true, false);
257
258        try {
259            ZipCompletionScanner.isCompleteZip(file);
260            Assert.fail("Scan of jar with nested content and leading bytes did not fail");
261        }
262        catch (NonScannableZipException good) {
263        }
264    }
265
266    // This test should be reworked if Zip64 support is added
267    @Test
268    public void testZip64() throws Exception {
269
270        File zip = testSupport.getFile("leadingbyte.jar");
271
272        testSupport.createZip(zip, 0, false, false, false, true);
273
274        try {
275            ZipCompletionScanner.isCompleteZip(zip);
276            Assert.fail("Scan of Zip64 file did not fail");
277        }
278        catch (NonScannableZipException good) {
279        }
280    }
281
282}