1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.xwss.callback;
18  
19  import java.util.Properties;
20  
21  import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
22  import org.junit.Assert;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class SimplePasswordValidationCallbackHandlerTest {
27  
28      private SimplePasswordValidationCallbackHandler handler;
29  
30      @Before
31      public void setUp() throws Exception {
32          handler = new SimplePasswordValidationCallbackHandler();
33          Properties users = new Properties();
34          users.setProperty("Bert", "Ernie");
35          handler.setUsers(users);
36      }
37  
38      @Test
39      public void testPlainTextPasswordValid() throws Exception {
40          PasswordValidationCallback.PlainTextPasswordRequest request =
41                  new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Ernie");
42          PasswordValidationCallback callback = new PasswordValidationCallback(request);
43          handler.handleInternal(callback);
44          boolean authenticated = callback.getResult();
45          Assert.assertTrue("Not authenticated", authenticated);
46      }
47  
48      @Test
49      public void testPlainTextPasswordInvalid() throws Exception {
50          PasswordValidationCallback.PlainTextPasswordRequest request =
51                  new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Big bird");
52          PasswordValidationCallback callback = new PasswordValidationCallback(request);
53          handler.handleInternal(callback);
54          boolean authenticated = callback.getResult();
55          Assert.assertFalse("Authenticated", authenticated);
56      }
57  
58      @Test
59      public void testPlainTextPasswordNoSuchUser() throws Exception {
60          PasswordValidationCallback.PlainTextPasswordRequest request =
61                  new PasswordValidationCallback.PlainTextPasswordRequest("Big bird", "Bert");
62          PasswordValidationCallback callback = new PasswordValidationCallback(request);
63          handler.handleInternal(callback);
64          boolean authenticated = callback.getResult();
65          Assert.assertFalse("Authenticated", authenticated);
66      }
67  
68      @Test
69      public void testDigestPasswordValid() throws Exception {
70          String username = "Bert";
71          String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
72          String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk=";
73          String creationTime = "2006-06-01T23:48:42Z";
74          PasswordValidationCallback.DigestPasswordRequest request =
75                  new PasswordValidationCallback.DigestPasswordRequest(username, passwordDigest, nonce, creationTime);
76          PasswordValidationCallback callback = new PasswordValidationCallback(request);
77          handler.handleInternal(callback);
78          boolean authenticated = callback.getResult();
79          Assert.assertTrue("Authenticated", authenticated);
80  
81      }
82  
83      @Test
84      public void testDigestPasswordInvalid() throws Exception {
85          String username = "Bert";
86          String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
87          String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk";
88          String creationTime = "2006-06-01T23:48:42Z";
89          PasswordValidationCallback.DigestPasswordRequest request =
90                  new PasswordValidationCallback.DigestPasswordRequest(username, passwordDigest, nonce, creationTime);
91          PasswordValidationCallback callback = new PasswordValidationCallback(request);
92          handler.handleInternal(callback);
93          boolean authenticated = callback.getResult();
94          Assert.assertFalse("Authenticated", authenticated);
95  
96      }
97  }