1 /*
   2  * Copyright (c) 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.event.io;
  27 
  28 import static jdk.test.lib.Asserts.fail;
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.RandomAccessFile;
  33 import java.nio.ByteBuffer;
  34 import java.nio.channels.FileChannel;
  35 import java.time.Duration;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 
  39 import jdk.jfr.Recording;
  40 import jdk.jfr.consumer.RecordedEvent;
  41 import jdk.test.lib.jfr.Events;
  42 
  43 /*
  44  * @test
  45  * @key jfr
  46  * @library /test/lib /test/jdk
  47  * @run main/othervm jdk.jfr.event.io.TestFileReadOnly
  48  */
  49 public class TestFileReadOnly {
  50 
  51     public static void main(String[] args) throws Throwable {
  52         File tmp = File.createTempFile("TestFileReadOnly", ".tmp", new File("."));
  53         tmp.deleteOnExit();
  54         Recording recording = new Recording();
  55         List<IOEvent> expectedEvents = new ArrayList<>();
  56 
  57         recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
  58         recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0));
  59         recording.start();
  60 
  61         final byte[] buf = { 1, 2, 3 };
  62 
  63         // Create the file.
  64         try (RandomAccessFile f = new RandomAccessFile(tmp, "rw")) {
  65             f.write(buf);
  66             expectedEvents.add(IOEvent.createFileWriteEvent(buf.length, tmp));
  67         }
  68 
  69         // Reopen the file as ReadOnly and try to write to it.
  70         // Should generate an event with bytesWritten = -1.
  71         try (RandomAccessFile f = new RandomAccessFile(tmp, "r")) {
  72             try {
  73                 f.write(buf);
  74                 fail("No exception for ReadOnly File");
  75             } catch (IOException e) {
  76                 // Expected exception
  77                 expectedEvents.add(IOEvent.createFileWriteEvent(-1, tmp));
  78             }
  79         }
  80 
  81         // Try to write to read-only FileChannel.
  82         try (RandomAccessFile f = new RandomAccessFile(tmp, "r"); FileChannel ch = f.getChannel()) {
  83             ByteBuffer writeBuf = ByteBuffer.allocateDirect(buf.length);
  84             writeBuf.put(buf);
  85             writeBuf.flip();
  86             ch.position(0);
  87             try {
  88                 ch.write(writeBuf);
  89                 fail("No exception for ReadOnly FileChannel");
  90             } catch (java.nio.channels.NonWritableChannelException e) {
  91                 // Expected exception
  92                 expectedEvents.add(IOEvent.createFileWriteEvent(-1, tmp));
  93             }
  94         }
  95 
  96         recording.stop();
  97         List<RecordedEvent> events = Events.fromRecording(recording);
  98         IOHelper.verifyEqualsInOrder(events, expectedEvents);
  99     }
 100 }