blob: 61613d054f4b7398cc048c444f99c24ee073ee64 [file] [log] [blame]
Dhruv Dhodyb5850122016-02-17 17:51:19 +05301/*
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 */
16package org.onosproject.ospf.controller.impl;
17
18
19import org.easymock.EasyMock;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.jboss.netty.buffer.ChannelBuffers;
22import org.jboss.netty.channel.Channel;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.packet.Ip4Address;
28import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
29import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
30import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
31import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
32import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
33import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
34import org.onosproject.ospf.protocol.util.OspfInterfaceState;
35
36import java.net.InetSocketAddress;
37import java.net.SocketAddress;
38
39import static org.hamcrest.CoreMatchers.is;
40import static org.hamcrest.CoreMatchers.notNullValue;
41import static org.hamcrest.MatcherAssert.assertThat;
42
43/**
44 * Created by sdn on 13/1/16.
45 */
46public 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}