Search in sources :

Example 26 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLInstructionFunctions method createInstructions.

/**
 * Saves an instruction to the database.
 *
 * @param provider The provider used to access the database.
 * @param instructions The instruction to save.
 *
 * @throws SQLException Thrown if the instruction could not be created.
 */
public static void createInstructions(final SQLProvider provider, final Iterable<INaviInstruction> instructions) throws SQLException {
    Preconditions.checkNotNull(provider, "IE01550: Provider argument can not be null");
    Preconditions.checkNotNull(instructions, "IE01554: Instruction argument can not be null");
    final String query = "INSERT INTO " + CTableNames.INSTRUCTIONS_TABLE + "(module_id, address, mnemonic, data, native, architecture, comment_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)";
    final PreparedStatement insertStatement = provider.getConnection().getConnection().prepareStatement(query);
    final ArrayList<INaviInstruction> instructionsWithUnsavedComments = new ArrayList<INaviInstruction>();
    final List<List<COperandTree>> operands = new ArrayList<List<COperandTree>>();
    for (final INaviInstruction instruction : instructions) {
        final String mnemonic = instruction.getMnemonic();
        final byte[] data = instruction.getData();
        operands.add(instruction.getOperands());
        final INaviModule module = instruction.getModule();
        final IAddress address = instruction.getAddress();
        final int moduleID = module.getConfiguration().getId();
        final List<IComment> comments = instruction.getGlobalComment();
        final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
        if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
            instructionsWithUnsavedComments.add(instruction);
        }
        try {
            insertStatement.setInt(1, moduleID);
            insertStatement.setObject(2, address.toBigInteger(), Types.BIGINT);
            insertStatement.setString(3, mnemonic);
            insertStatement.setBytes(4, data);
            insertStatement.setBoolean(5, false);
            insertStatement.setObject(6, instruction.getArchitecture(), Types.OTHER);
            if (commentId == null) {
                insertStatement.setNull(7, Types.INTEGER);
            } else {
                insertStatement.setInt(7, commentId);
            }
            insertStatement.execute();
        } finally {
            insertStatement.close();
        }
    }
    // unsaved comments.
    for (final INaviInstruction instruction : instructionsWithUnsavedComments) {
        final ArrayList<IComment> instructionComments = new ArrayList<IComment>();
        for (final IComment comment : instruction.getGlobalComment()) {
            try {
                final Integer commentId = PostgreSQLInstructionFunctions.appendGlobalInstructionComment(provider, instruction, comment.getComment(), comment.getUser().getUserId());
                final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                instructionComments.add(newComment);
            } catch (final CouldntSaveDataException exception) {
                CUtilityFunctions.logException(exception);
            }
        }
        instruction.initializeGlobalComment(instructionComments);
    }
    for (final List<COperandTree> operand : operands) {
        int position = 0;
        for (final COperandTree operandTree : operand) {
            createOperandTree(provider, operandTree, position);
            position++;
        }
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) BigInteger(java.math.BigInteger) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) ArrayList(java.util.ArrayList) List(java.util.List) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 27 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLCommentFunctions method loadCommentByCommentId.

/**
 * Loads comments by a comment root id it returns all ancestors of the comment.
 *
 * @param provider The provider used to access the database.
 * @param commentRootId The comment root id where to start the recursive query.
 * @return An array of comments from and including the comment referenced by the comment root id.
 *
 * @throws CouldntLoadDataException if the data could not be loaded from the database.
 */
