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.LocalDateTime;
  42 import java.time.ZoneId;
  43 import java.util.Collections;
  44 import java.util.zip.ZipEntry;
  45 import java.util.zip.ZipOutputStream;
  46 
  47 /* @test
  48  * @bug 8184940 8186227 8188869
  49  * @summary JDK 9 rejects zip files where the modified day or month is 0
  50  *          or otherwise represent an invalid date, such as 1980-02-30 24:60:60
  51  * @author Liam Miller-Cushon
  52  */
  53 public class ZeroDate {
  54 
  55     public static void main(String[] args) throws Exception {
  56         // create a zip file, and read it in as a byte array
  57         Path path = Files.createTempFile("bad", ".zip");
  58         try (OutputStream os = Files.newOutputStream(path);
  59                 ZipOutputStream zos = new ZipOutputStream(os)) {
  60             ZipEntry e = new ZipEntry("x");
  61             zos.putNextEntry(e);
  62             zos.write((int) 'x');
  63         }
  64         int len = (int) Files.size(path);
  65         byte[] data = new byte[len];
  66         try (InputStream is = Files.newInputStream(path)) {
  67             is.read(data);
  68         }
  69         Files.delete(path);
  70 
  71         // year, month, day are zero
  72         testDate(data.clone(), 0, LocalDate.of(1979, 11, 30).atStartOfDay());
  73         // only year is zero
  74         testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5).atStartOfDay());
  75         // month is greater than 12
  76         testDate(data.clone(), 0 << 25 | 13 << 21 | 1 << 16, LocalDate.of(1981, 1, 1).atStartOfDay());
  77         // 30th of February
  78         testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16, LocalDate.of(1980, 3, 1).atStartOfDay());
  79         // 30th of February, 24:60:60
  80         testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16 | 24 << 11 | 60 << 5 | 60 >> 1,
  81                 LocalDateTime.of(1980, 3, 2, 1, 1, 0));
  82     }
  83 
  84     private static void testDate(byte[] data, int date, LocalDateTime expected) throws IOException {
  85         // set the datetime
  86         int endpos = data.length - ENDHDR;
  87         int cenpos = u16(data, endpos + ENDOFF);
  88         int locpos = u16(data, cenpos + CENOFF);
  89         writeU32(data, cenpos + CENTIM, date);
  90         writeU32(data, locpos + LOCTIM, date);
  91 
  92         // ensure that the archive is still readable, and the date is 1979-11-30
  93         Path path = Files.createTempFile("out", ".zip");
  94         try (OutputStream os = Files.newOutputStream(path)) {
  95             os.write(data);
  96         }
  97         URI uri = URI.create("jar:" + path.toUri());
  98         try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
  99             Path entry = fs.getPath("x");
 100             Instant actualInstant =
 101                     Files.readAttributes(entry, BasicFileAttributes.class)
 102                             .lastModifiedTime()
 103                             .toInstant();
 104             Instant expectedInstant =
 105                     expected.atZone(ZoneId.systemDefault()).toInstant();
 106             if (!actualInstant.equals(expectedInstant)) {
 107                 throw new AssertionError(
 108                         String.format("actual: %s, expected: %s", actualInstant, expectedInstant));
 109             }
 110         } finally {
 111             Files.delete(path);
 112         }
 113     }
 114 
 115     static int u8(byte[] data, int offset) {
 116         return data[offset] & 0xff;
 117     }
 118 
 119     static int u16(byte[] data, int offset) {
 120         return u8(data, offset) + (u8(data, offset + 1) << 8);
 121     }
 122 
 123     private static void writeU32(byte[] data, int pos, int value) {
 124         data[pos] = (byte) (value & 0xff);
 125         data[pos + 1] = (byte) ((value >> 8) & 0xff);
 126         data[pos + 2] = (byte) ((value >> 16) & 0xff);
 127         data[pos + 3] = (byte) ((value >> 24) & 0xff);
 128     }
 129 }