1 /*
   2  * Copyright (c) 2014, 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 CdsWriteError
  26  * @summary Test how VM handles situation when it is impossible to write the
  27  *          CDS archive. VM is expected to exit gracefully and display the
  28  *          correct reason for the error.
  29  * @library /testlibrary
  30  * @run main CdsWriteError
  31  * @bug 8032222
  32  */
  33 
  34 import com.oracle.java.testlibrary.*;
  35 import java.io.File;
  36 
  37 public class CdsWriteError {
  38     public static void main(String[] args) throws Exception {
  39 
  40         if (Platform.isWindows()) {
  41             System.out.println("This test is ignored on Windows. This test " +
  42                 "manipulates folder writable attribute, which is known to be " +
  43                 "often ignored by Windows");
  44 
  45             return;
  46         }
  47 
  48         // This test has been unstable for Mac OSx (see JDK-8032222)
  49         if (Platform.isOSX()) {
  50             System.out.println("This test is skipped on Mac");
  51             return;
  52         }
  53 
  54         String folderName = "tmp";
  55         String fileName = folderName + File.separator + "empty.jsa";
  56 
  57         // create an empty archive file and make it read only
  58         File folder = new File(folderName);
  59         if (!folder.mkdir())
  60             throw new RuntimeException("Error when creating a tmp folder");
  61 
  62         File cdsFile = new File(fileName);
  63         if (!cdsFile.createNewFile())
  64             throw new RuntimeException("Error when creating an empty CDS file");
  65         if (!cdsFile.setWritable(false))
  66             throw new RuntimeException("Error: could not set writable attribute on cds file");
  67         if (!folder.setWritable(false))
  68             throw new RuntimeException("Error: could not set writable attribute on the cds folder");
  69 
  70         try {
  71            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  72              "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=./" + fileName, "-Xshare:dump");
  73 
  74             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  75             output.shouldContain("Unable to create shared archive file");
  76             output.shouldHaveExitValue(1);
  77         } finally {
  78             // doing this, just in case, to make sure that files can be deleted by the harness
  79             // on any subsequent run
  80             folder.setWritable(true);
  81             cdsFile.setWritable(true);
  82         }
  83     }
  84 }
  85