1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37 public class NtpMessageDecoderTest extends TestCase
38 {
39
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
63
64
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
77
78
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 }