Search in sources :

Example 6 with LdapServer

use of opengrok.auth.plugin.ldap.LdapServer in project OpenGrok by OpenGrok.

the class LdapServerTest method testIsReachablePositive.

@Test
void testIsReachablePositive() throws IOException, URISyntaxException {
    // Start simple TCP server on test port.
    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    try (ServerSocket serverSocket = new ServerSocket(0, 1)) {
        int testPort = serverSocket.getLocalPort();
        // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
        LdapServer server = new LdapServer("ldaps://foo.bar.com");
        LdapServer serverSpy = Mockito.spy(server);
        doReturn(new InetAddress[] { loopbackAddress }).when(serverSpy).getAddresses(any());
        doReturn(testPort).when(serverSpy).getPort();
        // Test reachability.
        boolean reachable = serverSpy.isReachable();
        assertTrue(reachable);
    }
}
Also used : LdapServer(opengrok.auth.plugin.ldap.LdapServer) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test)

Example 7 with LdapServer

use of opengrok.auth.plugin.ldap.LdapServer in project OpenGrok by OpenGrok.

the class LdapServerTest method testsReachableNegative.

@Test
void testsReachableNegative() throws Exception {
    InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
    LdapServer server = new LdapServer("ldaps://foo.bar.com");
    LdapServer serverSpy = Mockito.spy(server);
    doReturn(new InetAddress[] { loopbackAddress }).when(serverSpy).getAddresses(any());
    // port 0 should not be reachable.
    doReturn(0).when(serverSpy).getPort();
    assertFalse(serverSpy.isReachable());
}
Also used : LdapServer(opengrok.auth.plugin.ldap.LdapServer) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test)

Example 8 with LdapServer

use of opengrok.auth.plugin.ldap.LdapServer in project OpenGrok by OpenGrok.

the class ConfigurationTest method testEncodeDecode.

@Test
void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<>();
    ExceptionListener listener = exceptions::addLast;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Configuration configuration1 = new Configuration();
    configuration1.setInterval(500);
    configuration1.setSearchTimeout(1000);
    configuration1.setConnectTimeout(42);
    configuration1.setCountLimit(10);
    configuration1.setServers(new ArrayList<>(List.of(new LdapServer("http://server.com"))));
    WebHooks webHooks = new WebHooks();
    WebHook hook = new WebHook();
    hook.setContent("foo");
    hook.setURI("http://localhost:8080/source/api/v1/messages");
    webHooks.setFail(hook);
    configuration1.setWebHooks(webHooks);
    enc.writeObject(configuration1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Configuration configuration2 = (Configuration) dec.readObject();
    assertNotNull(configuration2);
    assertEquals(configuration1.getXMLRepresentationAsString(), configuration2.getXMLRepresentationAsString());
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
}
Also used : WebHook(opengrok.auth.plugin.util.WebHook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LdapServer(opengrok.auth.plugin.ldap.LdapServer) XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) WebHooks(opengrok.auth.plugin.util.WebHooks) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

LdapServer (opengrok.auth.plugin.ldap.LdapServer)8 Test (org.junit.jupiter.api.Test)8 InetAddress (java.net.InetAddress)2 ExceptionListener (java.beans.ExceptionListener)1 XMLDecoder (java.beans.XMLDecoder)1 XMLEncoder (java.beans.XMLEncoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 LinkedList (java.util.LinkedList)1 WebHook (opengrok.auth.plugin.util.WebHook)1 WebHooks (opengrok.auth.plugin.util.WebHooks)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1