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 package org.apache.harmony.jpda.tests.jdwp.DDM; 20 21 import java.nio.*; 22 23 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 24 import org.apache.harmony.jpda.tests.share.SyncDebuggee; 25 26 import org.apache.harmony.dalvik.ddmc.Chunk; 27 import org.apache.harmony.dalvik.ddmc.ChunkHandler; 28 import org.apache.harmony.dalvik.ddmc.DdmServer; 29 30 import java.util.Arrays; 31 import java.util.zip.Adler32; 32 33 /** 34 * Debuggee for JDWP DDMTest unit test which 35 * exercises DDM.Chunk command. 36 */ 37 public class DDMDebuggee extends SyncDebuggee { 38 39 public static final int DDM_TEST_TYPE = 0xADD0A110; 40 public static final int DDM_RESULT_TYPE = 0xADD04350; 41 calculateExpectedResult(byte[] in)42 public static byte[] calculateExpectedResult(byte[] in) { 43 return calculateExpectedResult(in, 0, in.length); 44 } calculateExpectedResult(byte[] in, int off, int len)45 public static byte[] calculateExpectedResult(byte[] in, int off, int len) { 46 ByteBuffer b = ByteBuffer.wrap(new byte[8]); 47 b.order(ByteOrder.BIG_ENDIAN); 48 Adler32 a = new Adler32(); 49 a.update(in, off, len); 50 b.putLong(a.getValue()); 51 return b.array(); 52 } 53 54 public class TestChunkHandler extends ChunkHandler { 55 public int connectCount = 0; 56 public int disconnectCount = 0; 57 58 @Override onConnected()59 public void onConnected() { connectCount++; } 60 61 @Override onDisconnected()62 public void onDisconnected() { disconnectCount++; } 63 64 @Override handleChunk(Chunk request)65 public Chunk handleChunk(Chunk request) { 66 if (request.type != DDM_TEST_TYPE) { 67 throw new Error("Bad type!"); 68 } 69 70 byte[] res = calculateExpectedResult(request.data, request.offset, request.length); 71 return new Chunk(DDM_RESULT_TYPE, res, 0, res.length); 72 } 73 toString()74 public String toString() { 75 return "TestChunkHandler { connectCount = " + connectCount + 76 ", disconnectCount = " + disconnectCount + " }"; 77 } 78 } 79 80 @Override run()81 public void run() { 82 TestChunkHandler h = new TestChunkHandler(); 83 DdmServer.registerHandler(DDM_TEST_TYPE, h); 84 DdmServer.registrationComplete(); 85 logWriter.println("-> Added chunk handler type: " + DDM_TEST_TYPE + " handler: " + h); 86 87 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 88 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 89 logWriter.println("Removing handler type: " + DDM_TEST_TYPE); 90 DdmServer.unregisterHandler(DDM_TEST_TYPE); 91 92 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 93 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 94 // Re-register the chunk handler so we can get the disconnect count. 95 DdmServer.registerHandler(DDM_TEST_TYPE, h); 96 97 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 98 // test disposes here. 99 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 100 101 // Tell the tester if we saw the connected message. 102 synchronizer.sendMessage(Integer.toString(h.connectCount)); 103 synchronizer.sendMessage(Integer.toString(h.disconnectCount)); 104 logWriter.println("Test complete with handler: " + h); 105 } 106 107 /** 108 * Starts DDMDebuggee with help of runDebuggee() method 109 * from <A HREF="../../share/Debuggee.html">Debuggee</A> super class. 110 */ main(String [] args)111 public static void main(String [] args) { 112 runDebuggee(DDMDebuggee.class); 113 } 114 } 115