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
8 package org.opendaylight.openflowplugin.openflow.md.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.math.BigInteger;
14 import org.junit.Test;
17 * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
19 public class ByteUtilTest {
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,";
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};
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};
33 private static final BigInteger bigInteger = new BigInteger("1684367103");
34 private static final BigInteger bigIntFF = new BigInteger("4294967295");
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;
40 private static final int shortByteLength = 2;
41 private static final int mediumByteLength = 3;
42 private static final int intByteLength = 4;
45 public void testBytesToHexstring() {
46 assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
47 assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
48 assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
52 public void testConvertBigIntegerToNBytes() {
53 byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
54 assertEquals(4, bigIntAsBytes.length);
56 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
57 assertEquals(6, bigIntAsBytes.length);
59 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
60 assertEquals(8, bigIntAsBytes.length);
64 public void testBytesToUnsignedInt() {
65 long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
66 assertEquals(bigInteger.longValue(), unsigned);
68 unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
69 assertEquals(0, unsigned);
71 unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
72 assertEquals(bigIntFF.longValue(), unsigned);
76 public void testBytesToUnsignedShort() {
78 byte[] twoBytes = {100, 101};
79 int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
80 assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
82 twoBytes = new byte[]{0, 0};
83 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
84 assertEquals(int00, unsigned);
86 twoBytes = new byte[]{(byte) 255, (byte) 255};
87 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
88 assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
92 public void testBytesToUnsignedMedium() {
93 long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
94 assertEquals(mediumInteger.longValue(), unsigned);
96 unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
97 assertEquals(0, unsigned);
99 unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
100 assertEquals(mediumIntegerFF.longValue(), unsigned);
103 @Test(expected = IllegalArgumentException.class)
104 public void exceptionTestBytesToUnsignedShort() {
105 ByteUtil.bytesToUnsignedShort(testBytes);
108 @Test(expected = IllegalArgumentException.class)
109 public void exceptionTestBytesToUnsignedInt() {
110 byte[] fiveBytes = {0, 0, 0, 0, 0};
111 ByteUtil.bytesToUnsignedInt(fiveBytes);
115 public void testUnsignedIntToBytes() {
117 byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
119 assertTrue(bytes.length == intByteLength);
122 bytes = ByteUtil.unsignedIntToBytes(intValue);
123 assertTrue(bytes.length == intByteLength);
126 bytes = ByteUtil.unsignedIntToBytes(intValue);
127 assertTrue(bytes.length == intByteLength);
131 public void testUnsignedShortToBytes() {
133 byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
135 assertTrue(bytes.length == shortByteLength);
138 bytes = ByteUtil.unsignedShortToBytes(intValue);
139 assertTrue(bytes.length == shortByteLength);
142 bytes = ByteUtil.unsignedShortToBytes(intValue);
143 assertTrue(bytes.length == shortByteLength);
147 public void testUnsignedMediumToBytes() {
149 byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
151 assertTrue(bytes.length == mediumByteLength);
154 bytes = ByteUtil.unsignedMediumToBytes(intValue);
155 assertTrue(bytes.length == mediumByteLength);
158 bytes = ByteUtil.unsignedMediumToBytes(intValue);
159 assertTrue(bytes.length == mediumByteLength);