Search in sources :

Example 6 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class ImapLoadBalancingMechanismTest method ClientIpHashMechanismIpHashFromPool.

@Test
public void ClientIpHashMechanismIpHashFromPool() throws Exception {
    ImapLoadBalancingMechanism mech = ImapLoadBalancingMechanism.newInstance(ImapLoadBalancingMechanism.ImapLBMech.ClientIpHash.name());
    ArrayList<Server> pool = new ArrayList<Server>();
    Server s0 = new MockServer("server-0", "0");
    Server s1 = new MockServer("server-1", "1");
    Server s2 = new MockServer("server-2", "2");
    // Add to pool out-of-order to verify that we are sorting correctly
    pool.add(s1);
    pool.add(s0);
    pool.add(s2);
    HashMap<String, String> headers = new HashMap<String, String>();
    /**
		 * For IPV4, Java uses the 32bit value of an IPV4 address as the hashCode of an IPV4
		 * address, so with the current test setup, and a pool size of 3:
		 *   127.0.0.2 == (0x7f << 24) + 2 == 2130706434
		 * And 2130706434 % 3 = 0
		 * So 127.0.0.2 should return server 0 in our 3 node pool,
		 *    127.0.0.3 should return server 1 in our 3 node pool,
		 *    127.0.0.4 should return server 2 in our 3 node pool
		 */
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.2");
    HttpServletRequest req = new MockHttpServletRequest(null, null, null, 123, "127.0.0.1", headers);
    Assert.assertEquals(s0, mech.getImapServerFromPool(req, pool));
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.3");
    Assert.assertEquals(s1, mech.getImapServerFromPool(req, pool));
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.4");
    Assert.assertEquals(s2, mech.getImapServerFromPool(req, pool));
    /* Verify we get the same results using IPV6 */
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.2");
    Assert.assertEquals(s0, mech.getImapServerFromPool(req, pool));
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.3");
    Assert.assertEquals(s1, mech.getImapServerFromPool(req, pool));
    headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.4");
    Assert.assertEquals(s2, mech.getImapServerFromPool(req, pool));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) MockServer(com.zimbra.cs.account.MockServer) Server(com.zimbra.cs.account.Server) MockServer(com.zimbra.cs.account.MockServer) HashMap(java.util.HashMap) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class ServiceTestUtil method getRequestContext.

public static Map<String, Object> getRequestContext(Account authAcct, Account targetAcct, DocumentService service) throws Exception {
    Map<String, Object> context = new HashMap<String, Object>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(authAcct), targetAcct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
    context.put(SoapServlet.SERVLET_REQUEST, new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), ""));
    context.put(SoapEngine.ZIMBRA_ENGINE, new MockSoapEngine(service));
    context.put(SoapServlet.SERVLET_RESPONSE, new MockHttpServletResponse());
    return context;
}
Also used : MockSoapEngine(com.zimbra.soap.MockSoapEngine) HashMap(java.util.HashMap) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) MockHttpServletResponse(com.zimbra.cs.service.MockHttpServletResponse)

Example 8 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class TestRemoteIP method testTrustedIPLogString.

@Test
public void testTrustedIPLogString() throws Exception {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
    headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
    headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
    RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
    remoteIp.addToLoggingContext();
    String updatedLogContext = ZimbraLog.getContextString();
    assertTrue(updatedLogContext.indexOf("oip=172.16.150.11") > -1);
    assertTrue(updatedLogContext.indexOf("oport=8080") > -1);
    assertTrue(updatedLogContext.indexOf("ip=172.16.150.11") > -1);
    assertTrue(updatedLogContext.indexOf("port=8080") > -1);
    assertTrue(updatedLogContext.indexOf("oproto=IMAP") > -1);
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 9 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class TestRemoteIP method testNonTrustedClientIPRemoteIP.

@Test
public void testNonTrustedClientIPRemoteIP() throws UnsupportedEncodingException, MalformedURLException {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "10.10.1.1", headers);
    RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
    // we should ignore X-Forwarded-XXX headers from non-trusted clients
    assertNull("originating IP should be null", remoteIp.getOrigIP());
    assertNull("originating port should be null", remoteIp.getOrigPort());
    assertNull("originating protocol should be null", remoteIp.getOrigProto());
    assertEquals("wrong request IP", "10.10.1.1", remoteIp.getRequestIP());
    assertEquals("wrong request port", "80", remoteIp.getRequestPort().toString());
    assertEquals("wrong client IP", "10.10.1.1", remoteIp.getClientIP());
    assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 10 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class TestRemoteIP method testXForwardedHeaders.

@Test
public void testXForwardedHeaders() throws UnsupportedEncodingException, MalformedURLException {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
    headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
    headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
    RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
    assertEquals("wrong originating IP", "172.16.150.11", remoteIp.getOrigIP());
    assertEquals("wrong originating port", "8080", remoteIp.getOrigPort().toString());
    assertEquals("wrong originating protocol", "IMAP", remoteIp.getOrigProto());
    assertEquals("wrong request IP", "172.16.150.11", remoteIp.getRequestIP());
    assertEquals("wrong request port", "8080", remoteIp.getRequestPort().toString());
    assertEquals("wrong client IP", "192.168.1.1", remoteIp.getClientIP());
    assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (com.zimbra.cs.service.MockHttpServletRequest)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 URL (java.net.URL)9 RemoteIP (com.zimbra.common.util.RemoteIP)5 MockSoapEngine (com.zimbra.soap.MockSoapEngine)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 Element (com.zimbra.common.soap.Element)2 Account (com.zimbra.cs.account.Account)2 MockServer (com.zimbra.cs.account.MockServer)2 Server (com.zimbra.cs.account.Server)2 MockHttpServletResponse (com.zimbra.cs.service.MockHttpServletResponse)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Folder (com.zimbra.cs.mailbox.Folder)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Ignore (org.junit.Ignore)1