View Javadoc
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   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.ntp;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class TestTimeInfo {
30  
31      @Test
32      public void testAddress() throws UnknownHostException {
33          final NtpV3Packet packet = new NtpV3Impl();
34          final TimeInfo info = new TimeInfo(packet, System.currentTimeMillis());
35          Assert.assertNull(info.getAddress());
36          packet.getDatagramPacket().setAddress(InetAddress.getByAddress("loopback", new byte[] { 127, 0, 0, 1 }));
37          Assert.assertNotNull(info.getAddress());
38      }
39  
40      @Test
41      public void testComputeDetails() {
42          // if (origTime > returnTime) // assert destTime >= origTime
43          final NtpV3Packet packet = new NtpV3Impl();
44          final long returnTimeMillis = System.currentTimeMillis();
45  
46          // example
47          // returntime=1370571658178
48          // origTime= 1370571659178
49  
50          // originate time as defined in RFC-1305 (t1)
51          packet.setOriginateTimeStamp(TimeStamp.getNtpTime(returnTimeMillis + 1000));
52          // Receive Time is time request received by server (t2)
53          packet.setReceiveTimeStamp(packet.getOriginateTimeStamp());
54          // Transmit time is time reply sent by server (t3)
55          packet.setTransmitTime(packet.getOriginateTimeStamp());
56          packet.setReferenceTime(packet.getOriginateTimeStamp());
57  
58          // long origTime = packet.getOriginateTimeStamp().getTime();
59          // System.out.println("returntime=" + returnTime);
60          // System.out.println("origTime= " + origTime);
61  
62          final TimeInfo info = new TimeInfo(packet, returnTimeMillis);
63          info.computeDetails();
64  
65          Assert.assertSame(packet, info.getMessage());
66          Assert.assertEquals(returnTimeMillis, info.getReturnTime());
67          Assert.assertEquals(Long.valueOf(500), info.getOffset());
68          Assert.assertEquals(Long.valueOf(-1000), info.getDelay());
69  
70          // comments: [Warning: processing time > total network time, Error: OrigTime > DestRcvTime]
71          Assert.assertEquals(2, info.getComments().size());
72      }
73  
74      @Test
75      public void testEquals() {
76          final NtpV3Packet packet = new NtpV3Impl();
77          final long returnTime = System.currentTimeMillis();
78          final TimeInfo info = new TimeInfo(packet, returnTime);
79          info.addComment("this is a comment");
80          final TimeInfo other = new TimeInfo(packet, returnTime);
81          other.addComment("this is a comment");
82          Assert.assertEquals(info, other); // fails
83          Assert.assertEquals(info.hashCode(), other.hashCode());
84          other.addComment("another comment");
85          // Assert.assertFalse(info.equals(other)); // comments not used for equality
86  
87          final TimeInfo another = new TimeInfo(packet, returnTime, new ArrayList<>());
88          Assert.assertEquals(info, another);
89      }
90  
91      @Test
92      public void testException() {
93          final NtpV3Packet packet = null;
94          assertThrows(IllegalArgumentException.class, () -> new TimeInfo(packet, 1L));
95      }
96  
97      @Test
98      public void testNotEquals() {
99          final NtpV3Packet packet = new NtpV3Impl();
100         final long returnTime = System.currentTimeMillis();
101         final TimeInfo info = new TimeInfo(packet, returnTime);
102 
103         // 1. different return time
104         final NtpV3Packet packet2 = new NtpV3Impl();
105         Assert.assertEquals(packet, packet2);
106         final TimeInfo info2 = new TimeInfo(packet2, returnTime + 1);
107         Assert.assertNotEquals(info, info2);
108 
109         // 2. different message / same time
110         packet2.setStratum(3);
111         packet2.setRootDelay(25);
112         final TimeInfo info3 = new TimeInfo(packet2, returnTime);
113         Assert.assertNotEquals(info, info3);
114 
115         // 3. different class
116         Object other = this;
117         Assert.assertNotEquals(info, other);
118 
119         // 4. null comparison
120         other = null;
121         Assert.assertNotEquals(info, other);
122     }
123 
124     @Test
125     public void testZeroTime() {
126         final NtpV3Packet packet = new NtpV3Impl();
127         final TimeInfo info = new TimeInfo(packet, 0);
128         info.computeDetails();
129         Assert.assertNull(info.getDelay());
130         Assert.assertNull(info.getOffset());
131         Assert.assertEquals(0L, info.getReturnTime());
132         // comments: Error: zero orig time -- cannot compute delay/offset
133         final List<String> comments = info.getComments();
134         Assert.assertEquals(1, comments.size());
135         Assert.assertTrue(comments.get(0).contains("zero orig time"));
136     }
137 
138 }