nicklesh adlakha | 90bfa6a | 2016-04-28 20:38:33 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
nicklesh adlakha | 90bfa6a | 2016-04-28 20:38:33 +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.isis.io.util; |
| 17 | |
| 18 | import com.google.common.primitives.Bytes; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.isis.controller.IsisPduType; |
| 24 | |
| 25 | import static org.hamcrest.CoreMatchers.is; |
| 26 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | |
| 29 | /** |
| 30 | * Unit test class for IsisUtil. |
| 31 | */ |
| 32 | public class IsisUtilTest { |
| 33 | |
| 34 | private final String systemId = "2929.2929.2929"; |
| 35 | private final String lanId = "2929.2929.2929.01"; |
| 36 | private final String areaAddres = "490001"; |
| 37 | private final byte[] l1Lsp = { |
| 38 | -125, 27, 1, 0, 18, 1, 0, 0, 0, 86, 4, -81, 34, 34, 34, |
| 39 | 34, 34, 34, 0, 0, 0, 0, 0, 9, 99, 11, 1, 1, 4, 3, 73, |
| 40 | 0, 10, -127, 1, -52, -119, 2, 82, 50, -124, 4, -64, -88, 10, 1, -128, |
| 41 | 24, 10, -128, -128, -128, 10, 0, 10, 0, -1, -1, -1, -4, 10, -128, -128, |
| 42 | -128, -64, -88, 10, 0, -1, -1, -1, 0, 2, 12, 0, 10, -128, -128, -128, |
| 43 | 51, 51, 51, 51, 51, 51, 2 |
| 44 | }; |
| 45 | private final byte[] intger = {0, 0, 0, 1}; |
| 46 | private Ip4Address ip4Address1 = Ip4Address.valueOf("10.10.10.10"); |
| 47 | private Ip4Address ip4Address2 = Ip4Address.valueOf("10.10.10.11"); |
| 48 | private Ip4Address mask = Ip4Address.valueOf("255.255.255.0"); |
| 49 | private boolean result; |
| 50 | private String result1; |
| 51 | private byte[] result2; |
| 52 | private int result3; |
| 53 | private long result4; |
| 54 | private byte[] prefixBytes = {0, 0, 0, 1}; |
| 55 | private String prefix = "192.16.17"; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() throws Exception { |
| 59 | |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests sameNetwork() method. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testSameNetwork() throws Exception { |
| 72 | result = IsisUtil.sameNetwork(ip4Address1, ip4Address2, mask.toOctets()); |
| 73 | assertThat(result, is(true)); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Tests systemId() method. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testSystemId() throws Exception { |
| 81 | result1 = IsisUtil.systemId(Bytes.toArray( |
| 82 | IsisUtil.sourceAndLanIdToBytes(systemId))); |
| 83 | assertThat(result1, is(systemId)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests systemIdPlus() method. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testSystemIdPlus() throws Exception { |
| 91 | result1 = IsisUtil.systemIdPlus(Bytes.toArray( |
| 92 | IsisUtil.sourceAndLanIdToBytes(lanId))); |
| 93 | assertThat(result1, is(lanId)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests areaAddres() method. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testAreaAddres() throws Exception { |
| 101 | result1 = IsisUtil.areaAddres(Bytes.toArray( |
| 102 | IsisUtil.areaAddressToBytes(areaAddres))); |
| 103 | assertThat(result1, is(areaAddres)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests areaAddressToBytes() method. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testAreaAddressToBytes() throws Exception { |
| 111 | result2 = Bytes.toArray(IsisUtil.areaAddressToBytes(areaAddres)); |
| 112 | assertThat(result2, is(notNullValue())); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests getPduHeaderLength() method. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testGetPduHeaderLength() throws Exception { |
| 120 | result3 = IsisUtil.getPduHeaderLength(IsisPduType.L1CSNP.value()); |
| 121 | assertThat(result3, is(33)); |
| 122 | result3 = IsisUtil.getPduHeaderLength(IsisPduType.L1PSNP.value()); |
| 123 | assertThat(result3, is(17)); |
| 124 | result3 = IsisUtil.getPduHeaderLength(IsisPduType.L1HELLOPDU.value()); |
| 125 | assertThat(result3, is(27)); |
| 126 | result3 = IsisUtil.getPduHeaderLength(IsisPduType.P2PHELLOPDU.value()); |
| 127 | assertThat(result3, is(20)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests addLengthAndMarkItInReserved() method. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testAddLengthAndMarkItInReserved() throws Exception { |
| 135 | result2 = IsisUtil.addLengthAndMarkItInReserved(l1Lsp, |
| 136 | IsisConstants.LENGTHPOSITION, IsisConstants.LENGTHPOSITION + 1, |
| 137 | IsisConstants.RESERVEDPOSITION); |
| 138 | assertThat(result2, is(notNullValue())); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Tests addChecksum() method. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testAddChecksum() throws Exception { |
| 146 | result2 = IsisUtil.addChecksum(l1Lsp, |
| 147 | IsisConstants.CHECKSUMPOSITION, IsisConstants.CHECKSUMPOSITION + 1); |
| 148 | assertThat(result2, is(notNullValue())); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests framePacket() method. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testFramePacket() throws Exception { |
| 156 | result2 = IsisUtil.framePacket(l1Lsp, 2); |
| 157 | assertThat(result2, is(notNullValue())); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests sourceAndLanIdToBytes() method. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testSourceAndLanIdToBytes() throws Exception { |
| 165 | result2 = Bytes.toArray(IsisUtil.sourceAndLanIdToBytes(lanId)); |
| 166 | assertThat(result2, is(notNullValue())); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Tests getPaddingTlvs() method. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testGetPaddingTlvs() throws Exception { |
| 174 | result2 = IsisUtil.getPaddingTlvs(250); |
| 175 | assertThat(result2, is(notNullValue())); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Tests convertToTwoBytes() method. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testConvertToTwoBytes() throws Exception { |
| 183 | result2 = IsisUtil.convertToTwoBytes(250); |
| 184 | assertThat(result2.length, is(2)); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Tests convertToFourBytes() method. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testConvertToFourBytes() throws Exception { |
| 192 | result2 = IsisUtil.convertToFourBytes(250); |
| 193 | assertThat(result2.length, is(4)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests byteToInteger() method. |
| 198 | */ |
| 199 | @Test |
| 200 | public void testByteToInteger() throws Exception { |
| 201 | result3 = IsisUtil.byteToInteger(intger); |
| 202 | assertThat(result3, is(1)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Tests byteToInteger() method. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testByteToLong() throws Exception { |
| 210 | result4 = IsisUtil.byteToLong(intger); |
| 211 | assertThat(result4, is(1L)); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Tests convertToFourBytes() method. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testConvertToFourBytes1() throws Exception { |
| 219 | result2 = IsisUtil.convertToFourBytes(250L); |
| 220 | assertThat(result2.length, is(4)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests toFourBitBinary() method. |
| 225 | */ |
| 226 | @Test |
| 227 | public void testToEightBitBinary() throws Exception { |
| 228 | result1 = IsisUtil.toEightBitBinary("01"); |
| 229 | assertThat(result1.length(), is(8)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Tests toFourBitBinary() method. |
| 234 | */ |
| 235 | @Test |
| 236 | public void testToFourBitBinary() throws Exception { |
| 237 | result1 = IsisUtil.toFourBitBinary("01"); |
| 238 | assertThat(result1.length(), is(4)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Tests convertToThreeBytes() method. |
| 243 | */ |
| 244 | @Test |
| 245 | public void testConvertToThreeBytes() throws Exception { |
| 246 | result2 = IsisUtil.convertToThreeBytes(30); |
| 247 | assertThat(result2.length, is(3)); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests prefixConversion() method. |
| 252 | */ |
| 253 | @Test |
| 254 | public void testPrefixConversion() throws Exception { |
| 255 | result1 = IsisUtil.prefixConversion(prefixBytes); |
| 256 | assertThat(result1, is(notNullValue())); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Tests prefixToBytes() method. |
| 261 | */ |
| 262 | @Test |
| 263 | public void testPrefixToBytes() throws Exception { |
| 264 | result2 = IsisUtil.prefixToBytes(prefix); |
| 265 | assertThat(result2, is(notNullValue())); |
| 266 | } |
| 267 | } |