blob: 3f257926a2dc5181c9cbb262337ea844c1f00a72 [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;
26
Mohamed Rahila3b9e992016-02-16 20:26:49 +053027import java.util.Vector;
28
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
31import static org.hamcrest.Matchers.notNullValue;
32
33/**
34 * Unit test class for NetworkLsa.
35 */
36public class NetworkLsaTest {
37
Jonathan Hart953bd002016-03-30 13:31:00 -070038 private static final Ip4Address LOCAL_ADDRESS = Ip4Address.valueOf("127.0.0.1");
39
Mohamed Rahila3b9e992016-02-16 20:26:49 +053040 private Vector<String> attachedRouters = new Vector();
41 private NetworkLsa networkLsa;
42 private Ip4Address result;
43 private Ip4Address inetAddres;
44 private byte[] inputByteArray;
45 private LsaHeader lsaHeader;
46 private ChannelBuffer channelBuffer;
47 private byte[] result1;
48 private OspfLsaType ospflsaType;
49 private int result2;
50
51 @Before
52 public void setUp() throws Exception {
53 networkLsa = new NetworkLsa();
54 }
55
56 @After
57 public void tearDown() throws Exception {
58 networkLsa = null;
59 attachedRouters = null;
60 result = null;
61 inetAddres = null;
62 inputByteArray = null;
63 lsaHeader = null;
64 channelBuffer = null;
65 result1 = null;
66 ospflsaType = null;
67 }
68
69 /**
70 * Tests networkMask() getter method.
71 */
72 @Test
73 public void testGetNetworkMask() throws Exception {
74 networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
75 result = networkLsa.networkMask();
76 assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
77 }
78
79 /**
80 * Tests networkMask() setter method.
81 */
82 @Test
83 public void testSetNetworkMask() throws Exception {
84 networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.165"));
85 result = networkLsa.networkMask();
86 result = networkLsa.networkMask();
87 assertThat(result, is(Ip4Address.valueOf("10.226.165.165")));
88 }
89
90 /**
91 * Tests addAttachedRouter() getter method.
92 */
93 @Test
94 public void testGetAttachedRouters() throws Exception {
95 attachedRouters.add("1.1.1.1");
96 networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1"));
97 assertThat(attachedRouters, is(notNullValue()));
98 }
99
100 /**
101 * Tests addAttachedRouter() setter method.
102 */
103 @Test
104 public void testSetAttachedRouters() throws Exception {
105 attachedRouters.add("1.1.1.1");
106 networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1"));
107 assertThat(attachedRouters, is(notNullValue()));
108 }
109
110 /**
111 * Tests addAttachedRouter() method.
112 */
113 @Test
114 public void testAddAttachedRouter() throws Exception {
Jonathan Hart953bd002016-03-30 13:31:00 -0700115 networkLsa.addAttachedRouter(LOCAL_ADDRESS);
116 networkLsa.addAttachedRouter(LOCAL_ADDRESS);
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530117 assertThat(networkLsa, is(notNullValue()));
118 }
119
120 /**
121 * Tests readFrom() method.
122 */
123
124 @Test
125 public void testReadFrom() throws Exception {
126 inputByteArray = createByteForNetworkLsa();
127 lsaHeader = createLsaHeader();
128 networkLsa = new NetworkLsa(lsaHeader);
129 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
130 networkLsa.readFrom(channelBuffer);
131 assertThat(networkLsa, is(notNullValue()));
132 }
133
134 /**
135 * Tests readFrom() method.
136 */
137 @Test(expected = Exception.class)
138 public void testReadFrom1() throws Exception {
139 byte[] temp = {0, 0, 0};
140 inputByteArray = temp;
141 lsaHeader = createLsaHeader();
142 networkLsa = new NetworkLsa(lsaHeader);
143 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
144 networkLsa.readFrom(channelBuffer);
145 assertThat(networkLsa, is(notNullValue()));
146 }
147
148 /**
149 * Tests asBytes() method.
150 */
151 @Test(expected = Exception.class)
152 public void testAsBytes() throws Exception {
153 result1 = networkLsa.asBytes();
154 assertThat(result1, is(notNullValue()));
155 }
156
157 /**
158 * Tests getLsaBodyAsByteArray() method.
159 */
160 @Test(expected = Exception.class)
161 public void testGetLsaBodyAsByteArray() throws Exception {
162 networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1"));
163 networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2"));
164 networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3"));
sunish vkaa48da82016-03-02 23:17:06 +0530165 result1 = networkLsa.getLsaBodyAsByteArray();
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530166 assertThat(result1, is(notNullValue()));
167 }
168
169 /**
170 * Tests getLsaBodyAsByteArray() method.
171 */
172 @Test
173 public void testGetLsaBodyAsByteArray1() throws Exception {
174 networkLsa.setNetworkMask(Ip4Address.valueOf("255.255.255.255"));
175 networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1"));
176 networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2"));
177 networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3"));
sunish vkaa48da82016-03-02 23:17:06 +0530178 result1 = networkLsa.getLsaBodyAsByteArray();
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530179 assertThat(result1, is(notNullValue()));
180 }
181
182 /**
183 * Tests getOspfLsaType() getter method.
184 */
185 @Test
186 public void testGetOspfLsaType() throws Exception {
187 networkLsa.setLsType(2);
188 ospflsaType = networkLsa.getOspfLsaType();
189 assertThat(ospflsaType, is(OspfLsaType.NETWORK));
190 }
191
192 /**
193 * Tests hashCode() method.
194 */
195 @Test
196 public void testHashcode() throws Exception {
197
198 result2 = networkLsa.hashCode();
199 assertThat(result2, is(notNullValue()));
200
201 }
202
203 /**
204 * Utility method used by junit methods.
205 */
206 private byte[] createByteForNetworkLsa() {
207 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,
208 -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,
209 1};
210 return packet;
211 }
212
213 /**
214 * Utility method used by junit methods.
215 */
216 private LsaHeader createLsaHeader() {
217 lsaHeader = new LsaHeader();
218 lsaHeader.setLsType(2);
219 lsaHeader.setLsPacketLen(48);
220 lsaHeader.setLsCheckSum(10);
221 lsaHeader.setAge(4);
222 lsaHeader.setLinkStateId("10.226.165.164");
223 lsaHeader.setLsSequenceNo(250);
224 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
225 lsaHeader.setOptions(2);
226 return lsaHeader;
227 }
Jonathan Hart953bd002016-03-30 13:31:00 -0700228}