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          public Header authenticate(
65                  final Credentials credentials,
66                  final HttpRequest request) throws AuthenticationException {
67              return null;
68          }
69  
70          @Override
71          public String getSchemeName() {
72              return "test";
73          }
74  
75          @Override
76          public boolean isComplete() {
77              return false;
78          }
79  
80          @Override
81          public boolean isConnectionBased() {
82              return false;
83          }
84  
85      }
86  
87      @Test
88      public void testProcessChallenge() throws Exception {
89          final TestAuthScheme authscheme = new TestAuthScheme();
90          final Header header = new BasicHeader(
91                  AUTH.WWW_AUTH,
92                  "Test realm=\"realm1\", test, test1 =  stuff, test2 =  \"stuff, stuff\", test3=\"crap");
93  
94          authscheme.processChallenge(header);
95  
96          Assert.assertEquals("test", authscheme.getSchemeName());
97          Assert.assertEquals("TEST", authscheme.toString());
98          Assert.assertEquals("realm1", authscheme.getParameter("realm"));
99          Assert.assertEquals(null, authscheme.getParameter("test"));
100         Assert.assertEquals("stuff", authscheme.getParameter("test1"));
101         Assert.assertEquals("stuff, stuff", authscheme.getParameter("test2"));
102         Assert.assertEquals("crap", authscheme.getParameter("test3"));
103         Assert.assertEquals(null, authscheme.getParameter(null));
104     }
105 
106     @Test
107     public void testProcessChallengeWithLotsOfBlanks() throws Exception {
108         final TestAuthScheme authscheme = new TestAuthScheme();
109         final CharArrayBuffer buffer = new CharArrayBuffer(32);
110         buffer.append(" WWW-Authenticate:    Test       realm=\"realm1\"");
111         final Header header = new BufferedHeader(buffer);
112 
113 
114         authscheme.processChallenge(header);
115 
116         Assert.assertEquals("test", authscheme.getSchemeName());
117         Assert.assertEquals("realm1", authscheme.getParameter("realm"));
118     }
119 
120     @Test
121     public void testNullHeader() throws Exception {
122         final TestAuthScheme authscheme = new TestAuthScheme();
123         try {
124             authscheme.processChallenge(null);
125             Assert.fail("IllegalArgumentException should have been thrown");
126         } catch (final IllegalArgumentException ex) {
127         }
128     }
129 
130     @Test(expected=MalformedChallengeException.class)
131     public void testInvalidHeader() throws Exception {
132         final TestAuthScheme authscheme = new TestAuthScheme();
133         final Header header = new BasicHeader("whatever", "Test realm=\"realm1\"");
134         authscheme.processChallenge(header);
135     }
136 
137     @Test(expected=MalformedChallengeException.class)
138     public void testInvalidSchemeName() throws Exception {
139         final TestAuthScheme authscheme = new TestAuthScheme();
140         final Header header = new BasicHeader(AUTH.WWW_AUTH, "Not-a-Test realm=\"realm1\"");
141         authscheme.processChallenge(header);
142     }
143 
144     @Test(expected=MalformedChallengeException.class)
145     public void testInvalidHeaderValue() throws Exception {
146         final TestAuthScheme authscheme = new TestAuthScheme();
147         final Header header = new BasicHeader("whatever", "whatever");
148         authscheme.processChallenge(header);
149     }
150 
151     @Test
152     public void testSerialization() throws Exception {
153         final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "test realm=\"test\", blah=blah, yada=\"yada yada\"");
154 
155         final TestAuthScheme testScheme = new TestAuthScheme(Consts.ISO_8859_1);
156         testScheme.processChallenge(challenge);
157 
158         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
159         final ObjectOutputStream out = new ObjectOutputStream(buffer);
160         out.writeObject(testScheme);
161         out.flush();
162         final byte[] raw = buffer.toByteArray();
163         final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw));
164         final TestAuthScheme authScheme = (TestAuthScheme) in.readObject();
165 
166         Assert.assertEquals(Consts.ISO_8859_1, authScheme.getCredentialsCharset());
167         Assert.assertEquals("test", authScheme.getParameter("realm"));
168         Assert.assertEquals("blah", authScheme.getParameter("blah"));
169         Assert.assertEquals("yada yada", authScheme.getParameter("yada"));
170     }
171 
172 }
173