Dhruv Dhody | b585012 | 2016-02-17 17:51:19 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | b585012 | 2016-02-17 17:51:19 +0530 | [diff] [blame] | 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.controller.impl; |
| 17 | |
| 18 | |
| 19 | import org.easymock.EasyMock; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.jboss.netty.buffer.ChannelBuffers; |
| 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.Ip4Address; |
| 28 | import org.onosproject.ospf.controller.area.OspfInterfaceImpl; |
| 29 | import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket; |
| 30 | import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket; |
| 31 | import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge; |
| 32 | import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest; |
| 33 | import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate; |
| 34 | import org.onosproject.ospf.protocol.util.OspfInterfaceState; |
| 35 | |
| 36 | import java.net.InetSocketAddress; |
| 37 | import java.net.SocketAddress; |
| 38 | |
| 39 | import static org.hamcrest.CoreMatchers.is; |
| 40 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 41 | import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | |
| 43 | /** |
| 44 | * Created by sdn on 13/1/16. |
| 45 | */ |
| 46 | public class OspfMessageEncoderTest { |
| 47 | private final byte[] hpacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59, |
| 48 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, |
| 49 | 40, -64, -88, -86, 8, 0, 0, 0, 0}; |
| 50 | private final byte[] dpacket = {2, 2, 0, 32, -64, -88, -86, 8, 0, 0, 0, 1, -96, 82, |
| 51 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119, -87, 126}; |
| 52 | private final byte[] lrpacket = {2, 3, 0, 36, -64, -88, -86, 3, 0, 0, 0, 1, -67, -57, |
| 53 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, -86, 8, -64, -88, -86, 8}; |
| 54 | private byte[] lAckpacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30, -12, |
| 55 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86, 2, -64, |
| 56 | -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48}; |
| 57 | private HelloPacket helloPacket; |
| 58 | private DdPacket ddPacket; |
| 59 | private LsAcknowledge lsAcknowledge; |
| 60 | private LsRequest lsRequest; |
| 61 | private LsUpdate lsUpdate; |
| 62 | private ChannelHandlerContext ctx; |
| 63 | private OspfMessageEncoder ospfMessageEncoder; |
| 64 | private ChannelBuffer buf; |
| 65 | private SocketAddress socketAddress; |
| 66 | private Channel channel; |
| 67 | |
| 68 | @Before |
| 69 | public void setUp() throws Exception { |
| 70 | ospfMessageEncoder = new OspfMessageEncoder(); |
| 71 | helloPacket = new HelloPacket(); |
| 72 | ddPacket = new DdPacket(); |
| 73 | lsAcknowledge = new LsAcknowledge(); |
| 74 | lsRequest = new LsRequest(); |
| 75 | lsUpdate = new LsUpdate(); |
| 76 | helloPacket.setOspftype(1); |
| 77 | ddPacket.setOspftype(2); |
| 78 | lsAcknowledge.setOspftype(5); |
| 79 | lsRequest.setOspftype(3); |
| 80 | lsUpdate.setOspftype(4); |
| 81 | OspfInterfaceImpl ospfInterface = new OspfInterfaceImpl(); |
| 82 | ospfInterface.setState(OspfInterfaceState.DROTHER); |
| 83 | ospfMessageEncoder = new OspfMessageEncoder(ospfInterface); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() throws Exception { |
| 89 | helloPacket = null; |
| 90 | ddPacket = null; |
| 91 | lsAcknowledge = null; |
| 92 | lsRequest = null; |
| 93 | lsUpdate = null; |
| 94 | ospfMessageEncoder = null; |
| 95 | buf = null; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests encode() method. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testEncode() throws Exception { |
| 103 | socketAddress = InetSocketAddress.createUnresolved("127.0.0.1", 8600); |
| 104 | channel = EasyMock.createMock(Channel.class); |
| 105 | helloPacket = new HelloPacket(); |
| 106 | helloPacket.setDestinationIp(Ip4Address.valueOf("15.15.15.15")); |
| 107 | buf = ChannelBuffers.buffer(hpacket.length); |
| 108 | buf.writeBytes(hpacket); |
| 109 | helloPacket.readFrom(buf); |
| 110 | ospfMessageEncoder.encode(ctx, channel, helloPacket); |
| 111 | ddPacket = new DdPacket(); |
| 112 | ddPacket.setDestinationIp(Ip4Address.valueOf("15.15.15.15")); |
| 113 | buf = ChannelBuffers.buffer(dpacket.length); |
| 114 | buf.writeBytes(dpacket); |
| 115 | ddPacket.readFrom(buf); |
| 116 | ospfMessageEncoder.encode(ctx, channel, ddPacket); |
| 117 | lsRequest = new LsRequest(); |
| 118 | lsRequest.setDestinationIp(Ip4Address.valueOf("15.15.15.15")); |
| 119 | buf = ChannelBuffers.buffer(lrpacket.length); |
| 120 | buf.writeBytes(lrpacket); |
| 121 | lsRequest.readFrom(buf); |
| 122 | ospfMessageEncoder.encode(ctx, channel, lsRequest); |
| 123 | |
| 124 | lsAcknowledge = new LsAcknowledge(); |
| 125 | lsAcknowledge.setDestinationIp(Ip4Address.valueOf("15.15.15.15")); |
| 126 | buf = ChannelBuffers.buffer(lAckpacket.length); |
| 127 | buf.writeBytes(lAckpacket); |
| 128 | lsAcknowledge.readFrom(buf); |
| 129 | ospfMessageEncoder.encode(ctx, channel, lsAcknowledge); |
| 130 | assertThat(ospfMessageEncoder, is(notNullValue())); |
| 131 | } |
| 132 | } |