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