xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java (revision 3b6be24f203f7da21fb11492853f457230dc3ea1)
11161d3e8SAdam Hornacek /*
21161d3e8SAdam Hornacek  * CDDL HEADER START
31161d3e8SAdam Hornacek  *
41161d3e8SAdam Hornacek  * The contents of this file are subject to the terms of the
51161d3e8SAdam Hornacek  * Common Development and Distribution License (the "License").
61161d3e8SAdam Hornacek  * You may not use this file except in compliance with the License.
71161d3e8SAdam Hornacek  *
81161d3e8SAdam Hornacek  * See LICENSE.txt included in this distribution for the specific
91161d3e8SAdam Hornacek  * language governing permissions and limitations under the License.
101161d3e8SAdam Hornacek  *
111161d3e8SAdam Hornacek  * When distributing Covered Code, include this CDDL HEADER in each
121161d3e8SAdam Hornacek  * file and include the License file at LICENSE.txt.
131161d3e8SAdam Hornacek  * If applicable, add the following below this CDDL HEADER, with the
141161d3e8SAdam Hornacek  * fields enclosed by brackets "[]" replaced with your own identifying
151161d3e8SAdam Hornacek  * information: Portions Copyright [yyyy] [name of copyright owner]
161161d3e8SAdam Hornacek  *
171161d3e8SAdam Hornacek  * CDDL HEADER END
181161d3e8SAdam Hornacek  */
191161d3e8SAdam Hornacek 
201161d3e8SAdam Hornacek /*
212f7dccc7SAdam Hornacek  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
221161d3e8SAdam Hornacek  */
2314c8a3ffSVladimir Kotal package opengrok.auth.plugin;
2414c8a3ffSVladimir Kotal 
2514c8a3ffSVladimir Kotal import opengrok.auth.plugin.ldap.LdapServer;
262f7dccc7SAdam Hornacek import org.junit.jupiter.api.Test;
2714c8a3ffSVladimir Kotal import org.mockito.Mockito;
2814c8a3ffSVladimir Kotal 
2914c8a3ffSVladimir Kotal import java.io.IOException;
3014c8a3ffSVladimir Kotal import java.net.InetAddress;
3114c8a3ffSVladimir Kotal import java.net.ServerSocket;
3214c8a3ffSVladimir Kotal import java.net.Socket;
3314c8a3ffSVladimir Kotal import java.net.URISyntaxException;
3414c8a3ffSVladimir Kotal import java.net.UnknownHostException;
3514c8a3ffSVladimir Kotal 
361161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertEquals;
371161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertFalse;
381161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertNotNull;
391161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertNull;
401161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertTrue;
4114c8a3ffSVladimir Kotal import static org.mockito.ArgumentMatchers.any;
4214c8a3ffSVladimir Kotal import static org.mockito.Mockito.doReturn;
4314c8a3ffSVladimir Kotal 
4414c8a3ffSVladimir Kotal public class LdapServerTest {
4514c8a3ffSVladimir Kotal 
4614c8a3ffSVladimir Kotal     @Test
testInvalidURI()4714c8a3ffSVladimir Kotal     public void testInvalidURI() {
4814c8a3ffSVladimir Kotal         LdapServer server = new LdapServer("foo:/\\/\\foo.bar");
4914c8a3ffSVladimir Kotal         assertFalse(server.isReachable());
5014c8a3ffSVladimir Kotal     }
5114c8a3ffSVladimir Kotal 
5214c8a3ffSVladimir Kotal     @Test
testGetPort()5314c8a3ffSVladimir Kotal     public void testGetPort() throws URISyntaxException {
5414c8a3ffSVladimir Kotal         LdapServer server = new LdapServer("ldaps://foo.bar");
5514c8a3ffSVladimir Kotal         assertEquals(636, server.getPort());
5614c8a3ffSVladimir Kotal 
5714c8a3ffSVladimir Kotal         server = new LdapServer("ldap://foo.bar");
5814c8a3ffSVladimir Kotal         assertEquals(389, server.getPort());
5914c8a3ffSVladimir Kotal 
6014c8a3ffSVladimir Kotal         server = new LdapServer("crumble://foo.bar");
6114c8a3ffSVladimir Kotal         assertEquals(-1, server.getPort());
6214c8a3ffSVladimir Kotal     }
6314c8a3ffSVladimir Kotal 
6414c8a3ffSVladimir Kotal     @Test
testSetGetUsername()6514c8a3ffSVladimir Kotal     public void testSetGetUsername() {
6614c8a3ffSVladimir Kotal         LdapServer server = new LdapServer();
6714c8a3ffSVladimir Kotal 
6814c8a3ffSVladimir Kotal         assertNull(server.getUsername());
6914c8a3ffSVladimir Kotal         assertNull(server.getPassword());
7014c8a3ffSVladimir Kotal 
7114c8a3ffSVladimir Kotal         final String testUsername = "foo";
7214c8a3ffSVladimir Kotal         server.setUsername(testUsername);
7314c8a3ffSVladimir Kotal         assertEquals(testUsername, server.getUsername());
7414c8a3ffSVladimir Kotal 
7514c8a3ffSVladimir Kotal         final String testPassword = "bar";
7614c8a3ffSVladimir Kotal         server.setPassword(testPassword);
7714c8a3ffSVladimir Kotal         assertEquals(testPassword, server.getPassword());
7814c8a3ffSVladimir Kotal     }
7914c8a3ffSVladimir Kotal 
8014c8a3ffSVladimir Kotal     @Test
testIsReachable()8114c8a3ffSVladimir Kotal     public void testIsReachable() throws IOException, InterruptedException, URISyntaxException {
82cb97e878SVladimir Kotal         // Start simple TCP server on test port.
8336f77d07SVladimir Kotal         InetAddress localhostAddr = InetAddress.getLoopbackAddress();
84*3b6be24fSVladimir Kotal         try (ServerSocket serverSocket = new ServerSocket(0, 1)) {
8514c8a3ffSVladimir Kotal             Thread thread = new Thread(() -> {
8614c8a3ffSVladimir Kotal                 try {
8714c8a3ffSVladimir Kotal                     while (true) {
8814c8a3ffSVladimir Kotal                         Socket client = serverSocket.accept();
8914c8a3ffSVladimir Kotal                         client.close();
9014c8a3ffSVladimir Kotal                     }
9114c8a3ffSVladimir Kotal                 } catch (IOException e) {
9214c8a3ffSVladimir Kotal                     e.printStackTrace();
9314c8a3ffSVladimir Kotal                 }
9414c8a3ffSVladimir Kotal             });
9514c8a3ffSVladimir Kotal 
96476443edSVladimir Kotal             int testPort = serverSocket.getLocalPort();
9714c8a3ffSVladimir Kotal             thread.start();
9814c8a3ffSVladimir Kotal             Socket socket = null;
9914c8a3ffSVladimir Kotal             for (int i = 0; i < 3; i++) {
10014c8a3ffSVladimir Kotal                 try {
10114c8a3ffSVladimir Kotal                     socket = new Socket(localhostAddr, testPort);
10214c8a3ffSVladimir Kotal                 } catch (IOException e) {
10314c8a3ffSVladimir Kotal                     Thread.sleep(1000);
10414c8a3ffSVladimir Kotal                 }
10514c8a3ffSVladimir Kotal             }
10614c8a3ffSVladimir Kotal 
10714c8a3ffSVladimir Kotal             assertNotNull(socket);
10814c8a3ffSVladimir Kotal             assertTrue(socket.isConnected());
10914c8a3ffSVladimir Kotal 
110cb97e878SVladimir Kotal             // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
11114c8a3ffSVladimir Kotal             LdapServer server = new LdapServer("ldaps://foo.bar.com");
11214c8a3ffSVladimir Kotal             LdapServer serverSpy = Mockito.spy(server);
11314c8a3ffSVladimir Kotal             Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{localhostAddr});
11414c8a3ffSVladimir Kotal             doReturn(testPort).when(serverSpy).getPort();
11514c8a3ffSVladimir Kotal 
11614c8a3ffSVladimir Kotal             // Test reachability.
11714c8a3ffSVladimir Kotal             boolean reachable = serverSpy.isReachable();
11814c8a3ffSVladimir Kotal             serverSocket.close();
11914c8a3ffSVladimir Kotal             thread.join(5000);
12014c8a3ffSVladimir Kotal             thread.interrupt();
12114c8a3ffSVladimir Kotal             assertTrue(reachable);
12214c8a3ffSVladimir Kotal 
12314c8a3ffSVladimir Kotal             // Test non-reachability.
12414c8a3ffSVladimir Kotal             reachable = serverSpy.isReachable();
12514c8a3ffSVladimir Kotal             assertFalse(reachable);
12614c8a3ffSVladimir Kotal         }
127449fe788SVladimir Kotal     }
12814c8a3ffSVladimir Kotal 
12914c8a3ffSVladimir Kotal     @Test
testEmptyAddressArray()13014c8a3ffSVladimir Kotal     public void testEmptyAddressArray() throws UnknownHostException {
13114c8a3ffSVladimir Kotal         LdapServer server = new LdapServer("ldaps://foo.bar.com");
13214c8a3ffSVladimir Kotal         LdapServer serverSpy = Mockito.spy(server);
13314c8a3ffSVladimir Kotal         Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{});
13414c8a3ffSVladimir Kotal         assertFalse(serverSpy.isReachable());
13514c8a3ffSVladimir Kotal     }
136d0624dbbSVladimir Kotal 
137d0624dbbSVladimir Kotal     @Test
testToString()138d0624dbbSVladimir Kotal     public void testToString() {
139d0624dbbSVladimir Kotal         LdapServer server = new LdapServer("ldaps://foo.bar.com", "foo", "bar");
140d0624dbbSVladimir Kotal         server.setConnectTimeout(2000);
141d0624dbbSVladimir Kotal         server.setReadTimeout(1000);
142d0624dbbSVladimir Kotal         assertEquals("ldaps://foo.bar.com, connect timeout: 2000, read timeout: 1000, username: foo",
143d0624dbbSVladimir Kotal                 server.toString());
144d0624dbbSVladimir Kotal     }
14514c8a3ffSVladimir Kotal }
146