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  
28  package org.apache.hc.client5.http.auth;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  
38  @SuppressWarnings("deprecation")
39  public class TestCredentials {
40  
41      @Test
42      public void testUsernamePasswordCredentialsBasics() {
43          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
44                  "name","pwd".toCharArray());
45          Assertions.assertEquals("name", creds1.getUserName());
46          Assertions.assertEquals(new BasicUserPrincipal("name"),
47                  creds1.getUserPrincipal());
48          Assertions.assertArrayEquals("pwd".toCharArray(), creds1.getUserPassword());
49          Assertions.assertEquals("[principal: name]", creds1.toString());
50          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
51              "name", null);
52          Assertions.assertEquals("name", creds2.getUserName());
53          Assertions.assertEquals(new BasicUserPrincipal("name"),
54                  creds2.getUserPrincipal());
55          Assertions.assertNull(creds2.getUserPassword());
56          Assertions.assertEquals("[principal: name]", creds2.toString());
57      }
58  
59      @Test
60      public void testNTCredentialsBasics() {
61          final NTCredentials creds1 = new NTCredentials(
62                  "name","pwd".toCharArray(), "localhost", "domain");
63          Assertions.assertEquals("name", creds1.getUserName());
64          Assertions.assertEquals(new NTUserPrincipal("DOMAIN", "name"),
65                  creds1.getUserPrincipal());
66          Assertions.assertArrayEquals("pwd".toCharArray(), creds1.getPassword());
67          Assertions.assertEquals("[principal: DOMAIN\\name][workstation: "+ creds1.getWorkstation() +"][netbiosDomain: DOMAIN]",
68                  creds1.toString());
69          final NTCredentials creds2 = new NTCredentials(
70                  "name", null, null, null);
71          Assertions.assertEquals("name", creds2.getUserName());
72          Assertions.assertEquals(new NTUserPrincipal(null, "name"),
73                  creds2.getUserPrincipal());
74          Assertions.assertNull(creds2.getPassword());
75          Assertions.assertEquals("[principal: name][workstation: "+creds1.getWorkstation() +"][netbiosDomain: null]",
76                  creds2.toString());
77      }
78  
79      @Test
80      public void testUsernamePasswordCredentialsHashCode() {
81          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
82                  "name","pwd".toCharArray());
83          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
84                  "othername","pwd".toCharArray());
85          final UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
86                  "name", "otherpwd".toCharArray());
87  
88          Assertions.assertTrue(creds1.hashCode() == creds1.hashCode());
89          Assertions.assertTrue(creds1.hashCode() != creds2.hashCode());
90          Assertions.assertTrue(creds1.hashCode() == creds3.hashCode());
91      }
92  
93      @Test
94      public void testUsernamePasswordCredentialsEquals() {
95          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
96                  "name","pwd".toCharArray());
97          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
98                  "othername","pwd".toCharArray());
99          final UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
100                 "name", "otherpwd".toCharArray());
101 
102         Assertions.assertEquals(creds1, creds1);
103         Assertions.assertNotEquals(creds1, creds2);
104         Assertions.assertEquals(creds1, creds3);
105     }
106 
107     @Test
108     public void tesBearerTokenBasics() {
109         final BearerToken creds1 = new BearerToken("token of some sort");
110         Assertions.assertEquals("token of some sort", creds1.getToken());
111     }
112 
113     @Test
114     public void testBearerTokenHashCode() {
115         final BearerToken creds1 = new BearerToken("token of some sort");
116         final BearerToken creds2 = new BearerToken("another token of some sort");
117         final BearerToken creds3 = new BearerToken("token of some sort");
118 
119         Assertions.assertTrue(creds1.hashCode() == creds1.hashCode());
120         Assertions.assertTrue(creds1.hashCode() != creds2.hashCode());
121         Assertions.assertTrue(creds1.hashCode() == creds3.hashCode());
122     }
123 
124     @Test
125     public void testBearerTokenEquals() {
126         final BearerToken creds1 = new BearerToken("token of some sort");
127         final BearerToken creds2 = new BearerToken("another token of some sort");
128         final BearerToken creds3 = new BearerToken("token of some sort");
129 
130         Assertions.assertEquals(creds1, creds1);
131         Assertions.assertNotEquals(creds1, creds2);
132         Assertions.assertEquals(creds1, creds3);
133     }
134 
135     @Test
136     public void testNTCredentialsHashCode() {
137         final NTCredentials creds1 = new NTCredentials(
138                 "name","pwd".toCharArray(), "somehost", "domain");
139         final NTCredentials creds2 = new NTCredentials(
140                 "othername","pwd".toCharArray(), "somehost", "domain");
141         final NTCredentials creds3 = new NTCredentials(
142                 "name", "otherpwd".toCharArray(), "SomeHost", "Domain");
143         final NTCredentials creds4 = new NTCredentials(
144                 "name","pwd".toCharArray(), "otherhost", "domain");
145         final NTCredentials creds5 = new NTCredentials(
146                 "name","pwd".toCharArray(), null, "domain");
147         final NTCredentials creds6 = new NTCredentials(
148                 "name","pwd".toCharArray(), "somehost", "ms");
149         final NTCredentials creds7 = new NTCredentials(
150                 "name","pwd".toCharArray(), "somehost", null);
151         final NTCredentials creds8 = new NTCredentials(
152                 "name","pwd".toCharArray(), null, "domain");
153         final NTCredentials creds9 = new NTCredentials(
154                 "name","pwd".toCharArray(), "somehost", null);
155 
156         Assertions.assertTrue(creds1.hashCode() == creds1.hashCode());
157         Assertions.assertTrue(creds1.hashCode() != creds2.hashCode());
158         Assertions.assertEquals(creds1.hashCode(), creds3.hashCode());
159         Assertions.assertEquals(creds1.hashCode(), creds4.hashCode());
160         Assertions.assertEquals(creds1.hashCode(), creds5.hashCode());
161         Assertions.assertNotEquals(creds1.hashCode(), creds6.hashCode());
162         Assertions.assertNotEquals(creds1.hashCode(), creds7.hashCode());
163         Assertions.assertEquals(creds8.hashCode(), creds5.hashCode());
164         Assertions.assertEquals(creds9.hashCode(), creds7.hashCode());
165     }
166 
167     @Test
168     public void testNTCredentialsEquals() {
169         final NTCredentials creds1 = new NTCredentials(
170                 "name","pwd".toCharArray(), "somehost", "domain");
171         final NTCredentials creds2 = new NTCredentials(
172                 "othername","pwd".toCharArray(), "somehost", "domain");
173         final NTCredentials creds3 = new NTCredentials(
174                 "name", "otherpwd".toCharArray(), "SomeHost", "Domain");
175         final NTCredentials creds4 = new NTCredentials(
176                 "name","pwd".toCharArray(), "otherhost", "domain");
177         final NTCredentials creds5 = new NTCredentials(
178                 "name","pwd".toCharArray(), null, "domain");
179         final NTCredentials creds6 = new NTCredentials(
180                 "name","pwd".toCharArray(), "somehost", "ms");
181         final NTCredentials creds7 = new NTCredentials(
182                 "name","pwd".toCharArray(), "somehost", null);
183         final NTCredentials creds8 = new NTCredentials(
184                 "name","pwd".toCharArray(), null, "domain");
185         final NTCredentials creds9 = new NTCredentials(
186                 "name","pwd".toCharArray(), "somehost", null);
187 
188         Assertions.assertEquals(creds1, creds1);
189         Assertions.assertNotEquals(creds1, creds2);
190         Assertions.assertEquals(creds1, creds3);
191         Assertions.assertEquals(creds1, creds4);
192         Assertions.assertEquals(creds1, creds5);
193         Assertions.assertNotEquals(creds1, creds6);
194         Assertions.assertNotEquals(creds1, creds7);
195         Assertions.assertEquals(creds8, creds5);
196         Assertions.assertEquals(creds9, creds7);
197 
198     }
199 
200     @Test
201     public void testUsernamePasswordCredentialsSerialization() throws Exception {
202         final UsernamePasswordCredentials orig = new UsernamePasswordCredentials("name","pwd".toCharArray());
203         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
204         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
205         outStream.writeObject(orig);
206         outStream.close();
207         final byte[] raw = outbuffer.toByteArray();
208         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
209         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
210         final UsernamePasswordCredentials clone = (UsernamePasswordCredentials) inStream.readObject();
211         Assertions.assertEquals(orig, clone);
212     }
213 
214     @Test
215     public void testNTCredentialsSerialization() throws Exception {
216         final NTCredentials orig = new NTCredentials("name","pwd".toCharArray(), "somehost", "domain");
217         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
218         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
219         outStream.writeObject(orig);
220         outStream.close();
221         final byte[] raw = outbuffer.toByteArray();
222         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
223         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
224         final NTCredentials clone = (NTCredentials) inStream.readObject();
225         Assertions.assertEquals(orig, clone);
226     }
227 }