1 /*
   2  * Copyright (c) 2001, 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
  26  *  @bug 4359628
  27  *  @summary Test fix for JDI: methods Accessible.is...() lie about array types
  28  *
  29  *  @author Tim Bell
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g AccessSpecifierTest.java
  34  *  @run driver AccessSpecifierTest
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42     /********** target program **********/
  43 
  44 
  45 /** Sample package-private interface. */
  46 interface AccessSpecifierPackagePrivateInterface {}
  47 
  48 /** Sample package-private class. */
  49 class AccessSpecifierPackagePrivateClass {}
  50 
  51 /** Sample package-private class. */
  52 class AccessSpecifierPackagePrivateClassTwo implements
  53     AccessSpecifierPackagePrivateInterface {}
  54 
  55 class AccessSpecifierTarg {
  56     private boolean z0;
  57     boolean z1[]={z0}, z2[][]={z1};
  58 
  59     public byte b0;
  60     byte b1[]={b0}, b2[][]={b1};
  61 
  62     protected short s0;
  63     short s1[]={s0}, s2[][]={s1};
  64 
  65     int i0;
  66     int i1[]={i0}, i2[][]={i1};
  67 
  68     private long l0;
  69     long l1[]={l0}, l2[][]={l1};
  70 
  71     public char c0;
  72     char c1[]={c0}, c2[][]={c1};
  73 
  74     protected float f0;
  75     float f1[]={f0}, f2[][]={f1};
  76 
  77     double d0;
  78     double d1[]={d0}, d2[][]={d1};
  79 
  80     Boolean   Z0 = Boolean.TRUE;
  81     Boolean   Z1[]={Z0}, Z2[][]={Z1};
  82     Byte      B0 = new Byte ((byte)0x1f);
  83     Byte      B1[]={B0}, B2[][]={B1};
  84     Character C0 = new Character ('a');
  85     Character C1[]={C0}, C2[][]={C1};
  86     Double    D0 = new Double (1.0d);
  87     Double    D1[]={D0}, D2[][]={D1};
  88     Float     F0 = new Float (2.0f);
  89     Float     F1[]={F0}, F2[][]={F1};
  90     Integer   I0 = new Integer (8675309);
  91     Integer   I1[]={I0}, I2[][]={I1};
  92     Long      L0 = new Long (973230999L);
  93     Long      L1[]={L0}, L2[][]={L1};
  94     String    S0 = "A String";
  95     String    S1[]={S0}, S2[][]={S1};
  96     Object    O0 = new Object();
  97     Object    O1[]={O0}, O2[][]={O1};
  98 
  99     private   static class  U {}
 100     protected static class  V {}
 101     public    static class  W {}
 102     static class  P {} // package private
 103 
 104     U u0=new U(), u1[]={u0}, u2[][]={u1};
 105     V v0=new V(), v1[]={v0}, v2[][]={v1};
 106     W w0=new W(), w1[]={w0}, w2[][]={w1};
 107     P p0=new P(), p1[]={p0}, p2[][]={p1};
 108 
 109     private static interface StaticInterface {}
 110     private static class ClassUsingStaticInterface
 111         implements StaticInterface {}
 112 
 113     StaticInterface staticInterface_0 = new ClassUsingStaticInterface();
 114     StaticInterface staticInterface_1[]={staticInterface_0};
 115     StaticInterface staticInterface_2[][]={staticInterface_1};
 116 
 117     AccessSpecifierTarg a0, a1[]={a0}, a2[][]={a1};
 118 
 119     AccessSpecifierPackagePrivateClass ppc0=new AccessSpecifierPackagePrivateClass();
 120     AccessSpecifierPackagePrivateClass ppc1[]={ppc0};
 121     AccessSpecifierPackagePrivateClass ppc2[][]={ppc1};
 122 
 123     AccessSpecifierPackagePrivateInterface ppi0 =
 124         new AccessSpecifierPackagePrivateClassTwo ();
 125     AccessSpecifierPackagePrivateInterface ppi1[]={ppi0};
 126     AccessSpecifierPackagePrivateInterface ppi2[][]={ppi1};
 127 
 128     public AccessSpecifierTarg() {
 129         super();
 130     }
 131 
 132     public void ready(){
 133         System.out.println("Ready!");
 134     }
 135 
 136     public static void main(String[] args){
 137         System.out.println("Howdy!");
 138         AccessSpecifierTarg my = new AccessSpecifierTarg();
 139         my.ready();
 140         System.out.println("Goodbye from AccessSpecifierTarg!");
 141     }
 142 }
 143 
 144     /********** test program **********/
 145 
 146 public class AccessSpecifierTest extends TestScaffold {
 147 
 148     private final static String debugeeName = "AccessSpecifierTarg";
 149 
 150     /** Known Accessible Information about the Debugee. */
 151     private static final int NAME = 0;
 152     private static final int ACCESS = 1;
 153     private final static String primitives[][] = {
 154         {"z", "private", "public", "public"},
 155         {"b", "public", "public", "public"},
 156         {"s", "protected", "public", "public"},
 157         {"i", "package private", "public", "public"},
 158         {"l", "private", "public", "public"},
 159         {"c", "public", "public", "public"},
 160         {"f", "protected", "public", "public"},
 161         {"d", "package private", "public", "public"},
 162     };
 163     private final static String references[][] = {
 164         {"java.lang.Boolean"  , "public"},
 165         {"java.lang.Character", "public"},
 166         {"java.lang.Class"    , "public"},
 167         {"java.lang.Double"   , "public"},
 168         {"java.lang.Float"    , "public"},
 169         {"java.lang.Integer"  , "public"},
 170         {"java.lang.Long"     , "public"},
 171         {"java.lang.String"   , "public"},
 172         {"java.lang.Object"   , "public"},
 173 
 174         {"AccessSpecifierTarg", "package private"},
 175         {"AccessSpecifierPackagePrivateClass", "package private"},
 176         {"AccessSpecifierPackagePrivateInterface", "package private"},
 177 
 178         {"AccessSpecifierTarg$StaticInterface", "private"},
 179 
 180         {"AccessSpecifierTarg$U", "private"},
 181         {"AccessSpecifierTarg$V", "protected"},
 182         {"AccessSpecifierTarg$W", "public"},
 183         {"AccessSpecifierTarg$P", "package private"}
 184     };
 185 
 186     AccessSpecifierTest (String args[]) {
 187         super(args);
 188     }
 189 
 190     public static void main(String[] args)      throws Exception {
 191         new AccessSpecifierTest (args).startTests();
 192     }
 193 
 194     /********** test core **********/
 195 
 196     private void testAccessible (String name, Accessible a,
 197                                  boolean isPublic, boolean isProtected,
 198                                  boolean isPrivate, boolean isPackagePrivate) {
 199         System.out.println ("  Testing: " + name + " modifiers = " +
 200                             Integer.toBinaryString(a.modifiers()));
 201         if (a.isPublic() != isPublic) {
 202             failure("**Name = " + name + " expecting: " + isPublic +
 203                     " isPublic() was: " + a.isPublic());
 204         }
 205         if (a.isPrivate() != isPrivate) {
 206             failure("**Name = " + name + " expecting: " + isPrivate +
 207                     " isPrivate() was: " + a.isPrivate());
 208         }
 209         if (a.isProtected() != isProtected) {
 210             failure("**Name = " + name + " expecting: " + isProtected +
 211                     " isProtected() is: " + a.isProtected());
 212         }
 213         if (a.isPackagePrivate() != isPackagePrivate) {
 214             failure("**Name = " + name + " expecting: " + isPackagePrivate +
 215                     " isPackagePrivate() is: " + a.isPackagePrivate());
 216         }
 217     }
 218 
 219     protected void runTests() throws Exception {
 220         /*
 221          * Get to the top of ready()
 222          */
 223         startTo(debugeeName, "ready", "()V");
 224 
 225         ReferenceType rt = findReferenceType(debugeeName);
 226         if (rt == null) {
 227             throw new Exception ("ReferenceType not found for: " + debugeeName);
 228         }
 229         for (int i = 0; i < primitives.length; i++) {
 230             for (int j = 0; j < 3; j++) {
 231                 String suffix = Integer.toString(j);
 232                 String fieldName = primitives[i][NAME] + suffix;
 233                 Field field = rt.fieldByName(fieldName);
 234                 if (field == null) {
 235                     throw new Exception ("Field not found for: " + fieldName);
 236                 }
 237 
 238                 Type t = field.type();
 239                 if (t instanceof ReferenceType) {
 240                     ReferenceType reft = (ReferenceType)t;
 241                     if (primitives[i][ACCESS + j].equals("public")) {
 242                         testAccessible(reft.name(), reft,
 243                                        true, false, false, false);
 244                     } else if (primitives[i][ACCESS + j].equals("protected")) {
 245                         testAccessible(reft.name(), reft,
 246                                        false, true, false, false);
 247                     } else if (primitives[i][ACCESS + j].equals("private")) {
 248                         testAccessible(reft.name(), reft,
 249                                        false, false, true, false);
 250                     } else if (primitives[i][ACCESS + j].equals("package private")) {
 251                         testAccessible(reft.name(), reft,
 252                                        false, false, false, true);
 253                     }
 254                 } else {
 255                     System.out.println ("  Skipping " + t +
 256                                         " (primitive scalar type)");
 257                 }
 258             }
 259         }
 260 
 261         String brackets[] = {"[][]", "[]", ""};
 262 
 263         for (int i = 0; i < references.length; i++) {
 264             for (int j = 0; j < 3; j++) {
 265                 String suffix = brackets[j];
 266                 String referenceName = references[i][NAME] + suffix;
 267                 ReferenceType refType = findReferenceType(referenceName);
 268                 if (refType == null) {
 269                     System.out.println ("Skipping " + referenceName +
 270                                         " (not found)");
 271                 } else {
 272                     if (references[i][ACCESS].equals("public")) {
 273                         testAccessible(refType.name(), refType, true, false, false, false);
 274                     } else if  (references[i][ACCESS].equals("protected")) {
 275                         testAccessible(refType.name(), refType, false, true, false, false);
 276                     } else if  (references[i][ACCESS].equals("private")) {
 277                         testAccessible(refType.name(), refType, false, false, true, false);
 278                     } else if  (references[i][ACCESS].equals("package private")) {
 279                         testAccessible(refType.name(), refType, false, false, false, true);
 280                     }
 281                 }
 282             }
 283         }
 284 
 285         /*
 286          * resume the target listening for events
 287          */
 288         listenUntilVMDisconnect();
 289 
 290         /*
 291          * deal with results of test
 292          * if anything has called failure("foo") testFailed will be true
 293          */
 294         if (!testFailed) {
 295             println("AccessSpecifierTest: passed");
 296         } else {
 297             throw new Exception("AccessSpecifierTest: failed");
 298         }
 299     }
 300 }