1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8153044
  27  * @summary Allow a property to control location of tzdb.dat file
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.*
  30  * @run main TestCustomizedTZDBLocation
  31  */
  32 
  33 import java.io.*;
  34 import java.nio.file.*;
  35 import jdk.testlibrary.*;
  36 
  37 import static java.nio.file.StandardCopyOption.*;
  38 
  39 public class TestCustomizedTZDBLocation {
  40 
  41     static final String TESTCLASSES = System.getProperty("test.classes", ".");
  42     static final String TESTDIR = System.getProperty("test.dir", ".");
  43     static final String JAVA_HOME = System.getProperty("java.home");
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (System.getProperty("tzdbTest") != null) {
  47             customLaunch(System.getProperty("jdk.time.tzdbfile"));
  48         } else {
  49             Path p = Paths.get(TESTDIR, "tzdb.dat");
  50             try {
  51                 Files.copy(Paths.get(JAVA_HOME, "lib", "tzdb.dat"),
  52                         p, REPLACE_EXISTING);
  53 
  54                 System.out.println("Found version: " +
  55                         getTzIDFromTZDB(p.toString()));
  56 
  57                 try (RandomAccessFile raf = new RandomAccessFile(p.toFile(), "rw")) {
  58                     raf.readByte(); // file format
  59                     raf.readUTF(); // group ID
  60                     raf.readShort(); // version count
  61                     raf.writeUTF("2010z"); // write a dummy test version
  62                 }
  63 
  64                 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  65                         "-DtzdbTest", "-Djdk.time.tzdbfile=" +
  66                                 p.toAbsolutePath(),
  67                         "-cp", Paths.get(TESTCLASSES).toAbsolutePath().toString(),
  68                         "TestCustomizedTZDBLocation"
  69                 );
  70                 OutputAnalyzer output = ProcessTools.executeProcess(pb);
  71                 System.out.println(output.getOutput());
  72                 output.shouldContain("2010z");
  73                 output.shouldHaveExitValue(0);
  74 
  75                 System.out.println("test a dummy file location..");
  76                 pb = ProcessTools.createJavaProcessBuilder(
  77                         "-DtzdbTest", "-Djdk.time.tzdbfile=" +
  78                                 p.toAbsolutePath() + "dummy",
  79                         "-cp", Paths.get(TESTCLASSES).toAbsolutePath().toString(),
  80                         "TestCustomizedTZDBLocation"
  81                 );
  82                 output = ProcessTools.executeProcess(pb);
  83                 System.out.println(output.getOutput());
  84                 output.shouldContain("Error reading tzdata version");
  85                 output.shouldHaveExitValue(1);
  86             } finally {
  87                 FileUtils.deleteFileIfExistsWithRetry(p);
  88             }
  89         }
  90 
  91     }
  92 
  93     static void customLaunch(String path) throws IOException {
  94          System.out.println("version is: " +
  95              getTzIDFromTZDB(path));
  96     }
  97 
  98     static String getTzIDFromTZDB(String tzdbFile) throws IOException {
  99         DataInputStream dis = null;
 100         try {
 101             dis = new DataInputStream(
 102                     new BufferedInputStream(new FileInputStream(new File(tzdbFile))));
 103             return getTzID(dis);
 104         } catch (Exception e) {
 105             throw new RuntimeException("Error reading tzdata version from " + tzdbFile, e);
 106         } finally {
 107             if (dis != null) dis.close();
 108         }
 109     }
 110 
 111     static String getTzID(DataInputStream dis) throws Exception {
 112         String versionID = "";
 113         if (dis.readByte() != 1) {
 114             throw new StreamCorruptedException("File format not recognised");
 115         }
 116         // group
 117         String groupId = dis.readUTF();
 118         if ("TZDB".equals(groupId) == false) {
 119             throw new StreamCorruptedException("File format not recognised");
 120         }
 121         // versions
 122         int versionCount = dis.readShort();
 123         for (int i = 0; i < versionCount; i++) {
 124             versionID = dis.readUTF();
 125         }
 126         return "tzdata" + versionID;
 127     }
 128 }
 129