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.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint32;
26 * Unit tests for ByteBufUtils.
28 * @author michal.polkorab
30 public class ByteBufUtilsTest {
32 private static final byte[] EXPECTED = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
33 private static final byte[] EXPECTEDVALUES1AND255 = new byte[]{0x00, 0x01, 0x00, (byte) 0xff};
36 * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}.
39 public void testHexStringToBytes() {
40 byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
42 Assert.assertArrayEquals(EXPECTED, data);
46 * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}.
49 public void testHexStringToBytes2() {
50 byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
52 Assert.assertArrayEquals(EXPECTED, data);
56 * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}.
59 public void testHexStringToByteBuf() {
60 ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
62 Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(bb));
66 * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}.
69 public void testHexStringToGivenByteBuf() {
70 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
71 ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
73 Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(buffer));
76 private static byte[] byteBufToByteArray(ByteBuf bb) {
77 byte[] result = new byte[bb.readableBytes()];
83 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}.
86 public void testFillBitmaskByEmptyMap() {
87 Map<Integer, Boolean> emptyMap = new HashMap<>();
88 String expectedBinaryString = "00000000000000000000000000000000";
89 String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
91 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
94 private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
95 String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
96 return String.format("%" + length + "s", binaryString).replaceAll(" ", "0");
100 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}.
103 public void testFillBitmaskByFullMap() {
104 Map<Integer, Boolean> fullMap = new HashMap<>();
105 String expectedBinaryString = "11111111111111111111111111111111";
106 String bitmaskValueInBinarySytring;
107 for (Integer i = 0; i <= 31; i++) {
108 fullMap.put(i, true);
110 bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
111 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
115 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}.
118 public void testFillBitmaskByZeroMap() {
119 Map<Integer, Boolean> zeroMap = new HashMap<>();
120 String expectedBinaryString = "00000000000000000000000000000000";
121 String bitmaskValueInBinarySytring;
122 for (Integer i = 0; i <= 31; i++) {
123 zeroMap.put(i, false);
125 bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
126 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
130 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}.
133 public void testFillBitmaskByRandomSet() {
134 Map<Integer, Boolean> randomMap = new HashMap<>();
135 String expectedBinaryString = "00000000000000000111100000000000";
136 String bitmaskValueInBinarySytring;
138 for (Integer i = 0; i <= 31; i++) {
140 if (i >= 11 && i <= 14) {
143 randomMap.put(i, mapValue);
145 bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
146 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
150 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}.
153 public void testFillBitmaskByEmptyList() {
154 List<Boolean> emptyList = new ArrayList<>();
156 String expectedBinaryString = "00000000000000000000000000000000";
157 String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
159 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
162 private static String listToBinaryString(List<Boolean> emptyList, int length) {
164 bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
165 String binaryString = Integer.toBinaryString(bitMaskArray[0]);
166 return String.format("%" + length + "s", binaryString).replaceAll(" ", "0");
170 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}.
173 public void testFillBitmaskByFullList() {
174 List<Boolean> fullList = new ArrayList<>();
175 String expectedBinaryString = "11111111111111111111111111111111";
176 String bitmaskValueInBinarySytring;
177 for (Integer i = 0; i <= 31; i++) {
180 bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
181 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
185 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}.
188 public void testFillBitmaskByZeroList() {
189 List<Boolean> zeroList = new ArrayList<>();
190 String expectedBinaryString = "00000000000000000000000000000000";
191 String bitmaskValueInBinarySytring;
192 for (Integer i = 0; i <= 31; i++) {
195 bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
196 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
200 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}.
203 public void testFillBitmaskFromRandomList() {
204 List<Boolean> randomList = new ArrayList<>();
205 String expectedBinaryString = "00000000000000000111100000000000";
206 String bitmaskValueInBinarySytring;
208 for (Integer i = 0; i <= 31; i++) {
210 if (i >= 11 && i <= 14) {
213 randomList.add(listValue);
215 bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
216 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
220 * Test of {@link ByteBufUtils#macAddressToString(byte[])}.
222 @Test(expected = IllegalArgumentException.class)
223 public void testMacToString() {
224 Assert.assertEquals("Wrong string decoded", "00:01:02:03:FF:05",
225 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5}));
226 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5, 6});
230 * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}.
233 public void testDecodeString() {
234 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
235 Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
237 ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
238 Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
242 * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}.
245 public void testByteBufToHexString() {
246 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
248 Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));
252 * Write OF header test.
255 public void testWriteHeader() {
256 HelloInputBuilder helloBuilder = new HelloInputBuilder();
257 helloBuilder.setVersion(EncodeConstants.OF_VERSION_1_3);
258 helloBuilder.setXid(Uint32.valueOf(12345));
259 helloBuilder.setElements(null);
260 HelloInput helloInput = helloBuilder.build();
261 ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
262 ByteBufUtils.writeOFHeader((byte) 0, helloInput, buf, EncodeConstants.OFHEADER_SIZE);
263 Assert.assertEquals("Wrong version", EncodeConstants.OF13_VERSION_ID, buf.readUnsignedByte());
264 Assert.assertEquals("Wrong type", 0, buf.readUnsignedByte());
265 Assert.assertEquals("Wrong length", EncodeConstants.OFHEADER_SIZE, buf.readUnsignedShort());
266 Assert.assertEquals("Wrong xid", 12345, buf.readUnsignedInt());
267 Assert.assertTrue("Unexpected data", buf.readableBytes() == 0);
274 public void testFillBitmask() {
275 Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(0, false));
276 Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true));
277 Assert.assertEquals("Wrong bitmask", 3, ByteBufUtils.fillBitMask(0, true, true));
278 Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(0, false, true));
279 Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true, false));
280 Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(1, true, false));
281 Assert.assertEquals("Wrong bitmask", 4, ByteBufUtils.fillBitMask(1, false, true));
282 Assert.assertEquals("Wrong bitmask", 6, ByteBufUtils.fillBitMask(1, true, true));
283 Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(1));
287 * Test bytes to hex string.
290 public void testBytesToHexString() {
291 byte[] array = new byte[]{10, 11, 12, 13, 14, 15, 16};
292 Assert.assertEquals("Wrong conversion", "0a 0b 0c 0d 0e 0f 10", ByteBufUtils.bytesToHexString(array));
293 byte[] empty = new byte[0];
294 Assert.assertEquals("Wrong conversion", "", ByteBufUtils.bytesToHexString(empty));
298 * Test ipv4 address conversion.
300 @Test(expected = IndexOutOfBoundsException.class)
301 public void testReadIpv4Address() {
302 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
303 buffer.writeByte(10);
304 buffer.writeByte(20);
305 buffer.writeByte(30);
306 buffer.writeByte(40);
307 String ipv4Address = ByteBufUtils.readIpv4Address(buffer);
308 Assert.assertEquals("Wrong conversion", "10.20.30.40", ipv4Address);
309 Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
311 ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
312 buffer.writeByte(10);
313 ipv4Address = ByteBufUtils.readIpv4Address(buffer2);
317 * Test ipv6 address conversion.
319 @Test(expected = IndexOutOfBoundsException.class)
320 public void testReadIpv6Address() {
321 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
322 buffer.writeShort(10);
323 buffer.writeShort(65535);
324 buffer.writeShort(4096);
325 buffer.writeShort(0);
326 buffer.writeShort(1024);
327 buffer.writeShort(42);
328 buffer.writeShort(2568);
329 buffer.writeShort(45689);
330 String ipv4Address = ByteBufUtils.readIpv6Address(buffer);
331 Assert.assertEquals("Wrong conversion", "000A:FFFF:1000:0000:0400:002A:0A08:B279", ipv4Address);
332 Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
334 ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
335 buffer.writeShort(10);
336 ipv4Address = ByteBufUtils.readIpv6Address(buffer2);
340 public void testSerializeList() {
342 List<Short> shorts = new ArrayList<>();
343 shorts.add((short) 1);
344 shorts.add((short) 255);
346 final byte[] bytes = ByteBufUtils.serializeList(shorts);
347 Assert.assertTrue(bytes.length == shorts.size() * 2);
348 Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
352 public void testUpdateHeader() {
353 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
355 final int start = buffer.writerIndex();
356 buffer.writeShort(4);
357 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
359 final int end = buffer.writerIndex();
361 ByteBufUtils.updateOFHeaderLength(buffer, start);
362 Assert.assertEquals(buffer.readInt(), 1);
363 Assert.assertEquals(buffer.readShort(), 4);
364 Assert.assertEquals(buffer.readShort(), 12);
365 Assert.assertEquals(buffer.readLong(), 8L);
366 Assert.assertEquals(buffer.getShort(start + EncodeConstants.OFHEADER_LENGTH_INDEX), end - start);