ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/users/TestDataSourceUserDatabase.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/users/TestDataSourceUserDatabase.java
Status
scanned
Type
file
Name
TestDataSourceUserDatabase.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
9720 (9.5 KB)
MD5
3b1c5bd1137c3a9949f1a8f852429021
SHA1
446e87845080724a2b3f70b5753288606c58efb7
SHA256
479b9bbe19d820bcac30b5bdd580f2a75d019eae94275be801605e540e043fb3
SHA512

      
    
SHA1_git
0e0a3c1561d098526505ce4ab2cf57c0f608e900
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestDataSourceUserDatabase.java | 9.5 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.catalina.users; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.util.Iterator; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Group; import org.apache.catalina.Role; import org.apache.catalina.User; import org.apache.catalina.startup.LoggingBaseTest; public class TestDataSourceUserDatabase extends LoggingBaseTest { public static final String SIMPLE_SCHEMA = "create table users ( " + " user_name varchar(15) not null primary key, " + " user_pass varchar(15) not null " + "); " + "create table user_roles ( " + " user_name varchar(15) not null, " + " role_name varchar(15) not null, " + " primary key (user_name, role_name) " + ");"; public static final String FULL_SCHEMA = "create table users ( " + " user_name varchar(15) not null primary key, " + " user_pass varchar(15) not null, " + " user_fullname varchar(128) " + " -- Add more attributes as needed " + "); " + "create table roles ( " + " role_name varchar(15) not null primary key, " + " role_description varchar(128) " + "); " + "create table groups ( " + " group_name varchar(15) not null primary key, " + " group_description varchar(128) " + "); " + "create table user_roles ( " + " user_name varchar(15) references users(user_name), " + " role_name varchar(15) references roles(role_name), " + " primary key (user_name, role_name) " + "); " + "create table user_groups ( " + " user_name varchar(15) references users(user_name), " + " group_name varchar(15) references groups(group_name), " + " primary key (user_name, group_name) " + "); " + "create table group_roles ( " + " group_name varchar(15) references groups(group_name), " + " role_name varchar(15) references roles(role_name), " + " primary key (group_name, role_name) " + ");"; protected class DerbyUserDatabase extends DataSourceUserDatabase { protected final String name; protected Connection connection = null; public DerbyUserDatabase(String name) { super(null, "tomcat"); this.name = "/" + name; } @Override protected Connection openConnection() { // Replace DataSource use and JNDI access with direct Derby // connection return connection; } @Override protected void closeConnection(Connection dbConnection) { } @Override public void close() throws Exception { if (connection != null) { connection.close(); } } @Override public void open() throws Exception { super.open(); Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); connection = DriverManager.getConnection("jdbc:derby:" + getTemporaryDirectory().getAbsolutePath() + name + ";create=true"); } public Connection getConnection() { return connection; } } private DerbyUserDatabase db; @Test public void testBasicUserRoleDatabase() throws Exception { // Test functionality with the DataSourceRealm schema db = new DerbyUserDatabase("simple"); db.setReadonly(false); db.setUserTable("users"); db.setUserNameCol("user_name"); db.setUserCredCol("user_pass"); db.setUserRoleTable("user_roles"); db.setRoleNameCol("role_name"); db.open(); // First create the DB tables Connection connection = db.getConnection(); for (String sql: SIMPLE_SCHEMA.split(";")) { try (Statement statement = connection.createStatement()) { statement.execute(sql); } } Iterator<User> users = db.getUsers(); Assert.assertFalse("Some users found", users.hasNext()); User tomcatUser = db.createUser("tomcat", "password", "A new user"); Role adminRole = db.createRole("admin", "Admin role"); Role managerRole = db.createRole("manager", "Manager role"); Role userRole = db.createRole("user", "User role"); tomcatUser.addRole(adminRole); tomcatUser.addRole(userRole); db.save(); users = db.getUsers(); Assert.assertTrue("No users found", users.hasNext()); tomcatUser = users.next(); Assert.assertTrue("Wrong user", tomcatUser.getUsername().equals("tomcat")); Assert.assertTrue("Wrong password", tomcatUser.getPassword().equals("password")); // Cannot save the user full name Assert.assertNull("Wrong user fullname", tomcatUser.getFullName()); adminRole = db.findRole("admin"); Assert.assertNotNull("No admin role", adminRole); Assert.assertTrue("No role for user", tomcatUser.isInRole(adminRole)); // Manager role cannot be saved, but remains valid in memory managerRole = db.findRole("manager"); Assert.assertFalse("Unexpected role for user", tomcatUser.isInRole(managerRole)); db.close(); } @Test public void testUserDatabase() throws Exception { db = new DerbyUserDatabase("full"); db.setReadonly(false); db.setUserTable("users"); db.setUserNameCol("user_name"); db.setUserCredCol("user_pass"); db.setUserRoleTable("user_roles"); db.setUserGroupTable("user_groups"); db.setRoleTable("roles"); db.setRoleNameCol("role_name"); db.setGroupTable("groups"); db.setGroupNameCol("group_name"); db.setGroupRoleTable("group_roles"); // Not setting the description or full name since it allows checking persistence, // as any modification is kept in memory until save() db.open(); // First create the DB tables Connection connection = db.getConnection(); for (String sql: FULL_SCHEMA.split(";")) { try (Statement statement = connection.createStatement()) { statement.execute(sql); } } Iterator<User> users = db.getUsers(); Assert.assertFalse("Some users found", users.hasNext()); User tomcatUser = db.createUser("tomcat", "password", "A new user"); User randomUser = db.createUser("random", "password", "Another new user"); Role adminRole = db.createRole("admin", "Admin role"); Role managerRole = db.createRole("manager", "Manager role"); Role userRole = db.createRole("user", "User role"); Group userGroup = db.createGroup("users", "All users"); userGroup.addRole(userRole); tomcatUser.addRole(adminRole); tomcatUser.addGroup(userGroup); randomUser.addGroup(userGroup); db.save(); users = db.getUsers(); Assert.assertTrue("No users found", users.hasNext()); tomcatUser = users.next(); if (!tomcatUser.getUsername().equals("tomcat")) { tomcatUser = users.next(); } Assert.assertTrue("Wrong user", tomcatUser.getUsername().equals("tomcat")); Assert.assertTrue("Wrong password", tomcatUser.getPassword().equals("password")); // Cannot save the user full name Assert.assertNull("Wrong user fullname", tomcatUser.getFullName()); adminRole = db.findRole("admin"); Assert.assertNotNull("No admin role", adminRole); Assert.assertNull("Wrong admin role", adminRole.getDescription()); Assert.assertTrue("No role for user", tomcatUser.isInRole(adminRole)); managerRole = db.findRole("manager"); Assert.assertFalse("Unexpected role for user", tomcatUser.isInRole(managerRole)); userRole = db.findRole("user"); userGroup = db.findGroup("users"); Assert.assertNull("Wrong users group", userGroup.getDescription()); Assert.assertTrue("No role for group", userGroup.isInRole(userRole)); randomUser = db.findUser("random"); Assert.assertTrue("No group for user", randomUser.isInGroup(userGroup)); db.close(); } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
12.69
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9