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