1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Aleksander V. Budniy
21  */
22 
23 /**
24  * Created on 14.07.2005
25  */
26 
27 package org.apache.harmony.jpda.tests.jdwp.Events;
28 
29 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
32 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
33 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
34 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
35 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
36 
37 
38 /**
39  * JDWP Unit test for requested VM_DEATH event.
40  */
41 
42 public class VMDeath002Test extends JDWPSyncTestCase {
43 
44     int requestID = 0;
45 
46     static String DEBUGGEE_CLASS_NAME = "org.apache.harmony.jpda.tests.jdwp.Events.EventDebuggee";
47 
getDebuggeeClassName()48     protected String getDebuggeeClassName() {
49         return DEBUGGEE_CLASS_NAME;
50     }
51 
52     /**
53      * This testcase is for requested VM_DEATH event.<BR>
54      * It starts EventDebuggee class, send request for
55      * VM_DEATH event, receives events and verifies that:
56      * <BR>&nbsp;&nbsp; - requested VM_DEATH event
57      * is received with the expected RequestID, returned by EventRequest.Set command;
58      * <BR>&nbsp;&nbsp; - automatic VM_DEATH event
59      * is received with the RequestID = 0;
60      */
testVMDeathRequest()61     public void testVMDeathRequest() {
62         logWriter.println("==> testVMDeathRequest started");
63 
64         //check capability, relevant for this test
65         logWriter.println("=> Check capability: canRequestVMDeathEvent");
66         debuggeeWrapper.vmMirror.capabilities();
67         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canRequestVMDeathEvent;
68         if (!isCapability) {
69             logWriter.println("##WARNING: this VM doesn't possess capability: canRequestVMDeathEvent");
70             return;
71         }
72         boolean success = true;
73 
74         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
75 
76         //set request for VM_DEATH event with suspend policy SUSPEND_ALL
77         byte suspendPolicy = JDWPConstants.SuspendPolicy.ALL;
78         logWriter.println("=> Create request for VM_DEATH event with suspend policy: "
79                 + suspendPolicy + "/"
80                 + JDWPConstants.SuspendPolicy.getName(suspendPolicy));
81         CommandPacket setRequestCommand = new CommandPacket(
82                 JDWPCommands.EventRequestCommandSet.CommandSetID,
83                 JDWPCommands.EventRequestCommandSet.SetCommand);
84 
85         setRequestCommand.setNextValueAsByte(JDWPConstants.EventKind.VM_DEATH);
86         setRequestCommand.setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
87         setRequestCommand.setNextValueAsInt(0);
88 
89         ReplyPacket setRequestReply = debuggeeWrapper.vmMirror
90                 .performCommand(setRequestCommand);
91         checkReplyPacket(setRequestReply, "Set VM_DEATH event");
92 
93         requestID = setRequestReply.getNextValueAsInt();
94         logWriter.println("==> RequestID = " + requestID);
95 
96         assertAllDataRead(setRequestReply);
97 
98         //release debuggee
99         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
100         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
101 
102         //receive and parse event set
103         logWriter.println("=> Wait for VM_DEATH event");
104         CommandPacket eventPacket = debuggeeWrapper.vmMirror.receiveEvent();
105         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(eventPacket);
106         int eventsCount = parsedEvents.length;
107         logWriter.println("==> Received event set: count=" + eventsCount);
108 
109         //ckeck if received events are expected
110         int requestedEvents = 0;
111         int autoEvents = 0;
112         int wrongEvents = 0;
113         for (int i = 0; i < eventsCount; i++) {
114             ParsedEvent event = parsedEvents[i];
115             logWriter.println("=> Event #" + i + ";");
116 
117             // print event info
118             byte eventSuspendPolicy = event.getSuspendPolicy();
119             logWriter.println("===> SuspendPolicy=" + eventSuspendPolicy + "/"
120                     + JDWPConstants.SuspendPolicy.getName(eventSuspendPolicy));
121             byte eventKind = event.getEventKind();
122             logWriter.println("===> EventKind=" + eventKind + "/"
123                     + JDWPConstants.EventKind.getName(eventKind));
124             int eventRequestID = event.getRequestID();
125             logWriter.println("===> RequestID=" + eventRequestID);
126 
127             // check if event is expected
128             if (eventKind == JDWPConstants.EventKind.VM_DEATH) {
129                 if (eventRequestID == requestID) {
130                     requestedEvents++;
131                     logWriter.println("===> found requested VM_DEATH event!");
132 
133                     // check suspend p[olicy for requested event
134                     if (eventSuspendPolicy != suspendPolicy) {
135                         logWriter.println("## FAILURE: requested VM_DEATH event "
136                                               + "with unexpected SuspendPolicy: "
137                                               + eventSuspendPolicy);
138                         success = false;
139                     }
140                 } else if (parsedEvents[i].getRequestID() == 0) {
141                     autoEvents++;
142                     logWriter.println("===> found auto VM_DEATH event!");
143                     // for automatical event suspend policy can be changed
144                 } else {
145                     logWriter.println("## FAILURE: VM_DEATH event "
146                                                + "with unexpected RequestID: "
147                                                + eventRequestID);
148                     success = false;
149                 }
150             } else {
151                 wrongEvents++;
152                 logWriter.println("## FAILURE: unexpected event kind: "
153                                                + eventKind);
154                 success = false;
155             }
156         }
157 
158         // check number of found events
159         {
160             if (eventsCount != 2) {
161                 logWriter.println("## FAILURE: received wrong number of events: "
162                                                + eventsCount);
163                 success = false;
164             }
165 
166             if (requestedEvents > 1) {
167                 logWriter.println("## FAILURE: too many requested VM_DEATH events: "
168                                                + requestedEvents);
169                 success = false;
170             } else if (requestedEvents < 1) {
171                 logWriter.println("## FAILURE: received no requested VM_DEATH events: "
172                                                + requestedEvents);
173                 success = false;
174             }
175 
176             if (autoEvents > 1) {
177                 logWriter.println("## FAILURE: too many auto VM_DEATH events: "
178                                                + autoEvents);
179                 success = false;
180             } else if (autoEvents < 1) {
181                 logWriter.println("## FAILURE: received no auto VM_DEATH events: "
182                                                + autoEvents);
183                 success = false;
184             }
185 
186             if (wrongEvents > 0) {
187                 logWriter.println("## FAILURE: Received unexpected events: "
188                                                + wrongEvents);
189                 success = false;
190             }
191 
192             assertTrue("Failure in processing VM_DEATH event", success);
193         }
194 
195         resumeDebuggee();
196 
197         logWriter.println("==> test PASSED!");
198     }
199 }
200