hotspot/test/runtime/modules/ImageFile/ImageFileHeaderTest.java
changeset 32761 d7c393b4e0d3
parent 32760 3851193a8c60
parent 32757 79d34d4b9627
child 32762 253adb0f4301
equal deleted inserted replaced
32760:3851193a8c60 32761:d7c393b4e0d3
     1 /*
       
     2  * Copyright (c) 2015, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * Test that opening image containing wrong headers fails.
       
    26  * @test ImageFileHeaderTest
       
    27  * @library /testlibrary /../../test/lib
       
    28  * @build ImageFileHeaderTest
       
    29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    30  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    31  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ImageFileHeaderTest
       
    32  */
       
    33 
       
    34 import java.nio.*;
       
    35 import java.nio.file.Files;
       
    36 import java.nio.file.Paths;
       
    37 import sun.hotspot.WhiteBox;
       
    38 import static jdk.test.lib.Asserts.*;
       
    39 
       
    40 public class ImageFileHeaderTest {
       
    41 
       
    42     public static final int MAGIC = 0xCAFEDADA;
       
    43     public static final short MAJOR = 0;
       
    44     public static final short MINOR = 1;
       
    45 
       
    46     public static final WhiteBox wb = WhiteBox.getWhiteBox();
       
    47     public static ByteBuffer buf;
       
    48 
       
    49     public static void main(String... args) throws Exception {
       
    50 
       
    51         ByteOrder endian = getEndian();
       
    52 
       
    53         // Try to read a non-existing file
       
    54         assertFalse(wb.readImageFile("bogus"));
       
    55 
       
    56         // Incomplete header, only include the correct magic
       
    57         buf = ByteBuffer.allocate(100);
       
    58         buf.order(endian);
       
    59         buf.putInt(MAGIC);
       
    60         assertFalse(testImageFile("invalidheader.jimage"));
       
    61 
       
    62         // Build a complete header but reverse the endian
       
    63         buf = ByteBuffer.allocate(100);
       
    64         buf.order(endian == ByteOrder.LITTLE_ENDIAN ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
       
    65         buf.putInt(MAGIC);
       
    66         buf.putShort(MAJOR);
       
    67         buf.putShort(MINOR);
       
    68         assertFalse(testImageFile("wrongendian.jimage"));
       
    69 
       
    70         // Use the wrong magic
       
    71         buf = ByteBuffer.allocate(100);
       
    72         buf.order(endian);
       
    73         buf.putInt(0xBEEFCACE);
       
    74         buf.putShort(MAJOR);
       
    75         buf.putShort(MINOR);
       
    76         assertFalse(testImageFile("wrongmagic.jimage"));
       
    77 
       
    78         // Wrong major version (current + 1)
       
    79         buf = ByteBuffer.allocate(100);
       
    80         buf.order(endian);
       
    81         buf.putInt(MAGIC);
       
    82         buf.putShort((short)(MAJOR + 1));
       
    83         buf.putShort((short)MINOR);
       
    84         assertFalse(testImageFile("wrongmajorversion.jimage"));
       
    85 
       
    86         // Wrong major version (negative)
       
    87         buf = ByteBuffer.allocate(100);
       
    88         buf.order(endian);
       
    89         buf.putInt(MAGIC);
       
    90         buf.putShort((short) -17);
       
    91         buf.putShort((short)MINOR);
       
    92         assertFalse(testImageFile("negativemajorversion.jimage"));
       
    93 
       
    94         // Wrong minor version (current + 1)
       
    95         buf = ByteBuffer.allocate(100);
       
    96         buf.order(endian);
       
    97         buf.putInt(MAGIC);
       
    98         buf.putShort((short)MAJOR);
       
    99         buf.putShort((short)(MINOR + 1));
       
   100         assertFalse(testImageFile("wrongminorversion.jimage"));
       
   101 
       
   102         // Wrong minor version (negative)
       
   103         buf = ByteBuffer.allocate(100);
       
   104         buf.order(endian);
       
   105         buf.putInt(MAGIC);
       
   106         buf.putShort((short)MAJOR);
       
   107         buf.putShort((short) -17);
       
   108         assertFalse(testImageFile("negativeminorversion.jimage"));
       
   109     }
       
   110 
       
   111     public static boolean testImageFile(String filename) throws Exception {
       
   112         Files.write(Paths.get(filename), buf.array());
       
   113         System.out.println("Calling ReadImageFile on " + filename);
       
   114         return wb.readImageFile(filename);
       
   115     }
       
   116 
       
   117     public static ByteOrder getEndian() {
       
   118         String endian = System.getProperty("sun.cpu.endian");
       
   119         if (endian.equalsIgnoreCase("little")) {
       
   120             return ByteOrder.LITTLE_ENDIAN;
       
   121         } else if (endian.equalsIgnoreCase("big")) {
       
   122             return ByteOrder.BIG_ENDIAN;
       
   123         }
       
   124         throw new RuntimeException("Unexpected sun.cpu.endian value: " + endian);
       
   125     }
       
   126 }