1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.javax.security.auth; 19 20 import junit.framework.TestCase; 21 22 import javax.security.auth.callback.PasswordCallback; 23 24 /** 25 * Tests for <code>PasswordCallback</code> class constructors and methods. 26 * 27 */ 28 public class PasswordCallbackTest extends TestCase { 29 30 /** 31 * javax.security.auth.callback.PasswordCallback#PasswordCallback(String prompt, boolean echoOn) 32 * javax.security.auth.callback.PasswordCallback#getPrompt() 33 * javax.security.auth.callback.PasswordCallback#isEchoOn() 34 */ 35 public void test_PasswordCallback() { 36 String prompt = "promptTest"; 37 38 try { 39 PasswordCallback pc = new PasswordCallback(prompt, true); 40 assertNotNull("Null object returned", pc); 41 assertEquals(prompt, pc.getPrompt()); 42 assertEquals(true, pc.isEchoOn()); 43 } catch (Exception e) { 44 fail("Unexpected exception: " + e); 45 } 46 47 try { 48 PasswordCallback pc = new PasswordCallback(prompt, false); 49 assertNotNull("Null object returned", pc); 50 assertEquals(prompt, pc.getPrompt()); 51 assertEquals(false, pc.isEchoOn()); 52 } catch (Exception e) { 53 fail("Unexpected exception: " + e); 54 } 55 56 try { 57 PasswordCallback pc = new PasswordCallback(null, true); 58 fail("IllegalArgumentException wasn't thrown"); 59 } catch (IllegalArgumentException npe) { 60 } 61 62 try { 63 PasswordCallback pc = new PasswordCallback("", true); 64 fail("IllegalArgumentException wasn't thrown"); 65 } catch (IllegalArgumentException npe) { 66 } 67 } 68 69 /** 70 * javax.security.auth.callback.PasswordCallback#getPassword() 71 * javax.security.auth.callback.PasswordCallback#setPassword(char[] password) 72 * javax.security.auth.callback.PasswordCallback#clearPassword() 73 */ 74 public void test_Password() { 75 String prompt = "promptTest"; 76 char[] psw1 = "testPassword".toCharArray(); 77 char[] psw2 = "newPassword".toCharArray(); 78 PasswordCallback pc = new PasswordCallback(prompt, true); 79 80 try { 81 assertNull(pc.getPassword()); 82 pc.setPassword(psw1); 83 assertEquals(psw1.length, pc.getPassword().length); 84 pc.setPassword(null); 85 assertNull(pc.getPassword()); 86 pc.setPassword(psw2); 87 char[] res = pc.getPassword(); 88 assertEquals(psw2.length, res.length); 89 for (int i = 0; i < res.length; i++) { 90 assertEquals("Incorrect password was returned", psw2[i], res[i]); 91 } 92 pc.clearPassword(); 93 res = pc.getPassword(); 94 if (res.equals(psw2)) { 95 fail("Incorrect password was returned after clear"); 96 } 97 pc.setPassword(psw1); 98 res = pc.getPassword(); 99 assertEquals(psw1.length, res.length); 100 for (int i = 0; i < res.length; i++) { 101 assertEquals("Incorrect result", psw1[i], res[i]); 102 } 103 } catch (Exception e) { 104 fail("Unexpected exception: " + e); 105 } 106 } 107 } 108