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.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader; |
| 25 | import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket; |
| 26 | import org.onosproject.ospf.protocol.util.OspfPacketType; |
| 27 | |
| 28 | import java.net.UnknownHostException; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static org.hamcrest.CoreMatchers.is; |
| 32 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | |
| 35 | /** |
| 36 | * Unit test class for LsRequest. |
| 37 | */ |
| 38 | public class LsRequestTest { |
| 39 | |
| 40 | private LsRequest lsRequest; |
| 41 | private List<LsRequestPacket> result; |
| 42 | private OspfPacketType ospfMessageType; |
| 43 | private OspfPacketHeader ospfPacketHeader; |
| 44 | private byte[] result1; |
| 45 | private String result2; |
| 46 | private ChannelBuffer channelBuffer; |
| 47 | private LsRequestPacket lsRequestPacket; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | lsRequest = new LsRequest(); |
| 52 | lsRequest.setAuthType(1); |
| 53 | lsRequest.setOspftype(3); |
| 54 | lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 55 | lsRequest.setAreaId(Ip4Address.valueOf("10.226.165.163")); |
| 56 | lsRequest.setChecksum(201); |
| 57 | lsRequest.setAuthentication(2); |
| 58 | lsRequest.setOspfPacLength(48); |
| 59 | lsRequest.setOspfVer(2); |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | lsRequest = null; |
| 65 | result = null; |
| 66 | ospfMessageType = null; |
| 67 | ospfPacketHeader = null; |
| 68 | result1 = null; |
| 69 | channelBuffer = null; |
| 70 | lsRequestPacket = null; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests addLinkStateRequests() method. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testAddLinkStateRequests() throws Exception { |
| 78 | lsRequest.addLinkStateRequests(createLsRequestPacket()); |
| 79 | result = lsRequest.getLinkStateRequests(); |
| 80 | assertThat(result, is(notNullValue())); |
| 81 | assertThat(result.size(), is(1)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests getLinkStateRequests() method. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testGetLinkStateRequests() throws Exception { |
| 89 | lsRequest.addLinkStateRequests(createLsRequestPacket()); |
| 90 | lsRequest.addLinkStateRequests(new LsRequestPacket()); |
| 91 | result = lsRequest.getLinkStateRequests(); |
| 92 | assertThat(result, is(notNullValue())); |
| 93 | assertThat(result.size(), is(2)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests ospfMessageType()getter method. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testGetOspfMessageType() throws Exception { |
| 101 | ospfMessageType = lsRequest.ospfMessageType(); |
| 102 | assertThat(ospfMessageType, is(notNullValue())); |
| 103 | assertThat(ospfMessageType, is(OspfPacketType.LSREQUEST)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests readFrom() method. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testReadFrom() throws Exception { |
| 111 | ospfPacketHeader = new OspfPacketHeader(); |
| 112 | ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); |
| 113 | ospfPacketHeader.setAuthentication(0); |
| 114 | ospfPacketHeader.setAuthType(0); |
| 115 | ospfPacketHeader.setChecksum(12345); |
| 116 | ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10")); |
| 117 | ospfPacketHeader.setOspfPacLength(56); |
| 118 | ospfPacketHeader.setOspftype(3); |
| 119 | ospfPacketHeader.setOspfVer(2); |
| 120 | ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2")); |
| 121 | ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3")); |
| 122 | lsRequest = new LsRequest(ospfPacketHeader); |
| 123 | result1 = createByteLsReqestPacket(); |
| 124 | channelBuffer = ChannelBuffers.copiedBuffer(result1); |
| 125 | lsRequest.readFrom(channelBuffer); |
| 126 | assertThat(lsRequest, is(notNullValue())); |
| 127 | assertThat(lsRequest.ospfMessageType(), is(OspfPacketType.LSREQUEST)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests asBytes() method. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testAsBytes() throws Exception { |
| 135 | result1 = lsRequest.asBytes(); |
| 136 | assertThat(result1, is(notNullValue())); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests getLsrHeaderAsByteArray() method. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testGetLsrHeaderAsByteArray() throws Exception { |
| 144 | result1 = lsRequest.getLsrHeaderAsByteArray(); |
| 145 | assertThat(result1, is(notNullValue())); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests getLsrBodyAsByteArray() method. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testGetLsrBodyAsByteArray() throws Exception { |
| 153 | lsRequest.addLinkStateRequests(createLsRequestPacket()); |
| 154 | lsRequest.addLinkStateRequests(new LsRequestPacket()); |
| 155 | result1 = lsRequest.getLsrBodyAsByteArray(); |
| 156 | assertThat(result1, is(notNullValue())); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests to string method. |
| 161 | */ |
| 162 | @Test |
| 163 | public void testToString() throws Exception { |
| 164 | result2 = lsRequest.toString(); |
| 165 | assertThat(result2, is(notNullValue())); |
| 166 | } |
| 167 | |
| 168 | private LsRequestPacket createLsRequestPacket() throws UnknownHostException { |
| 169 | lsRequestPacket = new LsRequestPacket(); |
| 170 | lsRequestPacket.setOwnRouterId("165"); |
| 171 | lsRequestPacket.setLinkStateId("10.226.165.164"); |
| 172 | lsRequestPacket.setLsType(2); |
| 173 | return lsRequestPacket; |
| 174 | } |
| 175 | |
| 176 | private byte[] createByteLsReqestPacket() { |
| 177 | byte[] lsRequestPacket = {2, 3, 0, 36, -64, -88, -86, 3, 0, 0, 0, 1, -67, |
| 178 | -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, -86, 8, |
| 179 | -64, -88, -86, 8}; |
| 180 | return lsRequestPacket; |
| 181 | } |
| 182 | } |