You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ad...@apache.org on 2003/11/18 06:20:12 UTC
cvs commit: incubator-geronimo/modules/core/src/test/org/apache/geronimo/security LoginPropertiesFileTest.java LoginSQLTest.java
adc 2003/11/17 21:20:12
Added: modules/core/src/test/org/apache/geronimo/security
LoginPropertiesFileTest.java LoginSQLTest.java
Log:
Some login tests
Revision Changes Path
1.1 incubator-geronimo/modules/core/src/test/org/apache/geronimo/security/LoginPropertiesFileTest.java
Index: LoginPropertiesFileTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.security;
import junit.framework.TestCase;
import javax.management.MBeanServer;
import javax.security.auth.login.LoginContext;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.NameCallback;
import org.apache.geronimo.security.providers.PropertiesFileSecurityRealm;
import org.apache.geronimo.test.util.ServerUtil;
import java.io.File;
import java.io.IOException;
/**
*
* @version $Revision: 1.1 $ $Date: 2003/11/18 05:20:12 $
*/
public class LoginPropertiesFileTest extends TestCase {
MBeanServer server;
SecurityService securityService;
public void setUp() throws Exception {
server = ServerUtil.newLocalServer();
securityService = new SecurityService();
server.registerMBean(securityService, null);
PropertiesFileSecurityRealm c = new PropertiesFileSecurityRealm();
c.setRealmName("Foo");
c.setUsersURI((new File(new File("."), "src/test-data/data/users.properties")).toURI());
c.setGroupsURI((new File(new File("."), "src/test-data/data/groups.properties")).toURI());
server.registerMBean(c, null);
securityService.startRecursive();
}
public void tearDown() throws Exception {
securityService.stop();
ServerUtil.stopLocalServer(server);
}
public void testLogin() throws Exception {
GeronimoLoginConfiguration.setMBeanServer(server);
Subject subject = new Subject();
CallbackHandler handler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword("starcraft".toCharArray());
} else if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName("alan");
}
}
}
};
LoginContext context = new LoginContext("Foo", subject, handler);
context.login();
Subject rSubject = context.getSubject();
}
}
1.1 incubator-geronimo/modules/core/src/test/org/apache/geronimo/security/LoginSQLTest.java
Index: LoginSQLTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.security;
import junit.framework.TestCase;
import javax.management.MBeanServer;
import javax.security.auth.login.LoginContext;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.NameCallback;
import org.apache.geronimo.security.providers.SQLSecurityRealm;
import org.apache.geronimo.test.util.ServerUtil;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.Connection;
/**
*
* @version $Revision: 1.1 $ $Date: 2003/11/18 05:20:12 $
*/
public class LoginSQLTest extends TestCase {
MBeanServer server;
SecurityService securityService;
public void setUp() throws Exception {
DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
Connection conn = DriverManager.getConnection("jdbc:hsqldb:target/database/LoginSQLTest", "sa", "");
conn.prepareStatement("CREATE USER loginmodule PASSWORD password ADMIN;").executeQuery();
conn.prepareStatement("CREATE TABLE Users(UserName VARCHAR(16), Password VARCHAR(16));").executeQuery();
conn.prepareStatement("CREATE TABLE Groups(GroupName VARCHAR(16), UserName VARCHAR(16));").executeQuery();
conn.prepareStatement("GRANT SELECT ON Users TO loginmodule;").executeQuery();
conn.prepareStatement("GRANT SELECT ON Groups TO loginmodule;").executeQuery();
conn.prepareStatement("INSERT INTO Users VALUES ('izumi', 'violin');").executeQuery();
conn.prepareStatement("INSERT INTO Users VALUES ('alan', 'starcraft');").executeQuery();
conn.prepareStatement("INSERT INTO Users VALUES ('george', 'bone');").executeQuery();
conn.prepareStatement("INSERT INTO Users VALUES ('gracie', 'biscuit');").executeQuery();
conn.prepareStatement("INSERT INTO Users VALUES ('metro', 'mouse');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('manager', 'izumi');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('it', 'alan');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('pet', 'george');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('pet', 'gracie');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('pet', 'metro');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('dog', 'george');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('dog', 'gracie');").executeQuery();
conn.prepareStatement("INSERT INTO Groups VALUES ('cat', 'metro');").executeQuery();
conn.close();
server = ServerUtil.newLocalServer();
securityService = new SecurityService();
server.registerMBean(securityService, null);
SQLSecurityRealm c = new SQLSecurityRealm();
c.setRealmName("Foo");
c.setConnectionURL("jdbc:hsqldb:target/database/LoginSQLTest");
c.setUser("loginmodule");
c.setPassword("password");
server.registerMBean(c, null);
securityService.startRecursive();
}
public void tearDown() throws Exception {
securityService.stop();
ServerUtil.stopLocalServer(server);
Connection conn = DriverManager.getConnection("jdbc:hsqldb:target/database/LoginSQLTest", "sa", "");
conn.prepareStatement("DROP USER loginmodule;").executeQuery();
conn.prepareStatement("DROP TABLE Users;").executeQuery();
conn.prepareStatement("DROP TABLE Groups;").executeQuery();
}
public void testLogin() throws Exception {
GeronimoLoginConfiguration.setMBeanServer(server);
Subject subject = new Subject();
CallbackHandler handler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword("starcraft".toCharArray());
} else if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName("alan");
}
}
}
};
LoginContext context = new LoginContext("Foo", subject, handler);
context.login();
Subject rSubject = context.getSubject();
}
}