Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / util / src / test / java / org / opendaylight / openflowjava / util / ByteBufUtilsTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.util;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22
23 /**
24  * @author michal.polkorab
25  *
26  */
27 public class ByteBufUtilsTest {
28
29     private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
30     
31     /**
32      * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
33      */
34     @Test
35     public void testHexStringToBytes() {
36         byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
37
38         Assert.assertArrayEquals(expected, data);
39     }
40
41     /**
42      * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
43      */
44     @Test
45     public void testHexStringToBytes2() {
46         byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
47
48         Assert.assertArrayEquals(expected, data);
49     }
50
51     /**
52      * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
53      */
54     @Test
55     public void testHexStringToByteBuf() {
56         ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
57         
58         Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
59     }
60     
61     /**
62      * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
63      */
64     @Test
65     public void testHexStringToGivenByteBuf() {
66         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
67         ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
68
69         Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
70     }
71     
72     private static byte[] byteBufToByteArray(ByteBuf bb) {
73         byte[] result = new byte[bb.readableBytes()];
74         bb.readBytes(result);
75         return result;
76     }
77     
78     /**
79      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
80      */
81     @Test
82     public void testFillBitmaskByEmptyMap() {
83         Map<Integer, Boolean> emptyMap = new HashMap<>();
84         String expectedBinaryString = "00000000000000000000000000000000";
85         String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
86         
87         Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
88     }
89
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");
93     }
94     
95     /**
96      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
97      */
98     @Test
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);
105         }
106         bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
107         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
108     }
109     
110     /**
111      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
112      */
113     @Test
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);
120         }
121         bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
122         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
123     }
124     
125     /**
126      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
127      */
128     @Test
129     public void testFillBitmaskByRandomSet() {
130         Map<Integer, Boolean> randomMap = new HashMap<>();
131         String expectedBinaryString = "00000000000000000111100000000000";
132         String bitmaskValueInBinarySytring;
133         Boolean mapValue;
134         for(Integer i=0;i<=31;i++) {
135             mapValue = false;
136             if(i>=11 && i<=14) {
137                 mapValue = true;
138             }
139             randomMap.put(i, mapValue);
140         }
141         bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
142         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
143     }
144     
145     /**
146      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
147      */
148     @Test
149     public void testFillBitmaskByEmptyList() {
150         List<Boolean> emptyList = new ArrayList<>();
151         emptyList.add(null);
152         String expectedBinaryString = "00000000000000000000000000000000";
153         String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
154         
155         Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
156     }
157
158     private static String listToBinaryString(List<Boolean> emptyList, int length) {
159         int[] bitMaskArray;
160         bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
161         String binaryString = Integer.toBinaryString(bitMaskArray[0]); 
162         return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
163     }
164     
165     /**
166      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
167      */
168     @Test
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++) {
174             fullList.add(true);
175         }
176         bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
177         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
178     }
179     
180     /**
181      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
182      */
183     @Test
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++) {
189             zeroList.add(false);
190         }
191         bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
192         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
193     }
194     
195     /**
196      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
197      */
198     @Test
199     public void testFillBitmaskFromRandomList() {
200         List<Boolean> randomList = new ArrayList<>();
201         String expectedBinaryString = "00000000000000000111100000000000";
202         String bitmaskValueInBinarySytring;
203         Boolean listValue;
204         for(Integer i=0;i<=31;i++) {
205             listValue = false;
206             if(i>=11 && i<=14) {
207                 listValue = true;
208             }
209             randomList.add(listValue);
210         }
211         bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
212         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
213     }
214
215     /**
216      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
217      */
218     @Test
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"));
222     }
223
224     /**
225      * Test of {@link ByteBufUtils#macAddressToString(byte[])}
226      */
227     @Test
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}));
231     }
232
233     /**
234      * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}
235      */
236     @Test
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));
240         
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));
243     }
244
245     /**
246      * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}
247      */
248     @Test
249     public void testByteBufToHexString() {
250         ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
251         buf.skipBytes(4);
252         Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));
253     }
254
255 }