1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ntp;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.directory.server.ntp.io.NtpMessageDecoder;
28  import org.apache.directory.server.ntp.messages.NtpMessage;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 547542 $, $Date: 2007-06-15 08:15:11 +0200 (Fr, 15 Jun 2007) $
36   */
37  public class NtpMessageDecoderTest extends TestCase
38  {
39      /** the log for this class */
40      private static final Logger log = LoggerFactory.getLogger( NtpMessageDecoderTest.class );
41  
42      private static byte[] clientRequest = new byte[]
43          { ( byte ) 0xe3, ( byte ) 0x00, ( byte ) 0x06, ( byte ) 0xee, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
44              ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x49, ( byte ) 0x4e,
45              ( byte ) 0x49, ( byte ) 0x54, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
46              ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
47              ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
48              ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0xc5, ( byte ) 0x0f,
49              ( byte ) 0x41, ( byte ) 0x5a, ( byte ) 0xbf, ( byte ) 0xba, ( byte ) 0xdc, ( byte ) 0x09 };
50  
51      private static byte[] serverResponse = new byte[]
52          { ( byte ) 0x24, ( byte ) 0x01, ( byte ) 0x06, ( byte ) 0xf0, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
53              ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x1b, ( byte ) 0x43, ( byte ) 0x44,
54              ( byte ) 0x4d, ( byte ) 0x41, ( byte ) 0xc5, ( byte ) 0x0f, ( byte ) 0x41, ( byte ) 0x51, ( byte ) 0xba,
55              ( byte ) 0x35, ( byte ) 0x2e, ( byte ) 0xb5, ( byte ) 0xc5, ( byte ) 0x0f, ( byte ) 0x41, ( byte ) 0x5a,
56              ( byte ) 0xbf, ( byte ) 0xba, ( byte ) 0xdc, ( byte ) 0x09, ( byte ) 0xc5, ( byte ) 0x0f, ( byte ) 0x41,
57              ( byte ) 0x5a, ( byte ) 0xc5, ( byte ) 0xeb, ( byte ) 0xa6, ( byte ) 0xac, ( byte ) 0xc5, ( byte ) 0x0f,
58              ( byte ) 0x41, ( byte ) 0x5a, ( byte ) 0xc6, ( byte ) 0x48, ( byte ) 0xd7, ( byte ) 0xe0 };
59  
60  
61      /**
62       * Tests the parsing of a client request.
63       *
64       * @throws Exception
65       */
66      public void testParseClient() throws Exception
67      {
68          ByteBuffer buffer = ByteBuffer.wrap( clientRequest );
69          NtpMessageDecoder decoder = new NtpMessageDecoder();
70          NtpMessage request = decoder.decode( buffer );
71          print( request );
72      }
73  
74  
75      /**
76       * Tests the parsing of a server response.
77       *
78       * @throws Exception
79       */
80      public void testParseServer() throws Exception
81      {
82          ByteBuffer buffer = ByteBuffer.wrap( serverResponse );
83          NtpMessageDecoder decoder = new NtpMessageDecoder();
84          NtpMessage request = decoder.decode( buffer );
85          print( request );
86      }
87  
88  
89      protected void print( NtpMessage request )
90      {
91          log.debug( String.valueOf( request.getLeapIndicator() ) );
92          log.debug( String.valueOf( request.getVersionNumber() ) );
93          log.debug( String.valueOf( request.getMode() ) );
94          log.debug( String.valueOf( request.getStratum() ) );
95          log.debug( String.valueOf( request.getPollInterval() ) );
96          log.debug( String.valueOf( request.getPrecision() ) );
97          log.debug( String.valueOf( request.getRootDelay() ) );
98          log.debug( String.valueOf( request.getRootDispersion() ) );
99          log.debug( String.valueOf( request.getReferenceIdentifier() ) );
100         log.debug( String.valueOf( request.getReferenceTimestamp() ) );
101         log.debug( String.valueOf( request.getOriginateTimestamp() ) );
102         log.debug( String.valueOf( request.getReceiveTimestamp() ) );
103         log.debug( String.valueOf( request.getTransmitTimestamp() ) );
104     }
105 }