Skip to content

Commit cb97e87

Browse files
author
Vladimir Kotal
committed
use port range in LdapServerTest
1 parent 2825cf7 commit cb97e87

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
import opengrok.auth.plugin.ldap.LdapServer;
2626
import org.junit.jupiter.api.Test;
2727
import org.mockito.Mockito;
28+
import org.opengrok.web.api.v1.controller.PortChecker;
2829

2930
import java.io.IOException;
3031
import java.net.InetAddress;
3132
import java.net.ServerSocket;
3233
import java.net.Socket;
3334
import java.net.URISyntaxException;
3435
import java.net.UnknownHostException;
36+
import java.util.Random;
3537

3638
import static org.junit.jupiter.api.Assertions.assertEquals;
3739
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -79,9 +81,24 @@ public void testSetGetUsername() {
7981

8082
@Test
8183
public void testIsReachable() throws IOException, InterruptedException, URISyntaxException {
82-
// Start simple TCP server on port 6336. It has to be > 1024 to avoid BindException
83-
// due to permission denied.
84-
int testPort = 6336;
84+
final int testPortBase = 6336; // It has to be > 1024 to avoid BindException due to permission denied.
85+
final int randomRange = 49152;
86+
int triesCount = 0;
87+
final int MAX_PORT_TRIES = 20;
88+
Random rand = new Random();
89+
int testPort;
90+
while (true) {
91+
testPort = testPortBase + rand.nextInt(randomRange);
92+
if (PortChecker.available(testPort)) {
93+
break;
94+
}
95+
if (++triesCount > MAX_PORT_TRIES) {
96+
throw new RuntimeException("Could not find an available port after " +
97+
MAX_PORT_TRIES + " tries");
98+
}
99+
}
100+
101+
// Start simple TCP server on test port.
85102
InetAddress localhostAddr = InetAddress.getLocalHost();
86103
ServerSocket serverSocket = new ServerSocket(testPort, 1, localhostAddr);
87104
Thread thread = new Thread(() -> {
@@ -108,7 +125,7 @@ public void testIsReachable() throws IOException, InterruptedException, URISynta
108125
assertNotNull(socket);
109126
assertTrue(socket.isConnected());
110127

111-
// Mock getAddresses() to return single localhost IP address.
128+
// Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
112129
LdapServer server = new LdapServer("ldaps://foo.bar.com");
113130
LdapServer serverSpy = Mockito.spy(server);
114131
Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{localhostAddr});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.opengrok.web.api.v1.controller;
21+
22+
import java.io.IOException;
23+
import java.net.DatagramSocket;
24+
import java.net.ServerSocket;
25+
26+
/**
27+
* Represents a helper for checking a network port, borrowed from Apache
28+
* Usergrid's AvailablePortFinder.java.
29+
*/
30+
public class PortChecker {
31+
/** The minimum number of server port number. */
32+
public static final int MIN_PORT_NUMBER = 1;
33+
34+
/** The maximum number of server port number. */
35+
public static final int MAX_PORT_NUMBER = 65535;
36+
37+
/**
38+
* Checks to see if a specific port is available.
39+
*
40+
* @param port the port to check for availability
41+
*/
42+
public static boolean available(int port) {
43+
if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
44+
throw new IllegalArgumentException("Port out of valid range: " + port);
45+
}
46+
47+
ServerSocket ss = null;
48+
DatagramSocket ds = null;
49+
try {
50+
ss = new ServerSocket(port);
51+
ss.setReuseAddress(true);
52+
ds = new DatagramSocket(port);
53+
ds.setReuseAddress(true);
54+
return true;
55+
} catch (IOException e) {
56+
return false;
57+
} finally {
58+
if (ds != null) {
59+
ds.close();
60+
}
61+
62+
if (ss != null) {
63+
try {
64+
ss.close();
65+
} catch (IOException e) {
66+
/* should not be thrown */
67+
}
68+
}
69+
}
70+
}
71+
72+
/* private to enforce static */
73+
private PortChecker() {
74+
}
75+
}

0 commit comments

Comments
 (0)