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 org.junit.Test;
12 import java.math.BigInteger;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertTrue;
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 BigInteger bigInteger = new BigInteger("1684367103");
31 private static final BigInteger bigIntFF = new BigInteger("4294967295");
32 private static final int int00 = 0;
34 private static final int shortByteLength = 2;
35 private static final int intByteLength = 4;
38 public void testBytesToHexstring() {
39 assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
40 assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
41 assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
45 public void testConvertBigIntegerToNBytes() {
46 byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
47 assertEquals(4, bigIntAsBytes.length);
49 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
50 assertEquals(6, bigIntAsBytes.length);
52 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
53 assertEquals(8, bigIntAsBytes.length);
57 public void testBytesToUnsignedInt() {
58 long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
59 assertEquals(bigInteger.longValue(), unsigned);
61 unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
62 assertEquals(0, unsigned);
64 unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
65 assertEquals(bigIntFF.longValue(), unsigned);
69 public void testBytesToUnsignedShort() {
71 byte[] twoBytes = {100, 101};
72 int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
73 assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
75 twoBytes = new byte[]{0, 0};
76 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
77 assertEquals(int00, unsigned);
79 twoBytes = new byte[]{(byte) 255, (byte) 255};
80 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
81 assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
84 @Test(expected = IllegalArgumentException.class)
85 public void exceptionTestBytesToUnsignedShort() {
86 ByteUtil.bytesToUnsignedShort(testBytes);
89 @Test(expected = IllegalArgumentException.class)
90 public void exceptionTestBytesToUnsignedInt() {
91 byte[] fiveBytes = {0, 0, 0, 0, 0};
92 ByteUtil.bytesToUnsignedInt(fiveBytes);
96 public void testUnsignedIntToBytes() {
98 byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
100 assertTrue(bytes.length == intByteLength);
103 bytes = ByteUtil.unsignedIntToBytes(intValue);
104 assertTrue(bytes.length == intByteLength);
107 bytes = ByteUtil.unsignedIntToBytes(intValue);
108 assertTrue(bytes.length == intByteLength);
112 public void testUnsignedShortToBytes() {
114 byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
116 assertTrue(bytes.length == shortByteLength);
119 bytes = ByteUtil.unsignedShortToBytes(intValue);
120 assertTrue(bytes.length == shortByteLength);
123 bytes = ByteUtil.unsignedShortToBytes(intValue);
124 assertTrue(bytes.length == shortByteLength);