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.assertEquals;
  29 import static jdk.test.lib.Asserts.assertNotEquals;
  30 
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.RandomAccessFile;
  35 import java.nio.ByteBuffer;
  36 import java.nio.channels.FileChannel;
  37 
  38 import jdk.jfr.Recording;
  39 import jdk.jfr.consumer.RecordedEvent;
  40 import jdk.test.lib.jfr.Events;
  41 
  42 /**
  43  * @test
  44  * @summary Test with FlightRecorder enabled but with the events disabled.
  45  * @key jfr
  46  *
  47  * @library /lib /
  48  * @run main/othervm jdk.jfr.event.io.TestDisabledEvents
  49  */
  50 
  51 // Verify that IO operations are correct and that no events are generated.
  52 public class TestDisabledEvents {
  53 
  54     private static final int writeInt = 'A';
  55     private static final byte[] writeBuf = { 'B', 'C', 'D' };
  56 
  57     public static void main(String[] args) throws Throwable {
  58         File tmp = File.createTempFile("TestDisabledEvents", ".tmp", new File("."));
  59         tmp.deleteOnExit();
  60         Recording recording = new Recording();
  61         recording.disable(IOEvent.EVENT_FILE_READ);
  62         recording.disable(IOEvent.EVENT_FILE_WRITE);
  63         recording.start();
  64 
  65         useRandomAccessFile(tmp);
  66         useFileStreams(tmp);
  67         useFileChannel(tmp);
  68 
  69         recording.stop();
  70         for (RecordedEvent event : Events.fromRecording(recording)) {
  71             final String eventName = event.getEventType().getName();
  72             System.out.println("Got eventName:" + eventName);
  73             assertNotEquals(eventName, IOEvent.EVENT_FILE_READ, "Got disabled read event");
  74             assertNotEquals(eventName, IOEvent.EVENT_FILE_WRITE, "Got disabled write event");
  75         }
  76     }
  77 
  78     private static void useRandomAccessFile(File tmp) throws Throwable {
  79         tmp.delete();
  80         try (RandomAccessFile ras = new RandomAccessFile(tmp, "rw")) {
  81             ras.write(writeInt);
  82             ras.write(writeBuf);
  83             ras.seek(0);
  84             int readInt = ras.read();
  85             assertEquals(readInt, writeInt, "Wrong readInt");
  86             byte[] readBuf = new byte[writeBuf.length];
  87             int readSize = ras.read(readBuf);
  88             assertEquals(readSize, writeBuf.length, "Wrong readSize");
  89             // Try to read more which should generate EOF.
  90             readInt = ras.read();
  91             assertEquals(readInt, -1, "Wrong readInt after EOF");
  92         }
  93     }
  94 
  95     private static void useFileStreams(File tmp) throws Throwable {
  96         tmp.delete();
  97         try (FileOutputStream fos = new FileOutputStream(tmp)) {
  98             fos.write(writeInt);
  99             fos.write(writeBuf);
 100         }
 101 
 102         try (FileInputStream fis = new FileInputStream(tmp)) {
 103             int readInt = fis.read();
 104             assertEquals(readInt, writeInt, "Wrong readInt");
 105             byte[] readBuf = new byte[writeBuf.length];
 106             int readSize = fis.read(readBuf);
 107             assertEquals(readSize, writeBuf.length, "Wrong readSize");
 108             // Try to read more which should generate EOF.
 109             readInt = fis.read();
 110             assertEquals(readInt, -1, "Wrong readInt after EOF");
 111         }
 112     }
 113 
 114     private static void useFileChannel(File tmp) throws Throwable {
 115         tmp.delete();
 116         try (RandomAccessFile rf = new RandomAccessFile(tmp, "rw");
 117                 FileChannel ch = rf.getChannel()) {
 118             final String bufContent = "0123456789";
 119             final int bufSize = bufContent.length();
 120             ByteBuffer writeBuf = ByteBuffer.allocateDirect(bufSize);
 121             writeBuf.put(bufContent.getBytes());
 122 
 123             writeBuf.flip();
 124             int writeSize = ch.write(writeBuf);
 125             assertEquals(writeSize, bufSize, "Wrong writeSize for FileChannel");
 126 
 127             ch.position(0);
 128             ByteBuffer readBuf = ByteBuffer.allocateDirect(bufSize);
 129             int readSize = ch.read(readBuf);
 130             assertEquals(readSize, bufSize, "Wrong readSize full for FileChannel");
 131             assertEquals(0, writeBuf.compareTo(readBuf), "Unexpected readBuf content");
 132         }
 133     }
 134 }