1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ntp;
21  
22  
23  import java.net.InetAddress;
24  
25  import junit.framework.Assert;
26  import junit.framework.TestCase;
27  import org.apache.commons.net.ntp.NTPUDPClient;
28  import org.apache.commons.net.ntp.TimeInfo;
29  import org.apache.directory.server.protocol.shared.DatagramAcceptor;
30  import org.apache.directory.server.unit.AbstractServerTest;
31  import org.apache.mina.util.AvailablePortFinder;
32  
33  
34  /**
35   * An {@link AbstractServerTest} testing the Network Time Protocol (NTP).
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class NtpITest extends TestCase
41  {
42      private NtpServer ntpConfig;
43      private int port;
44  
45  
46      /**
47       * Set up a partition for EXAMPLE.COM and enable the NTP service.  The LDAP service is disabled.
48       */
49      public void setUp() throws Exception
50      {
51          DatagramAcceptor datagramAcceptor = new DatagramAcceptor( null );
52          ntpConfig = new NtpServer();
53          ntpConfig.setDatagramAcceptor( datagramAcceptor );
54          ntpConfig.setEnabled( true );
55          port = AvailablePortFinder.getNextAvailable( 10123 );
56          ntpConfig.setIpPort( port );
57          ntpConfig.start();
58  
59      }
60  
61      /**
62       * Tests to make sure NTP works when enabled in the server.
63       *
64       * @throws Exception if there are errors
65       */
66      public void testNtp() throws Exception
67      {
68          long currentTime = System.currentTimeMillis();
69  
70          InetAddress host = InetAddress.getByName( null );
71  
72          NTPUDPClient ntp = new NTPUDPClient();
73          ntp.setDefaultTimeout( 5000 );
74  
75          TimeInfo timeInfo = ntp.getTime( host, port );
76          long returnTime = timeInfo.getReturnTime();
77          Assert.assertTrue( currentTime - returnTime < 1000 );
78  
79          timeInfo.computeDetails();
80  
81          Assert.assertTrue( 0 < timeInfo.getOffset() && timeInfo.getOffset() < 1000 );
82          Assert.assertTrue( 0 < timeInfo.getDelay() && timeInfo.getDelay() < 1000 );
83      }
84  
85  
86      /**
87       * Tear down.
88       */
89      public void tearDown() throws Exception
90      {
91          ntpConfig.stop();
92      }
93  }