2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.util;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import java.math.BigInteger;
15 import org.junit.Test;
18 * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
20 public class ByteUtilTest {
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";
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};
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};
34 private static final BigInteger bigInteger = new BigInteger("1684367103");
35 private static final BigInteger bigIntFF = new BigInteger("4294967295");
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;
41 private static final int shortByteLength = 2;
42 private static final int mediumByteLength = 3;
43 private static final int intByteLength = 4;
46 * test of {@link ByteUtil#bytesToHexstring(byte[], String)}
49 public void testBytesToHexstring() {
50 assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
51 assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
52 assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
56 public void testConvertBigIntegerToNBytes() {
57 byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
58 assertEquals(4, bigIntAsBytes.length);
60 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
61 assertEquals(6, bigIntAsBytes.length);
63 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
64 assertEquals(8, bigIntAsBytes.length);
68 public void testBytesToUnsignedInt() {
69 long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
70 assertEquals(bigInteger.longValue(), unsigned);
72 unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
73 assertEquals(0, unsigned);
75 unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
76 assertEquals(bigIntFF.longValue(), unsigned);
80 public void testBytesToUnsignedShort() {
82 byte[] twoBytes = {100, 101};
83 int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
84 assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
86 twoBytes = new byte[]{0, 0};
87 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
88 assertEquals(int00, unsigned);
90 twoBytes = new byte[]{(byte) 255, (byte) 255};
91 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
92 assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
96 public void testBytesToUnsignedMedium() {
97 long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
98 assertEquals(mediumInteger.longValue(), unsigned);
100 unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
101 assertEquals(0, unsigned);
103 unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
104 assertEquals(mediumIntegerFF.longValue(), unsigned);
107 @Test(expected = IllegalArgumentException.class)
108 public void exceptionTestBytesToUnsignedShort() {
109 ByteUtil.bytesToUnsignedShort(testBytes);
112 @Test(expected = IllegalArgumentException.class)
113 public void exceptionTestBytesToUnsignedInt() {
114 byte[] fiveBytes = {0, 0, 0, 0, 0};
115 ByteUtil.bytesToUnsignedInt(fiveBytes);
119 public void testUnsignedIntToBytes() {
121 byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
123 assertTrue(bytes.length == intByteLength);
126 bytes = ByteUtil.unsignedIntToBytes(intValue);
127 assertTrue(bytes.length == intByteLength);
130 bytes = ByteUtil.unsignedIntToBytes(intValue);
131 assertTrue(bytes.length == intByteLength);
135 public void testUnsignedShortToBytes() {
137 byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
139 assertTrue(bytes.length == shortByteLength);
142 bytes = ByteUtil.unsignedShortToBytes(intValue);
143 assertTrue(bytes.length == shortByteLength);
146 bytes = ByteUtil.unsignedShortToBytes(intValue);
147 assertTrue(bytes.length == shortByteLength);
151 public void testUnsignedMediumToBytes() {
153 byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
155 assertTrue(bytes.length == mediumByteLength);
158 bytes = ByteUtil.unsignedMediumToBytes(intValue);
159 assertTrue(bytes.length == mediumByteLength);
162 bytes = ByteUtil.unsignedMediumToBytes(intValue);
163 assertTrue(bytes.length == mediumByteLength);