0cea42d6823c8f933664aeeecd944af62eab8c01
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / ByteArrayTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
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;
15
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.File;
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;
28
29 public class ByteArrayTest {
30
31     final byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
32
33     @Test
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());
39
40         assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
41         assertEquals(buffer.readerIndex(), buffer.writerIndex());
42     }
43
44     @Test
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());
50
51         assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
52         assertNotSame(buffer.readerIndex(), buffer.writerIndex());
53     }
54
55     @Test
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);
63     }
64
65     @Test(expected = IllegalArgumentException.class)
66     public void testSubByte2() {
67         ByteArray.subByte(new byte[0], 2, 2);
68     }
69
70     @Test(expected = IllegalArgumentException.class)
71     public void testSubByte3() {
72         ByteArray.subByte(this.before, 2, -1);
73     }
74
75     @Test(expected = IllegalArgumentException.class)
76     public void testSubByte4() {
77         ByteArray.subByte(this.before, -1, 2);
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void testSubByte5() {
82         ByteArray.subByte(this.before, 9, 2);
83     }
84
85     @Test(expected = IllegalArgumentException.class)
86     public void testSubByte6() {
87         ByteArray.subByte(this.before, 2, 19);
88     }
89
90     @Test(expected = IllegalArgumentException.class)
91     public void testSubByte7() {
92         ByteArray.subByte(this.before, 2, 7);
93     }
94
95     @Test
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);
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testCutBytes2() {
107         ByteArray.cutBytes(new byte[0], 5);
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     public void testCutBytes3() {
112         ByteArray.cutBytes(this.before, 9);
113     }
114
115     @Test(expected = IllegalArgumentException.class)
116     public void testCutBytes4() {
117         ByteArray.cutBytes(this.before, 0);
118     }
119
120     @Test
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]);
132     }
133
134     private final byte[] inBytes = { (byte) 0x03, (byte) 0xFF, (byte) 0x01, (byte) 0x80 };
135     final BitSet inBitSet = new BitSet();
136
137     @Before
138     public void generateBitSet() {
139         // 0x03
140         this.inBitSet.set(6, 8);
141
142         // 0xFF
143         this.inBitSet.set(8, 16);
144
145         // 0x01
146         this.inBitSet.set(23);
147
148         // 0x80
149         this.inBitSet.set(24);
150     }
151
152     @Test
153     public void testBytesToBitSetFunction() {
154         final BitSet iputBitSet = ByteArray.bytesToBitSet(this.inBytes);
155
156         assertEquals(this.inBitSet, iputBitSet);
157     }
158
159     @Test
160     public void testBitSetToBytesFunction() {
161         byte[] resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length);
162         assertArrayEquals(this.inBytes, resultBytes);
163
164         resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length - 1);
165         assertArrayEquals(Arrays.copyOf(this.inBytes, this.inBytes.length - 1), resultBytes);
166
167         resultBytes = ByteArray.bitSetToBytes(this.inBitSet, this.inBytes.length + 1);
168         assertArrayEquals(Arrays.copyOf(this.inBytes, this.inBytes.length + 1), resultBytes);
169     }
170
171     @Test
172     public void testFileToBytes() throws IOException {
173         final String FILE_TO_TEST = "src/test/resources/PCEStatefulCapabilityTlv1.bin";
174
175         final File fileToCompareWith = new File(FILE_TO_TEST);
176         final InputStream bytesIStream = new FileInputStream(fileToCompareWith);
177
178         try {
179             final byte[] actualBytes = ByteArray.fileToBytes(FILE_TO_TEST);
180
181             if (fileToCompareWith.length() > Integer.MAX_VALUE) {
182                 throw new IOException("Too large file to load in byte array.");
183             }
184
185             final byte[] expectedBytes = new byte[(int) fileToCompareWith.length()];
186
187             int offset = 0;
188             int numRead = 0;
189             while (offset < expectedBytes.length && (numRead = bytesIStream.read(expectedBytes, offset, actualBytes.length - offset)) >= 0) {
190                 offset += numRead;
191             }
192
193             assertArrayEquals(expectedBytes, actualBytes);
194         } finally {
195             bytesIStream.close();
196         }
197     }
198
199     /**
200      * if less than 4 bytes are converted, zero bytes should be appended at the buffer's start
201      */
202     @Test
203     public void testBytesToLong_prependingZeros() {
204         assertEquals(1, ByteArray.bytesToLong(new byte[] { 0, 0, 1 }));
205     }
206
207     @Test(expected = IllegalArgumentException.class)
208     public void testBytesToInt() {
209         final byte[] b = new byte[Integer.SIZE + 1];
210         ByteArray.bytesToInt(b);
211     }
212
213     @Test(expected = IllegalArgumentException.class)
214     public void testBytesToShort2() {
215         final byte[] b = new byte[Short.SIZE + 1];
216         ByteArray.bytesToInt(b);
217     }
218
219     @Test
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));
228     }
229
230     @Test(expected = IllegalArgumentException.class)
231     public void testCopyBitsRange2() {
232         ByteArray.copyBitsRange((byte) 0x28, -1, 4);
233     }
234
235     @Test(expected = IllegalArgumentException.class)
236     public void testCopyBitsRange3() {
237         ByteArray.copyBitsRange((byte) 0x28, 1, 187);
238     }
239
240     @Test(expected = IllegalArgumentException.class)
241     public void testCopyBitsRange4() {
242         ByteArray.copyBitsRange((byte) 0x28, 1, 40);
243     }
244
245     @Test(expected = IllegalArgumentException.class)
246     public void testCopyBitsRange5() {
247         ByteArray.copyBitsRange((byte) 0x28, 28, 2);
248     }
249
250     @Test(expected = IllegalArgumentException.class)
251     public void testCopyBitsRange6() {
252         ByteArray.copyBitsRange((byte) 0x28, 2, -2);
253     }
254
255     @Test
256     public void testBytesToHRString() {
257         byte[] b;
258
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));
263
264         // test Utf-8 restricted bytes
265         b = new byte[] { (byte) 246, (byte) 248, (byte) 254 };
266         assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
267
268         // test unexpected continuation bytes
269         b = new byte[] { (byte) 128, (byte) 113, (byte) 98 };
270         assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
271     }
272
273     @Test(expected=UnsupportedOperationException.class)
274     public void testPrivateConstructor() throws Throwable {
275         final Constructor<ByteArray> c = ByteArray.class.getDeclaredConstructor();
276         c.setAccessible(true);
277         try {
278             c.newInstance();
279         } catch (final InvocationTargetException e) {
280             throw e.getCause();
281         }
282     }
283
284 }