olegk 2004/10/31 05:46:54
Modified: httpclient/src/test/org/apache/commons/httpclient
TestIdleConnectionTimeout.java TestNoHost.java
Added: httpclient/src/test/org/apache/commons/httpclient
FakeHttpMethod.java TestCredentials.java
httpclient/src/test/org/apache/commons/httpclient/auth
TestAuthAll.java TestDigestAuth.java
TestNTLMAuth.java
Removed: httpclient/src/test/org/apache/commons/httpclient
TestAuthenticator.java TestNoHostBase.java
Log:
* NTLM & Digest authentication scheme test cases refactored
* TestAuthenticator test case removed
Contributed by Oleg Kalnichevski
Revision Changes Path
1.4 +6 -4
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java
Index: TestIdleConnectionTimeout.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestIdleConnectionTimeout.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestIdleConnectionTimeout.java 5 Jul 2004 22:46:59 -0000 1.3
+++ TestIdleConnectionTimeout.java 31 Oct 2004 13:46:54 -0000 1.4
@@ -28,13 +28,15 @@
*/
package org.apache.commons.httpclient;
+import junit.framework.TestCase;
+
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.util.IdleConnectionHandler;
import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
/**
*/
-public class TestIdleConnectionTimeout extends TestNoHostBase {
+public class TestIdleConnectionTimeout extends TestCase {
/**
*
*/
1.44 +7 -11
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
Index: TestNoHost.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- TestNoHost.java 7 Oct 2004 16:14:15 -0000 1.43
+++ TestNoHost.java 31 Oct 2004 13:46:54 -0000 1.44
@@ -31,9 +31,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.commons.httpclient.auth.TestBasicAuth;
-import org.apache.commons.httpclient.auth.TestChallengeParser;
-import org.apache.commons.httpclient.auth.TestChallengeProcessor;
+import org.apache.commons.httpclient.auth.TestAuthAll;
import org.apache.commons.httpclient.cookie.TestCookieAll;
import org.apache.commons.httpclient.params.TestHttpParams;
@@ -79,10 +77,8 @@
suite.addTest(TestHttpState.suite());
suite.addTest(TestCookieAll.suite());
// Authentication
- suite.addTest(TestChallengeParser.suite());
- suite.addTest(TestChallengeProcessor.suite());
- suite.addTest(TestAuthenticator.suite());
- suite.addTest(TestBasicAuth.suite());
+ suite.addTest(TestCredentials.suite());
+ suite.addTest(TestAuthAll.suite());
// Redirects
suite.addTest(TestRedirects.suite());
// Connection management
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/FakeHttpMethod.java
Index: FakeHttpMethod.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/FakeHttpMethod.java,v
1.1 2004/10/31 13:46:54 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 13:46:54 $
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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.commons.httpclient;
/**
* For test-nohost testing purposes only.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a>
*/
public class FakeHttpMethod extends HttpMethodBase{
public FakeHttpMethod(){
super();
}
public FakeHttpMethod(String path){
super(path);
}
public String getName() {
return "Simple";
}
public void addResponseHeader(final Header header) {
getResponseHeaderGroup().addHeader(header);
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCredentials.java
Index: TestCredentials.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCredentials.java,v
1.1 2004/10/31 13:46:54 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 13:46:54 $
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit tests for [EMAIL PROTECTED] Credentials}.
*
* @author Rodney Waldhoff
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a>
* @version $Id: TestCredentials.java,v 1.1 2004/10/31 13:46:54 olegk Exp $
*/
public class TestCredentials extends TestCase {
// ------------------------------------------------------------ Constructor
public TestCredentials(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestCredentials.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestCredentials.class);
}
public void testCredentialConstructors() {
try {
new UsernamePasswordCredentials(null, null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) {
// expected
}
try {
new NTCredentials("user", "password", null, null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) {
// expected
}
try {
new NTCredentials("user", "password", "host", null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) {
// expected
}
NTCredentials creds = new NTCredentials("user", null, "host", "domain");
assertNotNull(creds.getUserName());
assertNull(creds.getPassword());
assertNotNull(creds.getDomain());
assertNotNull(creds.getHost());
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestAuthAll.java
Index: TestAuthAll.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestAuthAll.java,v
1.1 2004/10/31 13:46:54 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 13:46:54 $
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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.commons.httpclient.auth;
import junit.framework.*;
/**
* @author Oleg Kalnichevski
*
* @version $Id: TestAuthAll.java,v 1.1 2004/10/31 13:46:54 olegk Exp $
*/
public class TestAuthAll extends TestCase {
public TestAuthAll(String testName) {
super(testName);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestChallengeParser.suite());
suite.addTest(TestChallengeProcessor.suite());
suite.addTest(TestBasicAuth.suite());
suite.addTest(TestDigestAuth.suite());
suite.addTest(TestNTLMAuth.suite());
return suite;
}
public static void main(String args[]) {
String[] testCaseName = { TestAuthAll.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestDigestAuth.java
Index: TestDigestAuth.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestDigestAuth.java,v
1.1 2004/10/31 13:46:54 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 13:46:54 $
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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.commons.httpclient.auth;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.FakeHttpMethod;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.server.HttpService;
import org.apache.commons.httpclient.server.RequestLine;
import org.apache.commons.httpclient.server.SimpleHttpServer;
import org.apache.commons.httpclient.server.SimpleRequest;
import org.apache.commons.httpclient.server.SimpleResponse;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test Methods for DigestScheme Authentication.
*
* @author Rodney Waldhoff
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Oleg Kalnichevski</a>
*/
public class TestDigestAuth extends TestCase {
// ------------------------------------------------------------ Constructor
public TestDigestAuth(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestDigestAuth.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestDigestAuth.class);
}
public void testDigestAuthenticationWithNoRealm() throws Exception {
String challenge = "Digest";
try {
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
fail("Should have thrown MalformedChallengeException");
} catch(MalformedChallengeException e) {
// expected
}
}
public void testDigestAuthenticationWithNoRealm2() throws Exception {
String challenge = "Digest ";
try {
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
fail("Should have thrown MalformedChallengeException");
} catch(MalformedChallengeException e) {
// expected
}
}
public void testDigestAuthenticationWithDefaultCreds() throws Exception {
String challenge = "Digest realm=\"realm1\",
nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
FakeHttpMethod method = new FakeHttpMethod("/");
UsernamePasswordCredentials cred = new
UsernamePasswordCredentials("username","password");
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
Map table = AuthChallengeParser.extractParams(response);
assertEquals("username", table.get("username"));
assertEquals("realm1", table.get("realm"));
assertEquals("/", table.get("uri"));
assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}
public void testDigestAuthentication() throws Exception {
String challenge = "Digest realm=\"realm1\",
nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
UsernamePasswordCredentials cred = new
UsernamePasswordCredentials("username","password");
FakeHttpMethod method = new FakeHttpMethod("/");
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
Map table = AuthChallengeParser.extractParams(response);
assertEquals("username", table.get("username"));
assertEquals("realm1", table.get("realm"));
assertEquals("/", table.get("uri"));
assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response"));
}
public void testDigestAuthenticationWithMultipleRealms() throws Exception {
String challenge1 = "Digest realm=\"realm1\", nonce=\"abcde\"";
String challenge2 = "Digest realm=\"realm2\", nonce=\"123546\"";
UsernamePasswordCredentials cred = new
UsernamePasswordCredentials("username","password");
UsernamePasswordCredentials cred2 = new
UsernamePasswordCredentials("uname2","password2");
FakeHttpMethod method = new FakeHttpMethod("/");
AuthScheme authscheme1 = new DigestScheme();
authscheme1.processChallenge(challenge1);
String response1 = authscheme1.authenticate(cred, method);
Map table = AuthChallengeParser.extractParams(response1);
assertEquals("username", table.get("username"));
assertEquals("realm1", table.get("realm"));
assertEquals("/", table.get("uri"));
assertEquals("abcde", table.get("nonce"));
assertEquals("786f500303eac1478f3c2865e676ed68", table.get("response"));
AuthScheme authscheme2 = new DigestScheme();
authscheme2.processChallenge(challenge2);
String response2 = authscheme2.authenticate(cred2, method);
table = AuthChallengeParser.extractParams(response2);
assertEquals("uname2", table.get("username"));
assertEquals("realm2", table.get("realm"));
assertEquals("/", table.get("uri"));
assertEquals("123546", table.get("nonce"));
assertEquals("0283edd9ef06a38b378b3b74661391e9", table.get("response"));
}
/**
* Test digest authentication using the MD5-sess algorithm.
*/
public void testDigestAuthenticationMD5Sess() throws Exception {
// Example using Digest auth with MD5-sess
String realm="realm";
String username="username";
String password="password";
String nonce="e273f1776275974f1a120d8b92c5b3cb";
String challenge="Digest realm=\"" + realm + "\", "
+ "nonce=\"" + nonce + "\", "
+ "opaque=\"SomeString\", "
+ "stale=false, "
+ "algorithm=MD5-sess, "
+ "qop=\"auth,auth-int\""; // we pass both but expect auth to be used
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
FakeHttpMethod method = new FakeHttpMethod("/");
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
Map table = AuthChallengeParser.extractParams(response);
assertEquals(username, table.get("username"));
assertEquals(realm, table.get("realm"));
assertEquals("MD5-sess", table.get("algorithm"));
assertEquals("/", table.get("uri"));
assertEquals(nonce, table.get("nonce"));
assertEquals(1, Integer.parseInt((String) table.get("nc"),16));
assertTrue(null != table.get("cnonce"));
assertEquals("SomeString", table.get("opaque"));
assertEquals("auth", table.get("qop"));
//@TODO: add better check
assertTrue(null != table.get("response"));
}
/**
* Test digest authentication using the MD5-sess algorithm.
*/
public void testDigestAuthenticationMD5SessNoQop() throws Exception {
// Example using Digest auth with MD5-sess
String realm="realm";
String username="username";
String password="password";
String nonce="e273f1776275974f1a120d8b92c5b3cb";
String challenge="Digest realm=\"" + realm + "\", "
+ "nonce=\"" + nonce + "\", "
+ "opaque=\"SomeString\", "
+ "stale=false, "
+ "algorithm=MD5-sess";
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
FakeHttpMethod method = new FakeHttpMethod("/");
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
Map table = AuthChallengeParser.extractParams(response);
assertEquals(username, table.get("username"));
assertEquals(realm, table.get("realm"));
assertEquals("MD5-sess", table.get("algorithm"));
assertEquals("/", table.get("uri"));
assertEquals(nonce, table.get("nonce"));
assertTrue(null == table.get("nc"));
assertEquals("SomeString", table.get("opaque"));
assertTrue(null == table.get("qop"));
//@TODO: add better check
assertTrue(null != table.get("response"));
}
/**
* Test digest authentication with invalud qop value
*/
public void testDigestAuthenticationMD5SessInvalidQop() throws Exception {
// Example using Digest auth with MD5-sess
String realm="realm";
String username="username";
String password="password";
String nonce="e273f1776275974f1a120d8b92c5b3cb";
String challenge="Digest realm=\"" + realm + "\", "
+ "nonce=\"" + nonce + "\", "
+ "opaque=\"SomeString\", "
+ "stale=false, "
+ "algorithm=MD5-sess, "
+ "qop=\"jakarta\""; // jakarta is an invalid qop value
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
try {
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
fail("MalformedChallengeException exception expected due to invalid qop
value");
} catch(MalformedChallengeException e) {
}
}
private class StaleNonceService implements HttpService {
public StaleNonceService() {
super();
}
public boolean process(final SimpleRequest request, final SimpleResponse
response)
throws IOException
{
RequestLine requestLine = request.getRequestLine();
HttpVersion ver = requestLine.getHttpVersion();
Header auth = request.getFirstHeader("Authorization");
if (auth == null) {
response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
response.addHeader(new Header("WWW-Authenticate",
"Digest realm=\"realm1\", nonce=\"ABC123\""));
response.setBodyString("Authorization required");
return true;
} else {
Map table = AuthChallengeParser.extractParams(auth.getValue());
String nonce = (String)table.get("nonce");
if (nonce.equals("ABC123")) {
response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
response.addHeader(new Header("WWW-Authenticate",
"Digest realm=\"realm1\", nonce=\"321CBA\",
stale=\"true\""));
response.setBodyString("Authorization required");
return true;
} else {
response.setStatusLine(ver, HttpStatus.SC_OK);
response.setBodyString("Authorization successful");
return true;
}
}
}
}
public void testDigestAuthenticationWithStaleNonce() throws Exception {
// configure the server
SimpleHttpServer server = new SimpleHttpServer(); // use arbitrary port
server.setHttpService(new StaleNonceService());
// configure the client
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(
server.getLocalAddress(), server.getLocalPort(),
Protocol.getProtocol("http"));
client.getState().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("username","password"));
FakeHttpMethod httpget = new FakeHttpMethod("/");
try {
client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
Map table = AuthChallengeParser.extractParams(
httpget.getRequestHeader("Authorization").getValue());
assertEquals("username", table.get("username"));
assertEquals("realm1", table.get("realm"));
assertEquals("/", table.get("uri"));
assertEquals("321CBA", table.get("nonce"));
assertEquals("7f5948eefa115296e9279225041527b3", table.get("response"));
}
}
1.1
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java
Index: TestNTLMAuth.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java,v
1.1 2004/10/31 13:46:54 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 13:46:54 $
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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.commons.httpclient.auth;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.httpclient.FakeHttpMethod;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.server.HttpService;
import org.apache.commons.httpclient.server.RequestLine;
import org.apache.commons.httpclient.server.SimpleHttpServer;
import org.apache.commons.httpclient.server.SimpleRequest;
import org.apache.commons.httpclient.server.SimpleResponse;
/**
* Test Methods for NTLM Authentication.
*
* @author Rodney Waldhoff
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a>
* @version $Id: TestNTLMAuth.java,v 1.1 2004/10/31 13:46:54 olegk Exp $
*/
public class TestNTLMAuth extends TestCase {
// ------------------------------------------------------------ Constructor
public TestNTLMAuth(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestNTLMAuth.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestNTLMAuth.class);
}
// ---------------------------------
public void testNTLMAuthenticationResponse1() throws Exception {
String challenge = "NTLM";
String expected = "NTLM TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT" +
"1NURE9NQUlO";
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
FakeHttpMethod method = new FakeHttpMethod();
AuthScheme authscheme = new NTLMScheme(challenge);
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
assertEquals(expected, response);
assertFalse(authscheme.isComplete());
}
public void testNTLMAuthenticationResponse2() throws Exception {
String challenge =
"NTLM TlRMTVNTUAACAAAACgAKADAAAAAGgoEAPc4kP4LtCV8AAAAAAAAAAJ4AngA" +
"6AAAASU5UUkFFUEhPWAIAFABJAE4AVABSAEEARQBQAEgATwBYAAEAEgBCAE8AQQB" +
"SAEQAUgBPAE8ATQAEACgAaQBuAHQAcgBhAGUAcABoAG8AeAAuAGUAcABoAG8AeAA" +
"uAGMAbwBtAAMAPABCAG8AYQByAGQAcgBvAG8AbQAuAGkAbgB0AHIAYQBlAHAAaAB" +
"vAHgALgBlAHAAaABvAHgALgBjAG8AbQAAAAAA";
String expected = "NTLM TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgB" +
"AAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE" +
"9TVAaC+vLxUEHnUtpItj9Dp4kzwQfd61Lztg==";
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
FakeHttpMethod method = new FakeHttpMethod();
AuthScheme authscheme = new NTLMScheme(challenge);
authscheme.processChallenge(challenge);
String response = authscheme.authenticate(cred, method);
assertEquals(expected, response);
assertTrue(authscheme.isComplete());
}
private class NTLMAuthService implements HttpService {
public NTLMAuthService() {
super();
}
public boolean process(final SimpleRequest request, final SimpleResponse
response)
throws IOException
{
RequestLine requestLine = request.getRequestLine();
HttpVersion ver = requestLine.getHttpVersion();
Header auth = request.getFirstHeader("Authorization");
if (auth == null) {
response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
response.addHeader(new Header("WWW-Authenticate", "NTLM"));
response.setBodyString("Authorization required");
return true;
} else {
String authstr = auth.getValue();
if (authstr.equals("NTLM
TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT1NURE9NQUlO")) {
response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
response.addHeader(new Header("WWW-Authenticate",
"NTLM
TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA=="));
response.setBodyString("Authorization required");
return true;
} if (authstr.equals("NTLM
TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgBAAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE9TVJxndWIt46bHm11TPrt5Z6wrz7ziq04yRA=="))
{
response.setStatusLine(ver, HttpStatus.SC_OK);
response.setBodyString("Authorization successful");
return true;
} else {
response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
response.addHeader(new Header("WWW-Authenticate", "NTLM"));
response.setBodyString("Authorization required");
return true;
}
}
}
}
public void testNTLMAuthenticationRetry() throws Exception {
// configure the server
SimpleHttpServer server = new SimpleHttpServer(); // use arbitrary port
server.setHttpService(new NTLMAuthService());
// configure the client
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(
server.getLocalAddress(), server.getLocalPort(),
Protocol.getProtocol("http"));
client.getState().setCredentials(AuthScope.ANY,
new NTCredentials("username", "password", "host", "domain"));
FakeHttpMethod httpget = new FakeHttpMethod("/");
try {
client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNull(httpget.getResponseHeader("WWW-Authenticate"));
assertEquals(200, httpget.getStatusCode());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]