jdk/test/java/util/zip/TestExtraTime.java
changeset 19608 3a4407bc36d7
parent 19374 6773349693eb
child 29095 cb98c9bc4e6d
equal deleted inserted replaced
19607:bee007586d06 19608:3a4407bc36d7
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4759491 6303183 7012868 8015666
    26  * @bug 4759491 6303183 7012868 8015666 8023713
    27  * @summary Test ZOS and ZIS timestamp in extra field correctly
    27  * @summary Test ZOS and ZIS timestamp in extra field correctly
    28  */
    28  */
    29 
    29 
    30 import java.io.*;
    30 import java.io.*;
    31 import java.nio.file.Files;
    31 import java.nio.file.Files;
    32 import java.nio.file.Path;
    32 import java.nio.file.Path;
    33 import java.nio.file.Paths;
    33 import java.nio.file.Paths;
    34 import java.nio.file.attribute.FileTime;
    34 import java.nio.file.attribute.FileTime;
       
    35 import java.util.Arrays;
    35 import java.util.TimeZone;
    36 import java.util.TimeZone;
    36 import java.util.concurrent.TimeUnit;
    37 import java.util.concurrent.TimeUnit;
    37 import java.util.zip.ZipEntry;
    38 import java.util.zip.ZipEntry;
    38 import java.util.zip.ZipFile;
    39 import java.util.zip.ZipFile;
    39 import java.util.zip.ZipInputStream;
    40 import java.util.zip.ZipInputStream;
    50             FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
    51             FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
    51             FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
    52             FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
    52             FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
    53             FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
    53             TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
    54             TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
    54 
    55 
    55             test(mtime, null, null, null);
    56             for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
    56             // ms-dos 1980 epoch problem
    57                 test(mtime, null, null, null, extra);
    57             test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null);
    58                 // ms-dos 1980 epoch problem
    58             // non-default tz
    59                 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
    59             test(mtime, null, null, tz);
    60                 // non-default tz
       
    61                 test(mtime, null, null, tz, extra);
    60 
    62 
    61             test(mtime, atime, null, null);
    63                 test(mtime, atime, null, null, extra);
    62             test(mtime, null, ctime, null);
    64                 test(mtime, null, ctime, null, extra);
    63             test(mtime, atime, ctime, null);
    65                 test(mtime, atime, ctime, null, extra);
    64 
    66 
    65             test(mtime, atime, null, tz);
    67                 test(mtime, atime, null, tz, extra);
    66             test(mtime, null, ctime, tz);
    68                 test(mtime, null, ctime, tz, extra);
    67             test(mtime, atime, ctime, tz);
    69                 test(mtime, atime, ctime, tz, extra);
       
    70             }
    68         }
    71         }
    69     }
    72     }
    70 
    73 
    71     static void test(FileTime mtime, FileTime atime, FileTime ctime,
    74     static void test(FileTime mtime, FileTime atime, FileTime ctime,
    72                      TimeZone tz) throws Throwable {
    75                      TimeZone tz, byte[] extra) throws Throwable {
    73         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
    76         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
    74                           mtime, atime, ctime);
    77                           mtime, atime, ctime);
    75         TimeZone tz0 = TimeZone.getDefault();
    78         TimeZone tz0 = TimeZone.getDefault();
    76         if (tz != null) {
    79         if (tz != null) {
    77             TimeZone.setDefault(tz);
    80             TimeZone.setDefault(tz);
    78         }
    81         }
    79         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    82         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    80         ZipOutputStream zos = new ZipOutputStream(baos);
    83         ZipOutputStream zos = new ZipOutputStream(baos);
    81         ZipEntry ze = new ZipEntry("TestExtreTime.java");
    84         ZipEntry ze = new ZipEntry("TestExtraTime.java");
    82 
    85         ze.setExtra(extra);
    83         ze.setLastModifiedTime(mtime);
    86         ze.setLastModifiedTime(mtime);
    84         if (atime != null)
    87         if (atime != null)
    85             ze.setLastAccessTime(atime);
    88             ze.setLastAccessTime(atime);
    86         if (ctime != null)
    89         if (ctime != null)
    87             ze.setCreationTime(ctime);
    90             ze.setCreationTime(ctime);
    88         zos.putNextEntry(ze);
    91         zos.putNextEntry(ze);
    89         zos.write(new byte[] { 1,2 ,3, 4});
    92         zos.write(new byte[] { 1,2 ,3, 4});
       
    93 
       
    94         // append an extra entry to help check if the length and data
       
    95         // of the extra field are being correctly written (in previous
       
    96         // entry).
       
    97         if (extra != null) {
       
    98             ze = new ZipEntry("TestExtraEntry");
       
    99             zos.putNextEntry(ze);
       
   100         }
    90         zos.close();
   101         zos.close();
    91         if (tz != null) {
   102         if (tz != null) {
    92             TimeZone.setDefault(tz0);
   103             TimeZone.setDefault(tz0);
    93         }
   104         }
    94         // ZipInputStream
   105         // ZipInputStream
    95         ZipInputStream zis = new ZipInputStream(
   106         ZipInputStream zis = new ZipInputStream(
    96                                  new ByteArrayInputStream(baos.toByteArray()));
   107                                  new ByteArrayInputStream(baos.toByteArray()));
    97         ze = zis.getNextEntry();
   108         ze = zis.getNextEntry();
    98         zis.close();
   109         zis.close();
    99         check(mtime, atime, ctime, ze);
   110         check(mtime, atime, ctime, ze, extra);
   100 
   111 
   101         // ZipFile
   112         // ZipFile
   102         Path zpath = Paths.get(System.getProperty("test.dir", "."),
   113         Path zpath = Paths.get(System.getProperty("test.dir", "."),
   103                                "TestExtraTimp.zip");
   114                                "TestExtraTime.zip");
   104         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
   115         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
   105         ZipFile zf = new ZipFile(zpath.toFile());
   116         ZipFile zf = new ZipFile(zpath.toFile());
   106         ze = zf.getEntry("TestExtreTime.java");
   117         ze = zf.getEntry("TestExtraTime.java");
   107         // ZipFile read entry from cen, which does not have a/ctime,
   118         // ZipFile read entry from cen, which does not have a/ctime,
   108         // for now.
   119         // for now.
   109         check(mtime, null, null, ze);
   120         check(mtime, null, null, ze, extra);
   110         zf.close();
   121         zf.close();
   111         Files.delete(zpath);
   122         Files.delete(zpath);
   112     }
   123     }
   113 
   124 
   114     static void check(FileTime mtime, FileTime atime, FileTime ctime,
   125     static void check(FileTime mtime, FileTime atime, FileTime ctime,
   115                       ZipEntry ze) {
   126                       ZipEntry ze, byte[] extra) {
   116         /*
   127         /*
   117         System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
   128         System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
   118                           mtime.to(TimeUnit.MILLISECONDS),
   129                           mtime.to(TimeUnit.MILLISECONDS),
   119                           ze.getTime(),
   130                           ze.getTime(),
   120                           ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
   131                           ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
   128             throw new RuntimeException("Timestamp: storing atime failed!");
   139             throw new RuntimeException("Timestamp: storing atime failed!");
   129         if (ctime != null &&
   140         if (ctime != null &&
   130             ctime.to(TimeUnit.SECONDS) !=
   141             ctime.to(TimeUnit.SECONDS) !=
   131             ze.getCreationTime().to(TimeUnit.SECONDS))
   142             ze.getCreationTime().to(TimeUnit.SECONDS))
   132             throw new RuntimeException("Timestamp: storing ctime failed!");
   143             throw new RuntimeException("Timestamp: storing ctime failed!");
       
   144         if (extra != null) {
       
   145             // if extra data exists, the current implementation put it at
       
   146             // the end of the extra data array (implementation detail)
       
   147             byte[] extra1 = ze.getExtra();
       
   148             if (extra1 == null || extra1.length < extra.length ||
       
   149                 !Arrays.equals(Arrays.copyOfRange(extra1,
       
   150                                                   extra1.length - extra.length,
       
   151                                                   extra1.length),
       
   152                                extra)) {
       
   153                 throw new RuntimeException("Timestamp: storing extra field failed!");
       
   154             }
       
   155         }
   133     }
   156     }
   134 }
   157 }