use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLTypeInstanceFunctionsTests method setUp.
@Before
public void setUp() throws IOException, CouldntLoadDriverException, CouldntConnectException, IllegalStateException, CouldntLoadDataException, InvalidDatabaseException, CouldntInitializeDatabaseException, CouldntSaveDataException, InvalidExporterDatabaseFormatException, InvalidDatabaseVersionException, LoadCancelledException, FileReadException {
final String[] parts = CConfigLoader.loadPostgreSQL();
database = new CDatabase("None", CJdbcDriverNames.jdbcPostgreSQLDriverName, parts[0], "test_disassembly", parts[1], parts[2], parts[3], false, false);
database.connect();
database.load();
try {
final Field privateProviderField = CDatabase.class.getDeclaredField("provider");
privateProviderField.setAccessible(true);
provider = (SQLProvider) privateProviderField.get(database);
} catch (final Exception exception) {
throw new RuntimeException(exception);
}
provider.createDebuggerTemplate("Test Debugger", "localhost", 2222);
final CProject project = provider.createProject("Test Project");
provider.createAddressSpace(project, "Test Address Space");
ConfigManager.instance().read();
module = database.getContent().getModules().get(0);
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLProviderTest method testAddModule1.
@Test
public void testAddModule1() throws CouldntSaveDataException, CouldntLoadDataException, CouldntDeleteException {
final INaviModule module = getProvider().loadModules().get(0);
final CProject project = getProvider().createProject("FOOBAR_PROJECT");
getProvider().createAddressSpace(project, "FOOBAR_ADDRESS_SPACE");
final CAddressSpace as = getProvider().loadAddressSpaces(project).get(0);
getProvider().addModule(as, module);
try {
getProvider().addModule(as, module);
fail();
} catch (final CouldntSaveDataException exception) {
getProvider().removeModule(as, module);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLProjectCreator method createProject.
/**
* Creates a new project in the database.
*
* @param provider The SQL provider that provides the connection.
* @param name The name of the new project.
*
* @return The created project.
*
* @throws CouldntSaveDataException Thrown if the project could not be created.
*/
public static CProject createProject(final AbstractSQLProvider provider, final String name) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE00513: Provider argument can not be null");
Preconditions.checkNotNull(name, "IE00514: Project names can not be null");
Preconditions.checkArgument(!("".equals(name)), "IE00515: Project names can not be empty");
final CConnection connection = provider.getConnection();
NaviLogger.info("Creating new project %s", name);
final String query = "INSERT INTO " + CTableNames.PROJECTS_TABLE + "(name, description, creation_date, modification_date) VALUES(?, '', NOW(), NOW()) RETURNING id";
try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
statement.setString(1, name);
ResultSet resultSet = statement.executeQuery();
Integer id = null;
while (resultSet.next()) {
if (resultSet.isFirst()) {
id = resultSet.getInt(1);
break;
}
}
Preconditions.checkNotNull(id, "IE02044: Error id for a project after creation may not be null");
return PostgreSQLProjectCreator.loadProject(provider, id);
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLViewCreator method createView.
/**
* Inserts a new view in the database by copying an existing view.
*
* @param provider The connection to the database.
* @param containerId The ID of the container where the view is created.
* @param view The view to be copied.
* @param name The name of the new view.
* @param description The description of the new view.
* @param containerTable Name of the view container table.
* @param viewContainerTable Name of the view container views table.
* @param generator Generates the view.
* @return The created view.
* @throws CouldntSaveDataException Thrown if the view could not be created.
*/
private static CView createView(final AbstractSQLProvider provider, final int containerId, final INaviView view, final String name, final String description, final String containerTable, final String viewContainerTable, final ViewGenerator generator) throws CouldntSaveDataException {
final CConnection connection = provider.getConnection();
try {
PostgreSQLHelpers.beginTransaction(connection);
final int viewId = insertView(connection, name, description);
// Mark the view as a module view
connection.executeUpdate("INSERT INTO " + viewContainerTable + " VALUES(" + containerId + ", " + viewId + ")", true);
final List<INaviViewNode> nodes = view.getGraph().getNodes();
final List<INaviEdge> edges = view.getGraph().getEdges();
// Store all nodes
PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
// Store all edges
PostgreSQLEdgeSaver.writeEdges(provider, edges);
PostgreSQLHelpers.endTransaction(connection);
final String query = "SELECT creation_date, modification_date FROM " + CTableNames.VIEWS_TABLE + " WHERE id = " + viewId;
final ResultSet resultSet = connection.executeQuery(query, true);
try {
while (resultSet.next()) {
final Timestamp creationDate = resultSet.getTimestamp("creation_date");
final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
return generator.generate(viewId, name, description, ViewType.NonNative, view.getGraphType(), creationDate, modificationDate, view.getNodeCount(), view.getEdgeCount(), new HashSet<CTag>(), new HashSet<CTag>(), false);
}
throw new CouldntSaveDataException("Error: Couldnt't load the created view");
} finally {
resultSet.close();
}
} catch (final SQLException exception) {
CUtilityFunctions.logException(exception);
try {
PostgreSQLHelpers.rollback(connection);
} catch (final SQLException e) {
CUtilityFunctions.logException(e);
}
throw new CouldntSaveDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLAddressSpaceFunctions method setImageBase.
/**
* Changes the image base of a module inside an address space. If updating the image base was
* successful, the modification date of the address space is updated.
*
* The address space and the module must both be stored in the database connected to by the
* provider argument.
*
* TODO (timkornau): What happens if the module does not belong to the address space? This
* situation should be handled explicitly in the future.
*
* @param provider The SQL provider that provides the database connection.
* @param addressSpace The address space the module belongs to.
* @param module The module whose image base is changed.
* @param address The new image base of the module in the address space.
*
* @throws CouldntSaveDataException Thrown if the new image base value could not be stored in the
* database.
*/
public static void setImageBase(final AbstractSQLProvider provider, final INaviAddressSpace addressSpace, final INaviModule module, final IAddress address) throws CouldntSaveDataException {
checkArguments(provider, addressSpace);
Preconditions.checkNotNull(module, "IE00396: Module argument can not be null");
Preconditions.checkNotNull(address, "IE00397: Address argument can not be null");
Preconditions.checkArgument(module.inSameDatabase(provider), "IE00398: Module is not part of this database");
final CConnection connection = provider.getConnection();
try {
final String query = String.format("UPDATE %s SET image_base = %s " + " WHERE module_id = %d AND address_space_id = %d", CTableNames.SPACE_MODULES_TABLE, address.toBigInteger().toString(), module.getConfiguration().getId(), addressSpace.getConfiguration().getId());
connection.executeUpdate(query, true);
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
PostgreSQLHelpers.updateModificationDate(connection, CTableNames.ADDRESS_SPACES_TABLE, addressSpace.getConfiguration().getId());
}
Aggregations