Add missing license headers
[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  *
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.openflowplugin.openflow.md.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import java.math.BigInteger;
15 import org.junit.Test;
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 byte[] test3Bytes = {100, 101, 102};
31     private static final byte[] test3Bytes00 = {0, 0, 0};
32     private static final byte[] test3BytesFF = {(byte) 255, (byte) 255, (byte) 255};
33
34     private static final BigInteger bigInteger = new BigInteger("1684367103");
35     private static final BigInteger bigIntFF = new BigInteger("4294967295");
36
37     private static final Integer mediumInteger = new Integer("6579558");
38     private static final Integer mediumIntegerFF = new Integer("16777215");
39     private static final int int00 = 0;
40
41     private static final int shortByteLength = 2;
42     private static final int mediumByteLength = 3;
43     private static final int intByteLength = 4;
44
45     /**
46      * test of {@link ByteUtil#bytesToHexstring(byte[], String)}
47      */
48     @Test
49     public void testBytesToHexstring() {
50         assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
51         assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
52         assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
53     }
54
55     @Test
56     public void testConvertBigIntegerToNBytes() {
57         byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
58         assertEquals(4, bigIntAsBytes.length);
59
60         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
61         assertEquals(6, bigIntAsBytes.length);
62
63         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
64         assertEquals(8, bigIntAsBytes.length);
65     }
66
67     @Test
68     public void testBytesToUnsignedInt() {
69         long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
70         assertEquals(bigInteger.longValue(), unsigned);
71
72         unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
73         assertEquals(0, unsigned);
74
75         unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
76         assertEquals(bigIntFF.longValue(), unsigned);
77     }
78
79     @Test
80     public void testBytesToUnsignedShort() {
81
82         byte[] twoBytes = {100, 101};
83         int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
84         assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
85
86         twoBytes = new byte[]{0, 0};
87         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
88         assertEquals(int00, unsigned);
89
90         twoBytes = new byte[]{(byte) 255, (byte) 255};
91         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
92         assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
93     }
94
95     @Test
96     public void testBytesToUnsignedMedium() {
97         long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
98         assertEquals(mediumInteger.longValue(), unsigned);
99
100         unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
101         assertEquals(0, unsigned);
102
103         unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
104         assertEquals(mediumIntegerFF.longValue(), unsigned);
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void exceptionTestBytesToUnsignedShort() {
109         ByteUtil.bytesToUnsignedShort(testBytes);
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void exceptionTestBytesToUnsignedInt() {
114         byte[] fiveBytes = {0, 0, 0, 0, 0};
115         ByteUtil.bytesToUnsignedInt(fiveBytes);
116     }
117
118     @Test
119     public void testUnsignedIntToBytes() {
120         long intValue = 255;
121         byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
122
123         assertTrue(bytes.length == intByteLength);
124
125         intValue += 256;
126         bytes = ByteUtil.unsignedIntToBytes(intValue);
127         assertTrue(bytes.length == intByteLength);
128
129         intValue += 256;
130         bytes = ByteUtil.unsignedIntToBytes(intValue);
131         assertTrue(bytes.length == intByteLength);
132     }
133
134     @Test
135     public void testUnsignedShortToBytes() {
136         int intValue = 255;
137         byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
138
139         assertTrue(bytes.length == shortByteLength);
140
141         intValue += 256;
142         bytes = ByteUtil.unsignedShortToBytes(intValue);
143         assertTrue(bytes.length == shortByteLength);
144
145         intValue += 256;
146         bytes = ByteUtil.unsignedShortToBytes(intValue);
147         assertTrue(bytes.length == shortByteLength);
148     }
149
150     @Test
151     public void testUnsignedMediumToBytes() {
152         long intValue = 255;
153         byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
154
155         assertTrue(bytes.length == mediumByteLength);
156
157         intValue += 256;
158         bytes = ByteUtil.unsignedMediumToBytes(intValue);
159         assertTrue(bytes.length == mediumByteLength);
160
161         intValue += 256;
162         bytes = ByteUtil.unsignedMediumToBytes(intValue);
163         assertTrue(bytes.length == mediumByteLength);
164     }
165
166 }