Fix bug on ByteBufUtils method serializeList
[openflowjava.git] / openflowjava-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.PooledByteBufAllocator;
13 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
24
25 /**
26  * @author michal.polkorab
27  *
28  */
29 public class ByteBufUtilsTest {
30
31     private final byte[] EXPECTED = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
32     private final byte[] EXPECTEDVALUES1AND255 = new byte[]{0x00, 0x01, 0x00, (byte) 0xff};
33
34     /**
35      * Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
36      */
37     @Test
38     public void testHexStringToBytes() {
39         byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
40
41         Assert.assertArrayEquals(EXPECTED, data);
42     }
43
44     /**
45      * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
46      */
47     @Test
48     public void testHexStringToBytes2() {
49         byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
50
51         Assert.assertArrayEquals(EXPECTED, data);
52     }
53
54     /**
55      * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
56      */
57     @Test
58     public void testHexStringToByteBuf() {
59         ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
60
61         Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(bb));
62     }
63
64     /**
65      * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
66      */
67     @Test
68     public void testHexStringToGivenByteBuf() {
69         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
70         ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
71
72         Assert.assertArrayEquals(EXPECTED, byteBufToByteArray(buffer));
73     }
74
75     private static byte[] byteBufToByteArray(ByteBuf bb) {
76         byte[] result = new byte[bb.readableBytes()];
77         bb.readBytes(result);
78         return result;
79     }
80
81     /**
82      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
83      */
84     @Test
85     public void testFillBitmaskByEmptyMap() {
86         Map<Integer, Boolean> emptyMap = new HashMap<>();
87         String expectedBinaryString = "00000000000000000000000000000000";
88         String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
89
90         Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
91     }
92
93     private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
94         String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
95         return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
96     }
97
98     /**
99      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
100      */
101     @Test
102     public void testFillBitmaskByFullMap() {
103         Map<Integer, Boolean> fullMap = new HashMap<>();
104         String expectedBinaryString = "11111111111111111111111111111111";
105         String bitmaskValueInBinarySytring;
106         for(Integer i=0;i<=31;i++) {
107             fullMap.put(i, true);
108         }
109         bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
110         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
111     }
112
113     /**
114      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
115      */
116     @Test
117     public void testFillBitmaskByZeroMap() {
118         Map<Integer, Boolean> zeroMap = new HashMap<>();
119         String expectedBinaryString = "00000000000000000000000000000000";
120         String bitmaskValueInBinarySytring;
121         for(Integer i=0;i<=31;i++) {
122             zeroMap.put(i, false);
123         }
124         bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
125         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
126     }
127
128     /**
129      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
130      */
131     @Test
132     public void testFillBitmaskByRandomSet() {
133         Map<Integer, Boolean> randomMap = new HashMap<>();
134         String expectedBinaryString = "00000000000000000111100000000000";
135         String bitmaskValueInBinarySytring;
136         Boolean mapValue;
137         for(Integer i=0;i<=31;i++) {
138             mapValue = false;
139             if(i>=11 && i<=14) {
140                 mapValue = true;
141             }
142             randomMap.put(i, mapValue);
143         }
144         bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
145         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
146     }
147
148     /**
149      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
150      */
151     @Test
152     public void testFillBitmaskByEmptyList() {
153         List<Boolean> emptyList = new ArrayList<>();
154         emptyList.add(null);
155         String expectedBinaryString = "00000000000000000000000000000000";
156         String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
157
158         Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
159     }
160
161     private static String listToBinaryString(List<Boolean> emptyList, int length) {
162         int[] bitMaskArray;
163         bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
164         String binaryString = Integer.toBinaryString(bitMaskArray[0]);
165         return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
166     }
167
168     /**
169      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
170      */
171     @Test
172     public void testFillBitmaskByFullList() {
173         List<Boolean> fullList = new ArrayList<>();
174         String expectedBinaryString = "11111111111111111111111111111111";
175         String bitmaskValueInBinarySytring;
176         for(Integer i=0;i<=31;i++) {
177             fullList.add(true);
178         }
179         bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
180         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
181     }
182
183     /**
184      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
185      */
186     @Test
187     public void testFillBitmaskByZeroList() {
188         List<Boolean> zeroList = new ArrayList<>();
189         String expectedBinaryString = "00000000000000000000000000000000";
190         String bitmaskValueInBinarySytring;
191         for(Integer i=0;i<=31;i++) {
192             zeroList.add(false);
193         }
194         bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
195         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
196     }
197
198     /**
199      * Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
200      */
201     @Test
202     public void testFillBitmaskFromRandomList() {
203         List<Boolean> randomList = new ArrayList<>();
204         String expectedBinaryString = "00000000000000000111100000000000";
205         String bitmaskValueInBinarySytring;
206         Boolean listValue;
207         for(Integer i=0;i<=31;i++) {
208             listValue = false;
209             if(i>=11 && i<=14) {
210                 listValue = true;
211             }
212             randomList.add(listValue);
213         }
214         bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
215         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
216     }
217
218     /**
219      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
220      */
221     @Test
222     public void testMacToBytes() {
223         Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
224                 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05"));
225         Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
226                 ByteBufUtils.macAddressToBytes("01:02:03:04:FF:05"));
227         Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
228                 ByteBufUtils.macAddressToBytes("1:2:3:4:FF:5"));
229         Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, 5, (byte) 255},
230                 ByteBufUtils.macAddressToBytes("1:2:3:4:5:FF"));
231         Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 15, 3, 4, 5, 6},
232                 ByteBufUtils.macAddressToBytes("1:F:3:4:5:6"));
233     }
234
235     /**
236      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
237      */
238     @Test(expected=IllegalArgumentException.class)
239     public void testMacToBytes2() {
240         Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
241                 ByteBufUtils.macAddressToBytes("00:01:02:03:FF:0G"));
242     }
243
244     /**
245      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
246      */
247     @Test(expected=IllegalArgumentException.class)
248     public void testMacToBytesTooShort() {
249         ByteBufUtils.macAddressToBytes("00:01:02:03:FF");
250     }
251
252     /**
253      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
254      */
255     @Test(expected=IllegalArgumentException.class)
256     public void testMacToBytesTooShort2() {
257         ByteBufUtils.macAddressToBytes("00:01:02:03:FF:");
258     }
259
260     /**
261      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
262      */
263     @Test(expected=IllegalArgumentException.class)
264     public void testIncorrectMacToBytes() {
265         ByteBufUtils.macAddressToBytes("00:01:02:03:FF::");
266     }
267
268     /**
269      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
270      */
271     @Test(expected=IllegalArgumentException.class)
272     public void testIncorrectMacToBytes2() {
273         ByteBufUtils.macAddressToBytes("00:01:02:03:FF:::");
274     }
275
276     /**
277      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
278      */
279     @Test(expected=IllegalArgumentException.class)
280     public void testMacToBytesTooLong() {
281         ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05:85");
282     }
283
284     /**
285      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
286      */
287     @Test(expected=IllegalArgumentException.class)
288     public void testMacToBytesInvalidOctet() {
289         ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05d");
290     }
291
292     /**
293      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
294      */
295     @Test(expected=IllegalArgumentException.class)
296     public void testMacToBytesInvalidOctet2() {
297         ByteBufUtils.macAddressToBytes("00:01:rr:03:FF:05");
298     }
299
300     /**
301      * Test of {@link ByteBufUtils#macAddressToBytes(String)}
302      */
303     @Test(expected=IllegalArgumentException.class)
304     public void testMacToBytesInvalidOctet3() {
305         ByteBufUtils.macAddressToBytes("00:01:05d:03:FF:02");
306     }
307
308     /**
309      * Test of {@link ByteBufUtils#macAddressToString(byte[])}
310      */
311     @Test(expected=IllegalArgumentException.class)
312     public void testMacToString() {
313         Assert.assertEquals("Wrong string decoded", "00:01:02:03:FF:05",
314                 ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5}));
315         ByteBufUtils.macAddressToString(new byte[]{0, 1, 2, 3, (byte) 255, 5, 6});
316     }
317
318     /**
319      * Test of {@link ByteBufUtils#decodeNullTerminatedString(ByteBuf, int)}
320      */
321     @Test
322     public void testDecodeString() {
323         ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
324         Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
325
326         ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
327         Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
328     }
329
330     /**
331      * Test of {@link ByteBufUtils#byteBufToHexString(ByteBuf)}
332      */
333     @Test
334     public void testByteBufToHexString() {
335         ByteBuf buf = ByteBufUtils.hexStringToByteBuf("00 01 02 03 04 05 06 07");
336         buf.skipBytes(4);
337         Assert.assertEquals("Wrong data read", "04 05 06 07", ByteBufUtils.byteBufToHexString(buf));
338     }
339
340     /**
341      * Buffer padding test
342      */
343     @Test
344     public void testPadBuffer() {
345         ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
346         ByteBufUtils.padBuffer(4, buf);
347         Assert.assertEquals("Wrong padding", 0, buf.readUnsignedInt());
348         ByteBufUtils.padBuffer(0, buf);
349         Assert.assertTrue("Wrong padding", buf.readableBytes() == 0);
350     }
351
352     /**
353      * Write OF header test
354      */
355     @Test
356     public void testWriteHeader() {
357         ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
358         HelloInputBuilder helloBuilder = new HelloInputBuilder();
359         helloBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
360         helloBuilder.setXid(12345L);
361         helloBuilder.setElements(null);
362         HelloInput helloInput = helloBuilder.build();
363         ByteBufUtils.writeOFHeader((byte) 0, helloInput, buf, EncodeConstants.OFHEADER_SIZE);
364         Assert.assertEquals("Wrong version", EncodeConstants.OF13_VERSION_ID, buf.readUnsignedByte());
365         Assert.assertEquals("Wrong type", 0, buf.readUnsignedByte());
366         Assert.assertEquals("Wrong length", EncodeConstants.OFHEADER_SIZE, buf.readUnsignedShort());
367         Assert.assertEquals("Wrong xid", 12345, buf.readUnsignedInt());
368         Assert.assertTrue("Unexpected data", buf.readableBytes() == 0);
369     }
370
371     /**
372      * Fill bitmask test
373      */
374     @Test
375     public void testFillBitmask() {
376         Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(0, false));
377         Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true));
378         Assert.assertEquals("Wrong bitmask", 3, ByteBufUtils.fillBitMask(0, true, true));
379         Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(0, false, true));
380         Assert.assertEquals("Wrong bitmask", 1, ByteBufUtils.fillBitMask(0, true, false));
381         Assert.assertEquals("Wrong bitmask", 2, ByteBufUtils.fillBitMask(1, true, false));
382         Assert.assertEquals("Wrong bitmask", 4, ByteBufUtils.fillBitMask(1, false, true));
383         Assert.assertEquals("Wrong bitmask", 6, ByteBufUtils.fillBitMask(1, true, true));
384         Assert.assertEquals("Wrong bitmask", 0, ByteBufUtils.fillBitMask(1));
385     }
386
387     /**
388      * Test bytes to hex string
389      */
390     @Test
391     public void testBytesToHexString() {
392         byte[] array = new byte[]{10, 11, 12, 13, 14, 15, 16};
393         Assert.assertEquals("Wrong conversion", "0a 0b 0c 0d 0e 0f 10", ByteBufUtils.bytesToHexString(array));
394         byte[] empty = new byte[0];
395         Assert.assertEquals("Wrong conversion", "", ByteBufUtils.bytesToHexString(empty));
396     }
397
398     /**
399      * Test ipv4 address conversion
400      */
401     @Test(expected=IndexOutOfBoundsException.class)
402     public void testReadIpv4Address() {
403         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
404         buffer.writeByte(10);
405         buffer.writeByte(20);
406         buffer.writeByte(30);
407         buffer.writeByte(40);
408         String ipv4Address = ByteBufUtils.readIpv4Address(buffer);
409         Assert.assertEquals("Wrong conversion", "10.20.30.40", ipv4Address);
410         Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
411
412         ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
413         buffer.writeByte(10);
414         ipv4Address = ByteBufUtils.readIpv4Address(buffer2);
415     }
416
417     /**
418      * Test ipv6 address conversion
419      */
420     @Test(expected=IndexOutOfBoundsException.class)
421     public void testReadIpv6Address() {
422         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
423         buffer.writeShort(10);
424         buffer.writeShort(65535);
425         buffer.writeShort(4096);
426         buffer.writeShort(0);
427         buffer.writeShort(1024);
428         buffer.writeShort(42);
429         buffer.writeShort(2568);
430         buffer.writeShort(45689);
431         String ipv4Address = ByteBufUtils.readIpv6Address(buffer);
432         Assert.assertEquals("Wrong conversion", "000A:FFFF:1000:0000:0400:002A:0A08:B279", ipv4Address);
433         Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
434
435         ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
436         buffer.writeShort(10);
437         ipv4Address = ByteBufUtils.readIpv6Address(buffer2);
438     }
439
440     @Test
441     public void testSerializeList() throws IOException {
442
443         List<Short> shorts = new ArrayList<>();
444         shorts.add((short) 1);
445         shorts.add((short) 255);
446
447         final byte[] bytes = ByteBufUtils.serializeList(shorts);
448         Assert.assertTrue(bytes.length == shorts.size()*2);
449         Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
450     }
451 }