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