Author: erodriguez Date: Tue Jan 4 21:18:21 2005 New Revision: 124185 URL: http://svn.apache.org/viewcvs?view=rev&rev=124185 Log: Basic DHCP PDU decoding test. Added: incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpMessageDecoderTest.java incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpTestCase.java
Added: incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpMessageDecoderTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpMessageDecoderTest.java?view=auto&rev=124185 ============================================================================== --- (empty file) +++ incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpMessageDecoderTest.java Tue Jan 4 21:18:21 2005 @@ -0,0 +1,50 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.dhcp; + +import java.nio.ByteBuffer; + +import org.apache.dhcp.io.DhcpMessageDecoder; +import org.apache.dhcp.messages.AbstractDhcpMessage; + + +public class DhcpMessageDecoderTest extends DhcpTestCase +{ + private ByteBuffer requestByteBuffer; + + public void testParseDiscover() throws Exception + { + requestByteBuffer = getByteBufferFromFile( "DHCPDISCOVER.pdu" ); + + DhcpMessageDecoder decoder = new DhcpMessageDecoder(); + AbstractDhcpMessage dhcpRequest = decoder.decode( requestByteBuffer ); + + print( dhcpRequest ); + } + + public void testParseOffer() throws Exception + { + requestByteBuffer = getByteBufferFromFile( "DHCPOFFER.pdu" ); + + DhcpMessageDecoder decoder = new DhcpMessageDecoder(); + AbstractDhcpMessage dhcpRequest = decoder.decode( requestByteBuffer ); + + print( dhcpRequest ); + } +} + Added: incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpTestCase.java Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpTestCase.java?view=auto&rev=124185 ============================================================================== --- (empty file) +++ incubator/directory/dhcp/trunk/core/src/test/org/apache/dhcp/DhcpTestCase.java Tue Jan 4 21:18:21 2005 @@ -0,0 +1,68 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.dhcp; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +import junit.framework.TestCase; + +import org.apache.dhcp.messages.AbstractDhcpMessage; + + +public abstract class DhcpTestCase extends TestCase +{ + protected static final int MINIMUM_DHCP_DATAGRAM_SIZE = 576; + + protected void print( AbstractDhcpMessage message ) + { + System.out.println( message.getMessageType() ); + System.out.println( message.getHardwareAddressType() ); + System.out.println( message.getHardwareAddressLength() ); + System.out.println( message.getHops() ); + System.out.println( message.getTransactionId() ); + System.out.println( message.getSeconds() ); + System.out.println( message.getFlags() ); + System.out.println( message.getActualClientAddress() ); + System.out.println( message.getAssignedClientAddress() ); + System.out.println( message.getNextServerAddress() ); + System.out.println( message.getRelayAgentAddress() ); + System.out.println( message.getClientHardwareAddress() ); + System.out.println( message.getServerHostname() ); + System.out.println( message.getBootFileName() ); + } + + protected ByteBuffer getByteBufferFromFile( String file ) throws IOException + { + InputStream is = getClass().getResourceAsStream( file ); + + byte[] bytes = new byte[ MINIMUM_DHCP_DATAGRAM_SIZE ]; + + int offset = 0; + int numRead = 0; + while ( offset < bytes.length && ( numRead=is.read( bytes, offset, bytes.length-offset ) ) >= 0 ) + { + offset += numRead; + } + + is.close(); + return ByteBuffer.wrap( bytes ); + } +} +
