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