View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.compress.utils;
19  
20  import static org.apache.commons.compress.utils.ByteUtils.fromLittleEndian;
21  import static org.apache.commons.compress.utils.ByteUtils.toLittleEndian;
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.DataInput;
29  import java.io.DataInputStream;
30  import java.io.DataOutput;
31  import java.io.DataOutputStream;
32  import java.io.EOFException;
33  import java.io.IOException;
34  import java.util.Arrays;
35  
36  import org.apache.commons.compress.utils.ByteUtils.InputStreamByteSupplier;
37  import org.apache.commons.compress.utils.ByteUtils.OutputStreamByteConsumer;
38  import org.junit.jupiter.api.Test;
39  
40  public class ByteUtilsTest {
41  
42      @Test
43      public void testFromLittleEndianFromArray() {
44          final byte[] b = { 1, 2, 3, 4, 5 };
45          assertEquals(2 + 3 * 256 + 4 * 256 * 256, fromLittleEndian(b, 1, 3));
46      }
47  
48      @Test
49      public void testFromLittleEndianFromArrayOneArg() {
50          final byte[] b = { 2, 3, 4 };
51          assertEquals(2 + 3 * 256 + 4 * 256 * 256, fromLittleEndian(b));
52      }
53  
54      @Test
55      public void testFromLittleEndianFromArrayOneArgThrowsForLengthTooBig() {
56          assertThrows(IllegalArgumentException.class, () -> fromLittleEndian(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
57      }
58  
59      @Test
60      public void testFromLittleEndianFromArrayOneArgUnsignedInt32() {
61          final byte[] b = { 2, 3, 4, (byte) 128 };
62          assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, fromLittleEndian(b));
63      }
64  
65      @Test
66      public void testFromLittleEndianFromArrayThrowsForLengthTooBig() {
67          assertThrows(IllegalArgumentException.class, () -> fromLittleEndian(ByteUtils.EMPTY_BYTE_ARRAY, 0, 9));
68      }
69  
70      @Test
71      public void testFromLittleEndianFromArrayUnsignedInt32() {
72          final byte[] b = { 1, 2, 3, 4, (byte) 128 };
73          assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, fromLittleEndian(b, 1, 4));
74      }
75  
76      @Test
77      public void testFromLittleEndianFromDataInput() throws IOException {
78          final DataInput din = new DataInputStream(new ByteArrayInputStream(new byte[] { 2, 3, 4, 5 }));
79          assertEquals(2 + 3 * 256 + 4 * 256 * 256, fromLittleEndian(din, 3));
80      }
81  
82      @Test
83      public void testFromLittleEndianFromDataInputThrowsForLengthTooBig() {
84          final DataInput din = new DataInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY));
85          assertThrows(IllegalArgumentException.class, () -> fromLittleEndian(din, 9));
86      }
87  
88      @Test
89      public void testFromLittleEndianFromDataInputThrowsForPrematureEnd() {
90          final DataInput din = new DataInputStream(new ByteArrayInputStream(new byte[] { 2, 3 }));
91          assertThrows(EOFException.class, () -> fromLittleEndian(din, 3));
92      }
93  
94      @Test
95      public void testFromLittleEndianFromDataInputUnsignedInt32() throws IOException {
96          final DataInput din = new DataInputStream(new ByteArrayInputStream(new byte[] { 2, 3, 4, (byte) 128 }));
97          assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, fromLittleEndian(din, 4));
98      }
99  
100     @Test
101     public void testFromLittleEndianFromStream() throws IOException {
102         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3, 4, 5 });
103         assertEquals(2 + 3 * 256 + 4 * 256 * 256, fromLittleEndian(bin, 3));
104     }
105 
106     @Test
107     public void testFromLittleEndianFromStreamThrowsForLengthTooBig() {
108         assertThrows(IllegalArgumentException.class, () -> fromLittleEndian(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY), 9));
109     }
110 
111     @Test
112     public void testFromLittleEndianFromStreamThrowsForPrematureEnd() {
113         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3 });
114         assertThrows(IOException.class, () -> fromLittleEndian(bin, 3));
115     }
116 
117     @Test
118     public void testFromLittleEndianFromStreamUnsignedInt32() throws IOException {
119         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3, 4, (byte) 128 });
120         assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, fromLittleEndian(bin, 4));
121     }
122 
123     @Test
124     public void testFromLittleEndianFromSupplier() throws IOException {
125         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3, 4, 5 });
126         assertEquals(2 + 3 * 256 + 4 * 256 * 256, fromLittleEndian(new InputStreamByteSupplier(bin), 3));
127     }
128 
129     @Test
130     public void testFromLittleEndianFromSupplierThrowsForLengthTooBig() {
131         assertThrows(IllegalArgumentException.class,
132                 () -> fromLittleEndian(new InputStreamByteSupplier(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY)), 9));
133     }
134 
135     @Test
136     public void testFromLittleEndianFromSupplierThrowsForPrematureEnd() {
137         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3 });
138         assertThrows(IOException.class, () -> fromLittleEndian(new InputStreamByteSupplier(bin), 3));
139     }
140 
141     @Test
142     public void testFromLittleEndianFromSupplierUnsignedInt32() throws IOException {
143         final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] { 2, 3, 4, (byte) 128 });
144         assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, fromLittleEndian(new InputStreamByteSupplier(bin), 4));
145     }
146 
147     @Test
148     public void testToLittleEndianToByteArray() {
149         final byte[] b = new byte[4];
150         toLittleEndian(b, 2 + 3 * 256 + 4 * 256 * 256, 1, 3);
151         assertArrayEquals(new byte[] { 2, 3, 4 }, Arrays.copyOfRange(b, 1, 4));
152     }
153 
154     @Test
155     public void testToLittleEndianToByteArrayUnsignedInt32() {
156         final byte[] b = new byte[4];
157         toLittleEndian(b, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, 0, 4);
158         assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 }, b);
159     }
160 
161     @Test
162     public void testToLittleEndianToConsumer() throws IOException {
163         final byte[] byteArray;
164         final byte[] expected = { 2, 3, 4 };
165         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
166             toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4 * 256 * 256, 3);
167             byteArray = bos.toByteArray();
168             assertArrayEquals(expected, byteArray);
169         }
170         assertArrayEquals(expected, byteArray);
171     }
172 
173     @Test
174     public void testToLittleEndianToConsumerUnsignedInt32() throws IOException {
175         final byte[] byteArray;
176         final byte[] expected = { 2, 3, 4, (byte) 128 };
177         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
178             toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, 4);
179             byteArray = bos.toByteArray();
180             assertArrayEquals(expected, byteArray);
181         }
182         assertArrayEquals(expected, byteArray);
183     }
184 
185     @Test
186     public void testToLittleEndianToDataOutput() throws IOException {
187         final byte[] byteArray;
188         final byte[] expected = { 2, 3, 4 };
189         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
190             final DataOutput dos = new DataOutputStream(bos);
191             toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256, 3);
192             byteArray = bos.toByteArray();
193             assertArrayEquals(expected, byteArray);
194         }
195         assertArrayEquals(expected, byteArray);
196     }
197 
198     @Test
199     public void testToLittleEndianToDataOutputUnsignedInt32() throws IOException {
200         final byte[] byteArray;
201         final byte[] expected = { 2, 3, 4, (byte) 128 };
202         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
203             final DataOutput dos = new DataOutputStream(bos);
204             toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, 4);
205             byteArray = bos.toByteArray();
206             assertArrayEquals(expected, byteArray);
207         }
208         assertArrayEquals(expected, byteArray);
209     }
210 
211     @Test
212     public void testToLittleEndianToStream() throws IOException {
213         final byte[] byteArray;
214         final byte[] expected = { 2, 3, 4 };
215         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
216             toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256, 3);
217             byteArray = bos.toByteArray();
218             assertArrayEquals(expected, byteArray);
219         }
220         assertArrayEquals(expected, byteArray);
221     }
222 
223     @Test
224     public void testToLittleEndianToStreamUnsignedInt32() throws IOException {
225         final byte[] byteArray;
226         final byte[] expected = { 2, 3, 4, (byte) 128 };
227         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
228             toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256, 4);
229             byteArray = bos.toByteArray();
230             assertArrayEquals(expected, byteArray);
231         }
232         assertArrayEquals(expected, byteArray);
233     }
234 }