Bug-1370: Reads message body bytes as well as exception is thrown during parsing. 27/9027/1
authorMilos Fabian <milfabia@cisco.com>
Tue, 15 Jul 2014 14:25:13 +0000 (16:25 +0200)
committerMilos Fabian <milfabia@cisco.com>
Tue, 15 Jul 2014 15:26:23 +0000 (17:26 +0200)
Change-Id: I661d598ec662c7445aec93d56229eae3e67e1bd2
Signed-off-by: Milos Fabian <milfabia@cisco.com>
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java

index 3da6c166132fc5f07e3a0845feb00e4cc503efdb..146ac29f06ff557662c84b6e75d60dd80287d15a 100644 (file)
@@ -17,15 +17,12 @@ import static org.junit.matchers.JUnitMatchers.containsString;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -83,6 +80,13 @@ public class ParserTest {
         (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
         (byte) 0x01, (byte) 0x00, (byte) 0x01 };
 
+    public static final byte[] updateMsg = {
+        (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+        (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x51, 0x02, 0x00, 0x00, 0x00, 0x36, 0x40, 0x01, 0x01, 0x00, 0x40, 0x02,
+        0x1a, 0x02, 0x06, 0x00, 0x00, (byte) 0xfe, 0x55, 0x00, 0x00, (byte) 0xfc, 0x12, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x02, (byte) 0xbd, 0x00, 0x00, 0x0d, 0x1c, 0x00, 0x00, 0x6a, 0x74, 0x40, 0x03,
+        0x04, 0x0a, 0x20, 0x00, (byte) 0xfe, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x64, 0x18, (byte) 0xa8, (byte) 0xa1, (byte) 0xf7
+    };
+
     static MessageRegistry reg;
 
     @BeforeClass
@@ -306,4 +310,17 @@ public class ParserTest {
         // the capabilities can be swapped.
         assertTrue(Arrays.equals(openWithCpblt1, ByteArray.getAllBytes(result)) || Arrays.equals(openWithCpblt2, ByteArray.getAllBytes(result)));
     }
+
+    // https://bugs.opendaylight.org/show_bug.cgi?id=1370
+    // tests if all bytes are read after deserialization error occurs
+    @Test
+    public void testUpdateMsgParser() throws BGPParsingException {
+        final ByteBuf buffer = Unpooled.copiedBuffer(updateMsg);
+        try {
+            ParserTest.reg.parseMessage(buffer);
+            fail();
+        } catch(BGPDocumentedException e) {
+            assertEquals(0, buffer.readableBytes());
+        }
+    }
 }
index d1ea5e5eee7cd5fbfce43c0142abcb67a7c81bdb..99922413a14444c0c2fb15c22068c7c94eb3b4a8 100644 (file)
@@ -9,11 +9,8 @@ package org.opendaylight.protocol.bgp.parser.spi;
 
 import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
-
 import io.netty.buffer.ByteBuf;
-
 import java.util.Arrays;
-
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
@@ -57,11 +54,16 @@ public abstract class AbstractMessageRegistry implements MessageRegistry {
             throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes()
                     + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
         }
-        final Notification msg = parseBody(messageType, msgBody, messageLength);
+        Notification msg = null;
+        try {
+            msg = parseBody(messageType, msgBody, messageLength);
+        } finally {
+            // Always reads body bytes
+            buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
+        }
         if (msg == null) {
             throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes });
         }
-        buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
         return msg;
     }