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