xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java (revision 86a886132fbc9b3ccc0ffd663d252b20ae7dee35)
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) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin;
24 
25 import opengrok.auth.plugin.ldap.LdapServer;
26 import org.junit.jupiter.api.Test;
27 import org.mockito.Mockito;
28 
29 import java.io.IOException;
30 import java.net.InetAddress;
31 import java.net.ServerSocket;
32 import java.net.URISyntaxException;
33 import java.net.UnknownHostException;
34 
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertFalse;
37 import static org.junit.jupiter.api.Assertions.assertNull;
38 import static org.junit.jupiter.api.Assertions.assertTrue;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.Mockito.doReturn;
41 
42 class LdapServerTest {
43 
44     @Test
testInvalidURI()45     void testInvalidURI() {
46         LdapServer server = new LdapServer("foo:/\\/\\foo.bar");
47         assertFalse(server.isReachable());
48     }
49 
50     @Test
testGetPort()51     void testGetPort() throws URISyntaxException {
52         LdapServer server = new LdapServer("ldaps://foo.bar");
53         assertEquals(636, server.getPort());
54 
55         server = new LdapServer("ldap://foo.bar");
56         assertEquals(389, server.getPort());
57 
58         server = new LdapServer("crumble://foo.bar");
59         assertEquals(-1, server.getPort());
60     }
61 
62     @Test
testSetGetUsername()63     void testSetGetUsername() {
64         LdapServer server = new LdapServer();
65 
66         assertNull(server.getUsername());
67         assertNull(server.getPassword());
68 
69         final String testUsername = "foo";
70         server.setUsername(testUsername);
71         assertEquals(testUsername, server.getUsername());
72 
73         final String testPassword = "bar";
74         server.setPassword(testPassword);
75         assertEquals(testPassword, server.getPassword());
76     }
77 
78     @Test
testIsReachablePositive()79     void testIsReachablePositive() throws IOException, URISyntaxException {
80         // Start simple TCP server on test port.
81         InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
82         try (ServerSocket serverSocket = new ServerSocket(0, 1)) {
83             int testPort = serverSocket.getLocalPort();
84 
85             // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
86             LdapServer server = new LdapServer("ldaps://foo.bar.com");
87             LdapServer serverSpy = Mockito.spy(server);
88             doReturn(new InetAddress[] {loopbackAddress}).when(serverSpy).getAddresses(any());
89             doReturn(testPort).when(serverSpy).getPort();
90 
91             // Test reachability.
92             boolean reachable = serverSpy.isReachable();
93             assertTrue(reachable);
94         }
95     }
96 
97     @Test
testsReachableNegative()98     void testsReachableNegative() throws Exception {
99         InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
100 
101         // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
102         LdapServer server = new LdapServer("ldaps://foo.bar.com");
103         LdapServer serverSpy = Mockito.spy(server);
104         doReturn(new InetAddress[]{loopbackAddress}).when(serverSpy).getAddresses(any());
105         // port 0 should not be reachable.
106         doReturn(0).when(serverSpy).getPort();
107 
108         assertFalse(serverSpy.isReachable());
109     }
110 
111     @Test
testEmptyAddressArray()112     void testEmptyAddressArray() throws UnknownHostException {
113         LdapServer server = new LdapServer("ldaps://foo.bar.com");
114         LdapServer serverSpy = Mockito.spy(server);
115         doReturn(new InetAddress[]{}).when(serverSpy).getAddresses(any());
116         assertFalse(serverSpy.isReachable());
117     }
118 
119     @Test
testToString()120     void testToString() {
121         LdapServer server = new LdapServer("ldaps://foo.bar.com", "foo", "bar");
122         server.setConnectTimeout(2000);
123         server.setReadTimeout(1000);
124         assertEquals("ldaps://foo.bar.com, connect timeout: 2000, read timeout: 1000, username: foo",
125                 server.toString());
126     }
127 }
128