    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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    * ====================================================================
    6    *
    7    *  Licensed to the Apache Software Foundation (ASF) under one or more
    8    *  contributor license agreements.  See the NOTICE file distributed with
    9    *  this work for additional information regarding copyright ownership.
    10   *  The ASF licenses this file to You under the Apache License, Version 2.0
    11   *  (the "License"); you may not use this file except in compliance with
    12   *  the License.  You may obtain a copy of the License at
    13   *
    14   *      http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   *  Unless required by applicable law or agreed to in writing, software
    17   *  distributed under the License is distributed on an "AS IS" BASIS,
    18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   *  See the License for the specific language governing permissions and
    20   *  limitations under the License.
    21   * ====================================================================
    22   *
    23   * This software consists of voluntary contributions made by many
    24   * individuals on behalf of the Apache Software Foundation.  For more
    25   * information on the Apache Software Foundation, please see
    26   * <http://www.apache.org/>.
    27   *
    28   * [Additional notices, if required by prior licensing conditions]
    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      // ------------------------------------------------------------ Constructor
    54      public TestChallengeProcessor(String testName) {
    55          super(testName);
    56      }
    57
    58      // ------------------------------------------------------------------- Main
    59      public static void main(String args[]) {
    60          String[] testCaseName = { TestChallengeProcessor.class.getName() };
    61          junit.textui.TestRunner.main(testCaseName);
    62      }
    63
    64      // ------------------------------------------------------- TestCase Methods
    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             //ignore
    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             //expected
    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             // Basic authentication scheme expected
    168             authscheme = processor.processChallenge(authstate, map2);
    169             fail("AuthenticationException should have been thrown");
    170         } catch (AuthenticationException e) {
    171             //expected
    172         }
    173     }
    174 }