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