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 org.apache.harmony.auth.tests.module; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.InputStream; 22 import java.util.Arrays; 23 24 import javax.security.auth.login.LoginException; 25 26 import org.apache.harmony.auth.module.LoginModuleUtils; 27 28 import junit.framework.TestCase; 29 30 public class LoginModuleUtilsTest extends TestCase { 31 32 public void testGetPassword() throws Exception { 33 final String PASSWORD_AS_STRING = "TESTPASSWORD"; 34 final char[] PASSWORD_AS_CHARS = PASSWORD_AS_STRING.toCharArray(); 35 36 String password_file_content = PASSWORD_AS_STRING; 37 InputStream in = new ByteArrayInputStream(password_file_content 38 .getBytes()); 39 char[] password = LoginModuleUtils.getPassword(in); 40 assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password)); 41 42 password_file_content = "TESTPASSWORD" + "\nNONsense"; 43 in = new ByteArrayInputStream(password_file_content.getBytes()); 44 password = LoginModuleUtils.getPassword(in); 45 assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password)); 46 47 password_file_content = "TESTPASSWORD" + "\r\nNONsense"; 48 in = new ByteArrayInputStream(password_file_content.getBytes()); 49 password = LoginModuleUtils.getPassword(in); 50 assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password)); 51 52 password_file_content = "TESTPASSWORD" + "\n\rNONsense"; 53 in = new ByteArrayInputStream(password_file_content.getBytes()); 54 password = LoginModuleUtils.getPassword(in); 55 assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password)); 56 57 password_file_content = "TESTPASSWORD" + "\r\r\nNONsense"; 58 in = new ByteArrayInputStream(password_file_content.getBytes()); 59 password = LoginModuleUtils.getPassword(in); 60 String expectedString = PASSWORD_AS_STRING + "\r"; 61 assertTrue(Arrays.equals(expectedString.toCharArray(), password)); 62 } 63 64 public void testClearPassword() throws Exception { 65 final String PASSWORD_AS_STRING = "TESTPASSWORD"; 66 67 char[] password = PASSWORD_AS_STRING.toCharArray(); 68 LoginModuleUtils.clearPassword(password); 69 for (char c : password) { 70 assertEquals('\0', c); 71 } 72 73 password = null; 74 LoginModuleUtils.clearPassword(password); 75 password = new char[0]; 76 LoginModuleUtils.clearPassword(password); 77 } 78 79 public void testLoginModuleStatus() throws Exception { 80 LoginModuleUtils.LoginModuleStatus status = new LoginModuleUtils.LoginModuleStatus(); 81 try { 82 status.checkLogin(); 83 } catch (LoginException e) { 84 // expected 85 } 86 87 try { 88 status.checkCommit(); 89 } catch (LoginException e) { 90 // expected 91 } 92 93 try { 94 status.checkLogout(); 95 } catch (LoginException e) { 96 // expected 97 } 98 99 status.initialized(); 100 assertEquals(LoginModuleUtils.ACTION.login, status 101 .checkLogin()); 102 103 try { 104 status.checkCommit(); 105 } catch (LoginException e) { 106 // expected 107 } 108 109 assertEquals(LoginModuleUtils.ACTION.no_action, 110 status.checkLogout()); 111 112 status.logined(); 113 assertEquals(LoginModuleUtils.ACTION.no_action, 114 status.checkLogin()); 115 assertEquals(LoginModuleUtils.ACTION.commit, status 116 .checkCommit()); 117 assertEquals(LoginModuleUtils.ACTION.no_action, 118 status.checkLogout()); 119 120 status.committed(); 121 assertEquals(LoginModuleUtils.ACTION.no_action, 122 status.checkLogin()); 123 assertEquals(LoginModuleUtils.ACTION.no_action, 124 status.checkCommit()); 125 assertEquals(LoginModuleUtils.ACTION.logout, status 126 .checkLogout()); 127 128 status.logined(); 129 assertEquals(LoginModuleUtils.ACTION.no_action, 130 status.checkLogin()); 131 assertEquals(LoginModuleUtils.ACTION.commit, status 132 .checkCommit()); 133 assertEquals(LoginModuleUtils.ACTION.no_action, 134 status.checkLogout()); 135 } 136 137 } 138