1 /*
   2  * Copyright (c) 2017, 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 6364894
  27  * @requires (os.family == "windows")
  28  * @library /test/lib
  29  * @build jdk.test.lib.Asserts
  30  * @run main FileOpenTest
  31  * @summary Test to ensure that opening of hidden Vs non-hidden,
  32  *          read/write Vs read-only files for writing works as expected.
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.FileOutputStream;
  38 import java.nio.file.Files;
  39 
  40 import static jdk.test.lib.Asserts.assertTrue;
  41 
  42 public class FileOpenTest {
  43 
  44     private static File tmpFile;
  45 
  46     public static void main(String args[]) throws Exception {
  47         try {
  48             tmpFile = File.createTempFile("FileOpenTest", "suffix");
  49 
  50             // Opening Writable Normal File..
  51             test(true);
  52 
  53             // Opening Writable Hidden File..
  54             Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
  55             test(false);
  56 
  57             // Opening Read-Only Hidden File..
  58             Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);
  59             tmpFile.setReadOnly();
  60             test(false);
  61 
  62             // Opening Read-Only Normal File..
  63             Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
  64             test(false);
  65         } finally {
  66             tmpFile.delete();
  67         }
  68     }
  69 
  70     private static void test(boolean writable) throws Exception {
  71 
  72         try (FileOutputStream fs = new FileOutputStream(tmpFile)) {
  73             fs.write(1);
  74             assertTrue(writable, "Able to open READ-ONLY file for WRITING!");
  75             assertTrue(tmpFile.canWrite(), "Able to open READ-ONLY file for WRITING!");
  76         } catch(IOException e) {
  77             assertTrue(!writable, "Unable to open non-READ-ONLY file for WRITING!");
  78             System.out.println("Caught the Exception as expected");
  79             e.printStackTrace(System.out);
  80         }
  81     }
  82 }