Author: erodriguez Date: Mon Jan 3 21:16:39 2005 New Revision: 124070 URL: http://svn.apache.org/viewcvs?view=rev&rev=124070 Log: Basic NTP PDU processing tests. Added: incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageDecoderTest.java incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpTestCase.java
Added: incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageDecoderTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageDecoderTest.java?view=auto&rev=124070 ============================================================================== --- (empty file) +++ incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageDecoderTest.java Mon Jan 3 21:16:39 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.ntp; + +import java.nio.ByteBuffer; + +import org.apache.ntp.io.NtpMessageDecoder; +import org.apache.ntp.message.NtpMessage; + + +public class NtpMessageDecoderTest extends NtpTestCase +{ + private ByteBuffer requestByteBuffer; + + public void testParseClient() throws Exception + { + requestByteBuffer = getByteBufferFromFile( "NTP-CLIENT-UDP.pdu" ); + + NtpMessageDecoder decoder = new NtpMessageDecoder(); + NtpMessage request = decoder.decode( requestByteBuffer ); + + print( request ); + } + + public void testParseServer() throws Exception + { + requestByteBuffer = getByteBufferFromFile( "NTP-SERVER-UDP.pdu" ); + + NtpMessageDecoder decoder = new NtpMessageDecoder(); + NtpMessage request = decoder.decode( requestByteBuffer ); + + print( request ); + } +} + Added: incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java?view=auto&rev=124070 ============================================================================== --- (empty file) +++ incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java Mon Jan 3 21:16:39 2005 @@ -0,0 +1,62 @@ +/* + * 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.ntp; + +import java.nio.ByteBuffer; + +import org.apache.ntp.io.NtpMessageEncoder; +import org.apache.ntp.message.LeapIndicatorType; +import org.apache.ntp.message.ModeType; +import org.apache.ntp.message.NtpMessageModifier; +import org.apache.ntp.message.NtpTimeStamp; +import org.apache.ntp.message.ReferenceIdentifier; +import org.apache.ntp.message.StratumType; + + +public class NtpMessageEncoderTest extends NtpTestCase +{ + public void testEncodeMessage() throws Exception + { + ByteBuffer replyByteBuffer; + + NtpMessageModifier modifier = new NtpMessageModifier(); + modifier.setLeapIndicator( LeapIndicatorType.NO_WARNING ); + modifier.setVersionNumber( 4 ); + modifier.setMode( ModeType.SERVER ); + modifier.setStratum( StratumType.PRIMARY_REFERENCE ); + modifier.setPollInterval( 6 ); + modifier.setPrecision( 0.000015 ); + modifier.setRootDelay( 0.0 ); + modifier.setRootDispersion( 0.0006 ); + modifier.setReferenceIdentifier( ReferenceIdentifier.LOCL ); + + NtpTimeStamp now = new NtpTimeStamp(); + + modifier.setReferenceTimestamp( now ); + modifier.setOriginateTimestamp( now ); + modifier.setReceiveTimestamp( now ); + modifier.setTransmitTimestamp( now ); + + NtpMessageEncoder encoder = new NtpMessageEncoder(); + replyByteBuffer = encoder.encode( modifier.getNtpMessage() ); + replyByteBuffer.flip(); + + print( modifier.getNtpMessage() ); + } +} + Added: incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpTestCase.java Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpTestCase.java?view=auto&rev=124070 ============================================================================== --- (empty file) +++ incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpTestCase.java Mon Jan 3 21:16:39 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.ntp; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +import org.apache.ntp.message.NtpMessage; + +import junit.framework.TestCase; + + +public class NtpTestCase extends TestCase +{ + protected static final int MINIMUM_NTP_DATAGRAM_SIZE = 576; + + protected void print( NtpMessage request ) + { + System.out.println( request.getLeapIndicator() ); + System.out.println( request.getVersionNumber() ); + System.out.println( request.getMode() ); + System.out.println( request.getStratum() ); + System.out.println( request.getPollInterval() ); + System.out.println( request.getPrecision() ); + System.out.println( request.getRootDelay() ); + System.out.println( request.getRootDispersion() ); + System.out.println( request.getReferenceIdentifier() ); + System.out.println( request.getReferenceTimestamp() ); + System.out.println( request.getOriginateTimestamp() ); + System.out.println( request.getReceiveTimestamp() ); + System.out.println( request.getTransmitTimestamp() ); + } + + protected ByteBuffer getByteBufferFromFile(String file) throws IOException + { + InputStream is = getClass().getResourceAsStream(file); + + byte[] bytes = new byte[ MINIMUM_NTP_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); + } +} +
