1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import static java.util.zip.ZipFile.CENOFF; 25 import static java.util.zip.ZipFile.CENTIM; 26 import static java.util.zip.ZipFile.ENDHDR; 27 import static java.util.zip.ZipFile.ENDOFF; 28 import static java.util.zip.ZipFile.LOCTIM; 29 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.OutputStream; 33 import java.net.URI; 34 import java.nio.file.FileSystem; 35 import java.nio.file.FileSystems; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.attribute.BasicFileAttributes; 39 import java.time.Instant; 40 import java.time.LocalDate; 41 import java.time.ZoneId; 42 import java.util.Collections; 43 import java.util.zip.ZipEntry; 44 import java.util.zip.ZipOutputStream; 45 46 /* @test 47 * @bug 8184940 8186227 48 * @summary JDK 9 rejects zip files where the modified day or month is 0 49 * @author Liam Miller-Cushon 50 */ 51 public class ZeroDate { 52 53 public static void main(String[] args) throws Exception { 54 // create a zip file, and read it in as a byte array 55 Path path = Files.createTempFile("bad", ".zip"); 56 try (OutputStream os = Files.newOutputStream(path); 57 ZipOutputStream zos = new ZipOutputStream(os)) { 58 ZipEntry e = new ZipEntry("x"); 59 zos.putNextEntry(e); 60 zos.write((int) 'x'); 61 } 62 int len = (int) Files.size(path); 63 byte[] data = new byte[len]; 64 try (InputStream is = Files.newInputStream(path)) { 65 is.read(data); 66 } 67 Files.delete(path); 68 69 // year, month, day are zero 70 testDate(data.clone(), 0, LocalDate.of(1979, 11, 30)); 71 // only year is zero 72 testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5)); 73 } 74 75 private static void testDate(byte[] data, int date, LocalDate expected) throws IOException { 76 // set the datetime 77 int endpos = data.length - ENDHDR; 78 int cenpos = u16(data, endpos + ENDOFF); 79 int locpos = u16(data, cenpos + CENOFF); 80 writeU32(data, cenpos + CENTIM, date); 81 writeU32(data, locpos + LOCTIM, date); 82 83 // ensure that the archive is still readable, and the date is 1979-11-30 84 Path path = Files.createTempFile("out", ".zip"); 85 try (OutputStream os = Files.newOutputStream(path)) { 86 os.write(data); 87 } 88 URI uri = URI.create("jar:" + path.toUri()); 89 try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { 90 Path entry = fs.getPath("x"); 91 Instant actualInstant = 92 Files.readAttributes(entry, BasicFileAttributes.class) 93 .lastModifiedTime() 94 .toInstant(); 95 Instant expectedInstant = 96 expected.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); 97 if (!actualInstant.equals(expectedInstant)) { 98 throw new AssertionError( 99 String.format("actual: %s, expected: %s", actualInstant, expectedInstant)); 100 } 101 } finally { 102 Files.delete(path); 103 } 104 } 105 106 static int u8(byte[] data, int offset) { 107 return data[offset] & 0xff; 108 } 109 110 static int u16(byte[] data, int offset) { 111 return u8(data, offset) + (u8(data, offset + 1) << 8); 112 } 113 114 private static void writeU32(byte[] data, int pos, int value) { 115 data[pos] = (byte) (value & 0xff); 116 data[pos + 1] = (byte) ((value >> 8) & 0xff); 117 data[pos + 2] = (byte) ((value >> 16) & 0xff); 118 data[pos + 3] = (byte) ((value >> 24) & 0xff); 119 } 120 }