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.protocol.impl.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;
23 * @author michal.polkorab
26 public class ByteBufUtilsTest {
28 private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
31 * Test of {@link ByteBufUtils#hexStringToBytes(String)}
34 public void testHexStringToBytes() {
35 byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
37 Assert.assertArrayEquals(expected, data);
41 * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
44 public void testHexStringToBytes2() {
45 byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
47 Assert.assertArrayEquals(expected, data);
51 * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
54 public void testHexStringToByteBuf() {
55 ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
57 Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
61 * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
64 public void testHexStringToGivenByteBuf() {
65 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
66 ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
68 Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
71 private static byte[] byteBufToByteArray(ByteBuf bb) {
72 byte[] result = new byte[bb.readableBytes()];
78 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
81 public void testFillBitmaskByEmptyMap() {
82 Map<Integer, Boolean> emptyMap = new HashMap<>();
83 String expectedBinaryString = "00000000000000000000000000000000";
84 String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
86 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
89 private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
90 String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
91 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
95 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
98 public void testFillBitmaskByFullMap() {
99 Map<Integer, Boolean> fullMap = new HashMap<>();
100 String expectedBinaryString = "11111111111111111111111111111111";
101 String bitmaskValueInBinarySytring;
102 for(Integer i=0;i<=31;i++) {
103 fullMap.put(i, true);
105 bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
106 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
110 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
113 public void testFillBitmaskByZeroMap() {
114 Map<Integer, Boolean> zeroMap = new HashMap<>();
115 String expectedBinaryString = "00000000000000000000000000000000";
116 String bitmaskValueInBinarySytring;
117 for(Integer i=0;i<=31;i++) {
118 zeroMap.put(i, false);
120 bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
121 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
125 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
128 public void testFillBitmaskByRandomSet() {
129 Map<Integer, Boolean> randomMap = new HashMap<>();
130 String expectedBinaryString = "00000000000000000111100000000000";
131 String bitmaskValueInBinarySytring;
133 for(Integer i=0;i<=31;i++) {
138 randomMap.put(i, mapValue);
140 bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
141 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
145 * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
148 public void testFillBitmaskByEmptyList() {
149 List<Boolean> emptyList = new ArrayList<>();
151 String expectedBinaryString = "00000000000000000000000000000000";
152 String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
154 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
157 private static String listToBinaryString(List<Boolean> emptyList, int length) {
159 bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
160 String binaryString = Integer.toBinaryString(bitMaskArray[0]);
161 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
165 * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
168 public void testFillBitmaskByFullList() {
169 List<Boolean> fullList = new ArrayList<>();
170 String expectedBinaryString = "11111111111111111111111111111111";
171 String bitmaskValueInBinarySytring;
172 for(Integer i=0;i<=31;i++) {
175 bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
176 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
180 * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
183 public void testFillBitmaskByZeroList() {
184 List<Boolean> zeroList = new ArrayList<>();
185 String expectedBinaryString = "00000000000000000000000000000000";
186 String bitmaskValueInBinarySytring;
187 for(Integer i=0;i<=31;i++) {
190 bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
191 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
195 * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
198 public void testFillBitmaskFromRandomList() {
199 List<Boolean> randomList = new ArrayList<>();
200 String expectedBinaryString = "00000000000000000111100000000000";
201 String bitmaskValueInBinarySytring;
203 for(Integer i=0;i<=31;i++) {
208 randomList.add(listValue);
210 bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
211 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
215 * Test of {@link ByteBufUtils#macAddressToBytes(String)}
218 public void testMacToBytes() {
219 Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
220 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05"));
224 * Test of {@link ByteBufUtils#macAddressToString(byte[])}
227 public void testMacToString() {
228 Assert.assertEquals("Wrong string decoded", "00:01:02:03:FF:05",
229 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5}));
233 * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}
236 public void testDecodeString() {
237 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
238 Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
240 ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
241 Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
245 * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}
248 public void testByteBufToHexString() {
249 ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
251 Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));