Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +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; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Assert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | |
| 26 | import static org.hamcrest.CoreMatchers.*; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | |
| 29 | /** |
| 30 | * Unit test class for OspfPacketHeader. |
| 31 | */ |
| 32 | public class OspfPacketHeaderTest { |
| 33 | |
| 34 | private final byte[] packet = {0, 0, 0, 0}; |
| 35 | private OspfPacketHeader ospfPacketHeader; |
| 36 | private ChannelBuffer channelBuffer; |
| 37 | private byte[] result2; |
| 38 | private int result; |
| 39 | private Ip4Address result1; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | ospfPacketHeader = new OspfPacketHeader(); |
| 44 | } |
| 45 | |
| 46 | @After |
| 47 | public void tearDown() throws Exception { |
| 48 | ospfPacketHeader = null; |
| 49 | ospfPacketHeader = null; |
| 50 | channelBuffer = null; |
| 51 | result2 = null; |
| 52 | result1 = null; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Tests sourceIp() getter method. |
| 57 | */ |
| 58 | @Test |
| 59 | public void testGetSourceIP() throws Exception { |
| 60 | ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1")); |
| 61 | assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1"))); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Tests sourceIp() setter method. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testSetSourceIP() throws Exception { |
| 69 | ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1")); |
| 70 | assertThat(result, is(notNullValue())); |
| 71 | assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1"))); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests ospfMessageType() getter method. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testGetOspfMessageType() throws Exception { |
| 79 | assertThat(ospfPacketHeader.ospfMessageType(), nullValue()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests readFrom() method. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testReadFrom() throws Exception { |
| 87 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 88 | ospfPacketHeader.readFrom(channelBuffer); |
| 89 | assertThat(ospfPacketHeader, is(notNullValue())); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests asBytes() method. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testAsBytes() throws Exception { |
| 97 | result2 = ospfPacketHeader.asBytes(); |
| 98 | assertThat(result2, is(notNullValue())); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Tests ospfVersion() getter method. |
| 103 | */ |
| 104 | @Test |
| 105 | public void testGetOspfVer() throws Exception { |
| 106 | ospfPacketHeader.setOspfVer(2); |
| 107 | result = ospfPacketHeader.ospfVersion(); |
| 108 | assertThat(result, is(notNullValue())); |
| 109 | assertThat(result, is(2)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests ospfVersion() setter method. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testSetOspfVer() throws Exception { |
| 117 | ospfPacketHeader.setOspfVer(2); |
| 118 | result = ospfPacketHeader.ospfVersion(); |
| 119 | assertThat(result, is(notNullValue())); |
| 120 | assertThat(result, is(2)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Tests ospfType() getter method. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testGetOspfType() throws Exception { |
| 128 | ospfPacketHeader.setOspftype(3); |
| 129 | result = ospfPacketHeader.ospfType(); |
| 130 | assertThat(result, is(notNullValue())); |
| 131 | assertThat(result, is(3)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests ospfType() setter method. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testSetOspfType() throws Exception { |
| 139 | ospfPacketHeader.setOspftype(3); |
| 140 | result = ospfPacketHeader.ospfType(); |
| 141 | assertThat(result, is(notNullValue())); |
| 142 | assertThat(result, is(3)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Tests ospfPacLength() getter method. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testGetOspfPacLength() throws Exception { |
| 150 | ospfPacketHeader.setOspfPacLength(3); |
| 151 | result = ospfPacketHeader.ospfPacLength(); |
| 152 | assertThat(result, is(notNullValue())); |
| 153 | assertThat(result, is(3)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests ospfPacLength() setter method. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testSetOspfPacLength() throws Exception { |
| 161 | ospfPacketHeader.setOspfPacLength(3); |
| 162 | int result = ospfPacketHeader.ospfPacLength(); |
| 163 | assertThat(result, is(notNullValue())); |
| 164 | assertThat(result, is(3)); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Tests routerId()getter method. |
| 169 | */ |
| 170 | @Test |
| 171 | public void testGetRouterId() throws Exception { |
| 172 | |
| 173 | ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1")); |
| 174 | result1 = ospfPacketHeader.routerId(); |
| 175 | assertThat(result1, is(notNullValue())); |
| 176 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests routerId() setter method. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testSetRouterId() throws Exception { |
| 185 | ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1")); |
| 186 | result1 = ospfPacketHeader.routerId(); |
| 187 | assertThat(result1, is(notNullValue())); |
| 188 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Tests areaId() getter method. |
| 193 | */ |
| 194 | @Test |
| 195 | public void testGetAreaId() throws Exception { |
| 196 | ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); |
| 197 | result1 = ospfPacketHeader.areaId(); |
| 198 | assertThat(result1, is(notNullValue())); |
| 199 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Tests areaId() setter method. |
| 204 | */ |
| 205 | @Test |
| 206 | public void testSetAreaId() throws Exception { |
| 207 | ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); |
| 208 | result1 = ospfPacketHeader.areaId(); |
| 209 | assertThat(result1, is(notNullValue())); |
| 210 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Tests checksum() getter method. |
| 215 | */ |
| 216 | @Test |
| 217 | public void testGetChecksum() throws Exception { |
| 218 | ospfPacketHeader.setChecksum(3); |
| 219 | result = ospfPacketHeader.checksum(); |
| 220 | assertThat(result, is(notNullValue())); |
| 221 | assertThat(result, is(3)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tests checksum() setter method. |
| 226 | */ |
| 227 | @Test |
| 228 | public void testSetChecksum() throws Exception { |
| 229 | ospfPacketHeader.setChecksum(3); |
| 230 | result = ospfPacketHeader.checksum(); |
| 231 | assertThat(result, is(notNullValue())); |
| 232 | assertThat(result, is(3)); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Tests authType() getter method. |
| 237 | */ |
| 238 | @Test |
| 239 | public void testGetAutype() throws Exception { |
| 240 | ospfPacketHeader.setAuthType(3); |
| 241 | result = ospfPacketHeader.authType(); |
| 242 | Assert.assertNotNull(result); |
| 243 | Assert.assertEquals(3, result); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Tests authType() setter method. |
| 248 | */ |
| 249 | @Test |
| 250 | public void testSetAutype() throws Exception { |
| 251 | ospfPacketHeader.setAuthType(3); |
| 252 | result = ospfPacketHeader.authType(); |
| 253 | assertThat(result, is(notNullValue())); |
| 254 | assertThat(result, is(3)); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Tests authentication() getter method. |
| 259 | */ |
| 260 | @Test |
| 261 | public void testGetAuthentication() throws Exception { |
| 262 | ospfPacketHeader.setAuthentication(3); |
| 263 | result = ospfPacketHeader.authentication(); |
| 264 | assertThat(result, is(notNullValue())); |
| 265 | assertThat(result, is(3)); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Tests authentication() setter method. |
| 270 | */ |
| 271 | @Test |
| 272 | public void testSetAuthentication() throws Exception { |
| 273 | ospfPacketHeader.setAuthentication(3); |
| 274 | result = ospfPacketHeader.authentication(); |
| 275 | assertThat(result, is(notNullValue())); |
| 276 | assertThat(result, is(3)); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Tests destinationIp() getter method. |
| 281 | */ |
| 282 | @Test |
| 283 | public void testGetDestinationIP() throws Exception { |
| 284 | ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1")); |
| 285 | result1 = ospfPacketHeader.destinationIp(); |
| 286 | assertThat(result1, is(notNullValue())); |
| 287 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Tests destinationIp() setter method. |
| 292 | */ |
| 293 | @Test |
| 294 | public void testSetDestinationIP() throws Exception { |
| 295 | ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1")); |
| 296 | result1 = ospfPacketHeader.destinationIp(); |
| 297 | assertThat(result1, is(notNullValue())); |
| 298 | assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Tests to string method. |
| 303 | */ |
| 304 | @Test |
| 305 | public void testToString() throws Exception { |
| 306 | assertThat(ospfPacketHeader.toString(), is(notNullValue())); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Tests populateHeader() method. |
| 311 | */ |
| 312 | @Test |
| 313 | public void testPopulateHeader() throws Exception { |
| 314 | ospfPacketHeader.populateHeader(new OspfPacketHeader()); |
| 315 | assertThat(ospfPacketHeader, is(notNullValue())); |
| 316 | } |
| 317 | |
| 318 | } |