Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.ospf.protocol.ospfpacket.types; |
| 17 | |
| 18 | import org.hamcrest.MatcherAssert; |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.buffer.ChannelBuffers; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 26 | import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; |
| 27 | import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader; |
| 28 | import org.onosproject.ospf.protocol.util.OspfPacketType; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.hamcrest.CoreMatchers.is; |
| 33 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 34 | import static org.junit.Assert.assertThat; |
| 35 | |
| 36 | /** |
| 37 | * Unit test class for LsAck. |
| 38 | */ |
| 39 | public class LsAcknowledgeTest { |
| 40 | |
| 41 | private LsaHeader lsaHeader; |
| 42 | private LsAcknowledge lsAck; |
| 43 | private OspfPacketType ospfPacketType; |
| 44 | private OspfPacketHeader ospfPacketHeader; |
| 45 | private byte[] result; |
| 46 | private ChannelBuffer channelBuffer; |
| 47 | private OpaqueLsaHeader opaqueLsaHeader; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | lsaHeader = new LsaHeader(); |
| 52 | lsAck = new LsAcknowledge(); |
| 53 | lsAck.setAuthType(1); |
| 54 | lsAck.setOspftype(5); |
| 55 | lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 56 | lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 57 | lsAck.setChecksum(201); |
| 58 | lsAck.setAuthentication(2); |
| 59 | lsAck.setOspfPacLength(48); |
| 60 | lsAck.setOspfVer(2); |
| 61 | } |
| 62 | |
| 63 | @After |
| 64 | public void tearDown() throws Exception { |
| 65 | lsaHeader = null; |
| 66 | lsAck = null; |
| 67 | ospfPacketType = null; |
| 68 | ospfPacketHeader = null; |
| 69 | result = null; |
| 70 | channelBuffer = null; |
| 71 | opaqueLsaHeader = null; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests getLinkStateHeaders() getter method. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testGetLinkStateHeaders() throws Exception { |
| 79 | lsaHeader = createLsaHeader(); |
| 80 | lsAck.addLinkStateHeader(lsaHeader); |
| 81 | lsAck.addLinkStateHeader(lsaHeader); |
| 82 | List headers = lsAck.getLinkStateHeaders(); |
| 83 | assertThat(headers.size(), is(1)); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests addLinkStateHeader() method. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testAddLinkStateHeader() throws Exception { |
| 92 | lsaHeader = createLsaHeader(); |
| 93 | lsAck.addLinkStateHeader(lsaHeader); |
| 94 | lsAck.addLinkStateHeader(lsaHeader); |
| 95 | assertThat(lsAck, is(notNullValue())); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Tests ospfMessageType() getter method. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testGetOSPFMessageType() throws Exception { |
| 104 | ospfPacketType = lsAck.ospfMessageType(); |
| 105 | assertThat(ospfPacketType, is(notNullValue())); |
| 106 | assertThat(ospfPacketType, is(OspfPacketType.LSAACK)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests ospfMessageType() getter method. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testGetOspfMessageType() throws Exception { |
| 114 | ospfPacketType = lsAck.ospfMessageType(); |
| 115 | assertThat(ospfPacketType, is(notNullValue())); |
| 116 | assertThat(ospfPacketType, is(OspfPacketType.LSAACK)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests readFrom() method. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testReadFrom() throws Exception { |
| 124 | ospfPacketHeader = new OspfPacketHeader(); |
| 125 | ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); |
| 126 | ospfPacketHeader.setAuthentication(0); |
| 127 | ospfPacketHeader.setAuthType(0); |
| 128 | ospfPacketHeader.setChecksum(12345); |
| 129 | ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10")); |
| 130 | ospfPacketHeader.setOspfPacLength(56); |
| 131 | ospfPacketHeader.setOspftype(5); |
| 132 | ospfPacketHeader.setOspfVer(2); |
| 133 | ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2")); |
| 134 | ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3")); |
| 135 | result = createByteForLSAck(); |
| 136 | lsAck = new LsAcknowledge(ospfPacketHeader); |
| 137 | channelBuffer = ChannelBuffers.copiedBuffer(result); |
| 138 | lsAck.readFrom(channelBuffer); |
| 139 | assertThat(lsAck, is(notNullValue())); |
| 140 | assertThat(lsAck.ospfMessageType(), is(OspfPacketType.LSAACK)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests asBytes() method. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testAsBytes() throws Exception { |
| 148 | result = lsAck.asBytes(); |
| 149 | assertThat(result, is(notNullValue())); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Tests getLsAckAsByteArray() method. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testGetLsAckAsByteArray() throws Exception { |
| 157 | result = lsAck.getLsAckAsByteArray(); |
| 158 | assertThat(result, is(notNullValue())); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tests getLsAckBodyAsByteArray() method. |
| 163 | */ |
| 164 | @Test |
| 165 | public void testGetLsAckBodyAsByteArray() throws Exception { |
| 166 | lsaHeader = createLsaHeader(); |
| 167 | opaqueLsaHeader = new OpaqueLsaHeader(); |
| 168 | lsAck.addLinkStateHeader(lsaHeader); |
| 169 | lsAck.addLinkStateHeader(opaqueLsaHeader); |
| 170 | result = lsAck.getLsAckBodyAsByteArray(); |
| 171 | assertThat(result, is(notNullValue())); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests to string method. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testToString() throws Exception { |
| 179 | MatcherAssert.assertThat(lsAck.toString(), is(notNullValue())); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Utility method used by junit methods. |
| 184 | */ |
| 185 | private LsaHeader createLsaHeader() { |
| 186 | lsaHeader = new LsaHeader(); |
| 187 | lsaHeader.setAge(10); |
| 188 | lsaHeader.setLinkStateId("10.226.165.164"); |
| 189 | lsaHeader.setLsCheckSum(222); |
| 190 | lsaHeader.setLsPacketLen(48); |
| 191 | lsaHeader.setLsSequenceNo(2020); |
| 192 | lsaHeader.setLsType(5); |
| 193 | lsaHeader.setOptions(2); |
| 194 | lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165")); |
| 195 | return lsaHeader; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Utility method used by junit methods. |
| 200 | */ |
| 201 | private byte[] createByteForLSAck() { |
| 202 | byte[] lsAckPacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30, |
| 203 | -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86, |
| 204 | 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48}; |
| 205 | |
| 206 | return lsAckPacket; |
| 207 | } |
| 208 | } |