Jian Li
Committed by Thomas Vachuska

Add unit tests for LISP address serializer and deserializer

Change-Id: I56538221f3951d18c1feb9343d87cbb46c3ed04d
......@@ -126,6 +126,10 @@ public abstract class LispAfiAddress {
@Override
public void writeTo(ByteBuf byteBuf, LispAfiAddress address) throws LispWriterException {
// AFI code
byteBuf.writeShort(address.getAfi().getIanaCode());
switch (address.getAfi()) {
case IP:
new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
......
......@@ -21,6 +21,7 @@ import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
......@@ -352,7 +353,7 @@ public final class LispAppDataLcafAddress extends LispLcafAddress {
}
/**
* A utility function that obtains the partial int value from byte arrays.
* An utility function that obtains the partial int value from byte arrays.
*
* @param bytes an array of bytes
* @return converted integer
......@@ -378,8 +379,8 @@ public final class LispAppDataLcafAddress extends LispLcafAddress {
LispLcafAddress.serializeCommon(byteBuf, address);
// TODO: need to handle TOS
byte[] tos = getPartialByteArray(address.getIpTos());
byteBuf.writeBytes(tos);
byteBuf.writeByte(address.getProtocol());
byteBuf.writeShort(address.getLocalPortLow());
byteBuf.writeShort(address.getLocalPortHigh());
......@@ -389,5 +390,17 @@ public final class LispAppDataLcafAddress extends LispLcafAddress {
AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter();
writer.writeTo(byteBuf, address.getAddress());
}
/**
* An utility function that obtains byte array from partial int value.
*
* @param value integer value
* @return an array of bytes
*/
public static byte[] getPartialByteArray(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
byte[] array = buffer.putInt(value).array();
return Arrays.copyOfRange(array, 1, 4);
}
}
}
......
......@@ -84,10 +84,10 @@ public class LispDistinguishedNameAddress extends LispAfiAddress {
public LispDistinguishedNameAddress readFrom(ByteBuf byteBuf) throws LispParseError {
StringBuilder sb = new StringBuilder();
byte character = byteBuf.readByte();
while (character != 0) {
sb.append((char) character);
byte character;
while (byteBuf.readerIndex() < byteBuf.writerIndex()) {
character = byteBuf.readByte();
sb.append((char) character);
}
return new LispDistinguishedNameAddress(sb.toString());
......@@ -105,7 +105,7 @@ public class LispDistinguishedNameAddress extends LispAfiAddress {
String distinguishedName = address.getDistinguishedName();
byte[] nameBytes = distinguishedName.getBytes();
for (int i = 0; i < nameBytes.length; i++) {
byteBuf.writeChar(nameBytes[i]);
byteBuf.writeByte(nameBytes[i]);
}
}
}
......
......@@ -35,7 +35,7 @@ public class LispIpv6Address extends LispIpAddress {
* @param address IP address
*/
public LispIpv6Address(IpAddress address) {
super(address, AddressFamilyIdentifierEnum.IP);
super(address, AddressFamilyIdentifierEnum.IP6);
checkArgument(address.isIp6());
}
......
......@@ -128,8 +128,10 @@ public final class LispListLcafAddress extends LispLcafAddress {
LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
LispAfiAddress ipv4 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
LispAfiAddress ipv6 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
AfiAddressReader reader = new AfiAddressReader();
LispAfiAddress ipv4 = reader.readFrom(byteBuf);
LispAfiAddress ipv6 = reader.readFrom(byteBuf);
return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(),
lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6));
......@@ -141,6 +143,9 @@ public final class LispListLcafAddress extends LispLcafAddress {
*/
public static class ListLcafAddressWriter implements LispAddressWriter<LispListLcafAddress> {
private static final int IPV4_ADDRESS_INDEX = 0;
private static final int IPV6_ADDRESS_INDEX = 1;
@Override
public void writeTo(ByteBuf byteBuf, LispListLcafAddress address) throws LispWriterException {
......@@ -149,8 +154,16 @@ public final class LispListLcafAddress extends LispLcafAddress {
LispIpv4Address.Ipv4AddressWriter v4Writer = new LispIpv4Address.Ipv4AddressWriter();
LispIpv6Address.Ipv6AddressWriter v6Writer = new LispIpv6Address.Ipv6AddressWriter();
v4Writer.writeTo(byteBuf, (LispIpv4Address) address.getAddresses().get(0));
v6Writer.writeTo(byteBuf, (LispIpv6Address) address.getAddresses().get(1));
LispAfiAddress ipv4 = address.getAddresses().get(IPV4_ADDRESS_INDEX);
LispAfiAddress ipv6 = address.getAddresses().get(IPV6_ADDRESS_INDEX);
// IPv4 address
byteBuf.writeShort(ipv4.getAfi().getIanaCode());
v4Writer.writeTo(byteBuf, (LispIpv4Address) ipv4);
// IPv6 address
byteBuf.writeShort(ipv6.getAfi().getIanaCode());
v6Writer.writeTo(byteBuf, (LispIpv6Address) ipv6);
}
}
}
......
......@@ -227,7 +227,8 @@ public final class LispSegmentLcafAddress extends LispLcafAddress {
LispLcafAddress.serializeCommon(byteBuf, address);
byteBuf.writeInt(address.getInstanceId());
new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpAddress) address.getAddress());
new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress());
}
}
}
......
......@@ -16,12 +16,20 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressWriter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataAddressBuilder;
import static org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressReader;
/**
* Unit tests for LispAppDataLcafAddress class.
......@@ -35,8 +43,8 @@ public class LispAppDataLcafAddressTest {
@Before
public void setup() {
LispAppDataLcafAddress.AppDataAddressBuilder builder1 =
new LispAppDataLcafAddress.AppDataAddressBuilder();
AppDataAddressBuilder builder1 =
new AppDataAddressBuilder();
LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
......@@ -50,8 +58,8 @@ public class LispAppDataLcafAddressTest {
.withAddress(ipv4Address1)
.build();
LispAppDataLcafAddress.AppDataAddressBuilder builder2 =
new LispAppDataLcafAddress.AppDataAddressBuilder();
AppDataAddressBuilder builder2 =
new AppDataAddressBuilder();
sameAsAddress1 = builder2
.withProtocol((byte) 0x01)
......@@ -63,8 +71,8 @@ public class LispAppDataLcafAddressTest {
.withAddress(ipv4Address1)
.build();
LispAppDataLcafAddress.AppDataAddressBuilder builder3 =
new LispAppDataLcafAddress.AppDataAddressBuilder();
AppDataAddressBuilder builder3 =
new AppDataAddressBuilder();
LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
......@@ -100,4 +108,18 @@ public class LispAppDataLcafAddressTest {
assertThat(appDataLcafAddress.getRemotePortHigh(), is((short) 254));
assertThat(appDataLcafAddress.getAddress(), is(ipv4Address));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
ByteBuf byteBuf = Unpooled.buffer();
AppDataLcafAddressWriter writer = new AppDataLcafAddressWriter();
writer.writeTo(byteBuf, address1);
AppDataLcafAddressReader reader = new AppDataLcafAddressReader();
LispAppDataLcafAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,18 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispDistinguishedNameAddress.*;
/**
* Unit tests for LispDistinguishedNameAddress class.
*/
......@@ -52,4 +58,18 @@ public class LispDistinguishedNameAddressTest {
assertThat(distinguishedNameAddress.getDistinguishedName(), is("distAddress1"));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError {
ByteBuf byteBuf = Unpooled.buffer();
DistinguishedNameAddressWriter writer = new DistinguishedNameAddressWriter();
writer.writeTo(byteBuf, address1);
DistinguishedNameAddressReader reader = new DistinguishedNameAddressReader();
LispDistinguishedNameAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,18 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispIpv4Address.Ipv4AddressReader;
import static org.onosproject.lisp.msg.types.LispIpv4Address.Ipv4AddressWriter;
/**
* Unit tests for LispIpv4Address class.
......@@ -52,4 +58,18 @@ public class LispIpv4AddressTest {
LispIpv4Address ipv4Address = address1;
assertThat(ipv4Address.getAddress(), is(IpAddress.valueOf("192.168.1.1")));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError {
ByteBuf byteBuf = Unpooled.buffer();
Ipv4AddressWriter writer = new Ipv4AddressWriter();
writer.writeTo(byteBuf, address1);
Ipv4AddressReader reader = new Ipv4AddressReader();
LispIpv4Address deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,19 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispIpv6Address.Ipv6AddressReader;
import static org.onosproject.lisp.msg.types.LispIpv6Address.Ipv6AddressWriter;
/**
* Unit tests for LispIpv6Address class.
......@@ -52,4 +59,18 @@ public class LispIpv6AddressTest {
LispIpv6Address ipv6Address = address1;
assertThat(ipv6Address.getAddress(), is(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError {
ByteBuf byteBuf = Unpooled.buffer();
Ipv6AddressWriter writer = new Ipv6AddressWriter();
writer.writeTo(byteBuf, address1);
Ipv6AddressReader reader = new Ipv6AddressReader();
LispIpv6Address deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -17,14 +17,21 @@ package org.onosproject.lisp.msg.types;
import com.google.common.collect.Lists;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispListLcafAddress.ListLcafAddressReader;
import static org.onosproject.lisp.msg.types.LispListLcafAddress.ListLcafAddressWriter;
/**
* Unit tests for LispListLcafAddress class.
......@@ -79,4 +86,18 @@ public class LispListLcafAddressTest {
assertThat(listLcafAddress.getAddresses(), is(afiAddresses1));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
ByteBuf byteBuf = Unpooled.buffer();
ListLcafAddressWriter writer = new ListLcafAddressWriter();
writer.writeTo(byteBuf, address1);
ListLcafAddressReader reader = new ListLcafAddressReader();
LispListLcafAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,18 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.MacAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispMacAddress.MacAddressReader;
import static org.onosproject.lisp.msg.types.LispMacAddress.MacAddressWriter;
/**
* Unit tests for LispMacAddress class.
......@@ -52,4 +58,18 @@ public class LispMacAddressTest {
LispMacAddress macAddress = address1;
assertThat(macAddress.getAddress(), is(MacAddress.valueOf("00:00:00:00:00:01")));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError {
ByteBuf byteBuf = Unpooled.buffer();
MacAddressWriter writer = new MacAddressWriter();
writer.writeTo(byteBuf, address1);
MacAddressReader reader = new MacAddressReader();
LispMacAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,19 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressReader;
import static org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressWriter;
/**
* Unit tests for LispSegmentLcafAddress class.
......@@ -83,4 +90,18 @@ public class LispSegmentLcafAddressTest {
assertThat(segmentLcafAddress.getInstanceId(), is(1));
assertThat(segmentLcafAddress.getAddress(), is(ipv4Address));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
ByteBuf byteBuf = Unpooled.buffer();
SegmentLcafAddressWriter writer = new SegmentLcafAddressWriter();
writer.writeTo(byteBuf, address1);
SegmentLcafAddressReader reader = new SegmentLcafAddressReader();
LispSegmentLcafAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......
......@@ -16,12 +16,19 @@
package org.onosproject.lisp.msg.types;
import com.google.common.testing.EqualsTester;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressReader;
import static org.onosproject.lisp.msg.types.LispSourceDestLcafAddress.SourceDestLcafAddressWriter;
/**
* Unit tests for LispSourceDestLcafAddress class.
......@@ -95,4 +102,18 @@ public class LispSourceDestLcafAddressTest {
assertThat(sourceDestLcafAddress.getSrcPrefix(), is(srcAddress));
assertThat(sourceDestLcafAddress.getDstPrefix(), is(dstAddress));
}
@Test
public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
ByteBuf byteBuf = Unpooled.buffer();
SourceDestLcafAddressWriter writer = new SourceDestLcafAddressWriter();
writer.writeTo(byteBuf, address1);
SourceDestLcafAddressReader reader = new SourceDestLcafAddressReader();
LispSourceDestLcafAddress deserialized = reader.readFrom(byteBuf);
new EqualsTester()
.addEqualityGroup(address1, deserialized).testEquals();
}
}
......