1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package opengrok.auth.plugin.ldap; 24 25 import opengrok.auth.plugin.configuration.Configuration; 26 import org.junit.jupiter.api.Test; 27 import org.mockito.Mockito; 28 29 import javax.naming.directory.SearchControls; 30 31 import java.net.InetAddress; 32 import java.net.UnknownHostException; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.stream.Collectors; 36 37 import static org.junit.jupiter.api.Assertions.assertEquals; 38 import static org.junit.jupiter.api.Assertions.assertFalse; 39 import static org.mockito.ArgumentMatchers.any; 40 import static org.mockito.Mockito.doReturn; 41 import static org.mockito.Mockito.times; 42 import static org.mockito.Mockito.verify; 43 44 class LdapFacadeTest { 45 @Test testSearchControlsConfig()46 void testSearchControlsConfig() { 47 Configuration config = new Configuration(); 48 int searchTimeout = 1234; 49 config.setSearchTimeout(searchTimeout); 50 int countLimit = 32; 51 config.setCountLimit(countLimit); 52 53 LdapFacade facade = new LdapFacade(config); 54 SearchControls controls = facade.getSearchControls(); 55 assertEquals(searchTimeout, controls.getTimeLimit()); 56 assertEquals(countLimit, controls.getCountLimit()); 57 } 58 getSpyLdapServer(String name)59 private LdapServer getSpyLdapServer(String name) throws UnknownHostException { 60 return getSpyLdapServer(name, true); 61 } 62 getSpyLdapServer(String name, boolean working)63 private LdapServer getSpyLdapServer(String name, boolean working) throws UnknownHostException { 64 LdapServer server = new LdapServer(name); 65 LdapServer serverSpy = Mockito.spy(server); 66 doReturn(new InetAddress[]{InetAddress.getLocalHost()}).when(serverSpy).getAddresses(any()); 67 doReturn(working).when(serverSpy).isWorking(); 68 return serverSpy; 69 } 70 71 @Test testConnectTimeoutInheritance()72 void testConnectTimeoutInheritance() throws UnknownHostException { 73 Configuration config = new Configuration(); 74 75 LdapServer[] servers = {getSpyLdapServer("ldap://foo.com"), getSpyLdapServer("ldap://bar.com")}; 76 config.setServers(Arrays.asList(servers)); 77 78 int connectTimeoutValue = 42; 79 config.setConnectTimeout(connectTimeoutValue); 80 int readTimeoutValue = 24; 81 config.setReadTimeout(readTimeoutValue); 82 83 LdapFacade facade = new LdapFacade(config); 84 assertEquals(Collections.singleton(connectTimeoutValue), 85 facade.getServers().stream().map(LdapServer::getConnectTimeout).collect(Collectors.toSet())); 86 assertEquals(Collections.singleton(readTimeoutValue), 87 facade.getServers().stream().map(LdapServer::getReadTimeout).collect(Collectors.toSet())); 88 } 89 90 @Test testToStringNegative()91 void testToStringNegative() throws UnknownHostException { 92 Configuration config = new Configuration(); 93 LdapServer server1 = new LdapServer("ldap://foo.com"); 94 LdapServer serverSpy1 = Mockito.spy(server1); 95 doReturn(new InetAddress[]{InetAddress.getLocalHost()}).when(serverSpy1).getAddresses(any()); 96 doReturn(false).when(serverSpy1).isReachable(); 97 98 config.setServers(Collections.singletonList(serverSpy1)); 99 config.setSearchBase("dc=foo,dc=com"); 100 int timeoutValue = 3; 101 config.setConnectTimeout(timeoutValue); 102 LdapFacade facade = new LdapFacade(config); 103 assertEquals("{server=no active server, searchBase=dc=foo,dc=com}", 104 facade.toString()); 105 } 106 107 @Test testToStringPositive()108 void testToStringPositive() { 109 Configuration config = new Configuration(); 110 LdapServer server1 = new LdapServer("ldap://foo.com"); 111 LdapServer serverSpy1 = Mockito.spy(server1); 112 doReturn(true).when(serverSpy1).isWorking(); 113 114 config.setServers(Collections.singletonList(serverSpy1)); 115 config.setSearchBase("dc=foo,dc=com"); 116 int timeoutValue = 3; 117 config.setConnectTimeout(timeoutValue); 118 LdapFacade facade = new LdapFacade(config); 119 assertEquals("{server=ldap://foo.com, connect timeout: 3, searchBase=dc=foo,dc=com}", 120 facade.toString()); 121 } 122 123 @Test testPrepareServersNegative()124 void testPrepareServersNegative() throws UnknownHostException { 125 Configuration config = new Configuration(); 126 config.setServers(Arrays.asList(getSpyLdapServer("ldap://foo.com", false), 127 getSpyLdapServer("ldap://bar.com", true))); 128 LdapFacade facade = new LdapFacade(config); 129 assertFalse(facade.isConfigured()); 130 } 131 132 @Test testGetSearchDescription()133 void testGetSearchDescription() { 134 assertEquals("DN: foo, filter: bar, attributes: Bilbo,Frodo", 135 LdapFacade.getSearchDescription("foo", "bar", new String[]{"Bilbo", "Frodo"})); 136 assertEquals("DN: foo, filter: bar", 137 LdapFacade.getSearchDescription("foo", "bar", null)); 138 } 139 140 @Test testPrepareServersCloseUnused()141 void testPrepareServersCloseUnused() throws UnknownHostException { 142 Configuration config = new Configuration(); 143 144 LdapServer server1 = getSpyLdapServer("ldap://foo.com"); 145 LdapServer server2 = getSpyLdapServer("ldap://bar.com"); 146 LdapServer[] servers = {server1, server2}; 147 config.setServers(Arrays.asList(servers)); 148 149 LdapFacade facade = new LdapFacade(config); 150 verify(server1, times(0)).close(); 151 verify(server2).close(); 152 } 153 } 154