Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +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.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket; |
| 23 | import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket; |
| 24 | import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge; |
| 25 | import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest; |
| 26 | import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate; |
| 27 | |
| 28 | import static org.hamcrest.CoreMatchers.is; |
| 29 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 30 | import static org.junit.Assert.assertThat; |
| 31 | |
| 32 | /** |
| 33 | * Unit test class for OspfMessageWriter. |
| 34 | */ |
| 35 | public class OspfMessageWriterTest { |
| 36 | |
| 37 | private OspfMessageWriter ospfMessageWriter; |
| 38 | private HelloPacket helloPacket; |
| 39 | private DdPacket ddPacket; |
| 40 | private LsAcknowledge lsAck; |
| 41 | private LsRequest lsReq; |
| 42 | private LsUpdate lsUpdate; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() throws Exception { |
| 46 | ospfMessageWriter = new OspfMessageWriter(); |
| 47 | } |
| 48 | |
| 49 | @After |
| 50 | public void tearDown() throws Exception { |
| 51 | ospfMessageWriter = null; |
| 52 | helloPacket = null; |
| 53 | ddPacket = null; |
| 54 | lsAck = null; |
| 55 | lsReq = null; |
| 56 | lsUpdate = null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests writeToBuffer() method. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testWriteToBuffer() throws Exception { |
| 64 | helloPacket = new HelloPacket(); |
| 65 | helloPacket.setAuthType(1); |
| 66 | helloPacket.setOspftype(1); |
| 67 | helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 68 | helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 69 | helloPacket.setChecksum(201); |
| 70 | helloPacket.setAuthentication(2); |
| 71 | helloPacket.setOspfPacLength(48); |
| 72 | helloPacket.setOspfVer(2); |
| 73 | helloPacket.setNetworkMask(Ip4Address.valueOf("255.255.255.255")); |
| 74 | helloPacket.setOptions(2); //not setting now |
| 75 | helloPacket.setHelloInterval(10); |
| 76 | helloPacket.setRouterPriority(1); |
| 77 | helloPacket.setRouterDeadInterval(40); |
| 78 | helloPacket.setDr(Ip4Address.valueOf("1.1.1.1")); |
| 79 | helloPacket.setBdr(Ip4Address.valueOf("2.2.2.2")); |
| 80 | helloPacket.addNeighbor(Ip4Address.valueOf("8.8.8.8")); |
| 81 | helloPacket.setDestinationIp(Ip4Address.valueOf("5.5.5.5")); |
| 82 | ospfMessageWriter.writeToBuffer(helloPacket, 7, 1); |
| 83 | assertThat(ospfMessageWriter, is(notNullValue())); |
| 84 | } |
| 85 | |
| 86 | @Test(expected = Exception.class) |
| 87 | public void testWriteToBuffer1() throws Exception { |
| 88 | |
| 89 | ddPacket = new DdPacket(); |
| 90 | ddPacket.setAuthType(1); |
| 91 | ddPacket.setOspftype(2); |
| 92 | ddPacket.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 93 | ddPacket.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 94 | ddPacket.setChecksum(201); |
| 95 | ddPacket.setAuthentication(2); |
| 96 | ddPacket.setOspfPacLength(48); |
| 97 | ddPacket.setOspfVer(2); |
| 98 | ospfMessageWriter.writeToBuffer(ddPacket, 1, 1); |
| 99 | assertThat(ospfMessageWriter, is(notNullValue())); |
| 100 | } |
| 101 | |
| 102 | @Test(expected = Exception.class) |
| 103 | public void testWriteToBuffer2() throws Exception { |
| 104 | |
| 105 | lsAck = new LsAcknowledge(); |
| 106 | lsAck.setAuthType(1); |
| 107 | lsAck.setOspftype(5); |
| 108 | lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 109 | lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 110 | lsAck.setChecksum(201); |
| 111 | lsAck.setAuthentication(2); |
| 112 | lsAck.setOspfPacLength(48); |
| 113 | lsAck.setOspfVer(2); |
| 114 | ospfMessageWriter.writeToBuffer(lsAck, 1, 1); |
| 115 | assertThat(ospfMessageWriter, is(notNullValue())); |
| 116 | } |
| 117 | |
| 118 | @Test(expected = Exception.class) |
| 119 | public void testWriteToBuffer3() throws Exception { |
| 120 | lsReq = new LsRequest(); |
| 121 | lsReq.setAuthType(1); |
| 122 | lsReq.setOspftype(3); |
| 123 | lsReq.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 124 | lsReq.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 125 | lsReq.setChecksum(201); |
| 126 | lsReq.setAuthentication(2); |
| 127 | lsReq.setOspfPacLength(48); |
| 128 | lsReq.setOspfVer(2); |
| 129 | ospfMessageWriter.writeToBuffer(lsReq, 1, 1); |
| 130 | assertThat(ospfMessageWriter, is(notNullValue())); |
| 131 | } |
| 132 | |
| 133 | @Test(expected = Exception.class) |
| 134 | public void testWriteToBuffer4() throws Exception { |
| 135 | lsUpdate = new LsUpdate(); |
| 136 | lsUpdate.setAuthType(1); |
| 137 | lsUpdate.setOspftype(3); |
| 138 | lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 139 | lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 140 | lsUpdate.setChecksum(201); |
| 141 | lsUpdate.setAuthentication(2); |
| 142 | lsUpdate.setOspfPacLength(48); |
| 143 | lsUpdate.setOspfVer(2); |
| 144 | ospfMessageWriter.writeToBuffer(lsUpdate, 1, 1); |
| 145 | assertThat(ospfMessageWriter, is(notNullValue())); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests writeToBuffer() method. |
| 150 | */ |
| 151 | @Test(expected = Exception.class) |
| 152 | public void testWriteToBuffer5() throws Exception { |
| 153 | lsAck = new LsAcknowledge(); |
| 154 | lsAck.setAuthType(1); |
| 155 | lsAck.setOspftype(5); |
| 156 | lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 157 | lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 158 | lsAck.setChecksum(201); |
| 159 | lsAck.setAuthentication(2); |
| 160 | lsAck.setOspfPacLength(48); |
| 161 | lsAck.setOspfVer(2); |
| 162 | ospfMessageWriter.writeToBuffer(lsAck, 1, 1); |
| 163 | } |
| 164 | } |