Code clean up
[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.util.Arrays;
21 import java.util.BitSet;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 public class ByteArrayTest {
26
27     private final byte[] before = new byte[] { 15, 28, 4, 6, 9, 10 };
28
29     @Test
30     public void testReadBytes() {
31         final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
32         buffer.readerIndex(1);
33         assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.readBytes(buffer, 3));
34         assertEquals(4, buffer.readerIndex());
35
36         assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
37         assertEquals(buffer.readerIndex(), buffer.writerIndex());
38     }
39
40     @Test
41     public void testGetBytes() {
42         final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
43         buffer.readerIndex(1);
44         assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.getBytes(buffer, 3));
45         assertEquals(1, buffer.readerIndex());
46
47         assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
48         assertNotSame(buffer.readerIndex(), buffer.writerIndex());
49     }
50
51     @Test
52     public void testSubByte() {
53         byte[] after = ByteArray.subByte(this.before, 0, 3);
54         byte[] expected = new byte[] { 15, 28, 4 };
55         assertArrayEquals(expected, after);
56         after = ByteArray.subByte(this.before, 5, 1);
57         expected = new byte[] { 10 };
58         assertArrayEquals(expected, after);
59     }
60
61     @Test(expected = IllegalArgumentException.class)
62     public void testSubByte2() {
63         ByteArray.subByte(new byte[0], 2, 2);
64     }
65
66     @Test(expected = IllegalArgumentException.class)
67     public void testSubByte3() {
68         ByteArray.subByte(this.before, 2, -1);
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testSubByte4() {
73         ByteArray.subByte(this.before, -1, 2);
74     }
75
76     @Test(expected = IllegalArgumentException.class)
77     public void testSubByte5() {
78         ByteArray.subByte(this.before, 9, 2);
79     }
80
81     @Test(expected = IllegalArgumentException.class)
82     public void testSubByte6() {
83         ByteArray.subByte(this.before, 2, 19);
84     }
85
86     @Test(expected = IllegalArgumentException.class)
87     public void testSubByte7() {
88         ByteArray.subByte(this.before, 2, 7);
89     }
90
91     @Test
92     public void testCutBytes() {
93         byte[] after = ByteArray.cutBytes(this.before, 2);
94         byte[] expected = new byte[] { 4, 6, 9, 10 };
95         assertArrayEquals(expected, after);
96         after = ByteArray.cutBytes(this.before, 6);
97         expected = new byte[] {};
98         assertArrayEquals(expected, after);
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testCutBytes2() {
103         ByteArray.cutBytes(new byte[0], 5);
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testCutBytes3() {
108         ByteArray.cutBytes(this.before, 9);
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testCutBytes4() {
113         ByteArray.cutBytes(this.before, 0);
114     }
115
116     private final BitSet inBitSet = new BitSet();
117
118     @Before
119     public void generateBitSet() {
120         // 0x03
121         this.inBitSet.set(6, 8);
122
123         // 0xFF
124         this.inBitSet.set(8, 16);
125
126         // 0x01
127         this.inBitSet.set(23);
128
129         // 0x80
130         this.inBitSet.set(24);
131     }
132
133     @Test
134     public void testFileToBytes() throws IOException {
135         final String FILE_TO_TEST = "src/test/resources/PCEStatefulCapabilityTlv1.bin";
136
137         final File fileToCompareWith = new File(FILE_TO_TEST);
138
139         try (InputStream bytesIStream = new FileInputStream(fileToCompareWith)) {
140             final byte[] actualBytes = ByteArray.fileToBytes(FILE_TO_TEST);
141
142             if (fileToCompareWith.length() > Integer.MAX_VALUE) {
143                 throw new IOException("Too large file to load in byte array.");
144             }
145
146             final byte[] expectedBytes = new byte[(int) fileToCompareWith.length()];
147
148             int offset = 0;
149             int numRead = 0;
150             while (offset < expectedBytes.length
151                     && (numRead = bytesIStream.read(expectedBytes, offset, actualBytes.length - offset)) >= 0) {
152                 offset += numRead;
153             }
154
155             assertArrayEquals(expectedBytes, actualBytes);
156         }
157     }
158
159     /**
160      * if less than 4 bytes are converted, zero bytes should be appended at the buffer's start.
161      */
162     @Test
163     public void testBytesToLong_prependingZeros() {
164         assertEquals(1, ByteArray.bytesToLong(new byte[] { 0, 0, 1 }));
165     }
166
167     @Test(expected = IllegalArgumentException.class)
168     public void testBytesToInt() {
169         final byte[] b = new byte[Integer.SIZE + 1];
170         ByteArray.bytesToInt(b);
171     }
172
173     @Test(expected = IllegalArgumentException.class)
174     public void testBytesToShort2() {
175         final byte[] b = new byte[Short.SIZE + 1];
176         ByteArray.bytesToInt(b);
177     }
178
179     @Test
180     public void testCopyBitRange() {
181         assertEquals((byte) 10, ByteArray.copyBitsRange((byte) 0x28, 2, 4));
182         assertEquals((byte) 3, ByteArray.copyBitsRange((byte) 0xFF, 2, 2));
183         assertEquals((byte) 7, ByteArray.copyBitsRange((byte) 0xFF, 5, 3));
184         assertEquals((byte) 15, ByteArray.copyBitsRange((byte) 0xFF, 0, 4));
185         assertEquals((byte) 31, ByteArray.copyBitsRange((byte) 0xF9, 0, 5));
186         assertEquals((byte) 0xA2, ByteArray.copyBitsRange((byte) 0xA2, 0, 8));
187         assertEquals((byte) 1, ByteArray.copyBitsRange((byte) 0xFF, 5, 1));
188     }
189
190     @Test(expected = IllegalArgumentException.class)
191     public void testCopyBitsRange2() {
192         ByteArray.copyBitsRange((byte) 0x28, -1, 4);
193     }
194
195     @Test(expected = IllegalArgumentException.class)
196     public void testCopyBitsRange3() {
197         ByteArray.copyBitsRange((byte) 0x28, 1, 187);
198     }
199
200     @Test(expected = IllegalArgumentException.class)
201     public void testCopyBitsRange4() {
202         ByteArray.copyBitsRange((byte) 0x28, 1, 40);
203     }
204
205     @Test(expected = IllegalArgumentException.class)
206     public void testCopyBitsRange5() {
207         ByteArray.copyBitsRange((byte) 0x28, 28, 2);
208     }
209
210     @Test(expected = IllegalArgumentException.class)
211     public void testCopyBitsRange6() {
212         ByteArray.copyBitsRange((byte) 0x28, 2, -2);
213     }
214
215     @Test
216     public void testBytesToHRString() {
217         byte[] bytes;
218
219         // test valid US-ASCII string
220         bytes = new byte[] { (byte) 79, (byte) 102, (byte) 45, (byte) 57, (byte) 107, (byte) 45, (byte) 48, (byte) 50 };
221         final String expected = "Of-9k-02";
222         assertEquals(expected, ByteArray.bytesToHRString(bytes));
223
224         // test Utf-8 restricted bytes
225         bytes = new byte[] { (byte) 246, (byte) 248, (byte) 254 };
226         assertEquals(Arrays.toString(bytes), ByteArray.bytesToHRString(bytes));
227
228         // test unexpected continuation bytes
229         bytes = new byte[] { (byte) 128, (byte) 113, (byte) 98 };
230         assertEquals(Arrays.toString(bytes), ByteArray.bytesToHRString(bytes));
231     }
232
233     @Test
234     public void testEncodeBase64() {
235         final String result = ByteArray.encodeBase64(Unpooled.wrappedBuffer("abc123".getBytes()));
236         assertEquals("YWJjMTIz", result);
237     }
238
239 }