1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient.auth;
32  
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  import junit.framework.Test;
39  import junit.framework.TestCase;
40  import junit.framework.TestSuite;
41  
42  import org.apache.commons.httpclient.params.DefaultHttpParams;
43  import org.apache.commons.httpclient.params.HttpParams;
44  
45  /***
46   * Unit tests for {@link testParsingChallenge}.
47   *
48   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
49   */
50  public class TestChallengeProcessor extends TestCase {
51  
52      // ------------------------------------------------------------ Constructor
53      public TestChallengeProcessor(String testName) {
54          super(testName);
55      }
56  
57      // ------------------------------------------------------------------- Main
58      public static void main(String args[]) {
59          String[] testCaseName = { TestChallengeProcessor.class.getName() };
60          junit.textui.TestRunner.main(testCaseName);
61      }
62  
63      // ------------------------------------------------------- TestCase Methods
64  
65      public static Test suite() {
66          return new TestSuite(TestChallengeProcessor.class);
67      }
68  
69  
70      public void testChallengeSelection() throws Exception {
71          List authPrefs = new ArrayList(3);
72          authPrefs.add(AuthPolicy.NTLM);
73          authPrefs.add(AuthPolicy.DIGEST);
74          authPrefs.add(AuthPolicy.BASIC);
75          HttpParams httpparams = new DefaultHttpParams(); 
76          httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
77          
78          AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
79  
80          Map map = new HashMap(); 
81          map.put("unknown", "unknown realm=\"whatever\"");
82          map.put("basic", "basic realm=\"whatever\"");
83          
84          AuthScheme authscheme = processor.selectAuthScheme(map);
85          assertTrue(authscheme instanceof BasicScheme);
86      }
87  
88  
89      public void testInvalidChallenge() throws Exception {
90          List authPrefs = new ArrayList(3);
91          authPrefs.add("unsupported1");
92          authPrefs.add("unsupported2");
93          HttpParams httpparams = new DefaultHttpParams(); 
94          httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
95          
96          AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
97  
98          Map map = new HashMap(); 
99          map.put("unsupported1", "unsupported1 realm=\"whatever\"");
100         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
101         try {
102             AuthScheme authscheme = processor.selectAuthScheme(map);
103             fail("AuthChallengeException should have been thrown");
104         } catch (AuthChallengeException e) {
105             //ignore
106         }
107     }
108 
109 
110     public void testUnsupportedChallenge() throws Exception {
111         List authPrefs = new ArrayList(3);
112         authPrefs.add(AuthPolicy.NTLM);
113         authPrefs.add(AuthPolicy.BASIC);
114         authPrefs.add(AuthPolicy.DIGEST);
115         HttpParams httpparams = new DefaultHttpParams(); 
116         httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
117         
118         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
119 
120         Map map = new HashMap(); 
121         map.put("unsupported1", "unsupported1 realm=\"whatever\"");
122         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
123         
124         try {
125             AuthScheme authscheme = processor.selectAuthScheme(map);
126             fail("AuthChallengeException should have been thrown");
127         } catch (AuthChallengeException e) {
128             //expected
129         }
130     }
131 
132     public void testChallengeProcessing() throws Exception {
133         HttpParams httpparams = new DefaultHttpParams(); 
134         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
135 
136         Map map = new HashMap(); 
137         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
138         
139         AuthState authstate = new AuthState();
140         
141         AuthScheme authscheme = processor.processChallenge(authstate, map);
142         assertTrue(authscheme instanceof BasicScheme);
143         assertEquals("whatever", authscheme.getRealm());
144         assertEquals(authscheme, authstate.getAuthScheme());
145         assertEquals("value", authscheme.getParameter("param"));
146     }
147 
148     public void testInvalidChallengeProcessing() throws Exception {
149         HttpParams httpparams = new DefaultHttpParams(); 
150         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
151 
152         Map map = new HashMap(); 
153         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
154         
155         AuthState authstate = new AuthState();
156         
157         AuthScheme authscheme = processor.processChallenge(authstate, map);
158         assertTrue(authscheme instanceof BasicScheme);
159         assertEquals("whatever", authscheme.getRealm());
160         assertEquals(authscheme, authstate.getAuthScheme());
161         assertEquals("value", authscheme.getParameter("param"));
162 
163         Map map2 = new HashMap(); 
164         map2.put("ntlm", "NTLM");
165         try {
166             // Basic authentication scheme expected
167             authscheme = processor.processChallenge(authstate, map2);
168             fail("AuthenticationException should have been thrown");
169         } catch (AuthenticationException e) {
170             //expected
171         }
172     }
173 }