Charles Chan
Committed by Gerrit Code Review

[ONOS-2808] Properly deserialzes NDP packets without options

In addition, add following cases into NDP unit tests:
    - testDeserializeBadInput
    - testDeserializeTruncated (NDP headers only, options skipped)

Change-Id: Ia295a5bd7fcdcc25ac556da7bc2eaab13ad8e3b8
......@@ -263,12 +263,14 @@ public class NeighborAdvertisement extends BasePacket {
neighborAdvertisement.overrideFlag = (byte) (iscratch >> 29 & 0x1);
bb.get(neighborAdvertisement.targetAddress, 0, Ip6Address.BYTE_LENGTH);
if (bb.limit() - bb.position() > 0) {
NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
.deserialize(data, bb.position(), bb.limit() - bb.position());
for (NeighborDiscoveryOptions.Option option : options.options()) {
neighborAdvertisement.addOption(option.type(), option.data());
}
}
return neighborAdvertisement;
};
......
......@@ -177,12 +177,14 @@ public class NeighborSolicitation extends BasePacket {
bb.getInt();
bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH);
if (bb.limit() - bb.position() > 0) {
NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
.deserialize(data, bb.position(), bb.limit() - bb.position());
for (NeighborDiscoveryOptions.Option option : options.options()) {
neighborSolicitation.addOption(option.type(), option.data());
}
}
return neighborSolicitation;
};
......
......@@ -210,12 +210,14 @@ public class Redirect extends BasePacket {
bb.get(redirect.targetAddress, 0, Ip6Address.BYTE_LENGTH);
bb.get(redirect.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
if (bb.limit() - bb.position() > 0) {
NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
.deserialize(data, bb.position(), bb.limit() - bb.position());
for (NeighborDiscoveryOptions.Option option : options.options()) {
redirect.addOption(option.type(), option.data());
}
}
return redirect;
};
......
......@@ -310,12 +310,14 @@ public class RouterAdvertisement extends BasePacket {
routerAdvertisement.reachableTime = bb.getInt();
routerAdvertisement.retransmitTimer = bb.getInt();
if (bb.limit() - bb.position() > 0) {
NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
.deserialize(data, bb.position(), bb.limit() - bb.position());
for (NeighborDiscoveryOptions.Option option : options.options()) {
routerAdvertisement.addOption(option.type(), option.data());
}
}
return routerAdvertisement;
};
......
......@@ -140,12 +140,14 @@ public class RouterSolicitation extends BasePacket {
bb.getInt();
if (bb.limit() - bb.position() > 0) {
NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
.deserialize(data, bb.position(), bb.limit() - bb.position());
for (NeighborDiscoveryOptions.Option option : options.options()) {
routerSolicitation.addOption(option.type(), option.data());
}
}
return routerSolicitation;
};
......
......@@ -20,6 +20,9 @@ import org.junit.Test;
import org.onlab.packet.DeserializationException;
import org.onlab.packet.Deserializer;
import org.onlab.packet.MacAddress;
import org.onlab.packet.PacketTestUtils;
import java.nio.ByteBuffer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
......@@ -76,6 +79,20 @@ public class NeighborAdvertisementTest {
assertArrayEquals(na.serialize(), bytePacket);
}
@Test
public void testDeserializeBadInput() throws Exception {
PacketTestUtils.testDeserializeBadInput(NeighborAdvertisement.deserializer());
}
@Test
public void testDeserializeTruncated() throws Exception {
// Run the truncation test only on the NeighborAdvertisement header
byte[] naHeader = new byte[NeighborAdvertisement.HEADER_LENGTH];
ByteBuffer.wrap(bytePacket).get(naHeader);
PacketTestUtils.testDeserializeTruncated(NeighborAdvertisement.deserializer(), naHeader);
}
/**
* Tests deserialize and getters.
*/
......
......@@ -20,6 +20,9 @@ import org.junit.Test;
import org.onlab.packet.DeserializationException;
import org.onlab.packet.Deserializer;
import org.onlab.packet.MacAddress;
import org.onlab.packet.PacketTestUtils;
import java.nio.ByteBuffer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
......@@ -79,6 +82,20 @@ public class NeighborSolicitationTest {
assertArrayEquals(ns.serialize(), bytePacket);
}
@Test
public void testDeserializeBadInput() throws Exception {
PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer());
}
@Test
public void testDeserializeTruncated() throws Exception {
// Run the truncation test only on the NeighborSolicitation header
byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH];
ByteBuffer.wrap(bytePacket).get(nsHeader);
PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader);
}
/**
* Tests deserialize and getters.
*/
......
......@@ -20,6 +20,9 @@ import org.junit.Test;
import org.onlab.packet.DeserializationException;
import org.onlab.packet.Deserializer;
import org.onlab.packet.MacAddress;
import org.onlab.packet.PacketTestUtils;
import java.nio.ByteBuffer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
......@@ -89,6 +92,20 @@ public class RedirectTest {
assertArrayEquals(rd.serialize(), bytePacket);
}
@Test
public void testDeserializeBadInput() throws Exception {
PacketTestUtils.testDeserializeBadInput(Redirect.deserializer());
}
@Test
public void testDeserializeTruncated() throws Exception {
// Run the truncation test only on the Redirect header
byte[] rdHeader = new byte[Redirect.HEADER_LENGTH];
ByteBuffer.wrap(bytePacket).get(rdHeader);
PacketTestUtils.testDeserializeTruncated(Redirect.deserializer(), rdHeader);
}
/**
* Tests deserialize and getters.
*/
......
......@@ -20,6 +20,9 @@ import org.junit.Test;
import org.onlab.packet.DeserializationException;
import org.onlab.packet.Deserializer;
import org.onlab.packet.MacAddress;
import org.onlab.packet.PacketTestUtils;
import java.nio.ByteBuffer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
......@@ -70,6 +73,20 @@ public class RouterAdvertisementTest {
assertArrayEquals(ra.serialize(), bytePacket);
}
@Test
public void testDeserializeBadInput() throws Exception {
PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer());
}
@Test
public void testDeserializeTruncated() throws Exception {
// Run the truncation test only on the RouterAdvertisement header
byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH];
ByteBuffer.wrap(bytePacket).get(raHeader);
PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader);
}
/**
* Tests deserialize and getters.
*/
......
......@@ -21,6 +21,8 @@ import org.onlab.packet.Deserializer;
import org.onlab.packet.MacAddress;
import org.onlab.packet.PacketTestUtils;
import java.nio.ByteBuffer;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
......@@ -71,7 +73,11 @@ public class RouterSolicitationTest {
@Test
public void testDeserializeTruncated() throws Exception {
PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), bytePacket);
// Run the truncation test only on the RouterSolicitation header
byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH];
ByteBuffer.wrap(bytePacket).get(rsHeader);
PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader);
}
/**
......