Search in sources :

Example 61 with CComment

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

the class PostgreSQLLocalEdgeCommentTests method editLocalEdgeComment6.

@Test(expected = CouldntSaveDataException.class)
public void editLocalEdgeComment6() throws CouldntLoadDataException, CouldntSaveDataException {
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final List<IComment> storedComments = edge.getLocalComment() == null ? new ArrayList<IComment>() : edge.getLocalComment();
    final IComment lastComment = storedComments.isEmpty() ? null : Iterables.getLast(storedComments);
    final String beforeEditCommentString = " EDIT LOCAL EDGE COMMENT BEFORE EDIT ";
    final int editedCommentId = getProvider().appendLocalEdgeComment(edge, beforeEditCommentString, user.getUserId());
    final IComment commentBeforeEdit = new CComment(editedCommentId, user, lastComment, beforeEditCommentString);
    final ArrayList<IComment> commentsFromDatabase = getProvider().loadCommentById(editedCommentId);
    assertNotNull(commentsFromDatabase);
    assertTrue(commentsFromDatabase.contains(commentBeforeEdit));
    assertEquals(storedComments.size() + 1, commentsFromDatabase.size());
    final IUser wrongUser = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    getProvider().editLocalEdgeComment(edge, editedCommentId, wrongUser.getUserId(), " FAIL ");
}
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 62 with CComment

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

the class PostgreSQLLocalEdgeCommentTests method editLocalEdgeComment5.

@Test
public void editLocalEdgeComment5() throws CouldntSaveDataException, CouldntLoadDataException {
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final List<IComment> storedComments = edge.getLocalComment() == null ? new ArrayList<IComment>() : edge.getLocalComment();
    final IComment lastComment = storedComments.isEmpty() ? null : Iterables.getLast(storedComments);
    final String beforeEditCommentString = " EDIT LOCAL EDGE COMMENT BEFORE EDIT ";
    final int editedCommentId = getProvider().appendLocalEdgeComment(edge, beforeEditCommentString, user.getUserId());
    final IComment commentBeforeEdit = new CComment(editedCommentId, user, lastComment, beforeEditCommentString);
    final ArrayList<IComment> commentsFromDatabase = getProvider().loadCommentById(editedCommentId);
    assertNotNull(commentsFromDatabase);
    assertTrue(commentsFromDatabase.contains(commentBeforeEdit));
    assertEquals(storedComments.size() + 1, commentsFromDatabase.size());
    final String afterEditCommentString = " EDIT LOCAL EDGE COMMENT AFTER EDIT ";
    getProvider().editLocalEdgeComment(edge, editedCommentId, user.getUserId(), afterEditCommentString);
    final IComment commentAfterEdit = new CComment(editedCommentId, user, lastComment, afterEditCommentString);
    final ArrayList<IComment> commentsAfterEdit = getProvider().loadCommentById(editedCommentId);
    assertNotNull(commentsAfterEdit);
    assertTrue(commentsAfterEdit.contains(commentAfterEdit));
}
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 63 with CComment

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

the class PostgreSQLLocalInstructionCommentTests method editLocalInstructionComment6.

@Test
public void editLocalInstructionComment6() throws CouldntLoadDataException, CouldntSaveDataException {
    final List<IComment> comments = codeNode.getComments().getLocalInstructionComment(instruction) == null ? new ArrayList<IComment>() : codeNode.getComments().getLocalInstructionComment(instruction);
    final IComment lastComment = comments.size() == 0 ? null : Iterables.getLast(comments);
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final String commentText = " INSTRUCTION COMMENT TEST BEFORE EDIT ";
    final Integer commentId = getProvider().appendLocalInstructionComment(codeNode, instruction, commentText, user.getUserId());
    final IComment newComment = new CComment(commentId, user, lastComment, commentText);
    final ArrayList<IComment> newComments = getProvider().loadCommentById(commentId);
    assertNotNull(newComments);
    assertEquals(comments.size() + 1, newComments.size());
    assertEquals(newComment, newComments.get(newComments.size() - 1));
    final String commentAfterEdit = " INSTRUCTION COMMENT TEST AFTER EDIT ";
    getProvider().editLocalInstructionComment(codeNode, instruction, commentId, user.getUserId(), commentAfterEdit);
    final ArrayList<IComment> commentsAfterEdit = PostgreSQLCommentFunctions.loadCommentByCommentId(getProvider(), commentId);
    assertEquals(commentAfterEdit, commentsAfterEdit.get(commentsAfterEdit.size() - 1).getComment());
    assertEquals(commentsAfterEdit.size(), newComments.size());
}
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 64 with CComment

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

the class PostgreSQLCommentNotificationParser method processCommentNotification.

/**
   * Parses a {@link PGNotification} notification from the database back end for comment table
   * changes. These changes can not directly be mapped to any of the commentable objects as the
   * relation to which commentable object they belong is not in the notification message. This
   * notification is generated for the following situations for any commentable object:
   *
   * <pre>
   *
   *Delete a comment:
   *
   *[1] Comment 1    [1] Comment 1
   *[2] Comment 2 ->
   *[3] Comment 3    [3] Comment 3
   *
   *Edit a comment:
   *
   *[1] Comment 1    [1] Comment 1
   *[2] Comment 2 -> [2] Edited Comment 2
   *[3] Comment 3    [3] Comment 3
   *
   *</pre>
   *
   * @param notification The {@link PGNotification} from the PostgreSQL database server.
   * @param provider The {@link SQLProvider} which is used to communicate with the database.
   */
