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.assertNotSame;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.Arrays;
23 import java.util.BitSet;
24 import org.junit.Before;
25 import org.junit.Test;
27 public class ByteArrayTest {
29 final byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
32 public void testReadBytes() {
33 final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
34 buffer.readerIndex(1);
35 assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.readBytes(buffer, 3));
36 assertEquals(4, buffer.readerIndex());
38 assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
39 assertEquals(buffer.readerIndex(), buffer.writerIndex());
43 public void testGetBytes() {
44 final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
45 buffer.readerIndex(1);
46 assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.getBytes(buffer, 3));
47 assertEquals(1, buffer.readerIndex());
49 assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
50 assertNotSame(buffer.readerIndex(), buffer.writerIndex());
54 public void testSubByte() {
55 byte[] after = ByteArray.subByte(this.before, 0, 3);
56 byte[] expected = new byte[] { 15, 28, 4 };
57 assertArrayEquals(expected, after);
58 after = ByteArray.subByte(this.before, 5, 1);
59 expected = new byte[] { 10 };
60 assertArrayEquals(expected, after);
63 @Test(expected = IllegalArgumentException.class)
64 public void testSubByte2() {
65 ByteArray.subByte(new byte[0], 2, 2);
68 @Test(expected = IllegalArgumentException.class)
69 public void testSubByte3() {
70 ByteArray.subByte(this.before, 2, -1);
73 @Test(expected = IllegalArgumentException.class)
74 public void testSubByte4() {
75 ByteArray.subByte(this.before, -1, 2);
78 @Test(expected = IllegalArgumentException.class)
79 public void testSubByte5() {
80 ByteArray.subByte(this.before, 9, 2);
83 @Test(expected = IllegalArgumentException.class)
84 public void testSubByte6() {
85 ByteArray.subByte(this.before, 2, 19);
88 @Test(expected = IllegalArgumentException.class)
89 public void testSubByte7() {
90 ByteArray.subByte(this.before, 2, 7);
94 public void testCutBytes() {
95 byte[] after = ByteArray.cutBytes(this.before, 2);
96 byte[] expected = new byte[] { 4, 6, 9, 10 };
97 assertArrayEquals(expected, after);
98 after = ByteArray.cutBytes(this.before, 6);
99 expected = new byte[] {};
100 assertArrayEquals(expected, after);
103 @Test(expected = IllegalArgumentException.class)
104 public void testCutBytes2() {
105 ByteArray.cutBytes(new byte[0], 5);
108 @Test(expected = IllegalArgumentException.class)
109 public void testCutBytes3() {
110 ByteArray.cutBytes(this.before, 9);
113 @Test(expected = IllegalArgumentException.class)
114 public void testCutBytes4() {
115 ByteArray.cutBytes(this.before, 0);
118 private final byte[] inBytes = { (byte) 0x03, (byte) 0xFF, (byte) 0x01, (byte) 0x80 };
119 final BitSet inBitSet = new BitSet();
122 public void generateBitSet() {
124 this.inBitSet.set(6, 8);
127 this.inBitSet.set(8, 16);
130 this.inBitSet.set(23);
133 this.inBitSet.set(24);
137 public void testFileToBytes() throws IOException {
138 final String FILE_TO_TEST = "src/test/resources/PCEStatefulCapabilityTlv1.bin";
140 final File fileToCompareWith = new File(FILE_TO_TEST);
141 final InputStream bytesIStream = new FileInputStream(fileToCompareWith);
144 final byte[] actualBytes = ByteArray.fileToBytes(FILE_TO_TEST);
146 if (fileToCompareWith.length() > Integer.MAX_VALUE) {
147 throw new IOException("Too large file to load in byte array.");
150 final byte[] expectedBytes = new byte[(int) fileToCompareWith.length()];
154 while (offset < expectedBytes.length && (numRead = bytesIStream.read(expectedBytes, offset, actualBytes.length - offset)) >= 0) {
158 assertArrayEquals(expectedBytes, actualBytes);
160 bytesIStream.close();
165 * if less than 4 bytes are converted, zero bytes should be appended at the buffer's start
168 public void testBytesToLong_prependingZeros() {
169 assertEquals(1, ByteArray.bytesToLong(new byte[] { 0, 0, 1 }));
172 @Test(expected = IllegalArgumentException.class)
173 public void testBytesToInt() {
174 final byte[] b = new byte[Integer.SIZE + 1];
175 ByteArray.bytesToInt(b);
178 @Test(expected = IllegalArgumentException.class)
179 public void testBytesToShort2() {
180 final byte[] b = new byte[Short.SIZE + 1];
181 ByteArray.bytesToInt(b);
185 public void testCopyBitRange() {
186 assertEquals((byte) 10, ByteArray.copyBitsRange((byte) 0x28, 2, 4));
187 assertEquals((byte) 3, ByteArray.copyBitsRange((byte) 0xFF, 2, 2));
188 assertEquals((byte) 7, ByteArray.copyBitsRange((byte) 0xFF, 5, 3));
189 assertEquals((byte) 15, ByteArray.copyBitsRange((byte) 0xFF, 0, 4));
190 assertEquals((byte) 31, ByteArray.copyBitsRange((byte) 0xF9, 0, 5));
191 assertEquals((byte) 0xA2, ByteArray.copyBitsRange((byte) 0xA2, 0, 8));
192 assertEquals((byte) 1, ByteArray.copyBitsRange((byte) 0xFF, 5, 1));
195 @Test(expected = IllegalArgumentException.class)
196 public void testCopyBitsRange2() {
197 ByteArray.copyBitsRange((byte) 0x28, -1, 4);
200 @Test(expected = IllegalArgumentException.class)
201 public void testCopyBitsRange3() {
202 ByteArray.copyBitsRange((byte) 0x28, 1, 187);
205 @Test(expected = IllegalArgumentException.class)
206 public void testCopyBitsRange4() {
207 ByteArray.copyBitsRange((byte) 0x28, 1, 40);
210 @Test(expected = IllegalArgumentException.class)
211 public void testCopyBitsRange5() {
212 ByteArray.copyBitsRange((byte) 0x28, 28, 2);
215 @Test(expected = IllegalArgumentException.class)
216 public void testCopyBitsRange6() {
217 ByteArray.copyBitsRange((byte) 0x28, 2, -2);
221 public void testBytesToHRString() {
224 // test valid US-ASCII string
225 b = new byte[] { (byte) 79, (byte) 102, (byte) 45, (byte) 57, (byte) 107, (byte) 45, (byte) 48, (byte) 50 };
226 final String expected = "Of-9k-02";
227 assertEquals(expected, ByteArray.bytesToHRString(b));
229 // test Utf-8 restricted bytes
230 b = new byte[] { (byte) 246, (byte) 248, (byte) 254 };
231 assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
233 // test unexpected continuation bytes
234 b = new byte[] { (byte) 128, (byte) 113, (byte) 98 };
235 assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
238 @Test(expected=UnsupportedOperationException.class)
239 public void testPrivateConstructor() throws Throwable {
240 final Constructor<ByteArray> c = ByteArray.class.getDeclaredConstructor();
241 c.setAccessible(true);
244 } catch (final InvocationTargetException e) {
250 public void testEncodeBase64() {
251 final String result = ByteArray.encodeBase64(Unpooled.wrappedBuffer("abc123".getBytes()));
252 assertEquals("YWJjMTIz", result);