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  *
  47  * @library /lib /
  48  * @run main/othervm jdk.jfr.event.io.TestFileReadOnly
  49  */
  50 public class TestFileReadOnly {
  51 
  52     public static void main(String[] args) throws Throwable {
  53         File tmp = File.createTempFile("TestFileReadOnly", ".tmp", new File("."));
  54         tmp.deleteOnExit();
  55         Recording recording = new Recording();
  56         List<IOEvent> expectedEvents = new ArrayList<>();
  57 
  58         recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
  59         recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0));
  60         recording.start();
  61 
  62         final byte[] buf = { 1, 2, 3 };
  63 
  64         // Create the file.
  65         try (RandomAccessFile f = new RandomAccessFile(tmp, "rw")) {
  66             f.write(buf);
  67             expectedEvents.add(IOEvent.createFileWriteEvent(buf.length, tmp));
  68         }
  69 
  70         // Reopen the file as ReadOnly and try to write to it.
  71         // Should generate an event with bytesWritten = -1.
  72         try (RandomAccessFile f = new RandomAccessFile(tmp, "r")) {
  73             try {
  74                 f.write(buf);
  75                 fail("No exception for ReadOnly File");
  76             } catch (IOException e) {
  77                 // Expected exception
  78                 expectedEvents.add(IOEvent.createFileWriteEvent(-1, tmp));
  79             }
  80         }
  81 
  82         // Try to write to read-only FileChannel.
  83         try (RandomAccessFile f = new RandomAccessFile(tmp, "r"); FileChannel ch = f.getChannel()) {
  84             ByteBuffer writeBuf = ByteBuffer.allocateDirect(buf.length);
  85             writeBuf.put(buf);
  86             writeBuf.flip();
  87             ch.position(0);
  88             try {
  89                 ch.write(writeBuf);
  90                 fail("No exception for ReadOnly FileChannel");
  91             } catch (java.nio.channels.NonWritableChannelException e) {
  92                 // Expected exception
  93                 expectedEvents.add(IOEvent.createFileWriteEvent(-1, tmp));
  94             }
  95         }
  96 
  97         recording.stop();
  98         List<RecordedEvent> events = Events.fromRecording(recording);
  99         IOHelper.verifyEqualsInOrder(events, expectedEvents);
 100     }
 101 }