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  package org.apache.http.impl.client;
28  
29  import org.apache.http.HttpHost;
30  import org.apache.http.auth.AuthScope;
31  import org.apache.http.auth.Credentials;
32  import org.apache.http.auth.UsernamePasswordCredentials;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Simple tests for {@link BasicCredentialsProvider}.
38   */
39  public class TestBasicCredentialsProvider {
40  
41      public final static Credentials CREDS1 =
42          new UsernamePasswordCredentials("user1", "pass1");
43      public final static Credentials CREDS2 =
44          new UsernamePasswordCredentials("user2", "pass2");
45  
46      public final static AuthScope SCOPE1 =
47          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
48      public final static AuthScope SCOPE2 =
49          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
50      public final static AuthScope BOGUS =
51          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
52      public final static AuthScope DEFSCOPE =
53          new AuthScope("host", AuthScope.ANY_PORT, "realm");
54  
55      @Test
56      public void testBasicCredentialsProviderCredentials() {
57          final BasicCredentialsProvider state = new BasicCredentialsProvider();
58          state.setCredentials(SCOPE1, CREDS1);
59          state.setCredentials(SCOPE2, CREDS2);
60          Assert.assertEquals(CREDS1, state.getCredentials(SCOPE1));
61          Assert.assertEquals(CREDS2, state.getCredentials(SCOPE2));
62      }
63  
64      @Test
65      public void testBasicCredentialsProviderNoCredentials() {
66          final BasicCredentialsProvider state = new BasicCredentialsProvider();
67          Assert.assertEquals(null, state.getCredentials(BOGUS));
68      }
69  
70      @Test
71      public void testBasicCredentialsProviderDefaultCredentials() {
72          final BasicCredentialsProvider state = new BasicCredentialsProvider();
73          state.setCredentials(AuthScope.ANY, CREDS1);
74          state.setCredentials(SCOPE2, CREDS2);
75          Assert.assertEquals(CREDS1, state.getCredentials(BOGUS));
76      }
77  
78      @Test
79      public void testDefaultCredentials() throws Exception {
80          final BasicCredentialsProvider state = new BasicCredentialsProvider();
81          final Credentials expected = new UsernamePasswordCredentials("name", "pass");
82          state.setCredentials(AuthScope.ANY, expected);
83          final Credentials got = state.getCredentials(DEFSCOPE);
84          Assert.assertEquals(got, expected);
85      }
86  
87      @Test
88      public void testRealmCredentials() throws Exception {
89          final BasicCredentialsProvider state = new BasicCredentialsProvider();
90          final Credentials expected = new UsernamePasswordCredentials("name", "pass");
91          state.setCredentials(DEFSCOPE, expected);
92          final Credentials got = state.getCredentials(DEFSCOPE);
93          Assert.assertEquals(expected, got);
94      }
95  
96      @Test
97      public void testHostCredentials() throws Exception {
98          final BasicCredentialsProvider state = new BasicCredentialsProvider();
99          final Credentials expected = new UsernamePasswordCredentials("name", "pass");
100         state.setCredentials(
101             new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
102         final Credentials got = state.getCredentials(DEFSCOPE);
103         Assert.assertEquals(expected, got);
104     }
105 
106     @Test
107     public void testWrongHostCredentials() throws Exception {
108         final BasicCredentialsProvider state = new BasicCredentialsProvider();
109         final Credentials expected = new UsernamePasswordCredentials("name", "pass");
110         state.setCredentials(
111             new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
112         final Credentials got = state.getCredentials(
113             new AuthScope("host2", AuthScope.ANY_PORT, "realm"));
114         Assert.assertNotSame(expected, got);
115     }
116 
117     @Test
118     public void testWrongRealmCredentials() throws Exception {
119         final BasicCredentialsProvider state = new BasicCredentialsProvider();
120         final Credentials cred = new UsernamePasswordCredentials("name", "pass");
121         state.setCredentials(
122             new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred);
123         final Credentials got = state.getCredentials(
124             new AuthScope("host", AuthScope.ANY_PORT, "realm2"));
125         Assert.assertNotSame(cred, got);
126     }
127 
128     @Test
129     public void testMixedCaseHostname() throws Exception {
130         final HttpHost httpHost = new HttpHost("hOsT", 80);
131         final BasicCredentialsProvider state = new BasicCredentialsProvider();
132         final Credentials expected = new UsernamePasswordCredentials("name", "pass");
133         state.setCredentials(new AuthScope(httpHost), expected);
134         final Credentials got = state.getCredentials(DEFSCOPE);
135         Assert.assertEquals(expected, got);
136     }
137 
138     @Test
139     public void testCredentialsMatching() {
140         final Credentials creds1 = new UsernamePasswordCredentials("name1", "pass1");
141         final Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2");
142         final Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3");
143 
144         final AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
145         final AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm");
146         final AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM);
147 
148         final BasicCredentialsProvider state = new BasicCredentialsProvider();
149         state.setCredentials(scope1, creds1);
150         state.setCredentials(scope2, creds2);
151         state.setCredentials(scope3, creds3);
152 
153         Credentials got = state.getCredentials(
154             new AuthScope("someotherhost", 80, "someotherrealm", "basic"));
155         Credentials expected = creds1;
156         Assert.assertEquals(expected, got);
157 
158         got = state.getCredentials(
159             new AuthScope("someotherhost", 80, "somerealm", "basic"));
160         expected = creds2;
161         Assert.assertEquals(expected, got);
162 
163         got = state.getCredentials(
164             new AuthScope("somehost", 80, "someotherrealm", "basic"));
165         expected = creds3;
166         Assert.assertEquals(expected, got);
167     }
168 
169 }