hotspot/test/runtime/modules/ImageFile/ImageGetAttributesTest.java
changeset 32734 7029a3eb3008
parent 32467 23b29549f8d1
parent 32626 5d1d9327219c
child 32735 c6063d028c3c
equal deleted inserted replaced
32467:23b29549f8d1 32734:7029a3eb3008
     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 getting all attributes,
       
    26  * @test ImageGetAttributesTest
       
    27  * @summary Unit test for JVM_ImageGetAttributes() method
       
    28  * @library /testlibrary /../../test/lib
       
    29  * @build LocationConstants ImageGetAttributesTest
       
    30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ImageGetAttributesTest
       
    33  */
       
    34 
       
    35 import java.io.File;
       
    36 import java.nio.ByteOrder;
       
    37 import sun.hotspot.WhiteBox;
       
    38 import static jdk.test.lib.Asserts.*;
       
    39 
       
    40 public class ImageGetAttributesTest implements LocationConstants {
       
    41 
       
    42     public static final WhiteBox wb = WhiteBox.getWhiteBox();
       
    43 
       
    44     public static void main(String... args) throws Exception {
       
    45         String javaHome = System.getProperty("java.home");
       
    46         String imageFile = javaHome + File.separator + "lib" + File.separator
       
    47                 + "modules" + File.separator + "bootmodules.jimage";
       
    48 
       
    49         if (!(new File(imageFile)).exists()) {
       
    50             System.out.printf("Test skipped.");
       
    51             return;
       
    52         }
       
    53 
       
    54         testImageGetAttributes(imageFile);
       
    55     }
       
    56 
       
    57     private static void testImageGetAttributes(String imageFile) {
       
    58 
       
    59         boolean bigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
       
    60         long id = wb.imageOpenImage(imageFile, bigEndian);
       
    61         try {
       
    62             long stringsSize = wb.imageGetStringsSize(id);
       
    63             assertNE(stringsSize, 0, "strings size is 0");
       
    64 
       
    65             int[] array = wb.imageAttributeOffsets(id);
       
    66             assertNotNull(array, "Could not retrieve offsets of array");
       
    67 
       
    68             // Get non-null attributes
       
    69             boolean attFound = false;
       
    70             int[] idx = {-1, -1, -1};
       
    71             // first non-null attribute
       
    72             for (int i = 0; i < array.length; i++) {
       
    73                 if (array[i] != 0) {
       
    74                     attFound = true;
       
    75                     idx[0] = i;
       
    76                     break;
       
    77                 }
       
    78             }
       
    79 
       
    80             // middle non-null attribute
       
    81             for (int i = array.length / 2; i < array.length; i++) {
       
    82                 if (array[i] != 0) {
       
    83                     attFound = true;
       
    84                     idx[1] = i;
       
    85                     break;
       
    86                 }
       
    87             }
       
    88 
       
    89             // last non-null attribute
       
    90             for (int i = array.length - 1; i >= 0; i--) {
       
    91                 if (array[i] != 0) {
       
    92                     attFound = true;
       
    93                     idx[2] = i;
       
    94                     break;
       
    95                 }
       
    96             }
       
    97             assertTrue(attFound, "Failed. No non-null offset attributes");
       
    98                 // test cases above
       
    99                 for (int i = 0; i < 3; i++) {
       
   100                     if (idx[i] != -1) {
       
   101                         long[] attrs = wb.imageGetAttributes(id, (int) array[idx[i]]);
       
   102                         long module = attrs[LOCATION_ATTRIBUTE_MODULE];
       
   103                         long parent = attrs[LOCATION_ATTRIBUTE_PARENT];
       
   104                         long base = attrs[LOCATION_ATTRIBUTE_BASE];
       
   105                         long ext = attrs[LOCATION_ATTRIBUTE_EXTENSION];
       
   106 
       
   107                         if ((module >= 0) && (module < stringsSize)
       
   108                                 && (parent >= 0) && (parent < stringsSize)
       
   109                                 && (base != 0)
       
   110                                 && (ext >= 0) && (ext < stringsSize)) {
       
   111                         } else {
       
   112                             System.out.printf("Failed. Read attribute offset %d (position %d) but wrong offsets\n",
       
   113                                     array[idx[i]], idx[i]);
       
   114                             System.out.printf("    offsets: module = %d parent = %d base = %d extention = %d\n",
       
   115                                     module, parent, base, ext);
       
   116                             throw new RuntimeException("Read attribute offset error");
       
   117                         }
       
   118                     } else {
       
   119                         System.out.printf("Failed. Could not read attribute offset %d (position %d)\n",
       
   120                                 array[idx[i]], idx[i]);
       
   121                         throw new RuntimeException("Read attribute offset error");
       
   122                     }
       
   123                 }
       
   124         } finally {
       
   125             wb.imageCloseImage(id);
       
   126         }
       
   127     }
       
   128 }