use of org.exist.Database in project exist by eXist-db.
the class SimpleACLPermissionTest method roundtrip_write_read.
@Test
public void roundtrip_write_read() throws PermissionDeniedException, IOException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(2);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(2);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(2);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(2);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
assertEquals(0, permission.getACECount());
final int userId1 = 1;
final int mode1 = ALL;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, userId1, mode1);
final int groupId2 = 2;
final int mode2 = Permission.READ;
permission.addGroupACE(ACE_ACCESS_TYPE.DENIED, groupId2, mode2);
final VariableByteOutputStream os = new VariableByteOutputStream();
// write the acl out
permission.write(os);
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
assertEquals(2, permission.getACECount());
assertEquals(ACE_ACCESS_TYPE.ALLOWED, permission.getACEAccessType(0));
assertEquals(userId1, permission.getACEId(0));
assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
assertEquals(mode1, permission.getACEMode(0));
assertEquals(ACE_ACCESS_TYPE.DENIED, permission.getACEAccessType(1));
assertEquals(groupId2, permission.getACEId(1));
assertEquals(ACE_TARGET.GROUP, permission.getACETarget(1));
assertEquals(mode2, permission.getACEMode(1));
// get the written acl data
final byte[] data = os.toByteArray();
// create a new permission instance
SimpleACLPermission permission2 = new SimpleACLPermission(mockSecurityManager);
// read the acl in
permission2.read(new VariableByteInputStream(new UnsynchronizedByteArrayInputStream(data)));
assertEquals(2, permission2.getACECount());
assertEquals(ACE_ACCESS_TYPE.ALLOWED, permission2.getACEAccessType(0));
assertEquals(userId1, permission2.getACEId(0));
assertEquals(ACE_TARGET.USER, permission2.getACETarget(0));
assertEquals(mode1, permission2.getACEMode(0));
assertEquals(ACE_ACCESS_TYPE.DENIED, permission2.getACEAccessType(1));
assertEquals(groupId2, permission2.getACEId(1));
assertEquals(ACE_TARGET.GROUP, permission2.getACETarget(1));
assertEquals(mode2, permission2.getACEMode(1));
}
use of org.exist.Database in project exist by eXist-db.
the class SimpleACLPermissionTest method validate_cant_read_ACL_ordered_entries.
@Test
public void validate_cant_read_ACL_ordered_entries() throws PermissionDeniedException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(2);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(2);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(2);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(2);
final int ownerId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
final int mode = 0700;
final int ownerGroupId = new Random().nextInt(SecurityManagerImpl.MAX_GROUP_ID);
/**
* We create ACE entries which has -
* 1) user "userA" who is DENIED READ access
* 2) group "groupA" who is ALLOWED READ access
*
* userA is a member of groupA, and so should still be DENIED read access
* as the ACL is evaluated top-down
*/
final Subject mockUser = EasyMock.createMock(Subject.class);
final boolean mockUserHasDbaRole = false;
final int mockUserId = new Random().nextInt(SecurityManagerImpl.MAX_USER_ID);
final int mockGroupId = new Random().nextInt(SecurityManagerImpl.MAX_GROUP_ID);
expect(mockUser.hasDbaRole()).andReturn(mockUserHasDbaRole);
expect(mockUser.getId()).andReturn(mockUserId);
expect(mockUser.getGroupIds()).andReturn(new int[0]);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject, mockUser);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager, ownerId, ownerGroupId, mode);
permission.addUserACE(ACE_ACCESS_TYPE.DENIED, mockUserId, Permission.READ);
permission.addGroupACE(ACE_ACCESS_TYPE.ALLOWED, mockGroupId, Permission.READ);
boolean isValid = permission.validate(mockUser, Permission.READ);
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject, mockUser);
assertFalse(isValid);
}
use of org.exist.Database in project exist by eXist-db.
the class SimpleACLPermissionTest method insert_atEnd.
@Test(expected = PermissionDeniedException.class)
public void insert_atEnd() throws PermissionDeniedException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(3);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(3);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(3);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(3);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
assertEquals(0, permission.getACECount());
final int userId = 1112;
final int mode = ALL;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, userId, mode);
assertEquals(1, permission.getACECount());
assertEquals(userId, permission.getACEId(0));
assertEquals(ACE_ACCESS_TYPE.ALLOWED, permission.getACEAccessType(0));
assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
assertEquals(ALL, permission.getACEMode(0));
final int secondUserId = 1113;
final int secondMode = 04;
permission.insertUserACE(1, ACE_ACCESS_TYPE.ALLOWED, secondUserId, secondMode);
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
}
use of org.exist.Database in project exist by eXist-db.
the class SimpleACLPermissionTest method remove_middleACE.
@Test
public void remove_middleACE() throws PermissionDeniedException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(4);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(4);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(4);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(4);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
assertEquals(0, permission.getACECount());
final int firstUserId = 1;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, firstUserId, ALL);
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, 2, ALL);
final int thirdUserId = 3;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, thirdUserId, ALL);
assertEquals(3, permission.getACECount());
permission.removeACE(1);
assertEquals(2, permission.getACECount());
assertEquals(firstUserId, permission.getACEId(0));
assertEquals(thirdUserId, permission.getACEId(1));
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
}
use of org.exist.Database in project exist by eXist-db.
the class SimpleACLPermissionTest method remove_lastACE.
@Test
public void remove_lastACE() throws PermissionDeniedException {
final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
final Database mockDatabase = EasyMock.createMock(Database.class);
final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(3);
expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(3);
expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(3);
expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(3);
replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
assertEquals(0, permission.getACECount());
final int firstUserId = 1;
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, firstUserId, ALL);
permission.addUserACE(ACE_ACCESS_TYPE.ALLOWED, 2, ALL);
assertEquals(2, permission.getACECount());
permission.removeACE(1);
assertEquals(1, permission.getACECount());
assertEquals(firstUserId, permission.getACEId(0));
verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
}
Aggregations