1 /*
2  * Copyright (c) 2008, 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  * @test
26  *
27  * @summary converted from VM Testbase nsk/jvmti/AttachOnDemand/attach036.
28  * VM Testbase keywords: [jpda, jvmti, noras, feature_282, vm6, jdk]
29  * VM Testbase readme:
30  * Description :
31  *     Test tries to load java agents to the VM after the VM has started using
32  *     Attach API (com.sun.tools.attach).
33  *     This is negative test, it checks that attempt to attach java agent fails if
34  *     main agent class has no proper 'agentmain' method.
35  *
36  * @library /vmTestbase
37  *          /test/lib
38  * @run driver jdk.test.lib.FileInstaller . .
39  * @build nsk.jvmti.AttachOnDemand.attach036.attach036TestRunner
40  *
41  * @comment create attach036Agent00.jar in current directory
42  * @build nsk.jvmti.AttachOnDemand.attach036.attach036Agent00
43  * @run driver ClassFileInstaller nsk.jvmti.AttachOnDemand.attach036.attach036Agent00
44  * @build ExecDriver
45  * @run driver PropertyResolvingWrapper ExecDriver --cmd
46  *      ${compile.jdk}/bin/jar
47  *      -cfm attach036Agent00.jar ${test.src}/attach036Agent00.mf
48  *      nsk/jvmti/AttachOnDemand/attach036/attach036Agent00.class
49  *
50  * @run main/othervm
51  *      -XX:+UsePerfData
52  *      -Djdk.attach.allowAttachSelf
53  *      PropertyResolvingWrapper
54  *      nsk.jvmti.AttachOnDemand.attach036.attach036TestRunner
55  *      -jdk ${test.jdk}
56  *      -ja attach036Agent00.jar
57  */
58 
59 package nsk.jvmti.AttachOnDemand.attach036;
60 
61 import com.sun.tools.attach.AgentInitializationException;
62 import nsk.share.*;
63 import nsk.share.aod.*;
64 import nsk.share.test.TestUtils;
65 
66 /*
67  * Negative test: checks that java agent fails to attach if main agent class
68  * has no proper agentmain method
69  * (test tries to attach java agent to the same VM where attach036TestRunner is running)
70  */
71 public class attach036TestRunner extends AODTestRunner {
72 
attach036TestRunner(String[] args)73     public attach036TestRunner(String[] args) {
74         super(args);
75     }
76 
runTest()77     protected void runTest() {
78         try {
79             String currentVMId = getCurrentVMId();
80 
81             AgentsAttacher attacher = new AgentsAttacher(currentVMId, argParser.getAgents(), log);
82 
83             try {
84                 attacher.attachAgents();
85                 TestUtils.testFailed("Expected AgentLoadException wasn't thrown");
86             } catch (Failure failure) {
87                 if (failure.getCause() != null) {
88                     if (failure.getCause() instanceof AgentInitializationException)
89                         log.display("Expected AgentInitializationException was thrown");
90                     else
91                         TestUtils.testFailed("Unexpected exception was thrown instead of AgentInitializationException: " + failure);
92                 } else
93                     throw failure;
94             }
95         } catch (Failure f) {
96             throw f;
97         } catch (Throwable t) {
98             TestUtils.unexpctedException(t);
99         }
100     }
101 
main(String[] args)102     public static void main(String[] args) {
103         new attach036TestRunner(args).runTest();
104     }
105 }
106