test/jdk/java/util/zip/TestExtraTime.java

Print this page




   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 /**
  25  * @test
  26  * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526 8130914 8161942

  27  * @summary Test ZOS and ZIS timestamp in extra field correctly
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.nio.file.attribute.BasicFileAttributeView;
  35 import java.nio.file.attribute.FileOwnerAttributeView;
  36 import java.nio.file.attribute.FileTime;
  37 import java.nio.file.attribute.PosixFilePermission;
  38 import java.time.Instant;
  39 import java.util.Arrays;
  40 import java.util.Set;
  41 import java.util.TimeZone;
  42 import java.util.concurrent.TimeUnit;
  43 import java.util.zip.ZipEntry;
  44 import java.util.zip.ZipFile;
  45 import java.util.zip.ZipInputStream;
  46 import java.util.zip.ZipOutputStream;


  79                  tz, extra);
  80 
  81             // unix 2038
  82             time = 0x80000000L;
  83             test(FileTime.from(time, TimeUnit.SECONDS),
  84                  FileTime.from(time, TimeUnit.SECONDS),
  85                  FileTime.from(time, TimeUnit.SECONDS),
  86                  tz, extra);
  87 
  88             // mtime < unix 2038
  89             time = 0x7fffffffL;
  90             test(FileTime.from(time, TimeUnit.SECONDS),
  91                  FileTime.from(time + 30000, TimeUnit.SECONDS),
  92                  FileTime.from(time + 30000, TimeUnit.SECONDS),
  93                  tz, extra);
  94         }
  95 
  96         testNullHandling();
  97         testTagOnlyHandling();
  98         testTimeConversions();

  99     }
 100 
 101     static void test(FileTime mtime, FileTime atime, FileTime ctime,
 102                      TimeZone tz, byte[] extra) throws Throwable {
 103         test0(mtime, null, null, null, extra);
 104         test0(mtime, null, null, tz, extra);    // non-default tz
 105         test0(mtime, atime, null, null, extra);
 106         test0(mtime, null, ctime, null, extra);
 107         test0(mtime, atime, ctime, null, extra);
 108         test0(mtime, atime, null, tz, extra);
 109         test0(mtime, null, ctime, tz, extra);
 110         test0(mtime, atime, ctime, tz, extra);
 111     }
 112 
 113     static void test0(FileTime mtime, FileTime atime, FileTime ctime,
 114                      TimeZone tz, byte[] extra) throws Throwable {
 115         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
 116                           mtime, atime, ctime);
 117         TimeZone tz0 = TimeZone.getDefault();
 118         if (tz != null) {


 175         }
 176         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 177         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 178             ze = zf.getEntry("TestExtraTime.java");
 179             // ZipFile read entry from cen, which does not have a/ctime,
 180             // for now.
 181             check(mtime, null, null, ze, extra);
 182         } finally {
 183             Files.delete(zpath);
 184         }
 185     }
 186 
 187     static void check(FileTime mtime, FileTime atime, FileTime ctime,
 188                       ZipEntry ze, byte[] extra) {
 189         /*
 190         System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
 191                           mtime.to(TimeUnit.MILLISECONDS),
 192                           ze.getTime(),
 193                           ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
 194          */
 195         if (mtime.to(TimeUnit.SECONDS) !=
 196             ze.getLastModifiedTime().to(TimeUnit.SECONDS))

 197             throw new RuntimeException("Timestamp: storing mtime failed!");

 198         if (atime != null &&
 199             atime.to(TimeUnit.SECONDS) !=
 200             ze.getLastAccessTime().to(TimeUnit.SECONDS))
 201             throw new RuntimeException("Timestamp: storing atime failed!");
 202         if (ctime != null &&
 203             ctime.to(TimeUnit.SECONDS) !=
 204             ze.getCreationTime().to(TimeUnit.SECONDS))
 205             throw new RuntimeException("Timestamp: storing ctime failed!");
 206         if (extra != null) {
 207             // if extra data exists, the current implementation put it at
 208             // the end of the extra data array (implementation detail)
 209             byte[] extra1 = ze.getExtra();
 210             if (extra1 == null || extra1.length < extra.length ||
 211                 !Arrays.equals(Arrays.copyOfRange(extra1,
 212                                                   extra1.length - extra.length,
 213                                                   extra1.length),
 214                                extra)) {
 215                 throw new RuntimeException("Timestamp: storing extra field failed!");
 216             }
 217         }


 288             ZipEntry ze = new ZipEntry("TestExtraTime.java");
 289             ze.setExtra(extra);
 290             zos.putNextEntry(ze);
 291             zos.write(new byte[] { 1,2 ,3, 4});
 292         }
 293         try (ZipInputStream zis = new ZipInputStream(
 294                  new ByteArrayInputStream(baos.toByteArray()))) {
 295             ZipEntry ze = zis.getNextEntry();
 296             check(ze, extra);
 297         }
 298         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 299                                "TestExtraTime.zip");
 300         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 301         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 302             ZipEntry ze = zf.getEntry("TestExtraTime.java");
 303             check(ze, extra);
 304         } finally {
 305             Files.delete(zpath);
 306         }
 307     }
















































 308 }


   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 /**
  25  * @test
  26  * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526 8130914
  27  *      8161942 8206389
  28  * @summary Test ZOS and ZIS timestamp in extra field correctly
  29  */
  30 
  31 import java.io.*;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.nio.file.attribute.BasicFileAttributeView;
  36 import java.nio.file.attribute.FileOwnerAttributeView;
  37 import java.nio.file.attribute.FileTime;
  38 import java.nio.file.attribute.PosixFilePermission;
  39 import java.time.Instant;
  40 import java.util.Arrays;
  41 import java.util.Set;
  42 import java.util.TimeZone;
  43 import java.util.concurrent.TimeUnit;
  44 import java.util.zip.ZipEntry;
  45 import java.util.zip.ZipFile;
  46 import java.util.zip.ZipInputStream;
  47 import java.util.zip.ZipOutputStream;


  80                  tz, extra);
  81 
  82             // unix 2038
  83             time = 0x80000000L;
  84             test(FileTime.from(time, TimeUnit.SECONDS),
  85                  FileTime.from(time, TimeUnit.SECONDS),
  86                  FileTime.from(time, TimeUnit.SECONDS),
  87                  tz, extra);
  88 
  89             // mtime < unix 2038
  90             time = 0x7fffffffL;
  91             test(FileTime.from(time, TimeUnit.SECONDS),
  92                  FileTime.from(time + 30000, TimeUnit.SECONDS),
  93                  FileTime.from(time + 30000, TimeUnit.SECONDS),
  94                  tz, extra);
  95         }
  96 
  97         testNullHandling();
  98         testTagOnlyHandling();
  99         testTimeConversions();
 100         testNullMtime();
 101     }
 102 
 103     static void test(FileTime mtime, FileTime atime, FileTime ctime,
 104                      TimeZone tz, byte[] extra) throws Throwable {
 105         test0(mtime, null, null, null, extra);
 106         test0(mtime, null, null, tz, extra);    // non-default tz
 107         test0(mtime, atime, null, null, extra);
 108         test0(mtime, null, ctime, null, extra);
 109         test0(mtime, atime, ctime, null, extra);
 110         test0(mtime, atime, null, tz, extra);
 111         test0(mtime, null, ctime, tz, extra);
 112         test0(mtime, atime, ctime, tz, extra);
 113     }
 114 
 115     static void test0(FileTime mtime, FileTime atime, FileTime ctime,
 116                      TimeZone tz, byte[] extra) throws Throwable {
 117         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
 118                           mtime, atime, ctime);
 119         TimeZone tz0 = TimeZone.getDefault();
 120         if (tz != null) {


 177         }
 178         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 179         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 180             ze = zf.getEntry("TestExtraTime.java");
 181             // ZipFile read entry from cen, which does not have a/ctime,
 182             // for now.
 183             check(mtime, null, null, ze, extra);
 184         } finally {
 185             Files.delete(zpath);
 186         }
 187     }
 188 
 189     static void check(FileTime mtime, FileTime atime, FileTime ctime,
 190                       ZipEntry ze, byte[] extra) {
 191         /*
 192         System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
 193                           mtime.to(TimeUnit.MILLISECONDS),
 194                           ze.getTime(),
 195                           ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
 196          */
 197         if (mtime != null &&
 198             mtime.to(TimeUnit.SECONDS) !=
 199             ze.getLastModifiedTime().to(TimeUnit.SECONDS)) {
 200             throw new RuntimeException("Timestamp: storing mtime failed!");
 201         }
 202         if (atime != null &&
 203             atime.to(TimeUnit.SECONDS) !=
 204             ze.getLastAccessTime().to(TimeUnit.SECONDS))
 205             throw new RuntimeException("Timestamp: storing atime failed!");
 206         if (ctime != null &&
 207             ctime.to(TimeUnit.SECONDS) !=
 208             ze.getCreationTime().to(TimeUnit.SECONDS))
 209             throw new RuntimeException("Timestamp: storing ctime failed!");
 210         if (extra != null) {
 211             // if extra data exists, the current implementation put it at
 212             // the end of the extra data array (implementation detail)
 213             byte[] extra1 = ze.getExtra();
 214             if (extra1 == null || extra1.length < extra.length ||
 215                 !Arrays.equals(Arrays.copyOfRange(extra1,
 216                                                   extra1.length - extra.length,
 217                                                   extra1.length),
 218                                extra)) {
 219                 throw new RuntimeException("Timestamp: storing extra field failed!");
 220             }
 221         }


 292             ZipEntry ze = new ZipEntry("TestExtraTime.java");
 293             ze.setExtra(extra);
 294             zos.putNextEntry(ze);
 295             zos.write(new byte[] { 1,2 ,3, 4});
 296         }
 297         try (ZipInputStream zis = new ZipInputStream(
 298                  new ByteArrayInputStream(baos.toByteArray()))) {
 299             ZipEntry ze = zis.getNextEntry();
 300             check(ze, extra);
 301         }
 302         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 303                                "TestExtraTime.zip");
 304         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 305         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 306             ZipEntry ze = zf.getEntry("TestExtraTime.java");
 307             check(ze, extra);
 308         } finally {
 309             Files.delete(zpath);
 310         }
 311     }
 312 
 313     static void checkLastModifiedTimeDOS(FileTime mtime, ZipEntry ze) {
 314         FileTime lmt = ze.getLastModifiedTime();
 315         if ((lmt.to(TimeUnit.SECONDS) >>> 1) != (mtime.to(TimeUnit.SECONDS) >>> 1) ||
 316             lmt.to(TimeUnit.MILLISECONDS) != ze.getTime() ||
 317             lmt.to(TimeUnit.MILLISECONDS) % 1000 != 0) {
 318             throw new RuntimeException("Timestamp: storing mtime in dos format failed!");
 319         }
 320     }
 321 
 322     static void testNullMtime() throws Throwable {
 323         Instant now = Instant.now();
 324         FileTime ctime = FileTime.from(now);
 325         FileTime atime = FileTime.from(now.plusSeconds(7));
 326         FileTime mtime = FileTime.from(now.plusSeconds(13));
 327         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
 328                           mtime, atime, ctime);
 329 
 330         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 331         try (ZipOutputStream zos = new ZipOutputStream(baos)) {
 332             ZipEntry ze = new ZipEntry("TestExtraTime.java");
 333             ze.setCreationTime(ctime);
 334             ze.setLastAccessTime(atime);
 335             // ze.setLastModifiedTime(now);
 336             ze.setTime(mtime.toMillis());
 337             zos.putNextEntry(ze);
 338             zos.write(new byte[] { 1,2 ,3, 4});
 339         }
 340 
 341         try (ZipInputStream zis = new ZipInputStream(
 342                  new ByteArrayInputStream(baos.toByteArray()))) {
 343             ZipEntry ze = zis.getNextEntry();
 344             // check LOC
 345             check(null, atime, ctime, ze, null);
 346             checkLastModifiedTimeDOS(mtime, ze);
 347         }
 348 
 349         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 350                                "TestExtraTime.zip");
 351         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 352         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 353             ZipEntry ze = zf.getEntry("TestExtraTime.java");
 354             // check CEN
 355             checkLastModifiedTimeDOS(mtime, ze);
 356         } finally {
 357             Files.delete(zpath);
 358         }
 359     }
 360 }