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