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 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
53 public TestChallengeProcessor(String testName) {
54 super(testName);
55 }
56
57
58 public static void main(String args[]) {
59 String[] testCaseName = { TestChallengeProcessor.class.getName() };
60 junit.textui.TestRunner.main(testCaseName);
61 }
62
63
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
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
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
167 authscheme = processor.processChallenge(authstate, map2);
168 fail("AuthenticationException should have been thrown");
169 } catch (AuthenticationException e) {
170
171 }
172 }
173 }