blob: f25b66a6f7f00f2c7b97c561c9f6d3498114ce04 [file] [log] [blame]
Manicdb26412016-02-16 19:44:34 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Manicdb26412016-02-16 19:44:34 +05303 *
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.protocol.ospfpacket.types;
17
18import org.hamcrest.MatcherAssert;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.buffer.ChannelBuffers;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.Ip4Address;
sunishvkf7c56552016-07-18 16:02:39 +053025import org.onosproject.ospf.controller.OspfPacketType;
Manicdb26412016-02-16 19:44:34 +053026import org.onosproject.ospf.protocol.lsa.LsaHeader;
27import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
28import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
Manicdb26412016-02-16 19:44:34 +053029
30import java.util.List;
31
32import static org.hamcrest.CoreMatchers.is;
33import static org.hamcrest.CoreMatchers.notNullValue;
34import static org.junit.Assert.assertThat;
35
36/**
37 * Unit test class for LsAck.
38 */
39public class LsAcknowledgeTest {
40
41 private LsaHeader lsaHeader;
42 private LsAcknowledge lsAck;
43 private OspfPacketType ospfPacketType;
44 private OspfPacketHeader ospfPacketHeader;
45 private byte[] result;
46 private ChannelBuffer channelBuffer;
47 private OpaqueLsaHeader opaqueLsaHeader;
48
49 @Before
50 public void setUp() throws Exception {
51 lsaHeader = new LsaHeader();
52 lsAck = new LsAcknowledge();
53 lsAck.setAuthType(1);
54 lsAck.setOspftype(5);
55 lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
56 lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
57 lsAck.setChecksum(201);
58 lsAck.setAuthentication(2);
59 lsAck.setOspfPacLength(48);
60 lsAck.setOspfVer(2);
61 }
62
63 @After
64 public void tearDown() throws Exception {
65 lsaHeader = null;
66 lsAck = null;
67 ospfPacketType = null;
68 ospfPacketHeader = null;
69 result = null;
70 channelBuffer = null;
71 opaqueLsaHeader = null;
72 }
73
74 /**
75 * Tests getLinkStateHeaders() getter method.
76 */
77 @Test
78 public void testGetLinkStateHeaders() throws Exception {
79 lsaHeader = createLsaHeader();
80 lsAck.addLinkStateHeader(lsaHeader);
81 lsAck.addLinkStateHeader(lsaHeader);
82 List headers = lsAck.getLinkStateHeaders();
83 assertThat(headers.size(), is(1));
84
85 }
86
87 /**
88 * Tests addLinkStateHeader() method.
89 */
90 @Test
91 public void testAddLinkStateHeader() throws Exception {
92 lsaHeader = createLsaHeader();
93 lsAck.addLinkStateHeader(lsaHeader);
94 lsAck.addLinkStateHeader(lsaHeader);
95 assertThat(lsAck, is(notNullValue()));
96 }
97
Manicdb26412016-02-16 19:44:34 +053098 /**
99 * Tests ospfMessageType() getter method.
100 */
101 @Test
102 public void testGetOspfMessageType() throws Exception {
103 ospfPacketType = lsAck.ospfMessageType();
104 assertThat(ospfPacketType, is(notNullValue()));
105 assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
106 }
107
108 /**
109 * Tests readFrom() method.
110 */
111 @Test
112 public void testReadFrom() throws Exception {
113 ospfPacketHeader = new OspfPacketHeader();
114 ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
115 ospfPacketHeader.setAuthentication(0);
116 ospfPacketHeader.setAuthType(0);
117 ospfPacketHeader.setChecksum(12345);
118 ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
119 ospfPacketHeader.setOspfPacLength(56);
120 ospfPacketHeader.setOspftype(5);
121 ospfPacketHeader.setOspfVer(2);
122 ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
123 ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
124 result = createByteForLSAck();
125 lsAck = new LsAcknowledge(ospfPacketHeader);
126 channelBuffer = ChannelBuffers.copiedBuffer(result);
127 lsAck.readFrom(channelBuffer);
128 assertThat(lsAck, is(notNullValue()));
129 assertThat(lsAck.ospfMessageType(), is(OspfPacketType.LSAACK));
130 }
131
132 /**
133 * Tests asBytes() method.
134 */
135 @Test
136 public void testAsBytes() throws Exception {
137 result = lsAck.asBytes();
138 assertThat(result, is(notNullValue()));
139 }
140
141 /**
142 * Tests getLsAckAsByteArray() method.
143 */
144 @Test
145 public void testGetLsAckAsByteArray() throws Exception {
146 result = lsAck.getLsAckAsByteArray();
147 assertThat(result, is(notNullValue()));
148 }
149
150 /**
151 * Tests getLsAckBodyAsByteArray() method.
152 */
153 @Test
154 public void testGetLsAckBodyAsByteArray() throws Exception {
155 lsaHeader = createLsaHeader();
156 opaqueLsaHeader = new OpaqueLsaHeader();
157 lsAck.addLinkStateHeader(lsaHeader);
158 lsAck.addLinkStateHeader(opaqueLsaHeader);
159 result = lsAck.getLsAckBodyAsByteArray();
160 assertThat(result, is(notNullValue()));
161 }
162
163 /**
164 * Tests to string method.
165 */
166 @Test
167 public void testToString() throws Exception {
168 MatcherAssert.assertThat(lsAck.toString(), is(notNullValue()));
169 }
170
171 /**
172 * Utility method used by junit methods.
173 */
174 private LsaHeader createLsaHeader() {
175 lsaHeader = new LsaHeader();
176 lsaHeader.setAge(10);
177 lsaHeader.setLinkStateId("10.226.165.164");
178 lsaHeader.setLsCheckSum(222);
179 lsaHeader.setLsPacketLen(48);
180 lsaHeader.setLsSequenceNo(2020);
181 lsaHeader.setLsType(5);
182 lsaHeader.setOptions(2);
183 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165"));
184 return lsaHeader;
185 }
186
187 /**
188 * Utility method used by junit methods.
189 */
190 private byte[] createByteForLSAck() {
191 byte[] lsAckPacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30,
192 -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
193 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
194
195 return lsAckPacket;
196 }
197}