114c8a3ffSVladimir Kotal package opengrok.auth.plugin; 214c8a3ffSVladimir Kotal 314c8a3ffSVladimir Kotal import opengrok.auth.plugin.ldap.LdapServer; 414c8a3ffSVladimir Kotal import org.junit.Test; 514c8a3ffSVladimir Kotal import org.mockito.Mockito; 614c8a3ffSVladimir Kotal 714c8a3ffSVladimir Kotal import java.io.IOException; 814c8a3ffSVladimir Kotal import java.net.InetAddress; 914c8a3ffSVladimir Kotal import java.net.ServerSocket; 1014c8a3ffSVladimir Kotal import java.net.Socket; 1114c8a3ffSVladimir Kotal import java.net.URISyntaxException; 1214c8a3ffSVladimir Kotal import java.net.UnknownHostException; 1314c8a3ffSVladimir Kotal 1414c8a3ffSVladimir Kotal import static org.junit.jupiter.api.Assertions.*; 1514c8a3ffSVladimir Kotal import static org.mockito.ArgumentMatchers.any; 1614c8a3ffSVladimir Kotal import static org.mockito.Mockito.doReturn; 1714c8a3ffSVladimir Kotal 1814c8a3ffSVladimir Kotal public class LdapServerTest { 1914c8a3ffSVladimir Kotal 2014c8a3ffSVladimir Kotal @Test testInvalidURI()2114c8a3ffSVladimir Kotal public void testInvalidURI() { 2214c8a3ffSVladimir Kotal LdapServer server = new LdapServer("foo:/\\/\\foo.bar"); 2314c8a3ffSVladimir Kotal assertFalse(server.isReachable()); 2414c8a3ffSVladimir Kotal } 2514c8a3ffSVladimir Kotal 2614c8a3ffSVladimir Kotal @Test testGetPort()2714c8a3ffSVladimir Kotal public void testGetPort() throws URISyntaxException { 2814c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar"); 2914c8a3ffSVladimir Kotal assertEquals(636, server.getPort()); 3014c8a3ffSVladimir Kotal 3114c8a3ffSVladimir Kotal server = new LdapServer("ldap://foo.bar"); 3214c8a3ffSVladimir Kotal assertEquals(389, server.getPort()); 3314c8a3ffSVladimir Kotal 3414c8a3ffSVladimir Kotal server = new LdapServer("crumble://foo.bar"); 3514c8a3ffSVladimir Kotal assertEquals(-1, server.getPort()); 3614c8a3ffSVladimir Kotal } 3714c8a3ffSVladimir Kotal 3814c8a3ffSVladimir Kotal @Test testSetGetUsername()3914c8a3ffSVladimir Kotal public void testSetGetUsername() { 4014c8a3ffSVladimir Kotal LdapServer server = new LdapServer(); 4114c8a3ffSVladimir Kotal 4214c8a3ffSVladimir Kotal assertNull(server.getUsername()); 4314c8a3ffSVladimir Kotal assertNull(server.getPassword()); 4414c8a3ffSVladimir Kotal 4514c8a3ffSVladimir Kotal final String testUsername = "foo"; 4614c8a3ffSVladimir Kotal server.setUsername(testUsername); 4714c8a3ffSVladimir Kotal assertEquals(testUsername, server.getUsername()); 4814c8a3ffSVladimir Kotal 4914c8a3ffSVladimir Kotal final String testPassword = "bar"; 5014c8a3ffSVladimir Kotal server.setPassword(testPassword); 5114c8a3ffSVladimir Kotal assertEquals(testPassword, server.getPassword()); 5214c8a3ffSVladimir Kotal } 5314c8a3ffSVladimir Kotal 5414c8a3ffSVladimir Kotal @Test testIsReachable()5514c8a3ffSVladimir Kotal public void testIsReachable() throws IOException, InterruptedException, URISyntaxException { 5614c8a3ffSVladimir Kotal // Start simple TCP server on port 6336. It has to be > 1024 to avoid BindException 5714c8a3ffSVladimir Kotal // due to permission denied. 5814c8a3ffSVladimir Kotal int testPort = 6336; 5914c8a3ffSVladimir Kotal InetAddress localhostAddr = InetAddress.getLocalHost(); 6014c8a3ffSVladimir Kotal ServerSocket serverSocket = new ServerSocket(testPort, 1, localhostAddr); 6114c8a3ffSVladimir Kotal Thread thread = new Thread(() -> { 6214c8a3ffSVladimir Kotal try { 6314c8a3ffSVladimir Kotal while (true) { 6414c8a3ffSVladimir Kotal Socket client = serverSocket.accept(); 6514c8a3ffSVladimir Kotal client.close(); 6614c8a3ffSVladimir Kotal } 6714c8a3ffSVladimir Kotal } catch (IOException e) { 6814c8a3ffSVladimir Kotal e.printStackTrace(); 6914c8a3ffSVladimir Kotal } 7014c8a3ffSVladimir Kotal }); 7114c8a3ffSVladimir Kotal 7214c8a3ffSVladimir Kotal thread.start(); 7314c8a3ffSVladimir Kotal Socket socket = null; 7414c8a3ffSVladimir Kotal for (int i = 0; i < 3; i++) { 7514c8a3ffSVladimir Kotal try { 7614c8a3ffSVladimir Kotal socket = new Socket(localhostAddr, testPort); 7714c8a3ffSVladimir Kotal } catch (IOException e) { 7814c8a3ffSVladimir Kotal Thread.sleep(1000); 7914c8a3ffSVladimir Kotal } 8014c8a3ffSVladimir Kotal } 8114c8a3ffSVladimir Kotal 8214c8a3ffSVladimir Kotal assertNotNull(socket); 8314c8a3ffSVladimir Kotal assertTrue(socket.isConnected()); 8414c8a3ffSVladimir Kotal 8514c8a3ffSVladimir Kotal // Mock getAddresses() to return single localhost IP address. 8614c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 8714c8a3ffSVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 8814c8a3ffSVladimir Kotal Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{localhostAddr}); 8914c8a3ffSVladimir Kotal doReturn(testPort).when(serverSpy).getPort(); 9014c8a3ffSVladimir Kotal 9114c8a3ffSVladimir Kotal // Test reachability. 9214c8a3ffSVladimir Kotal boolean reachable = serverSpy.isReachable(); 9314c8a3ffSVladimir Kotal serverSocket.close(); 9414c8a3ffSVladimir Kotal thread.join(5000); 9514c8a3ffSVladimir Kotal thread.interrupt(); 9614c8a3ffSVladimir Kotal assertTrue(reachable); 9714c8a3ffSVladimir Kotal 9814c8a3ffSVladimir Kotal // Test non-reachability. 9914c8a3ffSVladimir Kotal reachable = serverSpy.isReachable(); 10014c8a3ffSVladimir Kotal assertFalse(reachable); 10114c8a3ffSVladimir Kotal } 10214c8a3ffSVladimir Kotal 10314c8a3ffSVladimir Kotal @Test testEmptyAddressArray()10414c8a3ffSVladimir Kotal public void testEmptyAddressArray() throws UnknownHostException { 10514c8a3ffSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com"); 10614c8a3ffSVladimir Kotal LdapServer serverSpy = Mockito.spy(server); 10714c8a3ffSVladimir Kotal Mockito.when(serverSpy.getAddresses(any())).thenReturn(new InetAddress[]{}); 10814c8a3ffSVladimir Kotal assertFalse(serverSpy.isReachable()); 10914c8a3ffSVladimir Kotal } 110*d0624dbbSVladimir Kotal 111*d0624dbbSVladimir Kotal @Test testToString()112*d0624dbbSVladimir Kotal public void testToString() { 113*d0624dbbSVladimir Kotal LdapServer server = new LdapServer("ldaps://foo.bar.com", "foo", "bar"); 114*d0624dbbSVladimir Kotal server.setConnectTimeout(2000); 115*d0624dbbSVladimir Kotal server.setReadTimeout(1000); 116*d0624dbbSVladimir Kotal assertEquals("ldaps://foo.bar.com, connect timeout: 2000, read timeout: 1000, username: foo", 117*d0624dbbSVladimir Kotal server.toString()); 118*d0624dbbSVladimir Kotal } 11914c8a3ffSVladimir Kotal } 120