1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  package org.apache.commons.httpclient.auth;
33  
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  import junit.framework.Test;
40  import junit.framework.TestCase;
41  import junit.framework.TestSuite;
42  
43  import org.apache.commons.httpclient.params.DefaultHttpParams;
44  import org.apache.commons.httpclient.params.HttpParams;
45  
46  /**
47   * Unit tests for {@link testParsingChallenge}.
48   *
49   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
50   */
51  public class TestChallengeProcessor extends TestCase {
52  
53      
54      public TestChallengeProcessor(String testName) {
55          super(testName);
56      }
57  
58      
59      public static void main(String args[]) {
60          String[] testCaseName = { TestChallengeProcessor.class.getName() };
61          junit.textui.TestRunner.main(testCaseName);
62      }
63  
64      
65  
66      public static Test suite() {
67          return new TestSuite(TestChallengeProcessor.class);
68      }
69  
70  
71      public void testChallengeSelection() throws Exception {
72          List authPrefs = new ArrayList(3);
73          authPrefs.add(AuthPolicy.NTLM);
74          authPrefs.add(AuthPolicy.DIGEST);
75          authPrefs.add(AuthPolicy.BASIC);
76          HttpParams httpparams = new DefaultHttpParams(); 
77          httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
78          
79          AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
80  
81          Map map = new HashMap(); 
82          map.put("unknown", "unknown realm=\"whatever\"");
83          map.put("basic", "basic realm=\"whatever\"");
84          
85          AuthScheme authscheme = processor.selectAuthScheme(map);
86          assertTrue(authscheme instanceof BasicScheme);
87      }
88  
89  
90      public void testInvalidChallenge() throws Exception {
91          List authPrefs = new ArrayList(3);
92          authPrefs.add("unsupported1");
93          authPrefs.add("unsupported2");
94          HttpParams httpparams = new DefaultHttpParams(); 
95          httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
96          
97          AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
98  
99          Map map = new HashMap(); 
100         map.put("unsupported1", "unsupported1 realm=\"whatever\"");
101         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
102         try {
103             AuthScheme authscheme = processor.selectAuthScheme(map);
104             fail("AuthChallengeException should have been thrown");
105         } catch (AuthChallengeException e) {
106             
107         }
108     }
109 
110 
111     public void testUnsupportedChallenge() throws Exception {
112         List authPrefs = new ArrayList(3);
113         authPrefs.add(AuthPolicy.NTLM);
114         authPrefs.add(AuthPolicy.BASIC);
115         authPrefs.add(AuthPolicy.DIGEST);
116         HttpParams httpparams = new DefaultHttpParams(); 
117         httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
118         
119         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
120 
121         Map map = new HashMap(); 
122         map.put("unsupported1", "unsupported1 realm=\"whatever\"");
123         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
124         
125         try {
126             AuthScheme authscheme = processor.selectAuthScheme(map);
127             fail("AuthChallengeException should have been thrown");
128         } catch (AuthChallengeException e) {
129             
130         }
131     }
132 
133     public void testChallengeProcessing() throws Exception {
134         HttpParams httpparams = new DefaultHttpParams(); 
135         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
136 
137         Map map = new HashMap(); 
138         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
139         
140         AuthState authstate = new AuthState();
141         
142         AuthScheme authscheme = processor.processChallenge(authstate, map);
143         assertTrue(authscheme instanceof BasicScheme);
144         assertEquals("whatever", authscheme.getRealm());
145         assertEquals(authscheme, authstate.getAuthScheme());
146         assertEquals("value", authscheme.getParameter("param"));
147     }
148 
149     public void testInvalidChallengeProcessing() throws Exception {
150         HttpParams httpparams = new DefaultHttpParams(); 
151         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
152 
153         Map map = new HashMap(); 
154         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
155         
156         AuthState authstate = new AuthState();
157         
158         AuthScheme authscheme = processor.processChallenge(authstate, map);
159         assertTrue(authscheme instanceof BasicScheme);
160         assertEquals("whatever", authscheme.getRealm());
161         assertEquals(authscheme, authstate.getAuthScheme());
162         assertEquals("value", authscheme.getParameter("param"));
163 
164         Map map2 = new HashMap(); 
165         map2.put("ntlm", "NTLM");
166         try {
167             
168             authscheme = processor.processChallenge(authstate, map2);
169             fail("AuthenticationException should have been thrown");
170         } catch (AuthenticationException e) {
171             
172         }
173     }
174 }