public static ArrayList<IComment> loadCommentByCommentId(final SQLProvider provider, final int commentRootId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE00440: connection argument can not be null");
    final HashMap<Integer, IComment> commentIdToComment = new HashMap<>();
    final ArrayList<IComment> comments = new ArrayList<>();
    final String commentQuery = "SELECT * FROM get_all_comment_ancestors(" + commentRootId + ");";
    try (ResultSet resultSet = provider.getConnection().executeQuery(commentQuery, true)) {
        while (resultSet.next()) {
            resultSet.getInt("level");
            final int commentId = resultSet.getInt("id");
            Integer parentId = resultSet.getInt("parent_id");
            if (resultSet.wasNull()) {
                parentId = null;
            }
            final int userId = resultSet.getInt("user_id");
            final String commentText = resultSet.getString("comment");
            final CComment comment = new CComment(commentId, CUserManager.get(provider).getUserById(userId), commentIdToComment.get(parentId), commentText);
            commentIdToComment.put(commentId, comment);
            comments.add(comment);
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return comments;
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Example 28 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLFunctionNodeCommentTests method deleteFunctionNodeComment4.

@Test
public void deleteFunctionNodeComment4() throws CouldntLoadDataException, CouldntSaveDataException, CouldntDeleteException {
    final List<IComment> comments = functionNode.getLocalFunctionComment() == null ? new ArrayList<IComment>() : functionNode.getLocalFunctionComment();
    final IComment lastComment = comments.size() == 0 ? null : Iterables.getLast(comments);
    final String commentString = " TEST DELETE FUNCTION NODE COMMENT ";
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final int commentId = getProvider().appendFunctionNodeComment(functionNode, commentString, user.getUserId());
    final IComment newComment = new CComment(commentId, user, lastComment, commentString);
    final ArrayList<IComment> storedComments = getProvider().loadCommentById(commentId);
    assertNotNull(storedComments);
    assertEquals(comments.size() + 1, storedComments.size());
    assertEquals(newComment, storedComments.get(storedComments.size() - 1));
    getProvider().deleteFunctionNodeComment(functionNode, commentId, newComment.getUser().getUserId());
    final ArrayList<IComment> commentsAfterDelete = getProvider().loadCommentById(commentId);
    assertNotNull(commentsAfterDelete);
    assertTrue(commentsAfterDelete.isEmpty());
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 29 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLGlobalEdgeCommentTests method deleteGlobalEdgeComment6.

/**
 * This test checks if the delete of a comment in a series of comments works if the comment is a
 * comment in the middle.
 *
 * <pre>
 * Comment 1:      Comment 1:
 * Comment 2:  ->
 * Comment 3:      Comment 3:
 * </pre>
 */
@Test
public void deleteGlobalEdgeComment6() throws CouldntLoadDataException, CouldntSaveDataException, CouldntDeleteException {
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final List<IComment> storedComments = edge.getGlobalComment() == null ? new ArrayList<IComment>() : edge.getGlobalComment();
    final IComment lastComment = storedComments.isEmpty() ? null : Iterables.getLast(storedComments);
    final String comment1String = " Comment 1: ";
    final int comment1Id = getProvider().appendGlobalEdgeComment(edge, comment1String, user.getUserId());
    final IComment comment1 = new CComment(comment1Id, user, lastComment, comment1String);
    final String comment2String = " Comment 2: ";
    final int comment2Id = getProvider().appendGlobalEdgeComment(edge, comment2String, user.getUserId());
    final IComment comment2 = new CComment(comment2Id, user, comment1, comment2String);
    final String comment3String = " Comment 3: ";
    final int comment3Id = getProvider().appendGlobalEdgeComment(edge, comment3String, user.getUserId());
    final IComment comment3 = new CComment(comment3Id, user, comment2, comment3String);
    final ArrayList<IComment> commentsBeforeDelete = getProvider().loadCommentById(comment3Id);
    assertNotNull(commentsBeforeDelete);
    assertEquals(storedComments.size() + 3, commentsBeforeDelete.size());
    assertTrue(commentsBeforeDelete.contains(comment1));
    assertTrue(commentsBeforeDelete.contains(comment2));
    assertTrue(commentsBeforeDelete.contains(comment3));
    assertEquals(comment3, Iterables.getLast(commentsBeforeDelete));
    assertEquals(comment2, commentsBeforeDelete.get(commentsBeforeDelete.size() - 2));
    assertEquals(comment1, commentsBeforeDelete.get(commentsBeforeDelete.size() - 3));
    getProvider().deleteGlobalEdgeComment(edge, comment2Id, user.getUserId());
    final ArrayList<IComment> commentsAfterDelete1 = getProvider().loadCommentById(comment2Id);
    assertNotNull(commentsAfterDelete1);
    assertTrue(commentsAfterDelete1.isEmpty());
    final ArrayList<IComment> commentsAfterDelete2 = getProvider().loadCommentById(comment3Id);
    assertNotNull(commentsAfterDelete2);
    assertEquals(storedComments.size() + 2, commentsAfterDelete2.size());
    final IComment comment3AfterDelete = new CComment(comment3Id, user, comment1, comment3String);
    assertTrue(commentsAfterDelete2.contains(comment3AfterDelete));
    assertTrue(commentsAfterDelete2.contains(comment1));
    assertEquals(comment3AfterDelete, Iterables.getLast(commentsAfterDelete2));
    assertEquals(comment1, commentsAfterDelete2.get(commentsAfterDelete2.size() - 2));
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 30 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLGlobalEdgeCommentTests method appendGlobalEdgeComment4.

@Test
public void appendGlobalEdgeComment4() throws CouldntLoadDataException, LoadCancelledException, CouldntSaveDataException, CPartialLoadException {
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final List<IComment> storedComments = edge.getGlobalComment() == null ? new ArrayList<IComment>() : edge.getGlobalComment();
    final IComment lastComment = storedComments.isEmpty() ? null : Iterables.getLast(storedComments);
    final String firstCommentString = " PASS GLOBAL EDGE COMMENT WITHOUT PARENT ID ";
    final int firstCommentId = getProvider().appendGlobalEdgeComment(edge, firstCommentString, user.getUserId());
    final IComment firstComment = new CComment(firstCommentId, user, lastComment, firstCommentString);
    final String secondCommentString = " PASS GLOBAL EDGE COMMENT WITH PARENT ID ";
    final int secondCommentId = getProvider().appendGlobalEdgeComment(edge, secondCommentString, user.getUserId());
    final IComment secondComment = new CComment(secondCommentId, user, firstComment, secondCommentString);
    final ArrayList<IComment> commentsFromDatabase = getProvider().loadCommentById(secondCommentId);
    assertNotNull(commentsFromDatabase);
    assertEquals(storedComments.size() + 2, commentsFromDatabase.size());
    assertTrue(commentsFromDatabase.contains(firstComment));
    assertTrue(commentsFromDatabase.contains(secondComment));
    final CNaviViewEdge callgraphEdge = (CNaviViewEdge) loadCallGraphEdge(getKernel32Module());
    getProvider().appendGlobalEdgeComment(callgraphEdge, " PASS GLOBAL CALLGRAPH EDGE COMMENT WITHOUT PARENT ID ", user.getUserId());
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Aggregations

IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)167 Test (org.junit.Test)129 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)114 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)97 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)89 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)89 ArrayList (java.util.ArrayList)20 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)18 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)16 INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)13 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)12 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)11 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)10 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)10 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)9 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)9 PreparedStatement (java.sql.PreparedStatement)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)7 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)7 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)7