Search in sources :

Example 46 with CouldntSaveDataException

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);
}
Also used : Field(java.lang.reflect.Field) CProject(com.google.security.zynamics.binnavi.disassembly.CProject) CDatabase(com.google.security.zynamics.binnavi.Database.CDatabase) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) CouldntLoadDriverException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) FileReadException(com.google.security.zynamics.binnavi.config.FileReadException) InvalidDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) IOException(java.io.IOException) InvalidDatabaseVersionException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseVersionException) CPartialLoadException(com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) Before(org.junit.Before)

Example 47 with CouldntSaveDataException

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);
    }
}
Also used : CProject(com.google.security.zynamics.binnavi.disassembly.CProject) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CAddressSpace(com.google.security.zynamics.binnavi.disassembly.AddressSpaces.CAddressSpace) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 48 with CouldntSaveDataException

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);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 49 with CouldntSaveDataException

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);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) Timestamp(java.sql.Timestamp) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Example 50 with CouldntSaveDataException

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());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Aggregations

CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)133 SQLException (java.sql.SQLException)71 PreparedStatement (java.sql.PreparedStatement)38 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)35 CallableStatement (java.sql.CallableStatement)20 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)17 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)13 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)10 CDefaultProgressOperation (com.google.security.zynamics.binnavi.Gui.Progress.CDefaultProgressOperation)10 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)9 ArrayList (java.util.ArrayList)9 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)8 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)8 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)8 BigInteger (java.math.BigInteger)8 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 TraceList (com.google.security.zynamics.binnavi.debug.models.trace.TraceList)7 LoadCancelledException (com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)6 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)5