1   /*
2    * Copyright 2006 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 com.sun.xml.wss.impl.callback.PasswordValidationCallback;
20  import junit.framework.TestCase;
21  
22  import java.util.Properties;
23  
24  public class SimplePasswordValidationCallbackHandlerTest extends TestCase {
25  
26      private SimplePasswordValidationCallbackHandler handler;
27  
28      protected void setUp() throws Exception {
29          handler = new SimplePasswordValidationCallbackHandler();
30          Properties users = new Properties();
31          users.setProperty("Bert", "Ernie");
32          handler.setUsers(users);
33      }
34  
35      public void testPlainTextPasswordValid() throws Exception {
36          PasswordValidationCallback.PlainTextPasswordRequest request =
37                  new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Ernie");
38          PasswordValidationCallback callback = new PasswordValidationCallback(request);
39          handler.handleInternal(callback);
40          boolean authenticated = callback.getResult();
41          assertTrue("Not authenticated", authenticated);
42      }
43  
44      public void testPlainTextPasswordInvalid() throws Exception {
45          PasswordValidationCallback.PlainTextPasswordRequest request =
46                  new PasswordValidationCallback.PlainTextPasswordRequest("Bert", "Big bird");
47          PasswordValidationCallback callback = new PasswordValidationCallback(request);
48          handler.handleInternal(callback);
49          boolean authenticated = callback.getResult();
50          assertFalse("Authenticated", authenticated);
51      }
52  
53      public void testPlainTextPasswordNoSuchUser() throws Exception {
54          PasswordValidationCallback.PlainTextPasswordRequest request =
55                  new PasswordValidationCallback.PlainTextPasswordRequest("Big bird", "Bert");
56          PasswordValidationCallback callback = new PasswordValidationCallback(request);
57          handler.handleInternal(callback);
58          boolean authenticated = callback.getResult();
59          assertFalse("Authenticated", authenticated);
60      }
61  
62      public void testDigestPasswordValid() throws Exception {
63          String username = "Bert";
64          String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
65          String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk=";
66          String creationTime = "2006-06-01T23:48:42Z";
67          PasswordValidationCallback.DigestPasswordRequest request =
68                  new PasswordValidationCallback.DigestPasswordRequest(username, passwordDigest, nonce, creationTime);
69          PasswordValidationCallback callback = new PasswordValidationCallback(request);
70          handler.handleInternal(callback);
71          boolean authenticated = callback.getResult();
72          assertTrue("Authenticated", authenticated);
73  
74      }
75  
76      public void testDigestPasswordInvalid() throws Exception {
77          String username = "Bert";
78          String nonce = "9mdsYDCrjjYRur0rxzYt2oD7";
79          String passwordDigest = "kwNstEaiFOrI7B31j7GuETYvdgk";
80          String creationTime = "2006-06-01T23:48:42Z";
81          PasswordValidationCallback.DigestPasswordRequest request =
82                  new PasswordValidationCallback.DigestPasswordRequest(username, passwordDigest, nonce, creationTime);
83          PasswordValidationCallback callback = new PasswordValidationCallback(request);
84          handler.handleInternal(callback);
85          boolean authenticated = callback.getResult();
86          assertFalse("Authenticated", authenticated);
87  
88      }
89  }