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 * test of {@link ByteUtil#bytesToHexstring(byte[], String)}
48 public void testBytesToHexstring() {
49 assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
50 assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
51 assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
55 public void testConvertBigIntegerToNBytes() {
56 byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
57 assertEquals(4, bigIntAsBytes.length);
59 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
60 assertEquals(6, bigIntAsBytes.length);
62 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
63 assertEquals(8, bigIntAsBytes.length);
67 public void testBytesToUnsignedInt() {
68 long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
69 assertEquals(bigInteger.longValue(), unsigned);
71 unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
72 assertEquals(0, unsigned);
74 unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
75 assertEquals(bigIntFF.longValue(), unsigned);
79 public void testBytesToUnsignedShort() {
81 byte[] twoBytes = {100, 101};
82 int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
83 assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
85 twoBytes = new byte[]{0, 0};
86 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
87 assertEquals(int00, unsigned);
89 twoBytes = new byte[]{(byte) 255, (byte) 255};
90 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
91 assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
95 public void testBytesToUnsignedMedium() {
96 long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
97 assertEquals(mediumInteger.longValue(), unsigned);
99 unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
100 assertEquals(0, unsigned);
102 unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
103 assertEquals(mediumIntegerFF.longValue(), unsigned);
106 @Test(expected = IllegalArgumentException.class)
107 public void exceptionTestBytesToUnsignedShort() {
108 ByteUtil.bytesToUnsignedShort(testBytes);
111 @Test(expected = IllegalArgumentException.class)
112 public void exceptionTestBytesToUnsignedInt() {
113 byte[] fiveBytes = {0, 0, 0, 0, 0};
114 ByteUtil.bytesToUnsignedInt(fiveBytes);
118 public void testUnsignedIntToBytes() {
120 byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
122 assertTrue(bytes.length == intByteLength);
125 bytes = ByteUtil.unsignedIntToBytes(intValue);
126 assertTrue(bytes.length == intByteLength);
129 bytes = ByteUtil.unsignedIntToBytes(intValue);
130 assertTrue(bytes.length == intByteLength);
134 public void testUnsignedShortToBytes() {
136 byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
138 assertTrue(bytes.length == shortByteLength);
141 bytes = ByteUtil.unsignedShortToBytes(intValue);
142 assertTrue(bytes.length == shortByteLength);
145 bytes = ByteUtil.unsignedShortToBytes(intValue);
146 assertTrue(bytes.length == shortByteLength);
150 public void testUnsignedMediumToBytes() {
152 byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
154 assertTrue(bytes.length == mediumByteLength);
157 bytes = ByteUtil.unsignedMediumToBytes(intValue);
158 assertTrue(bytes.length == mediumByteLength);
161 bytes = ByteUtil.unsignedMediumToBytes(intValue);
162 assertTrue(bytes.length == mediumByteLength);