use of org.exist.storage.io.VariableByteInputStream 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.storage.io.VariableByteInputStream in project exist by eXist-db.
the class SymbolTable method loadSymbols.
/**
* Read the global symbol table. The global symbol table stores QNames and
* namespace/prefix mappings.
*
* @throws EXistException in response to eXist-db error
*/
private synchronized void loadSymbols() throws EXistException {
try (final InputStream fis = new BufferedInputStream(Files.newInputStream(getFile()))) {
final VariableByteInput is = new VariableByteInputStream(fis);
final int magic = is.readFixedInt();
if (magic == LEGACY_FILE_FORMAT_VERSION_ID) {
LOG.info("Converting legacy symbols.dbx to new format...");
readLegacy(is);
saveSymbols();
} else if (magic != FILE_FORMAT_VERSION_ID) {
throw new EXistException("Symbol table was created by an older" + "or newer version of eXist" + " (file id: " + magic + "). " + "To avoid damage, the database will stop.");
} else {
read(is);
}
} catch (final FileNotFoundException e) {
throw new EXistException("Could not read " + this.getFile().toAbsolutePath().toString(), e);
} catch (final IOException e) {
throw new EXistException("IO error occurred while reading " + this.getFile().toAbsolutePath().toString() + ": " + e.getMessage(), e);
}
}
Aggregations