test/jdk/nio/zipfs/ZeroDate.java

Print this page




  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
  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:file://" + path.toAbsolutePath());
  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     }


  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     }