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 testIsReachablePositive()817c506958SVladimir Kotal public void testIsReachablePositive() throws IOException, InterruptedException, URISyntaxException { 82cb97e878SVladimir Kotal // Start simple TCP server on test port. 83*d3cd68dcSVladimir Kotal InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); 843b6be24fSVladimir 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 { 101*d3cd68dcSVladimir Kotal socket = new Socket(loopbackAddress, 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); 113*d3cd68dcSVladimir Kotal Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{loopbackAddress}); 11414c8a3ffSVladimir Kotal doReturn(testPort).when(serverSpy).getPort(); 11514c8a3ffSVladimir Kotal 11614c8a3ffSVladimir Kotal // Test reachability. 11714c8a3ffSVladimir Kotal boolean reachable = serverSpy.isReachable(); 11814c8a3ffSVladimir Kotal assertTrue(reachable); 11914c8a3ffSVladimir Kotal 1207c506958SVladimir Kotal thread.interrupt(); 1217c506958SVladimir Kotal thread.join(5000); 12214c8a3ffSVladimir Kotal } 123449fe788SVladimir Kotal } 12414c8a3ffSVladimir Kotal 12514c8a3ffSVladimir Kotal @Test testsReachableNegative()1267c506958SVladimir Kotal void testsReachableNegative() throws Exception { 127*d3cd68dcSVladimir Kotal InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); 1287c506958SVladimir Kotal 1297c506958SVladimir Kotal // Mock getAddresses() to return single localhost IP address and getPort() to return the test port. 1307c506958SVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 1317c506958SVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 132*d3cd68dcSVladimir Kotal Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{loopbackAddress}); 1337c506958SVladimir Kotal // port 0 should not be reachable. 1347c506958SVladimir Kotal doReturn(0).when(serverSpy).getPort(); 1357c506958SVladimir Kotal 1367c506958SVladimir Kotal assertFalse(serverSpy.isReachable()); 1377c506958SVladimir Kotal } 1387c506958SVladimir Kotal 1397c506958SVladimir Kotal @Test testEmptyAddressArray()14014c8a3ffSVladimir Kotal public void testEmptyAddressArray() throws UnknownHostException { 14114c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 14214c8a3ffSVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 14314c8a3ffSVladimir Kotal Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{}); 14414c8a3ffSVladimir Kotal assertFalse(serverSpy.isReachable()); 14514c8a3ffSVladimir Kotal } 146d0624dbbSVladimir Kotal 147d0624dbbSVladimir Kotal @Test testToString()148d0624dbbSVladimir Kotal public void testToString() { 149d0624dbbSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com", "foo", "bar"); 150d0624dbbSVladimir Kotal server.setConnectTimeout(2000); 151d0624dbbSVladimir Kotal server.setReadTimeout(1000); 152d0624dbbSVladimir Kotal assertEquals("ldaps://foo.bar.com, connect timeout: 2000, read timeout: 1000, username: foo", 153d0624dbbSVladimir Kotal server.toString()); 154d0624dbbSVladimir Kotal } 15514c8a3ffSVladimir Kotal } 156