ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding
Change-Id: Ifaaa4d3640c6a4fec4931db54a1f03100cc9d9e6
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10Test.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10Test.java
new file mode 100644
index 0000000..93254ef
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10Test.java
@@ -0,0 +1,249 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.lsa.types;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.onosproject.ospf.protocol.lsa.TlvHeader;
+import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
+import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
+
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit test class for OpaqueLsa10.
+ */
+public class OpaqueLsa10Test {
+
+ private final byte[] packet = {0, 1, 0, 4, 1, 1, 1, 1, 0, 2, 0, 84, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ private OpaqueLsa10 opaqueLsa10;
+ private TopLevelTlv tlv;
+ private OpaqueLsaHeader opqueHeader;
+ private ChannelBuffer channelBuffer;
+ private byte[] result;
+ private RouterTlv routerTlv;
+ private RouterTlv routerTlv1;
+ private LinkTlv linkTlv;
+ private LinkTlv linkTlv1;
+ private OspfLsaType ospflsaType;
+ private int result1;
+ private List result2;
+
+ @Before
+ public void setUp() throws Exception {
+ opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ opaqueLsa10 = null;
+ tlv = null;
+ opqueHeader = null;
+ channelBuffer = null;
+ result = null;
+ routerTlv = null;
+ routerTlv1 = null;
+ linkTlv1 = null;
+ ospflsaType = null;
+ }
+
+ /**
+ * Tests to string method.
+ */
+ @Test
+ public void testToString() throws Exception {
+ assertThat(opaqueLsa10.toString(), is(notNullValue()));
+ }
+
+ /**
+ * Tests addValue() method.
+ */
+ @Test
+ public void testAddValue() throws Exception {
+ tlv = new RouterTlv(new TlvHeader());
+ opaqueLsa10.addValue(tlv);
+ assertThat(opaqueLsa10, is(notNullValue()));
+ }
+
+ /**
+ * Tests readFrom() method.
+ */
+ @Test(expected = Exception.class)
+ public void testReadFrom() throws Exception {
+ opqueHeader = new OpaqueLsaHeader();
+ opqueHeader.setLsType(10);
+ opqueHeader.setLsPacketLen(48);
+ opqueHeader.setLsCheckSum(10);
+ opqueHeader.setAge(4);
+ opqueHeader.setOpaqueId(1);
+ opqueHeader.setOpaqueType(10);
+ opqueHeader.setLsSequenceNo(250);
+ opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ opqueHeader.setOptions(66);
+ opaqueLsa10 = new OpaqueLsa10(opqueHeader);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet);
+ opaqueLsa10.readFrom(channelBuffer);
+ result = opaqueLsa10.asBytes();
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests asBytes() method.
+ */
+ @Test(expected = Exception.class)
+ public void testAsBytes() throws Exception {
+ opqueHeader = new OpaqueLsaHeader();
+ opqueHeader.setLsType(10);
+ opqueHeader.setLsPacketLen(48);
+ opqueHeader.setLsCheckSum(10);
+ opqueHeader.setAge(4);
+ opqueHeader.setOpaqueId(1);
+ opqueHeader.setOpaqueType(10);
+ opqueHeader.setLsSequenceNo(250);
+ opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ opqueHeader.setOptions(66);
+ opaqueLsa10 = new OpaqueLsa10(opqueHeader);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet);
+ opaqueLsa10.readFrom(channelBuffer);
+ result = opaqueLsa10.getLsaBodyAsByteArray();
+ result = opaqueLsa10.asBytes();
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsaBodyAsByteArray() method.
+ */
+ @Test(expected = Exception.class)
+ public void testGetLsaBodyAsByteArray() throws Exception {
+ opqueHeader = new OpaqueLsaHeader();
+ opqueHeader.setLsType(10);
+ opqueHeader.setLsPacketLen(48);
+ opqueHeader.setLsCheckSum(10);
+ opqueHeader.setAge(4);
+ opqueHeader.setOpaqueId(1);
+ opqueHeader.setOpaqueType(10);
+ opqueHeader.setLsSequenceNo(250);
+ opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ opqueHeader.setOptions(2);
+ opaqueLsa10 = new OpaqueLsa10(opqueHeader);
+ routerTlv = new RouterTlv(new TlvHeader());
+ linkTlv = new LinkTlv(new TlvHeader());
+ opaqueLsa10.addValue(routerTlv);
+ opaqueLsa10.addValue(linkTlv);
+ routerTlv1 = new RouterTlv(new TlvHeader());
+ linkTlv1 = new LinkTlv(new TlvHeader());
+ opaqueLsa10.addValue(routerTlv1);
+ opaqueLsa10.addValue(linkTlv1);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet);
+ opaqueLsa10.readFrom(channelBuffer);
+ result = opaqueLsa10.getLsaBodyAsByteArray();
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsaBodyAsByteArray() method.
+ */
+ @Test(expected = Exception.class)
+ public void testGetLsaBodyAsByteArray1() throws Exception {
+ opqueHeader = new OpaqueLsaHeader();
+ opqueHeader.setLsType(10);
+ opqueHeader.setLsPacketLen(48);
+ opqueHeader.setLsCheckSum(10);
+ opqueHeader.setAge(4);
+ opqueHeader.setOpaqueId(1);
+ opqueHeader.setOpaqueType(10);
+ opqueHeader.setLsSequenceNo(250);
+ opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ opqueHeader.setOptions(2);
+ opaqueLsa10 = new OpaqueLsa10(opqueHeader);
+ routerTlv = new RouterTlv(new TlvHeader());
+ opaqueLsa10.addValue(routerTlv);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet);
+ result = opaqueLsa10.getLsaBodyAsByteArray();
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsaBodyAsByteArray() method.
+ */
+ @Test(expected = Exception.class)
+ public void testGetLsaBodyAsByteArray2() throws Exception {
+ opqueHeader = new OpaqueLsaHeader();
+ opqueHeader.setLsType(10);
+ opqueHeader.setLsPacketLen(48);
+ opqueHeader.setLsCheckSum(10);
+ opqueHeader.setAge(4);
+ opqueHeader.setOpaqueId(1);
+ opqueHeader.setOpaqueType(10);
+ opqueHeader.setLsSequenceNo(250);
+ opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ opqueHeader.setOptions(2);
+ opaqueLsa10 = new OpaqueLsa10(opqueHeader);
+ linkTlv = new LinkTlv(new TlvHeader());
+ opaqueLsa10.addValue(linkTlv);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet);
+ opaqueLsa10.readFrom(channelBuffer);
+ result = opaqueLsa10.getLsaBodyAsByteArray();
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests getOspfLsaType() getter method.
+ */
+ @Test
+ public void testGetOspfLsaType() throws Exception {
+ opaqueLsa10.setLsType(10);
+ ospflsaType = opaqueLsa10.getOspfLsaType();
+ assertThat(ospflsaType, is(notNullValue()));
+ assertThat(ospflsaType, is(OspfLsaType.AREA_LOCAL_OPAQUE_LSA));
+ }
+
+ /**
+ * Tests hashCode() method.
+ */
+ @Test
+ public void testHashcode() throws Exception {
+
+ result1 = opaqueLsa10.hashCode();
+ assertThat(result1, is(notNullValue()));
+
+ }
+
+ /**
+ * Tests topLevelValues() method.
+ */
+ @Test
+ public void testTopLevelValues() throws Exception {
+
+ result2 = opaqueLsa10.topLevelValues();
+ assertThat(result2, is(notNullValue()));
+
+ }
+
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/RouterLsaTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/RouterLsaTest.java
new file mode 100644
index 0000000..09a3b4d
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/RouterLsaTest.java
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.lsa.types;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+
+/**
+ * Unit test class for RouterLsa.
+ */
+public class RouterLsaTest {
+
+ private RouterLsa routerLsa;
+ private int result1;
+ private OspfLsaLink ospflsaLink;
+ private byte[] inputArray;
+ private LsaHeader lsaHeader;
+ private ChannelBuffer channelBuffer;
+ private byte[] result2;
+ private OspfLsaType result3;
+
+ @Before
+ public void setUp() throws Exception {
+ routerLsa = new RouterLsa();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ routerLsa = null;
+ ospflsaLink = null;
+ inputArray = null;
+ lsaHeader = null;
+ channelBuffer = null;
+ result2 = null;
+ result3 = null;
+ }
+
+
+ /**
+ * Tests virtualEndPoint() setter method.
+ */
+ @Test
+ public void testSetVirtualEndPoint() throws Exception {
+ routerLsa.setVirtualEndPoint(true);
+ assertThat(routerLsa, is(notNullValue()));
+ }
+
+
+ /**
+ * Tests isAsBoundaryRouter() setter method.
+ */
+ @Test
+ public void testSetAsBoundaryRouter() throws Exception {
+ routerLsa.setAsBoundaryRouter(true);
+ assertThat(routerLsa, is(notNullValue()));
+ }
+
+ /**
+ * Tests areaBorderRouter() setter method.
+ */
+ @Test
+ public void testSetAreaBorderRouter() throws Exception {
+ routerLsa.setAreaBorderRouter(true);
+ assertThat(routerLsa, is(notNullValue()));
+ }
+
+ /**
+ * Tests noLink() getter method.
+ */
+ @Test
+ public void testGetNoLink() throws Exception {
+ routerLsa.setNoLink(10);
+ result1 = routerLsa.noLink();
+ assertThat(result1, is(10));
+ }
+
+ /**
+ * Tests noLink() setter method.
+ */
+ @Test
+ public void testSetNoLink() throws Exception {
+ routerLsa.setNoLink(10);
+ result1 = routerLsa.noLink();
+ assertThat(result1, is(10));
+ }
+
+ /**
+ * Tests addRouterLink() method.
+ */
+ @Test
+ public void testAddRouterLink() throws Exception {
+ routerLsa.setNoLink(0);
+ ospflsaLink = createOspfLsaLink();
+ routerLsa.addRouterLink(ospflsaLink);
+ routerLsa.incrementLinkNo();
+ result1 = routerLsa.noLink();
+ assertThat(result1, is(1));
+
+ }
+
+
+ /**
+ * Tests readFrom() method.
+ */
+ @Test
+ public void testReadFrom() throws Exception {
+ ospflsaLink = createOspfLsaLink();
+ routerLsa.addRouterLink(ospflsaLink);
+ inputArray = createByteForRouterLsa();
+ lsaHeader = createLsaHeader();
+ routerLsa = new RouterLsa(lsaHeader);
+ channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
+ routerLsa.readFrom(channelBuffer);
+ assertThat(routerLsa, is(notNullValue()));
+ }
+
+ /**
+ * Tests readFrom() method.
+ */
+ @Test(expected = Exception.class)
+ public void testReadFrom1() throws Exception {
+ byte[] temp = {0, 0, 0};
+ ospflsaLink = createOspfLsaLink();
+ routerLsa.addRouterLink(ospflsaLink);
+ inputArray = temp;
+ lsaHeader = createLsaHeader();
+ routerLsa = new RouterLsa(lsaHeader);
+ channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
+ routerLsa.readFrom(channelBuffer);
+ assertThat(routerLsa, is(notNullValue()));
+ }
+
+ /**
+ * Tests asBytes() method.
+ */
+ @Test
+ public void testAsBytes() throws Exception {
+ result2 = routerLsa.asBytes();
+ assertThat(result2, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsaBodyAsByteArray() method.
+ */
+ @Test
+ public void testGetLsaBodyAsByteArray() throws Exception {
+ routerLsa.setAreaBorderRouter(true);
+ routerLsa.setVirtualEndPoint(true);
+ routerLsa.setAreaBorderRouter(true);
+ ospflsaLink = createOspfLsaLink();
+ routerLsa.addRouterLink(ospflsaLink);
+ result2 = routerLsa.getLsaBodyAsByteArray();
+ assertThat(result2, is(notNullValue()));
+ }
+
+ /**
+ * Tests getOspfLsaType() getter method.
+ */
+ @Test
+ public void testGetOspfLsaType() throws Exception {
+ routerLsa.setLsType(1);
+ result3 = routerLsa.getOspfLsaType();
+ assertThat(result3, is(OspfLsaType.ROUTER));
+ }
+
+ /**
+ * Tests incrementLinkNo() method.
+ */
+ @Test
+ public void testIncrementLinkNo() throws Exception {
+ routerLsa.setNoLink(1);
+ routerLsa.incrementLinkNo();
+ assertThat(routerLsa.noLink(), is(2));
+ }
+
+ /**
+ * Tests lsaHeader() method.
+ */
+ @Test
+ public void testGetLsaHeader() throws Exception {
+ lsaHeader = (LsaHeader) routerLsa.lsaHeader();
+ assertThat(lsaHeader, instanceOf(RouterLsa.class));
+ }
+
+ /**
+ * Tests to string method.
+ */
+ @Test
+ public void testToString() throws Exception {
+ assertThat(routerLsa.toString(), is(notNullValue()));
+
+ }
+
+ /**
+ * Utility method used by junit methods.
+ */
+ private OspfLsaLink createOspfLsaLink() {
+ ospflsaLink = new OspfLsaLink();
+ ospflsaLink.setLinkId("10.226.165.164");
+ ospflsaLink.setMetric(10);
+ ospflsaLink.setTos(50);
+ ospflsaLink.setLinkType(2);
+ ospflsaLink.setLinkData("10.226.165.170");
+ return ospflsaLink;
+ }
+
+ /**
+ * Utility method used by junit methods.
+ */
+ private byte[] createByteForRouterLsa() {
+ byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64,
+ -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56,
+ 1};
+ return packet;
+ }
+
+ /**
+ * Utility method used by junit methods.
+ */
+ private LsaHeader createLsaHeader() {
+ lsaHeader = new LsaHeader();
+ lsaHeader.setLsType(1);
+ lsaHeader.setLsPacketLen(48);
+ lsaHeader.setLsCheckSum(10);
+ lsaHeader.setAge(4);
+ lsaHeader.setLinkStateId("10.226.165.164");
+ lsaHeader.setLsSequenceNo(250);
+ lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
+ lsaHeader.setOptions(2);
+ return lsaHeader;
+ }
+
+ /**
+ * Tests hashcode() method.
+ */
+ @Test
+ public void testHashcode() throws Exception {
+
+ result1 = routerLsa.hashCode();
+ assertThat(result1, is(notNullValue()));
+
+ }
+}
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReaderTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReaderTest.java
new file mode 100644
index 0000000..8b1d089
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReaderTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.ospfpacket;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Unit test class for OspfMessageReader.
+ */
+
+public class OspfMessageReaderTest {
+
+ private final byte[] packet1 = {1, 1, 1, 1, 2, 1, 0, 44, -64, -88, -86, 8,
+ 0, 0, 0, 1, 39, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0,
+ 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
+ private final byte[] packet2 = {1, 1, 1, 1, 2, 2, 0, 52, -64, -88, -86, 8, 0,
+ 0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119,
+ -87, 126, 0, 23, 2, 1, 10, 10, 10, 10, 10, 10, 10, 10, -128, 0, 0, 6,
+ -69, 26, 0, 36};
+ private final byte[] packet3 = {1, 1, 1, 1, 2, 3, 0, 36, -64, -88, -86, 3, 0,
+ 0, 0, 1, -67, -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88,
+ -86, 8, -64, -88, -86, 8};
+ private final byte[] packet4 = {1, 1, 1, 1, 2, 4, 1, 36, -64, -88, -86, 3, 0,
+ 0, 0, 1, 54, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0,
+ 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0
+ , 0, 1, 58, -100, 0, 48, 2, 0, 0, 2, -64, -88, -86
+ , 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0,
+ -1, -1, -1, 0, 3, 0, 0, 10, 0, 3, 2, 5, 80, -44,
+ 16, 0, -64, -88, -86, 2, -128, 0, 0, 1, 42, 73, 0,
+ 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88,
+ -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1,
+ 0, -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0,
+ 3, 2, 5, -64, -126, 120, 0, -64, -88, -86, 2, -128, 0,
+ 0, 1, -45, 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0,
+ -64, -88, -86, 2, -128, 0, 0, 1, 55, 8, 0, 36, -1, -1
+ , -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 3, 2, 5, -64, -88, 1, 0, -64, -88, -86, 2, -128, 0, 0,
+ 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64,
+ -88, -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0,
+ -128, 0, 0, 20, -64, -88, -86, 10, 0, 0, 0, 0};
+ private final byte[] packet5 = {1, 1, 1, 1, 2, 5, 0, 44, -64, -88, -86, 8, 0, 0,
+ 0, 1, -30, -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
+ 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
+ private OspfMessageReader ospfMessageReader;
+ private ChannelBuffer channelBuffer;
+
+ @Before
+ public void setUp() throws Exception {
+ ospfMessageReader = new OspfMessageReader();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ ospfMessageReader = null;
+ channelBuffer = null;
+ }
+
+ /**
+ * Tests readFromBuffer() method.
+ */
+ @Test
+ public void testReadFromBuffer() throws Exception {
+ channelBuffer = ChannelBuffers.copiedBuffer(packet1);
+ ospfMessageReader.readFromBuffer(channelBuffer);
+
+ channelBuffer = ChannelBuffers.copiedBuffer(packet2);
+ ospfMessageReader.readFromBuffer(channelBuffer);
+
+ channelBuffer = ChannelBuffers.copiedBuffer(packet3);
+ ospfMessageReader.readFromBuffer(channelBuffer);
+
+ channelBuffer = ChannelBuffers.copiedBuffer(packet4);
+ ospfMessageReader.readFromBuffer(channelBuffer);
+
+ channelBuffer = ChannelBuffers.copiedBuffer(packet5);
+ ospfMessageReader.readFromBuffer(channelBuffer);
+ assertThat(ospfMessageReader, is(notNullValue()));
+
+ }
+}
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriterTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriterTest.java
new file mode 100644
index 0000000..0600ebc
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageWriterTest.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.ospfpacket;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test class for OspfMessageWriter.
+ */
+public class OspfMessageWriterTest {
+
+ private OspfMessageWriter ospfMessageWriter;
+ private HelloPacket helloPacket;
+ private DdPacket ddPacket;
+ private LsAcknowledge lsAck;
+ private LsRequest lsReq;
+ private LsUpdate lsUpdate;
+
+ @Before
+ public void setUp() throws Exception {
+ ospfMessageWriter = new OspfMessageWriter();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ ospfMessageWriter = null;
+ helloPacket = null;
+ ddPacket = null;
+ lsAck = null;
+ lsReq = null;
+ lsUpdate = null;
+ }
+
+ /**
+ * Tests writeToBuffer() method.
+ */
+ @Test
+ public void testWriteToBuffer() throws Exception {
+ helloPacket = new HelloPacket();
+ helloPacket.setAuthType(1);
+ helloPacket.setOspftype(1);
+ helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ helloPacket.setChecksum(201);
+ helloPacket.setAuthentication(2);
+ helloPacket.setOspfPacLength(48);
+ helloPacket.setOspfVer(2);
+ helloPacket.setNetworkMask(Ip4Address.valueOf("255.255.255.255"));
+ helloPacket.setOptions(2); //not setting now
+ helloPacket.setHelloInterval(10);
+ helloPacket.setRouterPriority(1);
+ helloPacket.setRouterDeadInterval(40);
+ helloPacket.setDr(Ip4Address.valueOf("1.1.1.1"));
+ helloPacket.setBdr(Ip4Address.valueOf("2.2.2.2"));
+ helloPacket.addNeighbor(Ip4Address.valueOf("8.8.8.8"));
+ helloPacket.setDestinationIp(Ip4Address.valueOf("5.5.5.5"));
+ ospfMessageWriter.writeToBuffer(helloPacket, 7, 1);
+ assertThat(ospfMessageWriter, is(notNullValue()));
+ }
+
+ @Test(expected = Exception.class)
+ public void testWriteToBuffer1() throws Exception {
+
+ ddPacket = new DdPacket();
+ ddPacket.setAuthType(1);
+ ddPacket.setOspftype(2);
+ ddPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ ddPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ ddPacket.setChecksum(201);
+ ddPacket.setAuthentication(2);
+ ddPacket.setOspfPacLength(48);
+ ddPacket.setOspfVer(2);
+ ospfMessageWriter.writeToBuffer(ddPacket, 1, 1);
+ assertThat(ospfMessageWriter, is(notNullValue()));
+ }
+
+ @Test(expected = Exception.class)
+ public void testWriteToBuffer2() throws Exception {
+
+ lsAck = new LsAcknowledge();
+ lsAck.setAuthType(1);
+ lsAck.setOspftype(5);
+ lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ lsAck.setChecksum(201);
+ lsAck.setAuthentication(2);
+ lsAck.setOspfPacLength(48);
+ lsAck.setOspfVer(2);
+ ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
+ assertThat(ospfMessageWriter, is(notNullValue()));
+ }
+
+ @Test(expected = Exception.class)
+ public void testWriteToBuffer3() throws Exception {
+ lsReq = new LsRequest();
+ lsReq.setAuthType(1);
+ lsReq.setOspftype(3);
+ lsReq.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ lsReq.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ lsReq.setChecksum(201);
+ lsReq.setAuthentication(2);
+ lsReq.setOspfPacLength(48);
+ lsReq.setOspfVer(2);
+ ospfMessageWriter.writeToBuffer(lsReq, 1, 1);
+ assertThat(ospfMessageWriter, is(notNullValue()));
+ }
+
+ @Test(expected = Exception.class)
+ public void testWriteToBuffer4() throws Exception {
+ lsUpdate = new LsUpdate();
+ lsUpdate.setAuthType(1);
+ lsUpdate.setOspftype(3);
+ lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ lsUpdate.setChecksum(201);
+ lsUpdate.setAuthentication(2);
+ lsUpdate.setOspfPacLength(48);
+ lsUpdate.setOspfVer(2);
+ ospfMessageWriter.writeToBuffer(lsUpdate, 1, 1);
+ assertThat(ospfMessageWriter, is(notNullValue()));
+ }
+
+ /**
+ * Tests writeToBuffer() method.
+ */
+ @Test(expected = Exception.class)
+ public void testWriteToBuffer5() throws Exception {
+ lsAck = new LsAcknowledge();
+ lsAck.setAuthType(1);
+ lsAck.setOspftype(5);
+ lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ lsAck.setChecksum(201);
+ lsAck.setAuthentication(2);
+ lsAck.setOspfPacLength(48);
+ lsAck.setOspfVer(2);
+ ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdateTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdateTest.java
new file mode 100644
index 0000000..8d48099
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsUpdateTest.java
@@ -0,0 +1,345 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.ospfpacket.types;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.controller.OspfLsa;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
+import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
+import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
+import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
+import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
+import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
+import org.onosproject.ospf.protocol.util.OspfPacketType;
+
+import java.util.List;
+import java.util.Vector;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Unit test class for LsUpdate.
+ */
+public class LsUpdateTest {
+
+ private final byte[] packet1 = {0, 0, 0, 2, 0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7,
+ -128, 0, 0, 2, 46, -126, 0, 48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1,
+ 0, 0, 10, 10, 10, 10, 0, -1, -1, -1, 0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0,
+ 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0, 116, 0, 1, 0, 4, 0, 0, 0, 0,
+ 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10, 10, 10, 0, 0, 5, 0,
+ 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73, -104, -106,
+ -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106,
+ -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73,
+ -104, -106, -128, 73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0};
+ private final byte[] packet3 = {0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9,
+ -128, 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2,
+ 0, 4, -64, -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0,
+ 0, 0, 5, 0, 4, 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0,
+ 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0,
+ 0, 0, 1};
+ private final byte[] packet2 = {0, 0,
+ 0, 1, 0, 1, 2, 2, -64, -88,
+ -86, 8, -64, -88, -86, 8, -128, 0, 0, 1, 55, -73, 0, 32, -1, -1, -1, 0, -64,
+ -88, -86, 3, -64, -88, -86, 8};
+ private final byte[] packet4 = {0, 0, 0, 1, 0, 100, 2, 9, 1, 0, 0, 1, 9, 9, 9, 9, -128,
+ 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
+ -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
+ 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
+
+ private final byte[] packet5 = {0, 0, 0, 1, 0, 100, 2, 11, 1, 0, 0, 1, 9, 9, 9, 9, -128, 0,
+ 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88,
+ 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0,
+ 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
+ private final byte[] packet6 = {0, 0, 0, 1, 0, 100, 2, 3, 1, 0, 0, 1, 9, 9, 9, 9, -128,
+ 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
+ -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4,
+ 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
+ private final byte[] packet7 = {0, 0, 0, 1, 0, 100, 2, 4, 1, 0, 0, 1, 9, 9, 9, 9, -128,
+ 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
+ -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
+ 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
+
+ private final byte[] packet8 = {0, 0, 0, 2, 1, 4, 2, 1, 5, 5, 5, 5, 5, 5, 5, 5, -128, 0, 0,
+ 4, -39, -84, 0, 36, 1, 0, 0, 1, -64, -88, 7, 90, -64, -88, 7, 92, 2, 0, 0, 10, 1, 4,
+ 2, 4, -34, -34, -34, -34, 5, 5, 5, 5, -128, 0, 0, 1, 31, -93, 0, 28, 0, 0, 0, 0, 0,
+ 0, 0, 10};
+ private LsUpdate lsUpdate;
+ private RouterLsa ospflsa;
+ private NetworkLsa ospflsa1;
+ private SummaryLsa ospflsa2;
+ private AsbrSummaryLsa ospflsa3;
+ private ExternalLsa ospflsa4;
+ private Vector<OspfLsa> listLSA = new Vector();
+ private List lsa;
+ private int result;
+ private OspfPacketType ospfMessageType;
+ private OspfPacketHeader ospfPacketHeader;
+ private byte[] result1;
+ private ChannelBuffer channelBuffer;
+ private OpaqueLsa10 opaqueLsa10;
+ private OpaqueLsa9 opaqueLsa9;
+ private OpaqueLsa11 opaqueLsa11;
+
+ @Before
+ public void setUp() throws Exception {
+ lsUpdate = new LsUpdate();
+ ospflsa = new RouterLsa();
+ lsUpdate.setAuthType(1);
+ lsUpdate.setOspftype(2);
+ lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
+ lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
+ lsUpdate.setChecksum(201);
+ lsUpdate.setAuthentication(2);
+ lsUpdate.setOspfPacLength(48);
+ lsUpdate.setOspfVer(2);
+ ospflsa.setLsType(1);
+ lsUpdate.addLsa(ospflsa);
+ ospflsa1 = new NetworkLsa();
+ ospflsa1.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
+ ospflsa1.setLsType(2);
+ lsUpdate.addLsa(ospflsa1);
+ ospflsa2 = new SummaryLsa(new LsaHeader());
+ ospflsa2.setLsType(3);
+ lsUpdate.addLsa(ospflsa2);
+ ospflsa3 = new AsbrSummaryLsa(new LsaHeader());
+ ospflsa3.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
+ ospflsa3.setLsType(4);
+ lsUpdate.addLsa(ospflsa3);
+ ospflsa4 = new ExternalLsa(new LsaHeader());
+ ospflsa4.setLsType(5);
+ lsUpdate.addLsa(ospflsa4);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ lsUpdate = null;
+ ospflsa = null;
+ ospflsa1 = null;
+ ospflsa2 = null;
+ ospflsa3 = null;
+ ospflsa4 = null;
+ listLSA.clear();
+ lsa = null;
+ ospfMessageType = null;
+ ospfPacketHeader = null;
+ result1 = null;
+ channelBuffer = null;
+ opaqueLsa9 = null;
+ opaqueLsa10 = null;
+ opaqueLsa11 = null;
+ }
+
+ /**
+ * Tests getLsaList() getter method.
+ */
+ @Test
+ public void testGetListLsa() throws Exception {
+ lsUpdate.addLsa(ospflsa);
+ lsUpdate.addLsa(ospflsa);
+ lsa = lsUpdate.getLsaList();
+ assertThat(lsa, is(notNullValue()));
+ assertThat(lsa.size(), is(5));
+ }
+
+ /**
+ * Tests addLsa() method.
+ */
+ @Test
+ public void testAddLsa() throws Exception {
+ lsUpdate.addLsa(ospflsa);
+ assertThat(lsUpdate, is(notNullValue()));
+ }
+
+ /**
+ * Tests noLsa() getter method.
+ */
+ @Test
+ public void testGetNoLsa() throws Exception {
+ lsUpdate.setNumberOfLsa(5);
+ result = lsUpdate.noLsa();
+ assertThat(result, is(notNullValue()));
+ assertThat(result, is(5));
+ }
+
+ /**
+ * Tests noLsa() setter method.
+ */
+ @Test
+ public void testSetNoLsa() throws Exception {
+ lsUpdate.setNumberOfLsa(5);
+ result = lsUpdate.noLsa();
+ assertThat(result, is(notNullValue()));
+ assertThat(result, is(5));
+ }
+
+ /**
+ * Tests ospfMessageType() getter method.
+ */
+ @Test
+ public void testGetOspfMessageType() throws Exception {
+ ospfMessageType = lsUpdate.ospfMessageType();
+ assertThat(ospfMessageType, is(OspfPacketType.LSUPDATE));
+ }
+
+ /**
+ * Tests readFrom() method.
+ */
+ @Test
+ public void testReadFrom() throws Exception {
+ ospfPacketHeader = new OspfPacketHeader();
+ ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
+ ospfPacketHeader.setAuthentication(0);
+ ospfPacketHeader.setAuthType(0);
+ ospfPacketHeader.setChecksum(12345);
+ ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
+ ospfPacketHeader.setOspfPacLength(56);
+ ospfPacketHeader.setOspftype(4);
+ ospfPacketHeader.setOspfVer(2);
+ ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
+ ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
+ lsUpdate = new LsUpdate(ospfPacketHeader);
+ result1 = createLsUpdatePacket();
+ channelBuffer = ChannelBuffers.copiedBuffer(result1);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet1);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet2);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet3);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet4);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet5);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet6);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet7);
+ lsUpdate.readFrom(channelBuffer);
+ channelBuffer = ChannelBuffers.copiedBuffer(packet8);
+ lsUpdate.readFrom(channelBuffer);
+ assertThat(lsUpdate, is(notNullValue()));
+ assertThat(lsUpdate.ospfMessageType(), is(OspfPacketType.LSUPDATE));
+ }
+
+ /**
+ * Tests asBytes() method.
+ */
+ @Test
+ public void testAsBytes() throws Exception {
+ result1 = lsUpdate.asBytes();
+ assertThat(result1, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsuHeaderAsByteArray() method.
+ */
+ @Test
+ public void testGetLsuHeaderAsByteArray() throws Exception {
+ result1 = lsUpdate.getLsuHeaderAsByteArray();
+ assertThat(result1, is(notNullValue()));
+ }
+
+ /**
+ * Tests getLsuBodyAsByteArray() method.
+ */
+ @Test
+ public void testGetLsuBodyAsByteArray() throws Exception {
+ lsUpdate.setNumberOfLsa(8);
+ lsUpdate.addLsa(ospflsa3);
+ opaqueLsa9 = new OpaqueLsa9(new OpaqueLsaHeader());
+ opaqueLsa9.setLsType(9);
+ lsUpdate.addLsa(opaqueLsa9);
+ opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
+ opaqueLsa10.setLsType(10);
+ lsUpdate.addLsa(opaqueLsa10);
+ opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
+ opaqueLsa10.setLsType(11);
+ lsUpdate.addLsa(opaqueLsa11);
+ result1 = lsUpdate.getLsuBodyAsByteArray();
+ assertThat(result1, is(notNullValue()));
+ }
+
+ @Test
+ public void testGetLsuBodyAsByteArray1() throws Exception {
+ lsUpdate.setNumberOfLsa(8);
+ opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
+ opaqueLsa10.setLsType(10);
+ lsUpdate.addLsa(opaqueLsa10);
+ assertThat(result1, is(nullValue()));
+ }
+
+ @Test
+ public void testGetLsuBodyAsByteArray2() throws Exception {
+ opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
+ opaqueLsa11.setLsType(11);
+ lsUpdate.addLsa(opaqueLsa11);
+ result1 = lsUpdate.getLsuBodyAsByteArray();
+ assertThat(result1, is(notNullValue()));
+ }
+
+ /**
+ * Tests to string method.
+ */
+ @Test
+ public void testToString() throws Exception {
+ assertThat(lsUpdate.toString(), is(notNullValue()));
+ }
+
+ /**
+ * Utility method used by junit methods.
+ */
+ private byte[] createLsUpdatePacket() {
+ byte[] lsUpdatePacket = {0, 0, 0, 7, 0, 2, 2,
+ 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58,
+ -100, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0,
+ 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10
+ , 0, 3, 2, 5, 80, -44, 16, 0, -64, -88, -86, 2, -128, 0, 0
+ , 1, 42, 73, 0, 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88
+ , -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1, 0,
+ -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0, 3, 2, 5
+ , -64, -126, 120, 0, -64, -88, -86, 2, -128, 0, 0, 1, -45,
+ 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0, -64, -88, -86, 2,
+ -128, 0, 0, 1, 55, 8, 0, 36, -1, -1, -1, 0, -128, 0, 0,
+ 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 1, 0,
+ -64, -88, -86, 2, -128, 0, 0, 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0,
+ 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64, -88,
+ -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, -64,
+ -88, -86, 10, 0, 0, 0, 0};
+ return lsUpdatePacket;
+ }
+}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/util/ChecksumCalculatorTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/util/ChecksumCalculatorTest.java
new file mode 100644
index 0000000..6b17cbb
--- /dev/null
+++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/util/ChecksumCalculatorTest.java
@@ -0,0 +1,367 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ospf.protocol.util;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.protocol.lsa.LsaHeader;
+import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
+import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
+import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
+import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
+import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
+import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
+import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
+import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
+import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Unit test class for ChecksumCalculator.
+ */
+public class ChecksumCalculatorTest {
+
+ private final int ospfChecksumPos1 = 12;
+ private final int ospfChecksumPos2 = 13;
+ private final int lsaChecksumPos1 = 16;
+ private final int lsaChecksumPos2 = 17;
+ private final byte[] updatePacket = {1, 1, 1, 1, 2, 4, 0, -96, 9, 9, 9, 9, 5, 5, 5, 5, 62, 125,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9, -128,
+ 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88,
+ 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 1,
+ 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
+ 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
+
+ private final byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88,
+ -86, 8, 0, 0, 0, 0};
+ private final byte[] rlsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0,
+ 0, 1, 74, -114, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10,
+ -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10};
+ private ChecksumCalculator checksumCalculator;
+ private boolean validate;
+ private HelloPacket hello;
+ private LsUpdate message;
+ private DdPacket message1;
+ private LsRequest message2;
+ private RouterLsa router;
+ private LsAcknowledge lsack;
+ private ExternalLsa external;
+ private NetworkLsa external1;
+ private SummaryLsa external2;
+ private AsbrSummaryLsa external3;
+ private OpaqueLsa9 external4;
+ private OpaqueLsa10 external5;
+ private OpaqueLsa11 external6;
+ private byte[] result;
+
+ @Before
+ public void setUp() throws Exception {
+ checksumCalculator = new ChecksumCalculator();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ checksumCalculator = null;
+ hello = null;
+ message = null;
+ message1 = null;
+ message2 = null;
+ router = null;
+ lsack = null;
+ external = null;
+ external1 = null;
+ external2 = null;
+ external3 = null;
+ result = null;
+ }
+
+ /**
+ * Tests convertToSixteenBits() method.
+ */
+ @Test
+ public void testConvertToSixteenBits() throws Exception {
+ int num = checksumCalculator.convertToSixteenBits("16cdd");
+ assertThat(num, is(27870));
+ }
+
+ /**
+ * Tests isValidOspfCheckSum() method.
+ */
+ @Test
+ public void testIsValidOspfCheckSum() throws Exception {
+ hello = new HelloPacket();
+ hello.setOspfVer(2);
+ hello.setOspftype(1);
+ hello.setOspfPacLength(172);
+ hello.setRouterId(Ip4Address.valueOf("192.168.170.3"));
+ hello.setAreaId(Ip4Address.valueOf("0.0.0.1"));
+ hello.setChecksum(5537);
+ hello.setAuthType(0);
+ hello.setAuthentication(0);
+ validate = checksumCalculator.isValidOspfCheckSum(hello, ospfChecksumPos1, ospfChecksumPos2);
+ assertThat(validate, is(false));
+ lsack = new LsAcknowledge();
+ lsack.setOspfVer(2);
+ lsack.setOspftype(5);
+ lsack.setOspfPacLength(172);
+ lsack.setRouterId(Ip4Address.valueOf("192.168.170.3"));
+ lsack.setAreaId(Ip4Address.valueOf("0.0.0.1"));
+ lsack.setChecksum(37537);
+ lsack.setAuthType(0);
+ lsack.setAuthentication(0);
+ validate = checksumCalculator.isValidOspfCheckSum(lsack, ospfChecksumPos1, ospfChecksumPos2);
+ assertThat(validate, is(true));
+ message = new LsUpdate();
+ message.setOspfVer(2);
+ message.setOspftype(5);
+ message.setOspfPacLength(172);
+ message.setRouterId(Ip4Address.valueOf("192.168.170.3"));
+ message.setAreaId(Ip4Address.valueOf("0.0.0.1"));
+ message.setChecksum(37537);
+ message.setAuthType(0);
+ message.setAuthentication(0);
+ validate = checksumCalculator.isValidOspfCheckSum(message, ospfChecksumPos1, ospfChecksumPos2);
+ assertThat(validate, is(true));
+ message1 = new DdPacket();
+ message1.setOspfVer(2);
+ message1.setOspftype(5);
+ message1.setOspfPacLength(172);
+ message1.setRouterId(Ip4Address.valueOf("192.168.170.3"));
+ message1.setAreaId(Ip4Address.valueOf("0.0.0.1"));
+ message1.setChecksum(37537);
+ message1.setAuthType(0);
+ message1.setAuthentication(0);
+ validate = checksumCalculator.isValidOspfCheckSum(message1, ospfChecksumPos1, ospfChecksumPos2);
+ assertThat(validate, is(true));
+ message2 = new LsRequest();
+ message2.setOspfVer(2);
+ message2.setOspftype(5);
+ message2.setOspfPacLength(172);
+ message2.setRouterId(Ip4Address.valueOf("192.168.170.3"));
+ message2.setAreaId(Ip4Address.valueOf("0.0.0.1"));
+ message2.setChecksum(37537);
+ message2.setAuthType(0);
+ message2.setAuthentication(0);
+ validate = checksumCalculator.isValidOspfCheckSum(message2, ospfChecksumPos1, ospfChecksumPos2);
+ assertThat(validate, is(true));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test
+ public void testIsValidLsaCheckSum() throws Exception {
+ router = new RouterLsa();
+ router.setAge(1);
+ router.setOptions(2);
+ router.setLsType(1);
+ router.setLinkStateId("192.168.170.3");
+ router.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.3"));
+ router.setLsSequenceNo(2147483649L);
+ router.setLsCheckSum(49499);
+ router.setLsPacketLen(48);
+ validate = checksumCalculator.isValidLsaCheckSum(router, router.lsType(), lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(true));
+
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test
+ public void testIsValidLsaCheckSum4() throws Exception {
+ external = new ExternalLsa(new LsaHeader());
+ external.setAge(2);
+ external.setOptions(2);
+ external.setLsType(5);
+ external.setLinkStateId("80.212.16.0");
+ external.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external.setLsSequenceNo(2147483649L);
+ external.setLsCheckSum(25125);
+ external.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external, external.lsType(), lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test(expected = Exception.class)
+ public void testIsValidLsaCheckSum5() throws Exception {
+ external1 = new NetworkLsa();
+ external1.setAge(2);
+ external1.setOptions(2);
+ external1.setLsType(2);
+ external1.setLinkStateId("80.212.16.0");
+ external1.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external1.setLsSequenceNo(2147483649L);
+ external1.setLsCheckSum(25125);
+ external1.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external1, external1.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test
+ public void testIsValidLsaCheckSum6() throws Exception {
+
+ external2 = new SummaryLsa(new LsaHeader());
+ external2.setAge(2);
+ external2.setOptions(2);
+ external2.setLsType(3);
+ external2.setLinkStateId("80.212.16.0");
+ external2.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external2.setLsSequenceNo(2147483649L);
+ external2.setLsCheckSum(25125);
+ external2.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external2, external2.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test(expected = Exception.class)
+ public void testIsValidLsaCheckSum7() throws Exception {
+ external3 = new AsbrSummaryLsa(new LsaHeader());
+ external3.setAge(2);
+ external3.setOptions(2);
+ external3.setLsType(4);
+ external3.setLinkStateId("80.212.16.0");
+ external3.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external3.setLsSequenceNo(2147483649L);
+ external3.setLsCheckSum(25125);
+ external3.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external3, external3.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test(expected = Exception.class)
+ public void testIsValidLsaCheckSum1() throws Exception {
+ external4 = new OpaqueLsa9(new OpaqueLsaHeader());
+ external4.setAge(2);
+ external4.setOptions(2);
+ external4.setLsType(9);
+ external4.setLinkStateId("80.212.16.0");
+ external4.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external4.setLsSequenceNo(2147483649L);
+ external4.setLsCheckSum(25125);
+ external4.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external4, external4.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test(expected = Exception.class)
+ public void testIsValidLsaCheckSum2() throws Exception {
+ external5 = new OpaqueLsa10(new OpaqueLsaHeader());
+ external5.setAge(2);
+ external5.setOptions(2);
+ external5.setLsType(10);
+ external5.setLinkStateId("80.212.16.0");
+ external5.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external5.setLsSequenceNo(2147483649L);
+ external5.setLsCheckSum(25125);
+ external5.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external5, external5.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests isValidLsaCheckSum() method.
+ */
+ @Test(expected = Exception.class)
+ public void testIsValidLsaCheckSum3() throws Exception {
+ external6 = new OpaqueLsa11(new OpaqueLsaHeader());
+ external6.setAge(2);
+ external6.setOptions(2);
+ external6.setLsType(10);
+ external6.setLinkStateId("80.212.16.0");
+ external6.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
+ external6.setLsSequenceNo(2147483649L);
+ external6.setLsCheckSum(25125);
+ external6.setLsPacketLen(36);
+ validate = checksumCalculator.isValidLsaCheckSum(external6, external6.lsType(),
+ lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(validate, is(false));
+ }
+
+ /**
+ * Tests validateLsaCheckSum() method.
+ */
+ @Test
+ public void testValidateLsaCheckSum() throws Exception {
+ assertThat(checksumCalculator.validateLsaCheckSum(rlsa, lsaChecksumPos1,
+ lsaChecksumPos2), is(true));
+
+ }
+
+ /**
+ * Tests validateOspfCheckSum() method.
+ */
+ @Test
+ public void testValidateOspfCheckSum() throws Exception {
+ assertThat(checksumCalculator.validateOspfCheckSum(helloPacket, ospfChecksumPos1,
+ ospfChecksumPos2), is(true));
+ }
+
+ /**
+ * Tests calculateLsaChecksum() method.
+ */
+ @Test
+ public void testCalculateLsaChecksum() throws Exception {
+ result = checksumCalculator.calculateLsaChecksum(rlsa, lsaChecksumPos1, lsaChecksumPos2);
+ assertThat(result, is(notNullValue()));
+ }
+
+ /**
+ * Tests calculateOspfCheckSum() method.
+ */
+ @Test
+ public void testCalculateOspfCheckSum() throws Exception {
+ result = checksumCalculator.calculateOspfCheckSum(helloPacket, ospfChecksumPos1,
+ ospfChecksumPos2);
+ assertThat(result, is(notNullValue()));
+ result = checksumCalculator.calculateOspfCheckSum(updatePacket, ospfChecksumPos1,
+ ospfChecksumPos2);
+ assertThat(result, is(notNullValue()));
+ }
+}
\ No newline at end of file