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.UnpooledByteBufAllocator;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
24 * @author michal.polkorab
27 public class ByteBufUtilsTest {
29 private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
32 * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
35 public void testHexStringToBytes() {
36 byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
38 Assert.assertArrayEquals(expected, data);
42 * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
45 public void testHexStringToBytes2() {
46 byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
48 Assert.assertArrayEquals(expected, data);
52 * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
55 public void testHexStringToByteBuf() {
56 ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
58 Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
62 * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
65 public void testHexStringToGivenByteBuf() {
66 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
67 ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
69 Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
72 private static byte[] byteBufToByteArray(ByteBuf bb) {
73 byte[] result = new byte[bb.readableBytes()];
79 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
82 public void testFillBitmaskByEmptyMap() {
83 Map<Integer, Boolean> emptyMap = new HashMap<>();
84 String expectedBinaryString = "00000000000000000000000000000000";
85 String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
87 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
90 private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
91 String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
92 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
96 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
99 public void testFillBitmaskByFullMap() {
100 Map<Integer, Boolean> fullMap = new HashMap<>();
101 String expectedBinaryString = "11111111111111111111111111111111";
102 String bitmaskValueInBinarySytring;
103 for(Integer i=0;i<=31;i++) {
104 fullMap.put(i, true);
106 bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
107 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
111 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
114 public void testFillBitmaskByZeroMap() {
115 Map<Integer, Boolean> zeroMap = new HashMap<>();
116 String expectedBinaryString = "00000000000000000000000000000000";
117 String bitmaskValueInBinarySytring;
118 for(Integer i=0;i<=31;i++) {
119 zeroMap.put(i, false);
121 bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
122 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
126 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
129 public void testFillBitmaskByRandomSet() {
130 Map<Integer, Boolean> randomMap = new HashMap<>();
131 String expectedBinaryString = "00000000000000000111100000000000";
132 String bitmaskValueInBinarySytring;
134 for(Integer i=0;i<=31;i++) {
139 randomMap.put(i, mapValue);
141 bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
142 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
146 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
149 public void testFillBitmaskByEmptyList() {
150 List<Boolean> emptyList = new ArrayList<>();
152 String expectedBinaryString = "00000000000000000000000000000000";
153 String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
155 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
158 private static String listToBinaryString(List<Boolean> emptyList, int length) {
160 bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
161 String binaryString = Integer.toBinaryString(bitMaskArray[0]);
162 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
166 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
169 public void testFillBitmaskByFullList() {
170 List<Boolean> fullList = new ArrayList<>();
171 String expectedBinaryString = "11111111111111111111111111111111";
172 String bitmaskValueInBinarySytring;
173 for(Integer i=0;i<=31;i++) {
176 bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
177 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
181 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
184 public void testFillBitmaskByZeroList() {
185 List<Boolean> zeroList = new ArrayList<>();
186 String expectedBinaryString = "00000000000000000000000000000000";
187 String bitmaskValueInBinarySytring;
188 for(Integer i=0;i<=31;i++) {
191 bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
192 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
196 * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
199 public void testFillBitmaskFromRandomList() {
200 List<Boolean> randomList = new ArrayList<>();
201 String expectedBinaryString = "00000000000000000111100000000000";
202 String bitmaskValueInBinarySytring;
204 for(Integer i=0;i<=31;i++) {
209 randomList.add(listValue);
211 bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
212 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
216 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
219 public void testMacToBytes() {
220 Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
221 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05"));
225 * Test of {@link ByteBufUtils#macAddressToString(byte[])}
228 public void testMacToString() {
229 Assert.assertEquals("Wrong string decoded", "00:01:02:03:FF:05",
230 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5}));
234 * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}
237 public void testDecodeString() {
238 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
239 Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
241 ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
242 Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
246 * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}
249 public void testByteBufToHexString() {
250 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
252 Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));