1 /*
2  * Copyright (c) 2001, 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.
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 package nsk.jdi.ArrayReference.length;
26 
27 import nsk.share.*;
28 import nsk.share.jpda.*;
29 import nsk.share.jdi.*;
30 
31 import com.sun.jdi.*;
32 import java.io.*;
33 
34 public class length001 {
35     final static String FIELD_NAME[][] = {
36         {"z1", "0"},
37         {"z2", "7"},
38         {"b1", "1"},
39         {"b2", "6"},
40         {"c1", "2"},
41         {"c2", "5"},
42         {"d1", "3"},
43         {"d2", "4"},
44         {"f1", "4"},
45         {"f2", "3"},
46         {"i1", "5"},
47         {"i2", "2"},
48         {"l1", "6"},
49         {"l2", "1"},
50         {"r1", "7"},
51         {"r2", "0"},
52 
53         {"lF1", "3"},
54         {"lP1", "3"},
55         {"lU1", "2"},
56         {"lR1", "2"},
57         {"lT1", "1"},
58         {"lV1", "1"},
59 
60         {"E1", "0"},
61         {"E2", "2"},
62         {"X1", "1"},
63         {"X2", "1"},
64         {"O1", "2"},
65         {"O2", "0"},
66 
67         {"LF1", "3"},
68         {"LP1", "3"},
69         {"LU1", "2"},
70         {"LR1", "2"},
71         {"LT1", "1"},
72         {"LV1", "1"},
73 
74         {"EF1", "0"},
75         {"EP1", "1"},
76         {"EU1", "1"},
77         {"ER1", "1"},
78         {"ET1", "1"},
79         {"EV1", "1"},
80     };
81 
82     private static Log log;
83     private final static String prefix = "nsk.jdi.ArrayReference.length.";
84     private final static String className = "length001";
85     private final static String debugerName = prefix + className;
86     private final static String debugeeName = debugerName + "a";
87     private final static String classToCheckName = prefix + "ClassToCheck";
88 
main(String argv[])89     public static void main(String argv[]) {
90         System.exit(95 + run(argv, System.out));
91     }
92 
run(String argv[], PrintStream out)93     public static int run(String argv[], PrintStream out) {
94         ArgumentHandler argHandler = new ArgumentHandler(argv);
95         log = new Log(out, argHandler);
96         Binder binder = new Binder(argHandler, log);
97         Debugee debugee = binder.bindToDebugee(debugeeName
98                               + (argHandler.verbose() ? " -verbose" : ""));
99         IOPipe pipe = debugee.createIOPipe();
100         boolean testFailed = false;
101 
102         // Connect with debugee and resume it
103         debugee.redirectStderr(out);
104         debugee.resume();
105         String line = pipe.readln();
106         if (line == null) {
107             log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null");
108             return 2;
109         }
110         if (!line.equals("ready")) {
111             log.complain("debuger FAILURE> UNEXPECTED debugee's signal - "
112                       + line);
113             return 2;
114         }
115         else {
116             log.display("debuger> debugee's \"ready\" signal recieved.");
117         }
118 
119         ReferenceType refType = debugee.classByName(classToCheckName);
120         if (refType == null) {
121            log.complain("debuger FAILURE> Class " + classToCheckName
122                       + " not found.");
123            return 2;
124         }
125         log.display("debuger> Total fields in debugee read: "
126                   + refType.allFields().size() + " total fields in debuger: "
127                   + FIELD_NAME.length + "\n");
128 
129         // Check all array fields from debugee
130         for (int i = 0; i < FIELD_NAME.length; i++) {
131             Field field;
132             String name = FIELD_NAME[i][0];
133             String realLength = FIELD_NAME[i][1];
134             Value value;
135             ArrayReference arrayRef;
136             int length;
137             String lengthStr;
138 
139             // Get field from debuggee by name
140             try {
141                 field = refType.fieldByName(name);
142             } catch (ClassNotPreparedException e) {
143                 log.complain("debuger FAILURE 1> Can't get field by name "
144                            + name);
145                 log.complain("debuger FAILURE 1> Exception: " + e);
146                 testFailed = true;
147                 continue;
148             } catch (ObjectCollectedException e) {
149                 log.complain("debuger FAILURE 1> Can't get field by name "
150                            + name);
151                 log.complain("debuger FAILURE 1> Exception: " + e);
152                 testFailed = true;
153                 continue;
154             }
155             log.display("debuger> " + i + " field " + field + " read.");
156 
157             // Get field's value
158             try {
159                 value = refType.getValue(field);
160             } catch (IllegalArgumentException e) {
161                 log.complain("debuger FAILURE 2> Cannot get value for field "
162                            + name);
163                 log.complain("debuger FAILURE 2> Exception: " + e);
164                 testFailed = true;
165                 continue;
166             } catch (ObjectCollectedException e) {
167                 log.complain("debuger FAILURE 2> Cannot get value for field "
168                            + name);
169                 log.complain("debuger FAILURE 2> Exception: " + e);
170                 testFailed = true;
171                 continue;
172             }
173             log.display("debuger> " + i + " field value is " + value);
174 
175             // Cast to ArrayReference. All fields in debugee are
176             // arrays, so ClassCastException should not be thrown
177             try {
178                 arrayRef = (ArrayReference)value;
179             } catch (ClassCastException e) {
180                 log.complain("debuger FAILURE 3> Cannot cast value for field "
181                            + name + " to ArrayReference.");
182                 log.complain("debuger FAILURE 3> Exception: " + e);
183                 testFailed = true;
184                 continue;
185             }
186 
187             // Get length of ArrayReference object
188             try {
189                 length = arrayRef.length();
190             } catch (ObjectCollectedException e) {
191                 log.complain("debuger FAILURE 4> Cannot get length for array "
192                            + name);
193                 log.complain("debuger FAILURE 4> Exception: " + e);
194                 testFailed = true;
195                 continue;
196             }
197             log.display("debuger> " + i + " field length is " + length);
198             lengthStr = realLength.valueOf(length);
199 
200             // Check array's length
201             if (!realLength.equals(lengthStr)) {
202                 log.complain("debuger FAILURE 5> Length of array " + name
203                            + " is " + length + ", but expected " + realLength);
204                 testFailed = true;
205             }
206             log.display("debuger> " + i + " field checked.\n");
207         }
208 
209         pipe.println("quit");
210         debugee.waitFor();
211         int status = debugee.getStatus();
212         if (testFailed) {
213             log.complain("debuger FAILURE> TEST FAILED");
214             return 2;
215         } else {
216             if (status == 95) {
217                 log.display("debuger> expected Debugee's exit "
218                           + "status - " + status);
219                 return 0;
220             } else {
221                 log.complain("debuger FAILURE> UNEXPECTED Debugee's exit "
222                            + "status (not 95) - " + status);
223                 return 2;
224             }
225         }
226     }
227 }
228