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