static CommentNotification processCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = COMMENTS_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    Integer commentId = null;
    try {
        commentId = Integer.parseInt(matcher.group(3));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    final IComment comment = CommentManager.get(provider).getCommentById(commentId);
    if (comment == null) {
        return null;
    }
    final String databaseOperation = matcher.group(2);
    Integer parentId = null;
    try {
        parentId = matcher.group(4).equalsIgnoreCase("null") ? null : Integer.parseInt(matcher.group(4));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    // parent.
    if (databaseOperation.equals("DELETE")) {
        if (((parentId == null) && (comment.getParent() != null)) || ((parentId != null) && (comment.getParent() != null) && (!parentId.equals(comment.getParent().getId())))) {
            final Integer localCommentParentId = parentId;
            final Integer notificationCommentParentId = comment.getParent() != null ? comment.getParent().getId() : null;
            throw new IllegalStateException("IE02521: The parent comment of the localy stored comment: " + localCommentParentId + " is not equal to the " + "notification comments parent comment: " + notificationCommentParentId);
        }
    }
    final String commentContent = matcher.group(9);
    if (!commentContent.equals(comment.getComment()) && databaseOperation.equals("DELETE")) {
        throw new IllegalStateException("IE02522: The local comments comment: " + comment.getComment() + "is not equal to the notification comments content: " + commentContent);
    }
    Integer commentUserId = null;
    try {
        commentUserId = Integer.parseInt(matcher.group(7));
    } catch (final NumberFormatException exception) {
        throw new IllegalStateException(exception);
    }
    if (!commentUserId.equals(comment.getUser().getUserId())) {
        throw new IllegalStateException("IE02523: The user of the localy stored comment: " + commentUserId + " is not equal to the " + "notifications comments user: " + comment.getUser().getUserId());
    }
    final IComment parentComment = CommentManager.get(provider).getCommentById(parentId);
    final IComment newComment = new CComment(comment.getId(), comment.getUser(), parentComment, commentContent);
    final CommentOperation operation = databaseOperation.equalsIgnoreCase("UPDATE") ? CommentOperation.EDIT : CommentOperation.DELETE;
    return new CommentNotificationContainer(comment, newComment, operation);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) Matcher(java.util.regex.Matcher) TypeInstanceCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TypeInstanceCommentNotificationContainer) GroupNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.GroupNodeCommentNotificationContainer) TextNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TextNodeCommentNotificationContainer) FunctionNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionNodeCommentNotificationContainer) CodeNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CodeNodeCommentNotificationContainer) CommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer) FunctionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionCommentNotificationContainer) InstructionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer) EdgeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer)

Example 65 with CComment

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

the class PostgreSQLNodeSaver method saveTextNodes.

/**
   * Saves the text nodes to the database.
   *
   * @param provider The connection to the database.
   * @param nodes The nodes to save.
   * @param firstNode The database index of the first node.
   * @param textNodeIndices Index into the nodes list that identifies the text nodes.
   *
   * @throws SQLException Thrown if saving the text nodes failed.
   */
protected static void saveTextNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> textNodeIndices) throws SQLException {
    Preconditions.checkNotNull(provider, "IE02527: provider argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02528: nodes argument can not be null");
    Preconditions.checkNotNull(textNodeIndices, "IE02529: textNodeIndices argument can not be null");
    if (!textNodeIndices.isEmpty()) {
        final String query = "INSERT INTO " + CTableNames.TEXT_NODES_TABLE + "(node_id, comment_id) VALUES (?, ?)";
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        final List<INaviTextNode> textNodesWithUnsavedComments = new ArrayList<INaviTextNode>();
        try {
            for (final Integer index : textNodeIndices) {
                final INaviTextNode node = (INaviTextNode) nodes.get(index);
                final List<IComment> comment = node.getComments();
                final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
                if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
                    textNodesWithUnsavedComments.add(node);
                }
                preparedStatement.setInt(1, firstNode + index);
                if (commentId == null) {
                    preparedStatement.setNull(2, Types.INTEGER);
                } else {
                    preparedStatement.setInt(2, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // unsaved comments to be stored. Possibly one can handle all of those in one query.
        for (final INaviTextNode textNode : textNodesWithUnsavedComments) {
            final ArrayList<IComment> textNodeComments = new ArrayList<IComment>();
            for (final IComment comment : textNode.getComments()) {
                try {
                    final Integer commentId = provider.appendTextNodeComment(textNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    textNodeComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            textNode.initializeComment(textNodeComments);
        }
    }
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)121 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)114 Test (org.junit.Test)104 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)96 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)89 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)89 ArrayList (java.util.ArrayList)13 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)11 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)10 INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)9 Date (java.util.Date)9 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)8 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)8 CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)8 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)7 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)6 CTextNode (com.google.security.zynamics.binnavi.disassembly.CTextNode)6 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)5 CFunction (com.google.security.zynamics.binnavi.disassembly.CFunction)5