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.time;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  import java.util.Calendar;
26  import java.util.TimeZone;
27  
28  import org.junit.jupiter.api.Test;
29  
30  class TimeTCPClientTest {
31      private TimeTestSimpleServer server1;
32  
33      private int _port = 3333; // default test port
34  
35      protected void closeConnections() {
36          try {
37              server1.stop();
38              Thread.sleep(1000);
39          } catch (final Exception e) {
40              // ignored
41          }
42      }
43  
44      protected void openConnections() throws Exception {
45          try {
46              server1 = new TimeTestSimpleServer(_port);
47              server1.connect();
48          } catch (final IOException ioe) {
49              // try again on another port
50              _port = 4000;
51              server1 = new TimeTestSimpleServer(_port);
52              server1.connect();
53          }
54          server1.start();
55      }
56  
57      /**
58       * Tests the times retrieved via the Time protocol implementation.
59       */
60      @Test
61      void testCompareTimes() throws Exception {
62          openConnections();
63  
64          final long time;
65          final long time2;
66          final long clientTime;
67          final long clientTime2;
68          final TimeTCPClient client = new TimeTCPClient();
69          try {
70              // Not sure why code used to use getLocalHost.
71              final InetAddress localHost = InetAddress.getByName("localhost"); // WAS InetAddress.getLocalHost();
72              try {
73                  // We want to timeout if a response takes longer than 60 seconds
74                  client.setDefaultTimeout(60000);
75                  client.connect(localHost, _port);
76                  clientTime = client.getDate().getTime();
77                  time = System.currentTimeMillis();
78              } catch (final IOException e) { // catch the first connect error; assume second will work if this does
79                  fail("IOError <" + e + "> trying to connect to " + localHost + " " + _port);
80                  throw e;
81              } finally {
82                  if (client.isConnected()) {
83                      client.disconnect();
84                  }
85              }
86  
87              try {
88                  // We want to timeout if a response takes longer than 60 seconds
89                  client.setDefaultTimeout(60000);
90                  client.connect(localHost, _port);
91                  clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970) * 1000L;
92                  time2 = System.currentTimeMillis();
93              } finally {
94                  if (client.isConnected()) {
95                      client.disconnect();
96                  }
97              }
98          } finally {
99              closeConnections();
100         }
101 
102         // current time shouldn't differ from time reported via network by 5 seconds
103         assertTrue(Math.abs(time - clientTime) < 5000);
104         assertTrue(Math.abs(time2 - clientTime2) < 5000);
105     }
106 
107     /**
108      * Tests the constant basetime used by TimeClient against tha computed from Calendar class.
109      */
110     @Test
111     void testInitial() {
112         final TimeZone utcZone = TimeZone.getTimeZone("UTC");
113         final Calendar calendar = Calendar.getInstance(utcZone);
114         calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
115         calendar.set(Calendar.MILLISECOND, 0);
116         final long baseTime = calendar.getTime().getTime() / 1000L;
117 
118         assertEquals(-TimeTCPClient.SECONDS_1900_TO_1970, baseTime);
119     }
120 }