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));
}
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;
}
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);
}
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());
}
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());
}
Aggregations