2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.util;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.PooledByteBufAllocator;
13 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
26 * @author michal.polkorab
29 public class ByteBufUtilsTest {
31 private final byte[] EXPECTED = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
32 private final byte[] EXPECTEDVALUES1AND255 = new byte[]{0x00, 0x01, 0x00, (byte) 0xff};
35 * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
38 public void testHexStringToBytes() {
39 byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
41 Assert.assertArrayEquals(EXPECTED, data);
45 * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
48 public void testHexStringToBytes2() {
49 byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
51 Assert.assertArrayEquals(EXPECTED, data);
55 * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
58 public void testHexStringToByteBuf() {
59 ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
61 Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(bb));
65 * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
68 public void testHexStringToGivenByteBuf() {
69 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
70 ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
72 Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(buffer));
75 private static byte[] byteBufToByteArray(ByteBuf bb) {
76 byte[] result = new byte[bb.readableBytes()];
82 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
85 public void testFillBitmaskByEmptyMap() {
86 Map<Integer, Boolean> emptyMap = new HashMap<>();
87 String expectedBinaryString = "00000000000000000000000000000000";
88 String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
90 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
93 private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
94 String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
95 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
99 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
102 public void testFillBitmaskByFullMap() {
103 Map<Integer, Boolean> fullMap = new HashMap<>();
104 String expectedBinaryString = "11111111111111111111111111111111";
105 String bitmaskValueInBinarySytring;
106 for(Integer i=0;i<=31;i++) {
107 fullMap.put(i, true);
109 bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
110 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
114 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
117 public void testFillBitmaskByZeroMap() {
118 Map<Integer, Boolean> zeroMap = new HashMap<>();
119 String expectedBinaryString = "00000000000000000000000000000000";
120 String bitmaskValueInBinarySytring;
121 for(Integer i=0;i<=31;i++) {
122 zeroMap.put(i, false);
124 bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
125 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
129 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
132 public void testFillBitmaskByRandomSet() {
133 Map<Integer, Boolean> randomMap = new HashMap<>();
134 String expectedBinaryString = "00000000000000000111100000000000";
135 String bitmaskValueInBinarySytring;
137 for(Integer i=0;i<=31;i++) {
142 randomMap.put(i, mapValue);
144 bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
145 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
149 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
152 public void testFillBitmaskByEmptyList() {
153 List<Boolean> emptyList = new ArrayList<>();
155 String expectedBinaryString = "00000000000000000000000000000000";
156 String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
158 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
161 private static String listToBinaryString(List<Boolean> emptyList, int length) {
163 bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
164 String binaryString = Integer.toBinaryString(bitMaskArray[0]);
165 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
169 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
172 public void testFillBitmaskByFullList() {
173 List<Boolean> fullList = new ArrayList<>();
174 String expectedBinaryString = "11111111111111111111111111111111";
175 String bitmaskValueInBinarySytring;
176 for(Integer i=0;i<=31;i++) {
179 bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
180 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
184 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
187 public void testFillBitmaskByZeroList() {
188 List<Boolean> zeroList = new ArrayList<>();
189 String expectedBinaryString = "00000000000000000000000000000000";
190 String bitmaskValueInBinarySytring;
191 for(Integer i=0;i<=31;i++) {
194 bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
195 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
199 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
202 public void testFillBitmaskFromRandomList() {
203 List<Boolean> randomList = new ArrayList<>();
204 String expectedBinaryString = "00000000000000000111100000000000";
205 String bitmaskValueInBinarySytring;
207 for(Integer i=0;i<=31;i++) {
212 randomList.add(listValue);
214 bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
215 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
219 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
222 public void testMacToBytes() {
223 Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
224 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05"));
225 Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
226 ByteBufUtils.macAddressToBytes("01:02:03:04:FF:05"));
227 Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
228 ByteBufUtils.macAddressToBytes("1:2:3:4:FF:5"));
229 Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, 5, (byte) 255},
230 ByteBufUtils.macAddressToBytes("1:2:3:4:5:FF"));
231 Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 15, 3, 4, 5, 6},
232 ByteBufUtils.macAddressToBytes("1:F:3:4:5:6"));
236 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
238 @Test(expected=IllegalArgumentException.class)
239 public void testMacToBytes2() {
240 Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
241 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:0G"));
245 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
247 @Test(expected=IllegalArgumentException.class)
248 public void testMacToBytesTooShort() {
249 ByteBufUtils.macAddressToBytes("00:01:02:03:FF");
253 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
255 @Test(expected=IllegalArgumentException.class)
256 public void testMacToBytesTooShort2() {
257 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:");
261 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
263 @Test(expected=IllegalArgumentException.class)
264 public void testIncorrectMacToBytes() {
265 ByteBufUtils.macAddressToBytes("00:01:02:03:FF::");
269 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
271 @Test(expected=IllegalArgumentException.class)
272 public void testIncorrectMacToBytes2() {
273 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:::");
277 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
279 @Test(expected=IllegalArgumentException.class)
280 public void testMacToBytesTooLong() {
281 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05:85");
285 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
287 @Test(expected=IllegalArgumentException.class)
288 public void testMacToBytesInvalidOctet() {
289 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05d");
293 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
295 @Test(expected=IllegalArgumentException.class)
296 public void testMacToBytesInvalidOctet2() {
297 ByteBufUtils.macAddressToBytes("00:01:rr:03:FF:05");
301 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
303 @Test(expected=IllegalArgumentException.class)
304 public void testMacToBytesInvalidOctet3() {
305 ByteBufUtils.macAddressToBytes("00:01:05d:03:FF:02");
309 * Test of {@link ByteBufUtils#macAddressToString(byte[])}
311 @Test(expected=IllegalArgumentException.class)
312 public void testMacToString() {
313 Assert.assertEquals("Wrong string decoded", "00:01:02:03:FF:05",
314 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5}));
315 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5, 6});
319 * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}
322 public void testDecodeString() {
323 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
324 Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
326 ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
327 Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
331 * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}
334 public void testByteBufToHexString() {
335 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
337 Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));
341 * Buffer padding test
344 public void testPadBuffer() {
345 ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
346 ByteBufUtils.padBuffer(4, buf);
347 Assert.assertEquals("Wrong padding", 0, buf.readUnsignedInt());
348 ByteBufUtils.padBuffer(0, buf);
349 Assert.assertTrue("Wrong padding", buf.readableBytes() == 0);
353 * Write OF header test
356 public void testWriteHeader() {
357 ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
358 HelloInputBuilder helloBuilder = new HelloInputBuilder();
359 helloBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
360 helloBuilder.setXid(12345L);
361 helloBuilder.setElements(null);
362 HelloInput helloInput = helloBuilder.build();
363 ByteBufUtils.writeOFHeader((byte) 0, helloInput, buf, EncodeConstants.OFHEADER_SIZE);
364 Assert.assertEquals("Wrong version", EncodeConstants.OF13_VERSION_ID, buf.readUnsignedByte());
365 Assert.assertEquals("Wrong type", 0, buf.readUnsignedByte());
366 Assert.assertEquals("Wrong length", EncodeConstants.OFHEADER_SIZE, buf.readUnsignedShort());
367 Assert.assertEquals("Wrong xid", 12345, buf.readUnsignedInt());
368 Assert.assertTrue("Unexpected data", buf.readableBytes() == 0);
375 public void testFillBitmask() {
376 Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(0, false));
377 Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true));
378 Assert.assertEquals("Wrong bitmask", 3, ByteBufUtils.fillBitMask(0, true, true));
379 Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(0, false, true));
380 Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true, false));
381 Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(1, true, false));
382 Assert.assertEquals("Wrong bitmask", 4, ByteBufUtils.fillBitMask(1, false, true));
383 Assert.assertEquals("Wrong bitmask", 6, ByteBufUtils.fillBitMask(1, true, true));
384 Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(1));
388 * Test bytes to hex string
391 public void testBytesToHexString() {
392 byte[] array = new byte[]{10, 11, 12, 13, 14, 15, 16};
393 Assert.assertEquals("Wrong conversion", "0a 0b 0c 0d 0e 0f 10", ByteBufUtils.bytesToHexString(array));
394 byte[] empty = new byte[0];
395 Assert.assertEquals("Wrong conversion", "", ByteBufUtils.bytesToHexString(empty));
399 * Test ipv4 address conversion
401 @Test(expected=IndexOutOfBoundsException.class)
402 public void testReadIpv4Address() {
403 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
404 buffer.writeByte(10);
405 buffer.writeByte(20);
406 buffer.writeByte(30);
407 buffer.writeByte(40);
408 String ipv4Address = ByteBufUtils.readIpv4Address(buffer);
409 Assert.assertEquals("Wrong conversion", "10.20.30.40", ipv4Address);
410 Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
412 ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
413 buffer.writeByte(10);
414 ipv4Address = ByteBufUtils.readIpv4Address(buffer2);
418 * Test ipv6 address conversion
420 @Test(expected=IndexOutOfBoundsException.class)
421 public void testReadIpv6Address() {
422 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
423 buffer.writeShort(10);
424 buffer.writeShort(65535);
425 buffer.writeShort(4096);
426 buffer.writeShort(0);
427 buffer.writeShort(1024);
428 buffer.writeShort(42);
429 buffer.writeShort(2568);
430 buffer.writeShort(45689);
431 String ipv4Address = ByteBufUtils.readIpv6Address(buffer);
432 Assert.assertEquals("Wrong conversion", "000A:FFFF:1000:0000:0400:002A:0A08:B279", ipv4Address);
433 Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
435 ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
436 buffer.writeShort(10);
437 ipv4Address = ByteBufUtils.readIpv6Address(buffer2);
441 public void testSerializeList() throws IOException {
443 List<Short> shorts = new ArrayList<>();
444 shorts.add((short) 1);
445 shorts.add((short) 255);
447 final byte[] bytes = ByteBufUtils.serializeList(shorts);
448 Assert.assertTrue(bytes.length == shorts.size()*2);
449 Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
453 public void testUpdateHeader() throws IOException {
454 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
456 int start = buffer.writerIndex();
457 buffer.writeShort(4);
458 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
460 int end = buffer.writerIndex();
462 ByteBufUtils.updateOFHeaderLength(buffer, start);
463 Assert.assertEquals(buffer.readInt(), 1);
464 Assert.assertEquals(buffer.readShort(), 4);
465 Assert.assertEquals(buffer.readShort(), 12);
466 Assert.assertEquals(buffer.readLong(), 8l);
467 Assert.assertEquals(buffer.getShort(start + EncodeConstants.OFHEADER_LENGTH_INDEX), end - start);