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