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