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