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