blob: 5796f3417643af7b3abba595637ee99293291890 [file] [log] [blame]
Mohamed Rahila3b9e992016-02-16 20:26:49 +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.protocol.lsa.types;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ip4Address;
24import org.onosproject.ospf.controller.OspfLsaType;
25import org.onosproject.ospf.protocol.lsa.LsaHeader;
26import org.onosproject.ospf.protocol.lsa.subtypes.OspfExternalDestination;
27
28
29import java.net.InetAddress;
30import java.net.UnknownHostException;
31import java.util.Vector;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
35import static org.hamcrest.Matchers.notNullValue;
36
37/**
38 * Unit test class for ExternalLsa.
39 */
40public class ExternalLsaTest {
41
42 private ExternalLsa externalLsa;
43 private Vector<OspfExternalDestination> externalDestinations = new Vector<OspfExternalDestination>();
44 private Ip4Address result;
45 private OspfExternalDestination ospfExternalDestination;
46 private OspfExternalDestination ospfExternalDestination1;
47 private LsaHeader lsaHeader;
48 private byte[] inputByteArray;
49 private ChannelBuffer channelBuffer;
50 private byte[] result1;
51 private OspfLsaType ospflsaType;
52 private int result2;
53
54 @Before
55 public void setUp() throws Exception {
56 externalLsa = new ExternalLsa(new LsaHeader());
57 }
58
59 @After
60 public void tearDown() throws Exception {
61 externalLsa = null;
62 externalDestinations = null;
63 result = null;
64 ospfExternalDestination = null;
65 ospfExternalDestination1 = null;
66 lsaHeader = null;
67 inputByteArray = null;
68 channelBuffer = null;
69 result1 = null;
70 ospflsaType = null;
71 }
72
73 /**
74 * Tests networkMask() getter method.
75 */
76 @Test
77 public void testGetNetworkMask() throws Exception {
78 externalLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
79 result = externalLsa.networkMask();
80 assertThat(result, is(notNullValue()));
81 assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
82 }
83
84 /**
85 * Tests networkMask() setter method.
86 */
87 @Test
88 public void testSetNetworkMask() throws Exception {
89 externalLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
90 result = externalLsa.networkMask();
91 assertThat(result, is(notNullValue()));
92 assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
93 }
94
95 /**
96 * Tests addExternalDesitnation() method.
97 */
98 @Test
99 public void testAddExternalDesitnation() throws Exception {
100 externalLsa.addExternalDestination(createOspfExternalDestination());
101 assertThat(externalLsa, is(notNullValue()));
102 }
103
104 /**
105 * Tests hashCode() method.
106 */
107 @Test
108 public void testHashcode() throws Exception {
109
110 result2 = externalLsa.hashCode();
111 assertThat(result2, is(notNullValue()));
112
113 }
114
115 /**
116 * Tests readFrom() method.
117 */
118 @Test
119 public void testReadFrom() throws Exception {
120 ospfExternalDestination = new OspfExternalDestination();
121 ospfExternalDestination.setExternalRouterTag(2);
122 ospfExternalDestination.setMetric(100);
123 ospfExternalDestination.setType1orType2Metric(true);
124 externalLsa.addExternalDestination(ospfExternalDestination);
125 ospfExternalDestination1 = new OspfExternalDestination();
126 ospfExternalDestination.setExternalRouterTag(3);
127 ospfExternalDestination.setMetric(50);
128 ospfExternalDestination.setType1orType2Metric(true);
129 externalLsa.addExternalDestination(ospfExternalDestination1);
130 ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost()));
131 inputByteArray = createByteForNetworkLsa();
132 lsaHeader = createLsaHeader();
133 externalLsa = new ExternalLsa(lsaHeader);
134 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
135 externalLsa.readFrom(channelBuffer);
136 assertThat(externalLsa, is(notNullValue()));
137 }
138
139 /**
140 * Tests readFrom() method.
141 */
142 @Test(expected = Exception.class)
143 public void testReadFrom1() throws Exception {
144 ospfExternalDestination = new OspfExternalDestination();
145 ospfExternalDestination.setExternalRouterTag(2);
146 ospfExternalDestination.setMetric(100);
147 ospfExternalDestination.setType1orType2Metric(true);
148 externalLsa.addExternalDestination(ospfExternalDestination);
149 ospfExternalDestination1 = new OspfExternalDestination();
150 ospfExternalDestination.setExternalRouterTag(3);
151 ospfExternalDestination.setMetric(50);
152 ospfExternalDestination.setType1orType2Metric(true);
153 externalLsa.addExternalDestination(ospfExternalDestination1);
154 ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost()));
155 byte[] temp = {0, 0, 0};
156 inputByteArray = temp;
157 lsaHeader = createLsaHeader();
158 externalLsa = new ExternalLsa(lsaHeader);
159 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
160 externalLsa.readFrom(channelBuffer);
161 assertThat(externalLsa, is(notNullValue()));
162 }
163
164 /**
165 * Tests asBytes() method.
166 */
167 @Test
168 public void testAsBytes() throws Exception {
169 result1 = externalLsa.asBytes();
170 assertThat(result1, is(notNullValue()));
171 }
172
173 /**
174 * Tests getLsaBodyAsByteArray() method.
175 */
176 @Test
177 public void testGetLsaBodyAsByteArray() throws Exception {
178 ospfExternalDestination = new OspfExternalDestination();
179 ospfExternalDestination.setExternalRouterTag(2);
180 ospfExternalDestination.setMetric(100);
181 ospfExternalDestination.setType1orType2Metric(true);
182 externalLsa.addExternalDestination(ospfExternalDestination);
183 ospfExternalDestination1 = new OspfExternalDestination();
184 ospfExternalDestination.setExternalRouterTag(3);
185 ospfExternalDestination.setMetric(100);
186 ospfExternalDestination.setType1orType2Metric(true);
187 externalLsa.addExternalDestination(ospfExternalDestination1);
188 result1 = externalLsa.getLsaBodyAsByteArray();
189 assertThat(result1, is(notNullValue()));
190 }
191
192 /**
193 * Tests getLsaBodyAsByteArray() method.
194 */
195 @Test
196 public void testGetLsaBodyAsByteArray1() throws Exception {
197 externalLsa.setNetworkMask(Ip4Address.valueOf("255.255.255.255"));
198 ospfExternalDestination = new OspfExternalDestination();
199 ospfExternalDestination.setExternalRouterTag(2);
200 ospfExternalDestination.setMetric(100);
201 ospfExternalDestination.setType1orType2Metric(true);
202 externalLsa.addExternalDestination(ospfExternalDestination);
203 ospfExternalDestination1 = new OspfExternalDestination();
204 ospfExternalDestination.setExternalRouterTag(3);
205 ospfExternalDestination.setMetric(100);
206 ospfExternalDestination.setType1orType2Metric(true);
207 externalLsa.addExternalDestination(ospfExternalDestination1);
208 result1 = externalLsa.getLsaBodyAsByteArray();
209 assertThat(result1, is(notNullValue()));
210 }
211
212 /**
213 * Tests getOspfLsaType() getter method.
214 */
215 @Test
216 public void testGetOspfLsaType() throws Exception {
217 ospflsaType = externalLsa.getOspfLsaType();
218 assertThat(ospflsaType, is(notNullValue()));
219 assertThat(ospflsaType, is(OspfLsaType.EXTERNAL_LSA));
220 }
221
222 /**
223 * Tests to string method.
224 */
225 @Test
226 public void testToString() throws Exception {
227 assertThat(externalLsa.toString(), is(notNullValue()));
228 }
229
230 /**
231 * Utility method used by junit methods.
232 */
233 private byte[] createByteForNetworkLsa() {
234 byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64,
235 -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56,
236 1};
237 return packet;
238 }
239
240 /**
241 * Utility method used by junit methods.
242 */
243 private LsaHeader createLsaHeader() {
244 lsaHeader = new LsaHeader();
245 lsaHeader.setLsType(1);
246 lsaHeader.setLsPacketLen(48);
247 lsaHeader.setLsCheckSum(10);
248 lsaHeader.setAge(4);
249 lsaHeader.setLinkStateId("10.226.165.164");
250 lsaHeader.setLsSequenceNo(250);
251 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
252 lsaHeader.setOptions(2);
253 return lsaHeader;
254 }
255
256 /**
257 * Utility method used by junit methods.
258 */
259 private OspfExternalDestination createOspfExternalDestination() throws UnknownHostException {
260 ospfExternalDestination = new OspfExternalDestination();
261 ospfExternalDestination.setExternalRouterTag(1);
262 ospfExternalDestination.setMetric(10);
263 ospfExternalDestination.setType1orType2Metric(true);
264 ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost()));
265 return ospfExternalDestination;
266 }
267}