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.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.io.IOException;
25  import java.net.InetAddress;
26  import java.time.Duration;
27  
28  import org.apache.commons.lang3.ThreadUtils;
29  import org.apache.commons.net.examples.ntp.SimpleNTPServer;
30  import org.junit.jupiter.api.AfterAll;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * JUnit test class for NtpClient using SimpleNTPServer
36   */
37  class TestNtpClient {
38  
39      private static SimpleNTPServer server; //NOPMD test code
40  
41      @BeforeAll
42      public static void oneTimeSetUp() throws IOException {
43          // one-time initialization code
44          server = new SimpleNTPServer(0);
45          server.connect();
46  
47          try {
48              server.start();
49          } catch (final IOException e) {
50              fail("failed to start NTP server: " + e);
51          }
52          assertTrue(server.isStarted());
53          // System.out.println("XXX: time server started");
54          boolean running = false;
55          for (int retries = 0; retries < 5; retries++) {
56              running = server.isRunning();
57              if (running) {
58                  break;
59              }
60              // if not running then sleep 2 seconds and try again
61              ThreadUtils.sleepQuietly(Duration.ofMillis(2000));
62          }
63          assertTrue(running);
64      }
65  
66      @AfterAll
67      public static void oneTimeTearDown() {
68          // one-time cleanup code
69          if (server != null) {
70              server.stop();
71              server = null;
72          }
73      }
74  
75      @Test
76      void testGetTime() throws IOException {
77          final long currentTimeMillis = System.currentTimeMillis();
78          final NTPUDPClient client = new NTPUDPClient();
79          // timeout if response takes longer than 2 seconds
80          client.setDefaultTimeout(Duration.ofSeconds(2));
81          try {
82              // Java 1.7: use InetAddress.getLoopbackAddress() instead
83              final InetAddress addr = InetAddress.getByAddress("loopback", new byte[] { 127, 0, 0, 1 });
84              final TimeInfo timeInfo = client.getTime(addr, server.getPort());
85              assertNotNull(timeInfo);
86              assertTrue(timeInfo.getReturnTime() >= currentTimeMillis);
87              final NtpV3Packet message = timeInfo.getMessage();
88              assertNotNull(message);
89  
90              final TimeStamp rcvTimeStamp = message.getReceiveTimeStamp();
91              final TimeStamp xmitTimeStamp = message.getTransmitTimeStamp();
92              assertTrue(xmitTimeStamp.compareTo(rcvTimeStamp) >= 0);
93  
94              final TimeStamp originateTimeStamp = message.getOriginateTimeStamp();
95              assertNotNull(originateTimeStamp);
96              assertTrue(originateTimeStamp.getTime() >= currentTimeMillis);
97  
98              assertEquals(NtpV3Packet.MODE_SERVER, message.getMode());
99  
100             // following assertions are specific to the SimpleNTPServer
101 
102             final TimeStamp referenceTimeStamp = message.getReferenceTimeStamp();
103             assertNotNull(referenceTimeStamp);
104             assertTrue(referenceTimeStamp.getTime() >= currentTimeMillis);
105 
106             assertEquals(NtpV3Packet.VERSION_3, message.getVersion());
107             assertEquals(1, message.getStratum());
108 
109             assertEquals("LCL", NtpUtils.getReferenceClock(message));
110         } finally {
111             client.close();
112         }
113     }
114 
115 }