xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
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 /*
21*2f7dccc7SAdam 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;
26*2f7dccc7SAdam 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 {
8214c8a3ffSVladimir Kotal         // Start simple TCP server on port 6336. It has to be > 1024 to avoid BindException
8314c8a3ffSVladimir Kotal         // due to permission denied.
8414c8a3ffSVladimir Kotal         int testPort = 6336;
8514c8a3ffSVladimir Kotal         InetAddress localhostAddr = InetAddress.getLocalHost();
8614c8a3ffSVladimir Kotal         ServerSocket serverSocket = new ServerSocket(testPort, 1, localhostAddr);
8714c8a3ffSVladimir Kotal         Thread thread = new Thread(() -> {
8814c8a3ffSVladimir Kotal             try {
8914c8a3ffSVladimir Kotal                 while (true) {
9014c8a3ffSVladimir Kotal                     Socket client = serverSocket.accept();
9114c8a3ffSVladimir Kotal                     client.close();
9214c8a3ffSVladimir Kotal                 }
9314c8a3ffSVladimir Kotal             } catch (IOException e) {
9414c8a3ffSVladimir Kotal                 e.printStackTrace();
9514c8a3ffSVladimir Kotal             }
9614c8a3ffSVladimir Kotal         });
9714c8a3ffSVladimir Kotal 
9814c8a3ffSVladimir Kotal         thread.start();
9914c8a3ffSVladimir Kotal         Socket socket = null;
10014c8a3ffSVladimir Kotal         for (int i = 0; i < 3; i++) {
10114c8a3ffSVladimir Kotal             try {
10214c8a3ffSVladimir Kotal                 socket = new Socket(localhostAddr, testPort);
10314c8a3ffSVladimir Kotal             } catch (IOException e) {
10414c8a3ffSVladimir Kotal                 Thread.sleep(1000);
10514c8a3ffSVladimir Kotal             }
10614c8a3ffSVladimir Kotal         }
10714c8a3ffSVladimir Kotal 
10814c8a3ffSVladimir Kotal         assertNotNull(socket);
10914c8a3ffSVladimir Kotal         assertTrue(socket.isConnected());
11014c8a3ffSVladimir Kotal 
11114c8a3ffSVladimir Kotal         // Mock getAddresses() to return single localhost IP address.
11214c8a3ffSVladimir Kotal         LdapServer server = new LdapServer("ldaps://foo.bar.com");
11314c8a3ffSVladimir Kotal         LdapServer serverSpy = Mockito.spy(server);
11414c8a3ffSVladimir Kotal         Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{localhostAddr});
11514c8a3ffSVladimir Kotal         doReturn(testPort).when(serverSpy).getPort();
11614c8a3ffSVladimir Kotal 
11714c8a3ffSVladimir Kotal         // Test reachability.
11814c8a3ffSVladimir Kotal         boolean reachable = serverSpy.isReachable();
11914c8a3ffSVladimir Kotal         serverSocket.close();
12014c8a3ffSVladimir Kotal         thread.join(5000);
12114c8a3ffSVladimir Kotal         thread.interrupt();
12214c8a3ffSVladimir Kotal         assertTrue(reachable);
12314c8a3ffSVladimir Kotal 
12414c8a3ffSVladimir Kotal         // Test non-reachability.
12514c8a3ffSVladimir Kotal         reachable = serverSpy.isReachable();
12614c8a3ffSVladimir Kotal         assertFalse(reachable);
12714c8a3ffSVladimir 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