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.URISyntaxException; 3314c8a3ffSVladimir Kotal import java.net.UnknownHostException; 3414c8a3ffSVladimir Kotal 351161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertEquals; 361161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertFalse; 371161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertNull; 381161d3e8SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertTrue; 3914c8a3ffSVladimir Kotal import static org.mockito.ArgumentMatchers.any; 4014c8a3ffSVladimir Kotal import static org.mockito.Mockito.doReturn; 4114c8a3ffSVladimir Kotal 42*86a88613SAdam Hornacek class LdapServerTest { 4314c8a3ffSVladimir Kotal 4414c8a3ffSVladimir Kotal @Test testInvalidURI()45*86a88613SAdam Hornacek void testInvalidURI() { 4614c8a3ffSVladimir Kotal LdapServer server = new LdapServer("foo:/\\/\\foo.bar"); 4714c8a3ffSVladimir Kotal assertFalse(server.isReachable()); 4814c8a3ffSVladimir Kotal } 4914c8a3ffSVladimir Kotal 5014c8a3ffSVladimir Kotal @Test testGetPort()51*86a88613SAdam Hornacek void testGetPort() throws URISyntaxException { 5214c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar"); 5314c8a3ffSVladimir Kotal assertEquals(636, server.getPort()); 5414c8a3ffSVladimir Kotal 5514c8a3ffSVladimir Kotal server = new LdapServer("ldap://foo.bar"); 5614c8a3ffSVladimir Kotal assertEquals(389, server.getPort()); 5714c8a3ffSVladimir Kotal 5814c8a3ffSVladimir Kotal server = new LdapServer("crumble://foo.bar"); 5914c8a3ffSVladimir Kotal assertEquals(-1, server.getPort()); 6014c8a3ffSVladimir Kotal } 6114c8a3ffSVladimir Kotal 6214c8a3ffSVladimir Kotal @Test testSetGetUsername()63*86a88613SAdam Hornacek void testSetGetUsername() { 6414c8a3ffSVladimir Kotal LdapServer server = new LdapServer(); 6514c8a3ffSVladimir Kotal 6614c8a3ffSVladimir Kotal assertNull(server.getUsername()); 6714c8a3ffSVladimir Kotal assertNull(server.getPassword()); 6814c8a3ffSVladimir Kotal 6914c8a3ffSVladimir Kotal final String testUsername = "foo"; 7014c8a3ffSVladimir Kotal server.setUsername(testUsername); 7114c8a3ffSVladimir Kotal assertEquals(testUsername, server.getUsername()); 7214c8a3ffSVladimir Kotal 7314c8a3ffSVladimir Kotal final String testPassword = "bar"; 7414c8a3ffSVladimir Kotal server.setPassword(testPassword); 7514c8a3ffSVladimir Kotal assertEquals(testPassword, server.getPassword()); 7614c8a3ffSVladimir Kotal } 7714c8a3ffSVladimir Kotal 7814c8a3ffSVladimir Kotal @Test testIsReachablePositive()79*86a88613SAdam Hornacek void testIsReachablePositive() throws IOException, URISyntaxException { 80cb97e878SVladimir Kotal // Start simple TCP server on test port. 81d3cd68dcSVladimir Kotal InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); 823b6be24fSVladimir Kotal try (ServerSocket serverSocket = new ServerSocket(0, 1)) { 83476443edSVladimir Kotal int testPort = serverSocket.getLocalPort(); 8414c8a3ffSVladimir Kotal 85cb97e878SVladimir Kotal // Mock getAddresses() to return single localhost IP address and getPort() to return the test port. 8614c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 8714c8a3ffSVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 88*86a88613SAdam Hornacek doReturn(new InetAddress[] {loopbackAddress}).when(serverSpy).getAddresses(any()); 8914c8a3ffSVladimir Kotal doReturn(testPort).when(serverSpy).getPort(); 9014c8a3ffSVladimir Kotal 9114c8a3ffSVladimir Kotal // Test reachability. 9214c8a3ffSVladimir Kotal boolean reachable = serverSpy.isReachable(); 9314c8a3ffSVladimir Kotal assertTrue(reachable); 9414c8a3ffSVladimir Kotal } 95449fe788SVladimir Kotal } 9614c8a3ffSVladimir Kotal 9714c8a3ffSVladimir Kotal @Test testsReachableNegative()987c506958SVladimir Kotal void testsReachableNegative() throws Exception { 99d3cd68dcSVladimir Kotal InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); 1007c506958SVladimir Kotal 1017c506958SVladimir Kotal // Mock getAddresses() to return single localhost IP address and getPort() to return the test port. 1027c506958SVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 1037c506958SVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 104*86a88613SAdam Hornacek doReturn(new InetAddress[]{loopbackAddress}).when(serverSpy).getAddresses(any()); 1057c506958SVladimir Kotal // port 0 should not be reachable. 1067c506958SVladimir Kotal doReturn(0).when(serverSpy).getPort(); 1077c506958SVladimir Kotal 1087c506958SVladimir Kotal assertFalse(serverSpy.isReachable()); 1097c506958SVladimir Kotal } 1107c506958SVladimir Kotal 1117c506958SVladimir Kotal @Test testEmptyAddressArray()112*86a88613SAdam Hornacek void testEmptyAddressArray() throws UnknownHostException { 11314c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 11414c8a3ffSVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 115*86a88613SAdam Hornacek doReturn(new InetAddress[]{}).when(serverSpy).getAddresses(any()); 11614c8a3ffSVladimir Kotal assertFalse(serverSpy.isReachable()); 11714c8a3ffSVladimir Kotal } 118d0624dbbSVladimir Kotal 119d0624dbbSVladimir Kotal @Test testToString()120*86a88613SAdam Hornacek void testToString() { 121d0624dbbSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com", "foo", "bar"); 122d0624dbbSVladimir Kotal server.setConnectTimeout(2000); 123d0624dbbSVladimir Kotal server.setReadTimeout(1000); 124d0624dbbSVladimir Kotal assertEquals("ldaps://foo.bar.com, connect timeout: 2000, read timeout: 1000, username: foo", 125d0624dbbSVladimir Kotal server.toString()); 126d0624dbbSVladimir Kotal } 12714c8a3ffSVladimir Kotal } 128