fac0ba28a3dac9f22cb29de08f1d06f51ee41290
[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.assertNotSame;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.io.File;
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;
26
27 public class ByteArrayTest {
28
29     final byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
30
31     @Test
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());
37
38         assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
39         assertEquals(buffer.readerIndex(), buffer.writerIndex());
40     }
41
42     @Test
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());
48
49         assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
50         assertNotSame(buffer.readerIndex(), buffer.writerIndex());
51     }
52
53     @Test
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);
61     }
62
63     @Test(expected = IllegalArgumentException.class)
64     public void testSubByte2() {
65         ByteArray.subByte(new byte[0], 2, 2);
66     }
67
68     @Test(expected = IllegalArgumentException.class)
69     public void testSubByte3() {
70         ByteArray.subByte(this.before, 2, -1);
71     }
72
73     @Test(expected = IllegalArgumentException.class)
74     public void testSubByte4() {
75         ByteArray.subByte(this.before, -1, 2);
76     }
77
78     @Test(expected = IllegalArgumentException.class)
79     public void testSubByte5() {
80         ByteArray.subByte(this.before, 9, 2);
81     }
82
83     @Test(expected = IllegalArgumentException.class)
84     public void testSubByte6() {
85         ByteArray.subByte(this.before, 2, 19);
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void testSubByte7() {
90         ByteArray.subByte(this.before, 2, 7);
91     }
92
93     @Test
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);
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void testCutBytes2() {
105         ByteArray.cutBytes(new byte[0], 5);
106     }
107
108     @Test(expected = IllegalArgumentException.class)
109     public void testCutBytes3() {
110         ByteArray.cutBytes(this.before, 9);
111     }
112
113     @Test(expected = IllegalArgumentException.class)
114     public void testCutBytes4() {
115         ByteArray.cutBytes(this.before, 0);
116     }
117
118     private final byte[] inBytes = { (byte) 0x03, (byte) 0xFF, (byte) 0x01, (byte) 0x80 };
119     final BitSet inBitSet = new BitSet();
120
121     @Before
122     public void generateBitSet() {
123         // 0x03
124         this.inBitSet.set(6, 8);
125
126         // 0xFF
127         this.inBitSet.set(8, 16);
128
129         // 0x01
130         this.inBitSet.set(23);
131
132         // 0x80
133         this.inBitSet.set(24);
134     }
135
136     @Test
137     public void testFileToBytes() throws IOException {
138         final String FILE_TO_TEST = "src/test/resources/PCEStatefulCapabilityTlv1.bin";
139
140         final File fileToCompareWith = new File(FILE_TO_TEST);
141         final InputStream bytesIStream = new FileInputStream(fileToCompareWith);
142
143         try {
144             final byte[] actualBytes = ByteArray.fileToBytes(FILE_TO_TEST);
145
146             if (fileToCompareWith.length() > Integer.MAX_VALUE) {
147                 throw new IOException("Too large file to load in byte array.");
148             }
149
150             final byte[] expectedBytes = new byte[(int) fileToCompareWith.length()];
151
152             int offset = 0;
153             int numRead = 0;
154             while (offset < expectedBytes.length && (numRead = bytesIStream.read(expectedBytes, offset, actualBytes.length - offset)) >= 0) {
155                 offset += numRead;
156             }
157
158             assertArrayEquals(expectedBytes, actualBytes);
159         } finally {
160             bytesIStream.close();
161         }
162     }
163
164     /**
165      * if less than 4 bytes are converted, zero bytes should be appended at the buffer's start
166      */
167     @Test
168     public void testBytesToLong_prependingZeros() {
169         assertEquals(1, ByteArray.bytesToLong(new byte[] { 0, 0, 1 }));
170     }
171
172     @Test(expected = IllegalArgumentException.class)
173     public void testBytesToInt() {
174         final byte[] b = new byte[Integer.SIZE + 1];
175         ByteArray.bytesToInt(b);
176     }
177
178     @Test(expected = IllegalArgumentException.class)
179     public void testBytesToShort2() {
180         final byte[] b = new byte[Short.SIZE + 1];
181         ByteArray.bytesToInt(b);
182     }
183
184     @Test
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));
193     }
194
195     @Test(expected = IllegalArgumentException.class)
196     public void testCopyBitsRange2() {
197         ByteArray.copyBitsRange((byte) 0x28, -1, 4);
198     }
199
200     @Test(expected = IllegalArgumentException.class)
201     public void testCopyBitsRange3() {
202         ByteArray.copyBitsRange((byte) 0x28, 1, 187);
203     }
204
205     @Test(expected = IllegalArgumentException.class)
206     public void testCopyBitsRange4() {
207         ByteArray.copyBitsRange((byte) 0x28, 1, 40);
208     }
209
210     @Test(expected = IllegalArgumentException.class)
211     public void testCopyBitsRange5() {
212         ByteArray.copyBitsRange((byte) 0x28, 28, 2);
213     }
214
215     @Test(expected = IllegalArgumentException.class)
216     public void testCopyBitsRange6() {
217         ByteArray.copyBitsRange((byte) 0x28, 2, -2);
218     }
219
220     @Test
221     public void testBytesToHRString() {
222         byte[] b;
223
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));
228
229         // test Utf-8 restricted bytes
230         b = new byte[] { (byte) 246, (byte) 248, (byte) 254 };
231         assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
232
233         // test unexpected continuation bytes
234         b = new byte[] { (byte) 128, (byte) 113, (byte) 98 };
235         assertEquals(Arrays.toString(b), ByteArray.bytesToHRString(b));
236     }
237
238     @Test(expected=UnsupportedOperationException.class)
239     public void testPrivateConstructor() throws Throwable {
240         final Constructor<ByteArray> c = ByteArray.class.getDeclaredConstructor();
241         c.setAccessible(true);
242         try {
243             c.newInstance();
244         } catch (final InvocationTargetException e) {
245             throw e.getCause();
246         }
247     }
248
249     @Test
250     public void testEncodeBase64() {
251         final String result = ByteArray.encodeBase64(Unpooled.wrappedBuffer("abc123".getBytes()));
252         assertEquals("YWJjMTIz", result);
253     }
254
255 }