Migrate most of openflowplugin to use Uint types
[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.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
15 import java.math.BigInteger;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.Uint32;
18 import org.opendaylight.yangtools.yang.common.Uint64;
19
20 /**
21  * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
22  */
23 public class ByteUtilTest {
24
25     private static final String HEX_STRING = "64,65,66,ff";
26     private static final String HEX_STRING00 = "00,00,00,00";
27     private static final String HEX_STRINGFF = "ff,ff,ff,ff";
28
29     private static final byte[] TEST_BYTES = {100, 101, 102, (byte) 255};
30     private static final byte[] TEST_BYTES00 = {0, 0, 0, 0};
31     private static final byte[] TEST_BYTESFF = {(byte) 255, (byte) 255, (byte) 255, (byte) 255};
32
33     private static final byte[] TEST3_BYTES = {100, 101, 102};
34     private static final byte[] TEST3_BYTES00 = {0, 0, 0};
35     private static final byte[] TEST3_BYTESFF = {(byte) 255, (byte) 255, (byte) 255};
36
37     private static final BigInteger BIG_INTEGER = new BigInteger("1684367103");
38     private static final BigInteger BIG_INTFF = new BigInteger("4294967295");
39
40     private static final long MEDIUM_INTEGER = 6579558;
41     private static final long MEDIUM_INTEGERFF = 16777215;
42     private static final int INT00 = 0;
43
44     private static final int SHORT_BYTE_LENGTH = 2;
45     private static final int MEDIUM_BYTE_LENGTH = 3;
46     private static final int INT_BYTE_LENGTH = 4;
47
48     /**
49      * test of {@link ByteUtil#bytesToHexstring(byte[], String)}.
50      */
51     @Test
52     public void testBytesToHexstring() {
53         assertEquals(HEX_STRING, ByteUtil.bytesToHexstring(TEST_BYTES, ","));
54         assertEquals(HEX_STRING00, ByteUtil.bytesToHexstring(TEST_BYTES00, ","));
55         assertEquals(HEX_STRINGFF, ByteUtil.bytesToHexstring(TEST_BYTESFF, ","));
56     }
57
58     @Test
59     public void testConvertBigIntegerToNBytes() {
60         byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 4);
61         assertEquals(4, bigIntAsBytes.length);
62
63         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 6);
64         assertEquals(6, bigIntAsBytes.length);
65
66         bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 8);
67         assertEquals(8, bigIntAsBytes.length);
68     }
69
70     @Test
71     public void testUint64toBytes() {
72         final Uint64 value = Uint64.valueOf("0102030405060708", 16);
73         assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8}, ByteUtil.uint64toBytes(value));
74     }
75
76     @Test
77     public void testBytesToUnsignedInt() {
78         Uint32 unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES);
79         assertEquals(BIG_INTEGER.longValue(), unsigned.toJava());
80
81         unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES00);
82         assertEquals(Uint32.ZERO, unsigned);
83
84         unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTESFF);
85         assertEquals(BIG_INTFF.longValue(), unsigned.toJava());
86     }
87
88     @Test
89     public void testBytesToUnsignedShort() {
90
91         byte[] twoBytes = {100, 101};
92         int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
93         assertEquals(BIG_INTEGER.shiftRight(16).shortValue(), unsigned);
94
95         twoBytes = new byte[]{0, 0};
96         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
97         assertEquals(INT00, unsigned);
98
99         twoBytes = new byte[]{(byte) 255, (byte) 255};
100         unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
101         assertEquals(BIG_INTFF.shiftRight(16).intValue(), unsigned);
102     }
103
104     @Test
105     public void testBytesToUnsignedMedium() {
106         long unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTES);
107         assertEquals(MEDIUM_INTEGER, unsigned);
108
109         unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTES00);
110         assertEquals(0, unsigned);
111
112         unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTESFF);
113         assertEquals(MEDIUM_INTEGERFF, unsigned);
114     }
115
116     @Test(expected = IllegalArgumentException.class)
117     public void exceptionTestBytesToUnsignedShort() {
118         ByteUtil.bytesToUnsignedShort(TEST_BYTES);
119     }
120
121     @Test(expected = IllegalArgumentException.class)
122     public void exceptionTestBytesToUnsignedInt() {
123         byte[] fiveBytes = {0, 0, 0, 0, 0};
124         ByteUtil.bytesToUnsignedInt(fiveBytes);
125     }
126
127     @Test
128     public void testUnsignedIntToBytes() {
129         long intValue = 255;
130         byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
131
132         assertTrue(bytes.length == INT_BYTE_LENGTH);
133
134         intValue += 256;
135         bytes = ByteUtil.unsignedIntToBytes(intValue);
136         assertTrue(bytes.length == INT_BYTE_LENGTH);
137
138         intValue += 256;
139         bytes = ByteUtil.unsignedIntToBytes(intValue);
140         assertTrue(bytes.length == INT_BYTE_LENGTH);
141     }
142
143     @Test
144     public void testUnsignedShortToBytes() {
145         int intValue = 255;
146         byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
147
148         assertTrue(bytes.length == SHORT_BYTE_LENGTH);
149
150         intValue += 256;
151         bytes = ByteUtil.unsignedShortToBytes(intValue);
152         assertTrue(bytes.length == SHORT_BYTE_LENGTH);
153
154         intValue += 256;
155         bytes = ByteUtil.unsignedShortToBytes(intValue);
156         assertTrue(bytes.length == SHORT_BYTE_LENGTH);
157     }
158
159     @Test
160     public void testUnsignedMediumToBytes() {
161         long intValue = 255;
162         byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
163
164         assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);
165
166         intValue += 256;
167         bytes = ByteUtil.unsignedMediumToBytes(intValue);
168         assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);
169
170         intValue += 256;
171         bytes = ByteUtil.unsignedMediumToBytes(intValue);
172         assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);
173     }
174
175 }