Search in sources :

Example 1 with CommentNotificationContainer

use of com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer in project binnavi by google.

the class PostgreSQLNotificationParserTest method testCommentTableNotificationParser1.

@Test
public void testCommentTableNotificationParser1() {
    CommentManager.get(provider).initializeGlobalInstructionComment(instruction, Lists.newArrayList(comment1, comment2, comment3));
    final String COMMENTS_TABLE_NOTIFICATION_1 = "bn_comments UPDATE 3333 2222 " + CommonTestObjects.TEST_USER_3.getUserId() + " AAA";
    final CommentNotification result = PostgreSQLCommentNotificationParser.processCommentNotification(new MockPGNotification("comment_changes", COMMENTS_TABLE_NOTIFICATION_1), provider);
    assertNotNull(result);
    final CommentNotificationContainer container = (CommentNotificationContainer) result;
    final IComment comment = container.getCurrentComment();
    assertEquals(comment3, comment);
    final IComment testComment = new CComment(3333, CommonTestObjects.TEST_USER_3, comment2, "AAA");
    assertEquals(testComment, container.getNewComment());
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CommentNotification(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.interfaces.CommentNotification) GroupNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.GroupNodeCommentNotificationContainer) FunctionNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionNodeCommentNotificationContainer) InstructionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer) TypeInstanceCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TypeInstanceCommentNotificationContainer) TextNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TextNodeCommentNotificationContainer) 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) EdgeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer) Test(org.junit.Test)

Example 2 with CommentNotificationContainer

use of com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer 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)

Aggregations

CodeNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CodeNodeCommentNotificationContainer)2 CommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CommentNotificationContainer)2 EdgeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer)2 FunctionCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionCommentNotificationContainer)2 FunctionNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionNodeCommentNotificationContainer)2 GroupNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.GroupNodeCommentNotificationContainer)2 InstructionCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer)2 TextNodeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TextNodeCommentNotificationContainer)2 TypeInstanceCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TypeInstanceCommentNotificationContainer)2 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)2 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)2 CommentNotification (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.interfaces.CommentNotification)1 CommentOperation (com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation)1 BigInteger (java.math.BigInteger)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1