2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.util;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.assertTrue;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.Arrays;
25 import java.util.BitSet;
26 import org.junit.Before;
27 import org.junit.Test;
29 public class ByteArrayTest {
31 final byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
34 public void testReadBytes() {
35 final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
36 buffer.readerIndex(1);
37 assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.readBytes(buffer, 3));
38 assertEquals(4, buffer.readerIndex());
40 assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
41 assertEquals(buffer.readerIndex(), buffer.writerIndex());
45 public void testGetBytes() {
46 final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
47 buffer.readerIndex(1);
48 assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.getBytes(buffer, 3));
49 assertEquals(1, buffer.readerIndex());
51 assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
52 assertNotSame(buffer.readerIndex(), buffer.writerIndex());
56 public void testSubByte() {
57 byte[] after = ByteArray.subByte(this.before, 0, 3);
58 byte[] expected = new byte[] { 15, 28, 4 };
59 assertArrayEquals(expected, after);
60 after = ByteArray.subByte(this.before, 5, 1);
61 expected = new byte[] { 10 };
62 assertArrayEquals(expected, after);
65 @Test(expected = IllegalArgumentException.class)
66 public void testSubByte2() {
67 ByteArray.subByte(new byte[0], 2, 2);
70 @Test(expected = IllegalArgumentException.class)
71 public void testSubByte3() {
72 ByteArray.subByte(this.before, 2, -1);
75 @Test(expected = IllegalArgumentException.class)
76 public void testSubByte4() {
77 ByteArray.subByte(this.before, -1, 2);
80 @Test(expected = IllegalArgumentException.class)
81 public void testSubByte5() {
82 ByteArray.subByte(this.before, 9, 2);
85 @Test(expected = IllegalArgumentException.class)
86 public void testSubByte6() {
87 ByteArray.subByte(this.before, 2, 19);
90 @Test(expected = IllegalArgumentException.class)
91 public void testSubByte7() {
92 ByteArray.subByte(this.before, 2, 7);
96 public void testCutBytes() {
97 byte[] after = ByteArray.cutBytes(this.before, 2);
98 byte[] expected = new byte[] { 4, 6, 9, 10 };
99 assertArrayEquals(expected, after);
100 after = ByteArray.cutBytes(this.before, 6);
101 expected = new byte[] {};
102 assertArrayEquals(expected, after);
105 @Test(expected = IllegalArgumentException.class)
106 public void testCutBytes2() {
107 ByteArray.cutBytes(new byte[0], 5);
110 @Test(expected = IllegalArgumentException.class)
111 public void testCutBytes3() {
112 ByteArray.cutBytes(this.before, 9);
115 @Test(expected = IllegalArgumentException.class)
116 public void testCutBytes4() {
117 ByteArray.cutBytes(this.before, 0);
121 public void testParseBits() {
122 final byte b = (byte) -76; // 1011 0100
123 final boolean[] after = ByteArray.parseBits(b);
124 assertTrue(after[0]);
125 assertFalse(after[1]);
126 assertTrue(after[2]);
127 assertTrue(after[3]);
128 assertFalse(after[4]);
129 assertTrue(after[5]);
130 assertFalse(after[6]);
131 assertFalse(after[7]);
134 private final byte[] inBytes = { (byte) 0x03, (byte) 0xFF, (byte) 0x01, (byte) 0x80 };
135 final BitSet inBitSet = new BitSet();
138 public void generateBitSet() {
140 this.inBitSet.set(6, 8);
143 this.inBitSet.set(8, 16);
146 this.inBitSet.set(23);
149 this.inBitSet.set(24);
153 public void testBytesToBitSetFunction() {
154 final BitSet iputBitSet = ByteArray.bytesToBitSet(this.inBytes);
156 assertEquals(this.inBitSet, iputBitSet);
160 public void testBitSetToBytesFunction() {
161 byte[] resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length);
162 assertArrayEquals(this.inBytes, resultBytes);
164 resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length - 1);
165 assertArrayEquals(Arrays.copyOf(this.inBytes, this.inBytes.length - 1), resultBytes);
167 resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length + 1);
168 assertArrayEquals(Arrays.copyOf(this.inBytes, this.inBytes.length + 1), resultBytes);
172 public void testFileToBytes() throws IOException {
173 final String FILE_TO_TEST = "src/test/resources/PCEStatefulCapabilityTlv1.bin";
175 final File fileToCompareWith = new File(FILE_TO_TEST);
176 final InputStream bytesIStream = new FileInputStream(fileToCompareWith);
179 final byte[] actualBytes = ByteArray.fileToBytes(FILE_TO_TEST);
181 if (fileToCompareWith.length() > Integer.MAX_VALUE) {
182 throw new IOException("Too large file to load in byte array.");
185 final byte[] expectedBytes = new byte[(int) fileToCompareWith.length()];
189 while (offset < expectedBytes.length && (numRead = bytesIStream.read(expectedBytes, offset, actualBytes.length - offset)) >= 0) {
193 assertArrayEquals(expectedBytes, actualBytes);
195 bytesIStream.close();
200 * if less than 4 bytes are converted, zero bytes should be appended at the buffer's start
203 public void testBytesToLong_prependingZeros() {
204 assertEquals(1, ByteArray.bytesToLong(new byte[] { 0, 0, 1 }));
207 @Test(expected = IllegalArgumentException.class)
208 public void testBytesToInt() {
209 final byte[] b = new byte[Integer.SIZE + 1];
210 ByteArray.bytesToInt(b);
213 @Test(expected = IllegalArgumentException.class)
214 public void testBytesToShort2() {
215 final byte[] b = new byte[Short.SIZE + 1];
216 ByteArray.bytesToInt(b);
220 public void testCopyBitRange() {
221 assertEquals((byte) 10, ByteArray.copyBitsRange((byte) 0x28, 2, 4));
222 assertEquals((byte) 3, ByteArray.copyBitsRange((byte) 0xFF, 2, 2));
223 assertEquals((byte) 7, ByteArray.copyBitsRange((byte) 0xFF, 5, 3));
224 assertEquals((byte) 15, ByteArray.copyBitsRange((byte) 0xFF, 0, 4));
225 assertEquals((byte) 31, ByteArray.copyBitsRange((byte) 0xF9, 0, 5));
226 assertEquals((byte) 0xA2, ByteArray.copyBitsRange((byte) 0xA2, 0, 8));
227 assertEquals((byte) 1, ByteArray.copyBitsRange((byte) 0xFF, 5, 1));
230 @Test(expected = IllegalArgumentException.class)
231 public void testCopyBitsRange2() {
232 ByteArray.copyBitsRange((byte) 0x28, -1, 4);
235 @Test(expected = IllegalArgumentException.class)
236 public void testCopyBitsRange3() {
237 ByteArray.copyBitsRange((byte) 0x28, 1, 187);
240 @Test(expected = IllegalArgumentException.class)
241 public void testCopyBitsRange4() {
242 ByteArray.copyBitsRange((byte) 0x28, 1, 40);
245 @Test(expected = IllegalArgumentException.class)
246 public void testCopyBitsRange5() {
247 ByteArray.copyBitsRange((byte) 0x28, 28, 2);
250 @Test(expected = IllegalArgumentException.class)
251 public void testCopyBitsRange6() {
252 ByteArray.copyBitsRange((byte) 0x28, 2, -2);
256 public void testBytesToHRString() {
259 // test valid US-ASCII string
260 b = new byte[] { (byte) 79, (byte) 102, (byte) 45, (byte) 57, (byte) 107, (byte) 45, (byte) 48, (byte) 50 };
261 final String expected = "Of-9k-02";
262 assertEquals(expected, ByteArray.bytesToHRString(b));
264 // test Utf-8 restricted bytes
265 b = new byte[] { (byte) 246, (byte) 248, (byte) 254 };
266 assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
268 // test unexpected continuation bytes
269 b = new byte[] { (byte) 128, (byte) 113, (byte) 98 };
270 assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
273 @Test(expected=UnsupportedOperationException.class)
274 public void testPrivateConstructor() throws Throwable {
275 final Constructor<ByteArray> c = ByteArray.class.getDeclaredConstructor();
276 c.setAccessible(true);
279 } catch (final InvocationTargetException e) {