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.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
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;
21 * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
23 public class ByteUtilTest {
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";
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};
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};
37 private static final BigInteger BIG_INTEGER = new BigInteger("1684367103");
38 private static final BigInteger BIG_INTFF = new BigInteger("4294967295");
40 private static final long MEDIUM_INTEGER = 6579558;
41 private static final long MEDIUM_INTEGERFF = 16777215;
42 private static final int INT00 = 0;
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;
49 * test of {@link ByteUtil#bytesToHexstring(byte[], String)}.
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, ","));
59 public void testConvertBigIntegerToNBytes() {
60 byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 4);
61 assertEquals(4, bigIntAsBytes.length);
63 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 6);
64 assertEquals(6, bigIntAsBytes.length);
66 bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(BIG_INTEGER, 8);
67 assertEquals(8, bigIntAsBytes.length);
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));
77 public void testBytesToUnsignedInt() {
78 Uint32 unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES);
79 assertEquals(BIG_INTEGER.longValue(), unsigned.toJava());
81 unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES00);
82 assertEquals(Uint32.ZERO, unsigned);
84 unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTESFF);
85 assertEquals(BIG_INTFF.longValue(), unsigned.toJava());
89 public void testBytesToUnsignedShort() {
91 byte[] twoBytes = {100, 101};
92 int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
93 assertEquals(BIG_INTEGER.shiftRight(16).shortValue(), unsigned);
95 twoBytes = new byte[]{0, 0};
96 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
97 assertEquals(INT00, unsigned);
99 twoBytes = new byte[]{(byte) 255, (byte) 255};
100 unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
101 assertEquals(BIG_INTFF.shiftRight(16).intValue(), unsigned);
105 public void testBytesToUnsignedMedium() {
106 long unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTES);
107 assertEquals(MEDIUM_INTEGER, unsigned);
109 unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTES00);
110 assertEquals(0, unsigned);
112 unsigned = ByteUtil.bytesToUnsignedMedium(TEST3_BYTESFF);
113 assertEquals(MEDIUM_INTEGERFF, unsigned);
116 @Test(expected = IllegalArgumentException.class)
117 public void exceptionTestBytesToUnsignedShort() {
118 ByteUtil.bytesToUnsignedShort(TEST_BYTES);
121 @Test(expected = IllegalArgumentException.class)
122 public void exceptionTestBytesToUnsignedInt() {
123 byte[] fiveBytes = {0, 0, 0, 0, 0};
124 ByteUtil.bytesToUnsignedInt(fiveBytes);
128 public void testUnsignedIntToBytes() {
130 byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
132 assertTrue(bytes.length == INT_BYTE_LENGTH);
135 bytes = ByteUtil.unsignedIntToBytes(intValue);
136 assertTrue(bytes.length == INT_BYTE_LENGTH);
139 bytes = ByteUtil.unsignedIntToBytes(intValue);
140 assertTrue(bytes.length == INT_BYTE_LENGTH);
144 public void testUnsignedShortToBytes() {
146 byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
148 assertTrue(bytes.length == SHORT_BYTE_LENGTH);
151 bytes = ByteUtil.unsignedShortToBytes(intValue);
152 assertTrue(bytes.length == SHORT_BYTE_LENGTH);
155 bytes = ByteUtil.unsignedShortToBytes(intValue);
156 assertTrue(bytes.length == SHORT_BYTE_LENGTH);
160 public void testUnsignedMediumToBytes() {
162 byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
164 assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);
167 bytes = ByteUtil.unsignedMediumToBytes(intValue);
168 assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);
171 bytes = ByteUtil.unsignedMediumToBytes(intValue);
172 assertTrue(bytes.length == MEDIUM_BYTE_LENGTH);