View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.auth;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.nio.charset.Charset;
35  
36  import org.apache.http.Consts;
37  import org.apache.http.Header;
38  import org.apache.http.HttpRequest;
39  import org.apache.http.auth.AUTH;
40  import org.apache.http.auth.AuthenticationException;
41  import org.apache.http.auth.Credentials;
42  import org.apache.http.auth.MalformedChallengeException;
43  import org.apache.http.message.BasicHeader;
44  import org.apache.http.message.BufferedHeader;
45  import org.apache.http.util.CharArrayBuffer;
46  import org.junit.Assert;
47  import org.junit.Test;
48  
49  public class TestRFC2617Scheme {
50  
51      static class TestAuthScheme extends RFC2617Scheme {
52  
53          private static final long serialVersionUID = 1L;
54  
55          public TestAuthScheme() {
56              super();
57          }
58  
59          public TestAuthScheme(final Charset charset) {
60              super(charset);
61          }
62  
63          @Override
64          @Deprecated
65          public Header authenticate(
66                  final Credentials credentials,
67                  final HttpRequest request) throws AuthenticationException {
68              return null;
69          }
70  
71          @Override
72          public String getSchemeName() {
73              return "test";
74          }
75  
76          @Override
77          public boolean isComplete() {
78              return false;
79          }
80  
81          @Override
82          public boolean isConnectionBased() {
83              return false;
84          }
85  
86      }
87  
88      @Test
89      public void testProcessChallenge() throws Exception {
90          final TestAuthScheme authscheme = new TestAuthScheme();
91          final Header header = new BasicHeader(
92                  AUTH.WWW_AUTH,
93                  "Test realm=\"realm1\", test, test1 =  stuff, test2 =  \"stuff, stuff\", test3=\"crap");
94  
95          authscheme.processChallenge(header);
96  
97          Assert.assertEquals("test", authscheme.getSchemeName());
98          Assert.assertEquals("TEST", authscheme.toString());
99          Assert.assertEquals("realm1", authscheme.getParameter("realm"));
100         Assert.assertEquals(null, authscheme.getParameter("test"));
101         Assert.assertEquals("stuff", authscheme.getParameter("test1"));
102         Assert.assertEquals("stuff, stuff", authscheme.getParameter("test2"));
103         Assert.assertEquals("\"crap", authscheme.getParameter("test3"));
104         Assert.assertEquals(null, authscheme.getParameter(null));
105     }
106 
107     @Test
108     public void testProcessChallengeWithLotsOfBlanks() throws Exception {
109         final TestAuthScheme authscheme = new TestAuthScheme();
110         final CharArrayBuffer buffer = new CharArrayBuffer(32);
111         buffer.append(" WWW-Authenticate:    Test       realm=\"realm1\"");
112         final Header header = new BufferedHeader(buffer);
113 
114 
115         authscheme.processChallenge(header);
116 
117         Assert.assertEquals("test", authscheme.getSchemeName());
118         Assert.assertEquals("realm1", authscheme.getParameter("realm"));
119     }
120 
121     @Test
122     public void testNullHeader() throws Exception {
123         final TestAuthScheme authscheme = new TestAuthScheme();
124         try {
125             authscheme.processChallenge(null);
126             Assert.fail("IllegalArgumentException should have been thrown");
127         } catch (final IllegalArgumentException ex) {
128         }
129     }
130 
131     @Test(expected=MalformedChallengeException.class)
132     public void testInvalidHeader() throws Exception {
133         final TestAuthScheme authscheme = new TestAuthScheme();
134         final Header header = new BasicHeader("whatever", "Test realm=\"realm1\"");
135         authscheme.processChallenge(header);
136     }
137 
138     @Test(expected=MalformedChallengeException.class)
139     public void testInvalidSchemeName() throws Exception {
140         final TestAuthScheme authscheme = new TestAuthScheme();
141         final Header header = new BasicHeader(AUTH.WWW_AUTH, "Not-a-Test realm=\"realm1\"");
142         authscheme.processChallenge(header);
143     }
144 
145     @Test(expected=MalformedChallengeException.class)
146     public void testEmptyHeader() throws Exception {
147         final TestAuthScheme authscheme = new TestAuthScheme();
148         final Header header = new BasicHeader(AUTH.WWW_AUTH, "Test    ");
149         authscheme.processChallenge(header);
150     }
151 
152     @Test(expected=MalformedChallengeException.class)
153     public void testInvalidHeaderValue() throws Exception {
154         final TestAuthScheme authscheme = new TestAuthScheme();
155         final Header header = new BasicHeader("whatever", "whatever");
156         authscheme.processChallenge(header);
157     }
158 
159     @Test
160     public void testSerialization() throws Exception {
161         final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "test realm=\"test\", blah=blah, yada=\"yada yada\"");
162 
163         final TestAuthScheme testScheme = new TestAuthScheme(Consts.ISO_8859_1);
164         testScheme.processChallenge(challenge);
165 
166         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
167         final ObjectOutputStream out = new ObjectOutputStream(buffer);
168         out.writeObject(testScheme);
169         out.flush();
170         final byte[] raw = buffer.toByteArray();
171         final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
172         final TestAuthScheme authScheme = (TestAuthScheme) in.readObject();
173 
174         Assert.assertEquals(Consts.ISO_8859_1, authScheme.getCredentialsCharset());
175         Assert.assertEquals("test", authScheme.getParameter("realm"));
176         Assert.assertEquals("blah", authScheme.getParameter("blah"));
177         Assert.assertEquals("yada yada", authScheme.getParameter("yada"));
178     }
179 
180 }
181