e0e3590d236ede3942ca894a48b9902041fc025e
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / util / ByteUtilTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7
8 package org.opendaylight.openflowplugin.openflow.md.util;
9
10 import org.junit.Test;
11
12 import java.math.BigInteger;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertTrue;
16
17 /**
18  * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
19  */
20 public class ByteUtilTest {
21
22     private static final String hexString = "64,65,66,ff,";
23     private static final String hexString00 = "00,00,00,00,";
24     private static final String hexStringFF = "ff,ff,ff,ff,";
25
26     private static final byte[] testBytes = {100, 101, 102, (byte) 255};
27     private static final byte[] testBytes00 = {0, 0, 0, 0};
28     private static final byte[] testBytesFF = {(byte) 255, (byte) 255, (byte) 255, (byte) 255};
29
30     private static final BigInteger bigInteger = new BigInteger("1684367103");
31     private static final BigInteger bigIntFF = new BigInteger("4294967295");
32     private static final int int00 = 0;
33
34     private static final int shortByteLength = 2;
35     private static final int intByteLength = 4;
36
37     @Test
38     public void testBytesToHexstring() {
39         assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
40         assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
41         assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
42     }
43
44     @Test
45     public void testConvertBigIntegerToNBytes() {
46         byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
47         assertEquals(4, bigIntAsBytes.length);
48
49         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
50         assertEquals(6, bigIntAsBytes.length);
51
52         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
53         assertEquals(8, bigIntAsBytes.length);
54     }
55
56     @Test
57     public void testBytesToUnsignedInt() {
58         long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
59         assertEquals(bigInteger.longValue(), unsigned);
60
61         unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
62         assertEquals(0, unsigned);
63
64         unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
65         assertEquals(bigIntFF.longValue(), unsigned);
66     }
67
68     @Test
69     public void testBytesToUnsignedShort() {
70
71         byte[] twoBytes = {100, 101};
72         int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
73         assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
74
75         twoBytes = new byte[]{0, 0};
76         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
77         assertEquals(int00, unsigned);
78
79         twoBytes = new byte[]{(byte) 255, (byte) 255};
80         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
81         assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
82     }
83
84     @Test(expected = IllegalArgumentException.class)
85     public void exceptionTestBytesToUnsignedShort() {
86         ByteUtil.bytesToUnsignedShort(testBytes);
87     }
88
89     @Test(expected = IllegalArgumentException.class)
90     public void exceptionTestBytesToUnsignedInt() {
91         byte[] fiveBytes = {0, 0, 0, 0, 0};
92         ByteUtil.bytesToUnsignedInt(fiveBytes);
93     }
94
95     @Test
96     public void testUnsignedIntToBytes() {
97         long intValue = 255;
98         byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
99
100         assertTrue(bytes.length == intByteLength);
101
102         intValue += 256;
103         bytes = ByteUtil.unsignedIntToBytes(intValue);
104         assertTrue(bytes.length == intByteLength);
105
106         intValue += 256;
107         bytes = ByteUtil.unsignedIntToBytes(intValue);
108         assertTrue(bytes.length == intByteLength);
109     }
110
111     @Test
112     public void testUnsignedShortToBytes() {
113         int intValue = 255;
114         byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
115
116         assertTrue(bytes.length == shortByteLength);
117
118         intValue += 256;
119         bytes = ByteUtil.unsignedShortToBytes(intValue);
120         assertTrue(bytes.length == shortByteLength);
121
122         intValue += 256;
123         bytes = ByteUtil.unsignedShortToBytes(intValue);
124         assertTrue(bytes.length == shortByteLength);
125     }
126 }