tests for RouteDirector at 100% coverage, fixed two bugs

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@550077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roland Weber 2007-06-23 18:25:33 +00:00
parent 0f0128450b
commit 3fc997fbed
3 changed files with 510 additions and 3 deletions

View File

@ -139,9 +139,14 @@ public class RouteDirector {
// consider it here until there is a real-life use case for it.
// Should we tolerate if security is better than planned?
// (plan.isSecure() && !fact.isSecure())
if (plan.isSecure() != fact.isSecure())
return UNREACHABLE;
// yes, this would cover the two checks above as well...
if (!plan.equals(fact))
// Local address has to match only if the plan specifies one.
if ((plan.getLocalAddress() != null) &&
!plan.getLocalAddress().equals(fact.getLocalAddress())
)
return UNREACHABLE;
return COMPLETE;
@ -168,7 +173,7 @@ public class RouteDirector {
if (phc < fhc)
return UNREACHABLE;
for (int i=0; i<phc-1; i++) {
for (int i=0; i<fhc-1; i++) {
if (!plan.getHopTarget(i).equals(fact.getHopTarget(i)))
return UNREACHABLE;
}
@ -188,6 +193,7 @@ public class RouteDirector {
// tunnel and layering are the same, remains to check the security
// Should we tolerate if security is better than planned?
// (plan.isSecure() && !fact.isSecure())
if (plan.isSecure() != fact.isSecure())
return UNREACHABLE;

View File

@ -44,6 +44,7 @@ public class TestAllConn extends TestCase {
TestSuite suite = new TestSuite();
suite.addTest(TestHttpRoute.suite());
suite.addTest(TestRouteDirector.suite());
suite.addTest(TestScheme.suite());
return suite;

View File

@ -0,0 +1,500 @@
/*
* $HeadURL$
* $Revision$
* $Date$
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.conn;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.http.HttpHost;
/**
* Tests for <code>RouteDirector</code>.
*/
public class TestRouteDirector extends TestCase {
// a selection of constants for generating routes
public final static
HttpHost TARGET1 = new HttpHost("target1.test.invalid");
public final static
HttpHost TARGET2 = new HttpHost("target2.test.invalid", 8080);
// It is not necessary to have extra targets for https.
// The 'layered' and 'secure' flags are specified explicitly
// for routes, they will not be determined from the scheme.
public final static
HttpHost PROXY1 = new HttpHost("proxy1.test.invalid");
public final static
HttpHost PROXY2 = new HttpHost("proxy2.test.invalid", 1080);
public final static
HttpHost PROXY3 = new HttpHost("proxy3.test.invalid", 88);
public final static InetAddress LOCAL41;
public final static InetAddress LOCAL42;
public final static InetAddress LOCAL61;
public final static InetAddress LOCAL62;
// need static initializer to deal with exceptions
static {
try {
LOCAL41 = InetAddress.getByAddress(new byte[]{ 127, 0, 0, 1 });
LOCAL42 = InetAddress.getByAddress(new byte[]{ 127, 0, 0, 2 });
LOCAL61 = InetAddress.getByAddress(new byte[]{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
});
LOCAL62 = InetAddress.getByAddress(new byte[]{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
});
} catch (Exception x) {
throw new ExceptionInInitializerError(x);
}
}
public TestRouteDirector(String testName) {
super(testName);
}
public static void main(String args[]) {
String[] testCaseName = { TestRouteDirector.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
public static Test suite() {
return new TestSuite(TestRouteDirector.class);
}
public void testIllegal() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route = new HttpRoute(TARGET1);
try {
rowdy.nextStep(null, route);
fail("null argument not detected");
} catch (IllegalArgumentException iax) {
// expected
}
}
public void testDirect() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route1 = new HttpRoute(TARGET1);
HttpRoute route2 = new HttpRoute(TARGET2);
HttpRoute route1p1 = new HttpRoute(TARGET1, null, PROXY1, false);
int step = rowdy.nextStep(route1, null);
assertEquals("wrong step to route1",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route2, null);
assertEquals("wrong step to route2",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1, route1);
assertEquals("complete route1 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route2, route2);
assertEquals("complete route2 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1, route2);
assertEquals("unreachable target not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1, route1p1);
assertEquals("invalid proxy not detected",
RouteDirector.UNREACHABLE, step);
}
public void testProxy() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route1p1 = new HttpRoute(TARGET1, null, PROXY1, false);
HttpRoute route1p2 = new HttpRoute(TARGET1, null, PROXY2, false);
HttpRoute route2p1 = new HttpRoute(TARGET2, null, PROXY1, false);
HttpRoute route0 = new HttpRoute(PROXY1);
HttpRoute route1 = new HttpRoute(TARGET1);
int step = rowdy.nextStep(route1p1, null);
assertEquals("wrong step to route1p1",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1p2, null);
assertEquals("wrong step to route1p2",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1p1, route1p1);
assertEquals("complete route1p1 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1p2, route1p2);
assertEquals("complete route1p2 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route2p1, route2p1);
assertEquals("complete route2p1 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1p1, route1p2);
assertEquals("unreachable route1p1 via route1p2 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1p1, route2p1);
assertEquals("unreachable route1p1 via route2p1 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1p1, route0);
assertEquals("unreachable route1p1 via route0 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1p1, route1);
assertEquals("unreachable route1p1 via route1 not detected",
RouteDirector.UNREACHABLE, step);
}
public void testProxyChain() {
HttpHost[] chainA = { PROXY1 };
HttpHost[] chainB = { PROXY1, PROXY2 };
HttpHost[] chainC = { PROXY2, PROXY1 };
HttpHost[] chainD = { PROXY2 };
RouteDirector rowdy = new RouteDirector();
HttpRoute route1cA = new HttpRoute(TARGET1, null, chainA,
false, false, false);
HttpRoute route1cB = new HttpRoute(TARGET1, null, chainB,
false, false, false);
HttpRoute route1cC = new HttpRoute(TARGET1, null, chainC,
false, false, false);
HttpRoute route1cD = new HttpRoute(TARGET1, null, chainC,
false, false, false);
HttpRoute route1c0 = new HttpRoute(TARGET1, null, false);
int step = rowdy.nextStep(route1cA, null);
assertEquals("wrong step to route1cA",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1cB, null);
assertEquals("wrong step to route1cB",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1cC, null);
assertEquals("wrong step to route1cC",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1cD, null);
assertEquals("wrong step to route1cD",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1cB, route1cA);
assertEquals("wrong step to route 1cB from 1cA",
RouteDirector.TUNNEL_PROXY, step);
step = rowdy.nextStep(route1cB, route1cB);
assertEquals("complete route 1cB not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1cB, route1cC);
assertEquals("unreachable route 1cB from 1cC not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1cB, route1cD);
assertEquals("unreachable route 1cB from 1cD not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1cA, route1cB);
assertEquals("unreachable route 1cA from 1cB not detected",
RouteDirector.UNREACHABLE, step);
}
public void testLocalDirect() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route1l41 = new HttpRoute(TARGET1, LOCAL41, false);
HttpRoute route1l42 = new HttpRoute(TARGET1, LOCAL42, false);
HttpRoute route1l61 = new HttpRoute(TARGET1, LOCAL61, false);
HttpRoute route1l00 = new HttpRoute(TARGET1, null, false);
int step = rowdy.nextStep(route1l41, null);
assertEquals("wrong step to route1l41",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1l42, null);
assertEquals("wrong step to route1l42",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1l61, null);
assertEquals("wrong step to route1l61",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1l00, null);
assertEquals("wrong step to route1l00",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1l41, route1l41);
assertEquals("complete route1l41 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l42, route1l42);
assertEquals("complete route1l42 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l61, route1l61);
assertEquals("complete route1l61 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l00, route1l00);
assertEquals("complete route1l00 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l41, route1l42);
assertEquals("unreachable route1l41 via route1l42 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1l41, route1l61);
assertEquals("unreachable route1l41 via route1l61 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1l41, route1l00);
assertEquals("unreachable route1l41 via route1l00 not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1l00, route1l41);
assertEquals("complete route1l00 as route1l41 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l00, route1l42);
assertEquals("complete route1l00 as route1l42 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1l00, route1l61);
assertEquals("complete route1l00 as route1l61 not detected",
RouteDirector.COMPLETE, step);
}
public void testDirectSecure() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route1u = new HttpRoute(TARGET1, null, false);
HttpRoute route1s = new HttpRoute(TARGET1, null, true);
HttpRoute route1p1u = new HttpRoute(TARGET1, null, PROXY1, false);
HttpRoute route1p1s = new HttpRoute(TARGET1, null, PROXY1, true);
int step = rowdy.nextStep(route1u, null);
assertEquals("wrong step to route1u",
RouteDirector.CONNECT_TARGET, step);
step = rowdy.nextStep(route1s, null);
assertEquals("wrong step to route1s",
RouteDirector.CONNECT_TARGET, step);
// unrequested security is currently not tolerated
step = rowdy.nextStep(route1u, route1s);
assertEquals("unreachable route 1u from 1s not detected",
RouteDirector.UNREACHABLE, step);
// secure layering of direct connections is currently not supported
step = rowdy.nextStep(route1s, route1u);
assertEquals("unreachable route 1s from 1u not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1s, route1p1u);
assertEquals("unreachable route 1s from 1p1u not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1s, route1p1s);
assertEquals("unreachable route 1s from 1p1s not detected",
RouteDirector.UNREACHABLE, step);
}
public void testProxyTLS() {
RouteDirector rowdy = new RouteDirector();
HttpRoute route1 = new HttpRoute(TARGET1, null, PROXY1,
false, false, false);
HttpRoute route1t = new HttpRoute(TARGET1, null, PROXY1,
false, true, false);
HttpRoute route1tl = new HttpRoute(TARGET1, null, PROXY1,
false, true, true);
HttpRoute route1s = new HttpRoute(TARGET1, null, PROXY1,
true, false, false);
HttpRoute route1ts = new HttpRoute(TARGET1, null, PROXY1,
true, true, false);
HttpRoute route1tls = new HttpRoute(TARGET1, null, PROXY1,
true, true, true);
// we don't consider a route that is layered but not tunnelled
int step = rowdy.nextStep(route1, null);
assertEquals("wrong step to route1",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1t, null);
assertEquals("wrong step to route1t",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1tl, null);
assertEquals("wrong step to route1tl",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1s, null);
assertEquals("wrong step to route1s",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1ts, null);
assertEquals("wrong step to route1ts",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1tls, null);
assertEquals("wrong step to route1tls",
RouteDirector.CONNECT_PROXY, step);
step = rowdy.nextStep(route1, route1);
assertEquals("complete route1 not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1t, route1t);
assertEquals("complete route1t not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1tl, route1tl);
assertEquals("complete route1tl not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1s, route1s);
assertEquals("complete route1s not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1ts, route1ts);
assertEquals("complete route1ts not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1tls, route1tls);
assertEquals("complete route1tls not detected",
RouteDirector.COMPLETE, step);
step = rowdy.nextStep(route1, route1t);
assertEquals("unreachable route1 from 1t not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1, route1tl);
assertEquals("unreachable route1 from 1tl not detected",
RouteDirector.UNREACHABLE, step);
// unrequested security is currently not tolerated
step = rowdy.nextStep(route1, route1s);
assertEquals("unreachable route1 from 1s not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1, route1ts);
assertEquals("unreachable route1 from 1ts not detected",
RouteDirector.UNREACHABLE, step);
step = rowdy.nextStep(route1, route1tls);
assertEquals("unreachable route1 from 1tls not detected",
RouteDirector.UNREACHABLE, step);
// securing requires layering
step = rowdy.nextStep(route1s, route1);
assertEquals("unreachable route1s from 1 not detected",
RouteDirector.UNREACHABLE, step);
// securing requires layering, and multiple layers are not supported
step = rowdy.nextStep(route1tls, route1tl);
assertEquals("unreachable route1tls from 1tl not detected",
RouteDirector.UNREACHABLE, step);
// cases where tunnelling to the target is required
step = rowdy.nextStep(route1t, route1);
assertEquals("wrong step to route1t from 1",
RouteDirector.TUNNEL_TARGET, step);
step = rowdy.nextStep(route1tl, route1);
assertEquals("wrong step to route1tl from 1",
RouteDirector.TUNNEL_TARGET, step);
step = rowdy.nextStep(route1tls, route1);
assertEquals("wrong step to route1tls from 1",
RouteDirector.TUNNEL_TARGET, step);
// cases where layering on the tunnel is required
step = rowdy.nextStep(route1tl, route1t);
assertEquals("wrong step to route1tl from 1t",
RouteDirector.LAYER_PROTOCOL, step);
step = rowdy.nextStep(route1tl, route1ts);
assertEquals("wrong step to route1tl from 1ts",
RouteDirector.LAYER_PROTOCOL, step);
step = rowdy.nextStep(route1tls, route1t);
assertEquals("wrong step to route1tls from 1t",
RouteDirector.LAYER_PROTOCOL, step);
step = rowdy.nextStep(route1tls, route1ts);
assertEquals("wrong step to route1tls from 1ts",
RouteDirector.LAYER_PROTOCOL, step);
// There are some odd cases left over, like having a secure tunnel
// that becomes unsecure by layering, or a secure connection to a
// proxy that becomes unsecure by tunnelling to another proxy.
}
} // class TestRouteDirector