File: ConnectorTest.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (238 lines) | stat: -rw-r--r-- 8,040 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package nsk.share.jdi;

import com.sun.jdi.connect.*;
import com.sun.jdi.*;
import java.io.*;
import java.util.*;

import nsk.share.*;
import nsk.share.jpda.*;

/*
 * This class contains several common methods used by connector tests.
 */
public abstract class ConnectorTest {
    protected Log log;

    protected VirtualMachine vm;

    protected int attempts; // attempts to connect to the debuggee VM

    protected int delay; // delay between connection attempts

    protected IORedirector outRedirector;

    protected IORedirector errRedirector;

    protected ArgHandler argHandler;

    protected boolean isTestFailed;

    // set test status 'FAILED'
    protected void testFailed() {
        isTestFailed = true;
    }

    // check if tested functionality implemented on current platform
    protected boolean shouldPass() {
        return argHandler.shouldPass(getConnectorName());
    }

    abstract protected void doTest();

    abstract protected String getConnectorName();

    abstract protected String getDebuggeeClass();

    static public class ArgHandler extends ArgumentHandler {
        public ArgHandler(String[] args) {
            super(args);

        }

        protected boolean checkOption(String option, String value) {
            if (super.checkOption(option, value))
                return true;

            if (option.equals("testWorkDir"))
                return true;

            if (option.equals("waitVMStartEvent"))
                return true;

            return false;
        }

        public String getTestWorkDir() {
            String dir = options.getProperty("testWorkDir");

            if (dir.endsWith(File.separator)) {
                dir = dir.substring(0, dir.length() - 1);
            }

            return dir;
        }

        public boolean waitVMStartEvent() {
            return options.containsKey("waitVMStartEvent");
        }
    }

    /*
     * Subclasses can provide another ArgumentHandlers
     */
    protected ArgHandler createArgumentHandler(String[] args) {
        return new ArgHandler(args);
    }

    protected void init(String[] args, PrintStream out) {
        argHandler = createArgumentHandler(args);

        log = new Log(out, argHandler);

        delay = argHandler.getConnectionDelay();

        // calculate number of connection attempts to not exceed WAITTIME
        long timeout = argHandler.getWaitTime() * 60 * 1000;
        attempts = (int) (timeout / delay);
    }

    protected int runIt(String argv[], PrintStream out) {
        try {
            init(argv, out);

            if (shouldPass()) {
                log.display("Tested functionality isn't implemented on this platform. Treat test as passed.");
                return Consts.TEST_PASSED;
            }

            doTest();

            if (isTestFailed)
                return Consts.TEST_FAILED;
            else
                return Consts.TEST_PASSED;

        } catch (Throwable t) {
            out.println("Unexpected exception: " + t);
            t.printStackTrace(out);
            return Consts.TEST_FAILED;
        }
    }

    protected void waitForVMInit(VirtualMachine vm) {
        Debugee.waitForVMInit(vm, log, argHandler.getWaitTime() * 60 * 1000);
    }

    // set connector argument value with given name
    protected void setConnectorArg(Map<String, Connector.Argument> args, String argName, String value) {
        for (String key : args.keySet()) {
            Connector.Argument arg = args.get(key);
            if (arg.name().equals(argName)) {
                arg.setValue(value);
                return;
            }
        }

        throw new Error("There is no argument '" + argName + "'");
    }

    // try attach to target VM using attaching connector
    protected VirtualMachine tryAttach(AttachingConnector connector, Map<String, Connector.Argument> cArgs) {
        // make several attempts to connect to the debuggee VM until WAITTIME exceeds
        for (int i = 0; i < attempts; i++) {
            try {
                return connector.attach(cArgs);
            } catch (IOException e) {
                // could not connect; sleep a few and make new attempt
                log.display("Connection attempt #" + i + " failed: " + e);
                e.printStackTrace(log.getOutStream());
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException ie) {
                    testFailed();
                    log.complain("TEST INCOMPLETE: interrupted sleep: " + ie);
                    ie.printStackTrace(log.getOutStream());
                }
            } catch (IllegalConnectorArgumentsException e) {
                testFailed();
                log.complain("TEST: Illegal connector arguments: " + e.getMessage());
                return null;
            } catch (Exception e) {
                testFailed();
                log.complain("TEST: Internal error: " + e.getMessage());
                e.printStackTrace(log.getOutStream());
                return null;
            }
        }

        testFailed();
        // return null after all attempts failed
        log.complain("FAILURE: all attempts to connect to the debuggee VM failed");
        return null;
    }

    // try find connector with given name
    protected Connector findConnector(String connectorName) {
        List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();
        Iterator<Connector> iter = connectors.iterator();

        while (iter.hasNext()) {
            Connector connector = (Connector) iter.next();
            if (connector.name().equals(connectorName)) {
                log.display("Connector name=" + connector.name() + "\n\tdescription=" + connector.description() + "\n\ttransport="
                        + connector.transport().name());
                return connector;
            }
        }
        throw new Error("No appropriate connector");
    }

    // wait when debuggee process finishes and check exit code
    protected void waitDebuggeeExit(Debugee debuggee) {
        log.display("\nwaiting for debuggee VM exit");
        int code = debuggee.waitFor();
        if (code != (Consts.JCK_STATUS_BASE + Consts.TEST_PASSED)) {
            testFailed();
            log.complain("Debuggee VM has crashed: exit code=" + code);
            return;
        }
        log.display("debuggee VM: exit code=" + code);
    }

    // wait 'READY' command from debuggee VM (this method is used by debuggers establishing socket connection with debuggee VM)
    protected boolean waitReadyCommand(IOPipe pipe) {
        String command = pipe.readln();
        log.display("Command: " + command);

        if (!command.equals(AbstractDebuggeeTest.COMMAND_READY)) {
            testFailed();
            log.complain("Unexpected debuggee answer: " + command + ", expected is " + AbstractDebuggeeTest.COMMAND_READY);
            return false;
        }

        return true;
    }
}