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.Assert;
36  import org.junit.Test;
37  
38  public class TestCredentials {
39  
40      @Test
41      public void testUsernamePasswordCredentialsBasics() {
42          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
43                  "name","pwd".toCharArray());
44          Assert.assertEquals("name", creds1.getUserName());
45          Assert.assertEquals(new BasicUserPrincipal("name"),
46                  creds1.getUserPrincipal());
47          Assert.assertArrayEquals("pwd".toCharArray(), creds1.getPassword());
48          Assert.assertEquals("[principal: name]", creds1.toString());
49          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
50              "name", null);
51          Assert.assertEquals("name", creds2.getUserName());
52          Assert.assertEquals(new BasicUserPrincipal("name"),
53                  creds2.getUserPrincipal());
54          Assert.assertEquals(null, creds2.getPassword());
55          Assert.assertEquals("[principal: name]", creds2.toString());
56      }
57  
58      @Test
59      public void testNTCredentialsBasics() {
60          final NTCredentials creds1 = new NTCredentials(
61                  "name","pwd".toCharArray(), "localhost", "domain");
62          Assert.assertEquals("name", creds1.getUserName());
63          Assert.assertEquals(new NTUserPrincipal("DOMAIN", "name"),
64                  creds1.getUserPrincipal());
65          Assert.assertArrayEquals("pwd".toCharArray(), creds1.getPassword());
66          Assert.assertEquals("[principal: DOMAIN\\name][workstation: LOCALHOST][netbiosDomain: DOMAIN]",
67                  creds1.toString());
68          final NTCredentials creds2 = new NTCredentials(
69                  "name", null, null, null);
70          Assert.assertEquals("name", creds2.getUserName());
71          Assert.assertEquals(new NTUserPrincipal(null, "name"),
72                  creds2.getUserPrincipal());
73          Assert.assertEquals(null, creds2.getPassword());
74          Assert.assertEquals("[principal: name][workstation: null][netbiosDomain: null]",
75                  creds2.toString());
76      }
77  
78      @Test
79      public void testUsernamePasswordCredentialsHashCode() {
80          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
81                  "name","pwd".toCharArray());
82          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
83                  "othername","pwd".toCharArray());
84          final UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
85                  "name", "otherpwd".toCharArray());
86  
87          Assert.assertTrue(creds1.hashCode() == creds1.hashCode());
88          Assert.assertTrue(creds1.hashCode() != creds2.hashCode());
89          Assert.assertTrue(creds1.hashCode() == creds3.hashCode());
90      }
91  
92      @Test
93      public void testUsernamePasswordCredentialsEquals() {
94          final UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
95                  "name","pwd".toCharArray());
96          final UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
97                  "othername","pwd".toCharArray());
98          final UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
99                  "name", "otherpwd".toCharArray());
100 
101         Assert.assertTrue(creds1.equals(creds1));
102         Assert.assertFalse(creds1.equals(creds2));
103         Assert.assertTrue(creds1.equals(creds3));
104     }
105 
106     @Test
107     public void testNTCredentialsHashCode() {
108         final NTCredentials creds1 = new NTCredentials(
109                 "name","pwd".toCharArray(), "somehost", "domain");
110         final NTCredentials creds2 = new NTCredentials(
111                 "othername","pwd".toCharArray(), "somehost", "domain");
112         final NTCredentials creds3 = new NTCredentials(
113                 "name", "otherpwd".toCharArray(), "SomeHost", "Domain");
114         final NTCredentials creds4 = new NTCredentials(
115                 "name","pwd".toCharArray(), "otherhost", "domain");
116         final NTCredentials creds5 = new NTCredentials(
117                 "name","pwd".toCharArray(), null, "domain");
118         final NTCredentials creds6 = new NTCredentials(
119                 "name","pwd".toCharArray(), "somehost", "ms");
120         final NTCredentials creds7 = new NTCredentials(
121                 "name","pwd".toCharArray(), "somehost", null);
122         final NTCredentials creds8 = new NTCredentials(
123                 "name","pwd".toCharArray(), null, "domain");
124         final NTCredentials creds9 = new NTCredentials(
125                 "name","pwd".toCharArray(), "somehost", null);
126 
127         Assert.assertTrue(creds1.hashCode() == creds1.hashCode());
128         Assert.assertTrue(creds1.hashCode() != creds2.hashCode());
129         Assert.assertTrue(creds1.hashCode() == creds3.hashCode());
130         Assert.assertFalse(creds1.hashCode() == creds4.hashCode());
131         Assert.assertFalse(creds1.hashCode() == creds5.hashCode());
132         Assert.assertFalse(creds1.hashCode() == creds6.hashCode());
133         Assert.assertFalse(creds1.hashCode() == creds7.hashCode());
134         Assert.assertTrue(creds8.hashCode() == creds5.hashCode());
135         Assert.assertTrue(creds9.hashCode() == creds7.hashCode());
136     }
137 
138     @Test
139     public void testNTCredentialsEquals() {
140         final NTCredentials creds1 = new NTCredentials(
141                 "name","pwd".toCharArray(), "somehost", "domain");
142         final NTCredentials creds2 = new NTCredentials(
143                 "othername","pwd".toCharArray(), "somehost", "domain");
144         final NTCredentials creds3 = new NTCredentials(
145                 "name", "otherpwd".toCharArray(), "SomeHost", "Domain");
146         final NTCredentials creds4 = new NTCredentials(
147                 "name","pwd".toCharArray(), "otherhost", "domain");
148         final NTCredentials creds5 = new NTCredentials(
149                 "name","pwd".toCharArray(), null, "domain");
150         final NTCredentials creds6 = new NTCredentials(
151                 "name","pwd".toCharArray(), "somehost", "ms");
152         final NTCredentials creds7 = new NTCredentials(
153                 "name","pwd".toCharArray(), "somehost", null);
154         final NTCredentials creds8 = new NTCredentials(
155                 "name","pwd".toCharArray(), null, "domain");
156         final NTCredentials creds9 = new NTCredentials(
157                 "name","pwd".toCharArray(), "somehost", null);
158 
159         Assert.assertTrue(creds1.equals(creds1));
160         Assert.assertFalse(creds1.equals(creds2));
161         Assert.assertTrue(creds1.equals(creds3));
162         Assert.assertFalse(creds1.equals(creds4));
163         Assert.assertFalse(creds1.equals(creds5));
164         Assert.assertFalse(creds1.equals(creds6));
165         Assert.assertFalse(creds1.equals(creds7));
166         Assert.assertTrue(creds8.equals(creds5));
167         Assert.assertTrue(creds9.equals(creds7));
168 
169     }
170 
171     @Test
172     public void testUsernamePasswordCredentialsSerialization() throws Exception {
173         final UsernamePasswordCredentials orig = new UsernamePasswordCredentials("name","pwd".toCharArray());
174         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
175         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
176         outStream.writeObject(orig);
177         outStream.close();
178         final byte[] raw = outbuffer.toByteArray();
179         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
180         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
181         final UsernamePasswordCredentials clone = (UsernamePasswordCredentials) inStream.readObject();
182         Assert.assertEquals(orig, clone);
183     }
184 
185     @Test
186     public void testNTCredentialsSerialization() throws Exception {
187         final NTCredentials orig = new NTCredentials("name","pwd".toCharArray(), "somehost", "domain");
188         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
189         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
190         outStream.writeObject(orig);
191         outStream.close();
192         final byte[] raw = outbuffer.toByteArray();
193         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
194         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
195         final NTCredentials clone = (NTCredentials) inStream.readObject();
196         Assert.assertEquals(orig, clone);
197     }
198 
199 }