test/jdk/java/util/zip/ZipFile/ZeroDate.java
changeset 47216 71c04702a3d5
parent 46154 fcef65412084
child 47331 39d1de71faca
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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.nio.file.Files;
       
    34 import java.nio.file.Path;
       
    35 import java.time.Instant;
       
    36 import java.time.LocalDate;
       
    37 import java.time.ZoneId;
       
    38 import java.util.zip.ZipEntry;
       
    39 import java.util.zip.ZipFile;
       
    40 import java.util.zip.ZipOutputStream;
       
    41 
       
    42 /* @test
       
    43  * @bug 8184940
       
    44  * @summary JDK 9 rejects zip files where the modified day or month is 0
       
    45  * @author Liam Miller-Cushon
       
    46  */
       
    47 public class ZeroDate {
       
    48 
       
    49     public static void main(String[] args) throws Exception {
       
    50         // create a zip file, and read it in as a byte array
       
    51         Path path = Files.createTempFile("bad", ".zip");
       
    52         try (OutputStream os = Files.newOutputStream(path);
       
    53                 ZipOutputStream zos = new ZipOutputStream(os)) {
       
    54             ZipEntry e = new ZipEntry("x");
       
    55             zos.putNextEntry(e);
       
    56             zos.write((int) 'x');
       
    57         }
       
    58         int len = (int) Files.size(path);
       
    59         byte[] data = new byte[len];
       
    60         try (InputStream is = Files.newInputStream(path)) {
       
    61             is.read(data);
       
    62         }
       
    63         Files.delete(path);
       
    64 
       
    65         // year, month, day are zero
       
    66         testDate(data.clone(), 0, LocalDate.of(1979, 11, 30));
       
    67         // only year is zero
       
    68         testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5));
       
    69     }
       
    70 
       
    71     private static void testDate(byte[] data, int date, LocalDate expected) throws IOException {
       
    72         // set the datetime
       
    73         int endpos = data.length - ENDHDR;
       
    74         int cenpos = u16(data, endpos + ENDOFF);
       
    75         int locpos = u16(data, cenpos + CENOFF);
       
    76         writeU32(data, cenpos + CENTIM, date);
       
    77         writeU32(data, locpos + LOCTIM, date);
       
    78 
       
    79         // ensure that the archive is still readable, and the date is 1979-11-30
       
    80         Path path = Files.createTempFile("out", ".zip");
       
    81         try (OutputStream os = Files.newOutputStream(path)) {
       
    82             os.write(data);
       
    83         }
       
    84         try (ZipFile zf = new ZipFile(path.toFile())) {
       
    85             ZipEntry ze = zf.entries().nextElement();
       
    86             Instant actualInstant = ze.getLastModifiedTime().toInstant();
       
    87             Instant expectedInstant =
       
    88                     expected.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
       
    89             if (!actualInstant.equals(expectedInstant)) {
       
    90                 throw new AssertionError(
       
    91                         String.format("actual: %s, expected: %s", actualInstant, expectedInstant));
       
    92             }
       
    93         } finally {
       
    94             Files.delete(path);
       
    95         }
       
    96     }
       
    97 
       
    98     static int u8(byte[] data, int offset) {
       
    99         return data[offset] & 0xff;
       
   100     }
       
   101 
       
   102     static int u16(byte[] data, int offset) {
       
   103         return u8(data, offset) + (u8(data, offset + 1) << 8);
       
   104     }
       
   105 
       
   106     private static void writeU32(byte[] data, int pos, int value) {
       
   107         data[pos] = (byte) (value & 0xff);
       
   108         data[pos + 1] = (byte) ((value >> 8) & 0xff);
       
   109         data[pos + 2] = (byte) ((value >> 16) & 0xff);
       
   110         data[pos + 3] = (byte) ((value >> 24) & 0xff);
       
   111     }
       
   112 }