Bug 2280 - MatchConvertorImpl bugs found during test creation
[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     @Test
45     public void testBytesToHexstring() {
46         assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
47         assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
48         assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
49     }
50
51     @Test
52     public void testConvertBigIntegerToNBytes() {
53         byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
54         assertEquals(4, bigIntAsBytes.length);
55
56         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
57         assertEquals(6, bigIntAsBytes.length);
58
59         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
60         assertEquals(8, bigIntAsBytes.length);
61     }
62
63     @Test
64     public void testBytesToUnsignedInt() {
65         long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
66         assertEquals(bigInteger.longValue(), unsigned);
67
68         unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
69         assertEquals(0, unsigned);
70
71         unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
72         assertEquals(bigIntFF.longValue(), unsigned);
73     }
74
75     @Test
76     public void testBytesToUnsignedShort() {
77
78         byte[] twoBytes = {100, 101};
79         int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
80         assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
81
82         twoBytes = new byte[]{0, 0};
83         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
84         assertEquals(int00, unsigned);
85
86         twoBytes = new byte[]{(byte) 255, (byte) 255};
87         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
88         assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
89     }
90
91     @Test
92     public void testBytesToUnsignedMedium() {
93         long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
94         assertEquals(mediumInteger.longValue(), unsigned);
95
96         unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
97         assertEquals(0, unsigned);
98
99         unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
100         assertEquals(mediumIntegerFF.longValue(), unsigned);
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void exceptionTestBytesToUnsignedShort() {
105         ByteUtil.bytesToUnsignedShort(testBytes);
106     }
107
108     @Test(expected = IllegalArgumentException.class)
109     public void exceptionTestBytesToUnsignedInt() {
110         byte[] fiveBytes = {0, 0, 0, 0, 0};
111         ByteUtil.bytesToUnsignedInt(fiveBytes);
112     }
113
114     @Test
115     public void testUnsignedIntToBytes() {
116         long intValue = 255;
117         byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
118
119         assertTrue(bytes.length == intByteLength);
120
121         intValue += 256;
122         bytes = ByteUtil.unsignedIntToBytes(intValue);
123         assertTrue(bytes.length == intByteLength);
124
125         intValue += 256;
126         bytes = ByteUtil.unsignedIntToBytes(intValue);
127         assertTrue(bytes.length == intByteLength);
128     }
129
130     @Test
131     public void testUnsignedShortToBytes() {
132         int intValue = 255;
133         byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
134
135         assertTrue(bytes.length == shortByteLength);
136
137         intValue += 256;
138         bytes = ByteUtil.unsignedShortToBytes(intValue);
139         assertTrue(bytes.length == shortByteLength);
140
141         intValue += 256;
142         bytes = ByteUtil.unsignedShortToBytes(intValue);
143         assertTrue(bytes.length == shortByteLength);
144     }
145
146     @Test
147     public void testUnsignedMediumToBytes() {
148         long intValue = 255;
149         byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
150
151         assertTrue(bytes.length == mediumByteLength);
152
153         intValue += 256;
154         bytes = ByteUtil.unsignedMediumToBytes(intValue);
155         assertTrue(bytes.length == mediumByteLength);
156
157         intValue += 256;
158         bytes = ByteUtil.unsignedMediumToBytes(intValue);
159         assertTrue(bytes.length == mediumByteLength);
160     }
161
162 }