1 /*
2  * Copyright (c) 2000, 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 package nsk.jdi.Argument.value;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.connect.*;
32 import java.io.*;
33 import javax.naming.directory.Attribute;
34 import java.util.*;
35 
36 /**
37  * Test for the control of
38  *
39  *      Interface:      com.sun.jdi.connect.Connector.Argument
40  *      Method:         public java.lang.String value()
41  *      Assertion:      "Returns the current value of the argument."
42  *
43  *
44  *      Comments:       The test aims on the concrete Sun's JDI
45  *                      reference implementations. It uses
46  *                      com.sun.jdi.CommandLineLaunch connector and its
47  *                      "options" and "main" arguments.
48  *                      The test sets up the new "options" and "main" arguments
49  *                      values and then checks that new values remain previously
50  *                      after connection establishing with debugee VM and
51  *                      after debugee VM finishing.
52  */
53 
54 public class value003 {
55     private static Log log;
56 
main(String argv[] )57     public static void main(String argv[] ) {
58         System.exit(run(argv, System.out)+95); // JCK-compatible exit status
59     }
60 
run(String argv[], PrintStream out)61     public static int run(String argv[], PrintStream out) {
62         ArgumentHandler argHandler = new ArgumentHandler(argv);
63         log = new Log(out, argHandler);
64         VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
65 
66         String javaKind = argHandler.getOptions().getProperty("debugee.vmkind");
67         boolean java_g = javaKind != null && javaKind.startsWith("java_g"); // ...or java_g.exe
68         if (java_g)
69             log.display("Test option: java_g");
70 
71         List lcl = vmm.launchingConnectors();
72         if (lcl.size() > 0) {
73             log.display("Number of all known JDI launching connectors: " +
74                         lcl.size());
75         } else {
76             log.complain("FAILURE: no JDI launching connectors found!");
77             return 2;
78         }
79 
80         Iterator lci = lcl.iterator();
81         for (int i = 1; lci.hasNext(); i++) {
82             Connector c = (Connector) lci.next();
83             if (c.name().compareTo("com.sun.jdi.CommandLineLaunch") != 0) {
84                 continue;
85             }
86             Map<String,? extends Connector.Argument> cdfltArgmnts = c.defaultArguments();
87             int ksz = cdfltArgmnts.size();
88             String av[] = new String[ksz + 1];
89             Set ks = cdfltArgmnts.keySet();
90             if (ks.isEmpty()) {
91                 log.complain("FAILURE: empty default argument set is found "
92                            + "for " + c.name() + " connector!");
93                 return 2;
94             }
95 
96             log.display("Looking over " + c.name() + " connector arguments: ");
97 
98             boolean flg = false;
99             Iterator argi = ks.iterator();
100             String ovl = null;
101             String nvl = null;
102             for (int j = 1; argi.hasNext(); j++) {
103                 String argkey = (String)argi.next();
104                 Connector.Argument argval =
105                     (Connector.Argument)cdfltArgmnts.get((Object) argkey);
106 
107                 if (java_g && argval.name().equals("vmexec")) {
108                     log.display("Substitute: vmexec --> java_g");
109                     argval.setValue("java_g");
110                 };
111 
112                 log.display("Connector.Argument argval = "+ argval);
113                 if (argkey.compareTo("options") != 0 &&
114                     argkey.compareTo("main") != 0) {
115                     continue;
116                 }
117                 if (argkey.compareTo("main") == 0) {
118                     if (argval.isValid("nsk.jdi.Argument.value.value003a")) {
119                         argval.setValue("nsk.jdi.Argument.value.value003a");
120                     } else {
121                         log.complain("FAILURE: Can't set up new value for "
122                                    + "main-argument");
123                         return 2;
124                     }
125                     continue;
126                 }
127                 flg = true;
128                 ovl = argval.value();
129                 if (argval.isValid(ovl + "-verify ")) {
130                     argval.setValue(ovl + "-verify ");
131                 } else {
132                     log.complain("FAILURE: Can't set up new value for "
133                                + "options-argument");
134                     return 2;
135                 }
136 
137                 nvl = argval.value();
138                 if (nvl.compareTo(ovl + "-verify ") != 0) {
139                     log.complain("FAILURE: Can't set up argument value!");
140                     return 2;
141                 }
142                 log.display("Changed " + argval.name() + " argument's "
143                           + "value is: " + nvl);
144             };
145 
146             Binder binder = new Binder(argHandler, log);
147             Debugee debugee = null;
148 
149             try {
150                 if (flg) {
151                     flg = false;
152                     VirtualMachine vm =
153                         ((LaunchingConnector) c).launch(cdfltArgmnts);
154                     log.display("VM = (" + vm + ")");
155                     debugee = binder.enwrapDebugee(vm, vm.process());
156 
157                     if (((Connector.Argument)cdfltArgmnts
158                        .get((Object)"options")).value()
159                        .compareTo(ovl + "-verify ") != 0) {
160                         log.complain("FAILURE: Current 'options' argument "
161                                    + "value is not coinsides with the last "
162                                    + "setted up value.");
163                         return 2;
164                     }
165                     if (((Connector.Argument)c.defaultArguments()
166                         .get((Object) "options")).value()
167                         .compareTo(ovl) != 0) {
168                         log.complain("FAILURE: Default 'options' argument "
169                                    + "value can not be changed.");
170                         return 2;
171                     }
172 
173                     debugee.resume();
174                 }
175             } catch ( java.io.IOException exc) {
176                 log.complain("FAILURE: Unable to launch, so "
177                            + "java.io.IOException is arisen.");
178                 log.complain(exc.getMessage());
179                 return 2;
180             } catch (com.sun.jdi.connect.IllegalConnectorArgumentsException
181                      exc) {
182                 log.complain("FAILURE: One of the connector arguments is "
183                            + "invalid, so IllegalConnectorArgumentsException "
184                            + "is arisen.");
185                 log.complain(exc.getMessage());
186                 return 2;
187             } catch ( com.sun.jdi.connect.VMStartException exc) {
188                 log.complain("FAILURE: VM was terminated with error before "
189                            + "a connection could be established, so "
190                            + "VMStartException is arisen.");
191                 log.complain(exc.getMessage());
192                 log.complain(Binder.readVMStartExceptionOutput(exc, log.getOutStream()));
193                 return 2;
194             } finally {
195                 if (debugee != null) {
196                     try {
197                         debugee.dispose();
198                     } catch (VMDisconnectedException ignore) {
199                     }
200 
201                     int extcd = debugee.waitFor();
202                     if (extcd != 95) {
203                         log.complain("FAILURE: Launching VM crushes with "
204                                      + extcd + " exit code.");
205                         return 2;
206                     }
207                 }
208             }
209         };
210 
211         log.display("Test PASSED!");
212         return 0;
213     }
214 }
215