|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, The National Archives <[email protected]> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following |
| 7 | + * conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of the The National Archives nor the |
| 17 | + * names of its contributors may be used to endorse or promote products |
| 18 | + * derived from this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package uk.gov.nationalarchives.droid.container.zip; |
| 33 | + |
| 34 | +import net.java.truevfs.comp.zip.ZipEntry; |
| 35 | +import net.java.truevfs.comp.zip.ZipOutputStream; |
| 36 | +import org.junit.Test; |
| 37 | +import uk.gov.nationalarchives.droid.container.*; |
| 38 | +import uk.gov.nationalarchives.droid.core.interfaces.IdentificationRequest; |
| 39 | +import uk.gov.nationalarchives.droid.core.interfaces.RequestIdentifier; |
| 40 | +import uk.gov.nationalarchives.droid.core.interfaces.archive.AbstractArchiveRequestFactory; |
| 41 | +import uk.gov.nationalarchives.droid.core.interfaces.resource.RequestMetaData; |
| 42 | +import uk.gov.nationalarchives.droid.core.interfaces.resource.ZipEntryIdentificationRequest; |
| 43 | +import uk.gov.nationalarchives.droid.core.signature.droid6.ByteSequence; |
| 44 | +import uk.gov.nationalarchives.droid.core.signature.droid6.InternalSignature; |
| 45 | +import uk.gov.nationalarchives.droid.core.signature.droid6.InternalSignatureCollection; |
| 46 | + |
| 47 | +import java.io.ByteArrayInputStream; |
| 48 | +import java.io.ByteArrayOutputStream; |
| 49 | +import java.io.IOException; |
| 50 | +import java.io.InputStream; |
| 51 | +import java.nio.file.Files; |
| 52 | +import java.util.List; |
| 53 | + |
| 54 | +import static org.junit.Assert.*; |
| 55 | + |
| 56 | +public class ZipFailureFallbackTest { |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testFallbackWithCorruptedZipFile() throws IOException { |
| 60 | + String fileName = "file.txt"; |
| 61 | + byte[] zipBytes = createZipBytes(fileName); |
| 62 | + |
| 63 | + //14 bytes from the end is the file count. Setting it to zero causes truevfs to fail but commons-compress succeeds. |
| 64 | + zipBytes[zipBytes.length - 14] = 0x00; |
| 65 | + |
| 66 | + List<ContainerSignatureMatch> containerSignatureMatches = getContainerSignatureMatches(zipBytes, fileName); |
| 67 | + |
| 68 | + assertEquals(containerSignatureMatches.size(), 1); |
| 69 | + assertEquals(containerSignatureMatches.getFirst().getSignature().getId(), 1); |
| 70 | + assertTrue(containerSignatureMatches.getFirst().isMatch()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testValidZipFileMatches() throws IOException { |
| 75 | + String fileName = "file.txt"; |
| 76 | + byte[] zipBytes = createZipBytes(fileName); |
| 77 | + |
| 78 | + List<ContainerSignatureMatch> containerSignatureMatches = getContainerSignatureMatches(zipBytes, fileName); |
| 79 | + |
| 80 | + assertEquals(containerSignatureMatches.size(), 1); |
| 81 | + assertEquals(containerSignatureMatches.getFirst().getSignature().getId(), 1); |
| 82 | + assertTrue(containerSignatureMatches.getFirst().isMatch()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testNoMatchesIfFileNotFound() throws IOException { |
| 87 | + String fileName = "file.txt"; |
| 88 | + String missingFileName = "missingFile.txt"; |
| 89 | + byte[] zipBytes = createZipBytes(fileName); |
| 90 | + |
| 91 | + List<ContainerSignatureMatch> containerSignatureMatches = getContainerSignatureMatches(zipBytes, missingFileName); |
| 92 | + |
| 93 | + assertEquals(containerSignatureMatches.size(), 1); |
| 94 | + assertFalse(containerSignatureMatches.getFirst().isMatch()); |
| 95 | + } |
| 96 | + |
| 97 | + private static List<ContainerSignatureMatch> getContainerSignatureMatches(byte[] zipBytes, String filePath) throws IOException { |
| 98 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(zipBytes); |
| 99 | + |
| 100 | + RequestMetaData metaData = new RequestMetaData(1L, 1L, ""); |
| 101 | + IdentificationRequest<InputStream> request = new ZipEntryIdentificationRequest(metaData, null, Files.createTempDirectory("test")); |
| 102 | + request.open(inputStream); |
| 103 | + |
| 104 | + ContainerSignature containerSignature = new ContainerSignature(); |
| 105 | + containerSignature.setId(1); |
| 106 | + ContainerFile containerFile = new ContainerFile(); |
| 107 | + containerFile.setPath(filePath); |
| 108 | + |
| 109 | + InternalSignatureCollection internalSignatureCollection = new InternalSignatureCollection(); |
| 110 | + InternalSignature internalSignature = new InternalSignature(); |
| 111 | + ByteSequence byteSequence = new ByteSequence(); |
| 112 | + byteSequence.setSequence(""); |
| 113 | + internalSignature.addByteSequence(byteSequence); |
| 114 | + internalSignatureCollection.setInternalSignatures(List.of(internalSignature)); |
| 115 | + containerFile.setBinarySignatures(internalSignatureCollection); |
| 116 | + containerSignature.setFiles(List.of(containerFile)); |
| 117 | + |
| 118 | + ContainerSignatureMatchCollection matchCollection = new ContainerSignatureMatchCollection(List.of(containerSignature), List.of(filePath), 1024); |
| 119 | + AbstractArchiveRequestFactory<InputStream> requestFactory = new AbstractArchiveRequestFactory<>() { |
| 120 | + |
| 121 | + @Override |
| 122 | + public IdentificationRequest<InputStream> newRequest(RequestMetaData metaData, RequestIdentifier identifier) { |
| 123 | + return request; |
| 124 | + } |
| 125 | + }; |
| 126 | + ZipIdentifierEngine zipIdentifierEngine = new ZipIdentifierEngine(); |
| 127 | + zipIdentifierEngine.setRequestFactory(requestFactory); |
| 128 | + zipIdentifierEngine.process(request, matchCollection); |
| 129 | + return matchCollection.getContainerSignatureMatches(); |
| 130 | + } |
| 131 | + |
| 132 | + private static byte[] createZipBytes(String filePath) throws IOException { |
| 133 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 134 | + try (ZipOutputStream zos = new ZipOutputStream(baos)) { |
| 135 | + ZipEntry entry1 = new ZipEntry(filePath); |
| 136 | + zos.putNextEntry(entry1); |
| 137 | + zos.write("Test".getBytes()); |
| 138 | + zos.closeEntry(); |
| 139 | + } |
| 140 | + |
| 141 | + return baos.toByteArray(); |
| 142 | + } |
| 143 | +} |
0 commit comments