001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/auth/TestChallengeProcessor.java,v 1.1 2004/03/25 20:37:20 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient.auth;
033:
034: import java.util.ArrayList;
035: import java.util.HashMap;
036: import java.util.List;
037: import java.util.Map;
038:
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: import org.apache.commons.httpclient.params.DefaultHttpParams;
044: import org.apache.commons.httpclient.params.HttpParams;
045:
046: /**
047: * Unit tests for {@link testParsingChallenge}.
048: *
049: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
050: */
051: public class TestChallengeProcessor extends TestCase {
052:
053: // ------------------------------------------------------------ Constructor
054: public TestChallengeProcessor(String testName) {
055: super (testName);
056: }
057:
058: // ------------------------------------------------------------------- Main
059: public static void main(String args[]) {
060: String[] testCaseName = { TestChallengeProcessor.class
061: .getName() };
062: junit.textui.TestRunner.main(testCaseName);
063: }
064:
065: // ------------------------------------------------------- TestCase Methods
066:
067: public static Test suite() {
068: return new TestSuite(TestChallengeProcessor.class);
069: }
070:
071: public void testChallengeSelection() throws Exception {
072: List authPrefs = new ArrayList(3);
073: authPrefs.add(AuthPolicy.NTLM);
074: authPrefs.add(AuthPolicy.DIGEST);
075: authPrefs.add(AuthPolicy.BASIC);
076: HttpParams httpparams = new DefaultHttpParams();
077: httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
078: authPrefs);
079:
080: AuthChallengeProcessor processor = new AuthChallengeProcessor(
081: httpparams);
082:
083: Map map = new HashMap();
084: map.put("unknown", "unknown realm=\"whatever\"");
085: map.put("basic", "basic realm=\"whatever\"");
086:
087: AuthScheme authscheme = processor.selectAuthScheme(map);
088: assertTrue(authscheme instanceof BasicScheme);
089: }
090:
091: public void testInvalidChallenge() throws Exception {
092: List authPrefs = new ArrayList(3);
093: authPrefs.add("unsupported1");
094: authPrefs.add("unsupported2");
095: HttpParams httpparams = new DefaultHttpParams();
096: httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
097: authPrefs);
098:
099: AuthChallengeProcessor processor = new AuthChallengeProcessor(
100: httpparams);
101:
102: Map map = new HashMap();
103: map.put("unsupported1", "unsupported1 realm=\"whatever\"");
104: map.put("unsupported2", "unsupported2 realm=\"whatever\"");
105: try {
106: AuthScheme authscheme = processor.selectAuthScheme(map);
107: fail("AuthChallengeException should have been thrown");
108: } catch (AuthChallengeException e) {
109: //ignore
110: }
111: }
112:
113: public void testUnsupportedChallenge() throws Exception {
114: List authPrefs = new ArrayList(3);
115: authPrefs.add(AuthPolicy.NTLM);
116: authPrefs.add(AuthPolicy.BASIC);
117: authPrefs.add(AuthPolicy.DIGEST);
118: HttpParams httpparams = new DefaultHttpParams();
119: httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
120: authPrefs);
121:
122: AuthChallengeProcessor processor = new AuthChallengeProcessor(
123: httpparams);
124:
125: Map map = new HashMap();
126: map.put("unsupported1", "unsupported1 realm=\"whatever\"");
127: map.put("unsupported2", "unsupported2 realm=\"whatever\"");
128:
129: try {
130: AuthScheme authscheme = processor.selectAuthScheme(map);
131: fail("AuthChallengeException should have been thrown");
132: } catch (AuthChallengeException e) {
133: //expected
134: }
135: }
136:
137: public void testChallengeProcessing() throws Exception {
138: HttpParams httpparams = new DefaultHttpParams();
139: AuthChallengeProcessor processor = new AuthChallengeProcessor(
140: httpparams);
141:
142: Map map = new HashMap();
143: map.put("basic", "basic realm=\"whatever\", param=\"value\"");
144:
145: AuthState authstate = new AuthState();
146:
147: AuthScheme authscheme = processor.processChallenge(authstate,
148: map);
149: assertTrue(authscheme instanceof BasicScheme);
150: assertEquals("whatever", authscheme.getRealm());
151: assertEquals(authscheme, authstate.getAuthScheme());
152: assertEquals("value", authscheme.getParameter("param"));
153: }
154:
155: public void testInvalidChallengeProcessing() throws Exception {
156: HttpParams httpparams = new DefaultHttpParams();
157: AuthChallengeProcessor processor = new AuthChallengeProcessor(
158: httpparams);
159:
160: Map map = new HashMap();
161: map.put("basic", "basic realm=\"whatever\", param=\"value\"");
162:
163: AuthState authstate = new AuthState();
164:
165: AuthScheme authscheme = processor.processChallenge(authstate,
166: map);
167: assertTrue(authscheme instanceof BasicScheme);
168: assertEquals("whatever", authscheme.getRealm());
169: assertEquals(authscheme, authstate.getAuthScheme());
170: assertEquals("value", authscheme.getParameter("param"));
171:
172: Map map2 = new HashMap();
173: map2.put("ntlm", "NTLM");
174: try {
175: // Basic authentication scheme expected
176: authscheme = processor.processChallenge(authstate, map2);
177: fail("AuthenticationException should have been thrown");
178: } catch (AuthenticationException e) {
179: //expected
180: }
181: }
182: }
|