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 package org.apache.commons.io; 18 19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 import static org.junit.jupiter.api.Assertions.assertThrows; 21 22 import java.io.ByteArrayInputStream; 23 import java.io.ByteArrayOutputStream; 24 import java.io.EOFException; 25 import java.io.IOException; 26 27 import org.junit.jupiter.api.Test; 28 29 /** 30 * Tests {@link EndianUtils}. 31 */ 32 public class EndianUtilsTest { 33 34 @Test testCtor()35 public void testCtor() { 36 new EndianUtils(); 37 // Constructor does not blow up. 38 } 39 40 @Test testEOFException()41 public void testEOFException() { 42 final ByteArrayInputStream input = new ByteArrayInputStream(new byte[] {}); 43 assertThrows(EOFException.class, () -> EndianUtils.readSwappedDouble(input)); 44 } 45 46 @Test testReadSwappedDouble()47 public void testReadSwappedDouble() throws IOException { 48 final byte[] bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; 49 final double d1 = Double.longBitsToDouble( 0x0102030405060708L ); 50 final double d2 = EndianUtils.readSwappedDouble( bytes, 0 ); 51 assertEquals( d1, d2, 0.0 ); 52 53 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 54 assertEquals( d1, EndianUtils.readSwappedDouble( input ), 0.0 ); 55 } 56 57 @Test testReadSwappedFloat()58 public void testReadSwappedFloat() throws IOException { 59 final byte[] bytes = { 0x04, 0x03, 0x02, 0x01 }; 60 final float f1 = Float.intBitsToFloat( 0x01020304 ); 61 final float f2 = EndianUtils.readSwappedFloat( bytes, 0 ); 62 assertEquals( f1, f2, 0.0 ); 63 64 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 65 assertEquals( f1, EndianUtils.readSwappedFloat( input ), 0.0 ); 66 } 67 68 @Test testReadSwappedInteger()69 public void testReadSwappedInteger() throws IOException { 70 final byte[] bytes = { 0x04, 0x03, 0x02, 0x01 }; 71 assertEquals( 0x01020304, EndianUtils.readSwappedInteger( bytes, 0 ) ); 72 73 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 74 assertEquals( 0x01020304, EndianUtils.readSwappedInteger( input ) ); 75 } 76 77 @Test testReadSwappedLong()78 public void testReadSwappedLong() throws IOException { 79 final byte[] bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; 80 assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( bytes, 0 ) ); 81 82 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 83 assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( input ) ); 84 } 85 86 @Test testReadSwappedShort()87 public void testReadSwappedShort() throws IOException { 88 final byte[] bytes = { 0x02, 0x01 }; 89 assertEquals( 0x0102, EndianUtils.readSwappedShort( bytes, 0 ) ); 90 91 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 92 assertEquals( 0x0102, EndianUtils.readSwappedShort( input ) ); 93 } 94 95 @Test testReadSwappedUnsignedInteger()96 public void testReadSwappedUnsignedInteger() throws IOException { 97 final byte[] bytes = { 0x04, 0x03, 0x02, 0x01 }; 98 assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( bytes, 0 ) ); 99 100 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 101 assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( input ) ); 102 } 103 104 @Test testReadSwappedUnsignedShort()105 public void testReadSwappedUnsignedShort() throws IOException { 106 final byte[] bytes = { 0x02, 0x01 }; 107 assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( bytes, 0 ) ); 108 109 final ByteArrayInputStream input = new ByteArrayInputStream(bytes); 110 assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( input ) ); 111 } 112 113 @Test testSwapDouble()114 public void testSwapDouble() { 115 assertEquals( 0.0, EndianUtils.swapDouble( 0.0 ), 0.0 ); 116 final double d1 = Double.longBitsToDouble( 0x0102030405060708L ); 117 final double d2 = Double.longBitsToDouble( 0x0807060504030201L ); 118 assertEquals( d2, EndianUtils.swapDouble( d1 ), 0.0 ); 119 } 120 121 @Test testSwapFloat()122 public void testSwapFloat() { 123 assertEquals( 0.0f, EndianUtils.swapFloat( 0.0f ), 0.0 ); 124 final float f1 = Float.intBitsToFloat( 0x01020304 ); 125 final float f2 = Float.intBitsToFloat( 0x04030201 ); 126 assertEquals( f2, EndianUtils.swapFloat( f1 ), 0.0 ); 127 } 128 129 @Test testSwapInteger()130 public void testSwapInteger() { 131 assertEquals( 0, EndianUtils.swapInteger( 0 ) ); 132 assertEquals( 0x04030201, EndianUtils.swapInteger( 0x01020304 ) ); 133 assertEquals( 0x01000000, EndianUtils.swapInteger( 0x00000001 ) ); 134 assertEquals( 0x00000001, EndianUtils.swapInteger( 0x01000000 ) ); 135 assertEquals( 0x11111111, EndianUtils.swapInteger( 0x11111111 ) ); 136 assertEquals( 0xabcdef10, EndianUtils.swapInteger( 0x10efcdab ) ); 137 assertEquals( 0xab, EndianUtils.swapInteger( 0xab000000 ) ); 138 } 139 140 @Test testSwapLong()141 public void testSwapLong() { 142 assertEquals( 0, EndianUtils.swapLong( 0 ) ); 143 assertEquals( 0x0807060504030201L, EndianUtils.swapLong( 0x0102030405060708L ) ); 144 assertEquals( 0xffffffffffffffffL, EndianUtils.swapLong( 0xffffffffffffffffL ) ); 145 assertEquals( 0xab, EndianUtils.swapLong( 0xab00000000000000L ) ); 146 } 147 148 @Test testSwapShort()149 public void testSwapShort() { 150 assertEquals( (short) 0, EndianUtils.swapShort( (short) 0 ) ); 151 assertEquals( (short) 0x0201, EndianUtils.swapShort( (short) 0x0102 ) ); 152 assertEquals( (short) 0xffff, EndianUtils.swapShort( (short) 0xffff ) ); 153 assertEquals( (short) 0x0102, EndianUtils.swapShort( (short) 0x0201 ) ); 154 } 155 156 /** 157 * Tests all swapXxxx methods for symmetry when going from one endian 158 * to another and back again. 159 */ 160 @Test testSymmetry()161 public void testSymmetry() { 162 assertEquals( (short) 0x0102, EndianUtils.swapShort( EndianUtils.swapShort( (short) 0x0102 ) ) ); 163 assertEquals( 0x01020304, EndianUtils.swapInteger( EndianUtils.swapInteger( 0x01020304 ) ) ); 164 assertEquals( 0x0102030405060708L, EndianUtils.swapLong( EndianUtils.swapLong( 0x0102030405060708L ) ) ); 165 final float f1 = Float.intBitsToFloat( 0x01020304 ); 166 assertEquals( f1, EndianUtils.swapFloat( EndianUtils.swapFloat( f1 ) ), 0.0 ); 167 final double d1 = Double.longBitsToDouble( 0x0102030405060708L ); 168 assertEquals( d1, EndianUtils.swapDouble( EndianUtils.swapDouble( d1 ) ), 0.0 ); 169 } 170 171 // tests #IO-101 172 @Test testSymmetryOfLong()173 public void testSymmetryOfLong() { 174 175 final double[] tests = {34.345, -345.5645, 545.12, 10.043, 7.123456789123}; 176 for (final double test : tests) { 177 178 // testing the real problem 179 byte[] buffer = new byte[8]; 180 final long ln1 = Double.doubleToLongBits( test ); 181 EndianUtils.writeSwappedLong(buffer, 0, ln1); 182 final long ln2 = EndianUtils.readSwappedLong(buffer, 0); 183 assertEquals( ln1, ln2 ); 184 185 // testing the bug report 186 buffer = new byte[8]; 187 EndianUtils.writeSwappedDouble(buffer, 0, test); 188 final double val = EndianUtils.readSwappedDouble(buffer, 0); 189 assertEquals( test, val, 0 ); 190 } 191 } 192 193 // tests #IO-117 194 @Test testUnsignedOverrun()195 public void testUnsignedOverrun() throws Exception { 196 final byte[] target = { 0, 0, 0, (byte)0x80 }; 197 final long expected = 0x80000000L; 198 199 long actual = EndianUtils.readSwappedUnsignedInteger(target, 0); 200 assertEquals(expected, actual, "readSwappedUnsignedInteger(byte[], int) was incorrect"); 201 202 final ByteArrayInputStream in = new ByteArrayInputStream(target); 203 actual = EndianUtils.readSwappedUnsignedInteger(in); 204 assertEquals(expected, actual, "readSwappedUnsignedInteger(InputStream) was incorrect"); 205 } 206 207 @Test testWriteSwappedDouble()208 public void testWriteSwappedDouble() throws IOException { 209 byte[] bytes = new byte[8]; 210 final double d1 = Double.longBitsToDouble( 0x0102030405060708L ); 211 EndianUtils.writeSwappedDouble( bytes, 0, d1 ); 212 assertEquals( 0x08, bytes[0] ); 213 assertEquals( 0x07, bytes[1] ); 214 assertEquals( 0x06, bytes[2] ); 215 assertEquals( 0x05, bytes[3] ); 216 assertEquals( 0x04, bytes[4] ); 217 assertEquals( 0x03, bytes[5] ); 218 assertEquals( 0x02, bytes[6] ); 219 assertEquals( 0x01, bytes[7] ); 220 221 final ByteArrayOutputStream baos = new ByteArrayOutputStream(8); 222 EndianUtils.writeSwappedDouble( baos, d1 ); 223 bytes = baos.toByteArray(); 224 assertEquals( 0x08, bytes[0] ); 225 assertEquals( 0x07, bytes[1] ); 226 assertEquals( 0x06, bytes[2] ); 227 assertEquals( 0x05, bytes[3] ); 228 assertEquals( 0x04, bytes[4] ); 229 assertEquals( 0x03, bytes[5] ); 230 assertEquals( 0x02, bytes[6] ); 231 assertEquals( 0x01, bytes[7] ); 232 } 233 234 @Test testWriteSwappedFloat()235 public void testWriteSwappedFloat() throws IOException { 236 byte[] bytes = new byte[4]; 237 final float f1 = Float.intBitsToFloat( 0x01020304 ); 238 EndianUtils.writeSwappedFloat( bytes, 0, f1 ); 239 assertEquals( 0x04, bytes[0] ); 240 assertEquals( 0x03, bytes[1] ); 241 assertEquals( 0x02, bytes[2] ); 242 assertEquals( 0x01, bytes[3] ); 243 244 final ByteArrayOutputStream baos = new ByteArrayOutputStream(4); 245 EndianUtils.writeSwappedFloat( baos, f1 ); 246 bytes = baos.toByteArray(); 247 assertEquals( 0x04, bytes[0] ); 248 assertEquals( 0x03, bytes[1] ); 249 assertEquals( 0x02, bytes[2] ); 250 assertEquals( 0x01, bytes[3] ); 251 } 252 253 @Test testWriteSwappedInteger()254 public void testWriteSwappedInteger() throws IOException { 255 byte[] bytes = new byte[4]; 256 EndianUtils.writeSwappedInteger( bytes, 0, 0x01020304 ); 257 assertEquals( 0x04, bytes[0] ); 258 assertEquals( 0x03, bytes[1] ); 259 assertEquals( 0x02, bytes[2] ); 260 assertEquals( 0x01, bytes[3] ); 261 262 final ByteArrayOutputStream baos = new ByteArrayOutputStream(4); 263 EndianUtils.writeSwappedInteger( baos, 0x01020304 ); 264 bytes = baos.toByteArray(); 265 assertEquals( 0x04, bytes[0] ); 266 assertEquals( 0x03, bytes[1] ); 267 assertEquals( 0x02, bytes[2] ); 268 assertEquals( 0x01, bytes[3] ); 269 } 270 271 @Test testWriteSwappedLong()272 public void testWriteSwappedLong() throws IOException { 273 byte[] bytes = new byte[8]; 274 EndianUtils.writeSwappedLong( bytes, 0, 0x0102030405060708L ); 275 assertEquals( 0x08, bytes[0] ); 276 assertEquals( 0x07, bytes[1] ); 277 assertEquals( 0x06, bytes[2] ); 278 assertEquals( 0x05, bytes[3] ); 279 assertEquals( 0x04, bytes[4] ); 280 assertEquals( 0x03, bytes[5] ); 281 assertEquals( 0x02, bytes[6] ); 282 assertEquals( 0x01, bytes[7] ); 283 284 final ByteArrayOutputStream baos = new ByteArrayOutputStream(8); 285 EndianUtils.writeSwappedLong( baos, 0x0102030405060708L ); 286 bytes = baos.toByteArray(); 287 assertEquals( 0x08, bytes[0] ); 288 assertEquals( 0x07, bytes[1] ); 289 assertEquals( 0x06, bytes[2] ); 290 assertEquals( 0x05, bytes[3] ); 291 assertEquals( 0x04, bytes[4] ); 292 assertEquals( 0x03, bytes[5] ); 293 assertEquals( 0x02, bytes[6] ); 294 assertEquals( 0x01, bytes[7] ); 295 } 296 297 @Test testWriteSwappedShort()298 public void testWriteSwappedShort() throws IOException { 299 byte[] bytes = new byte[2]; 300 EndianUtils.writeSwappedShort( bytes, 0, (short) 0x0102 ); 301 assertEquals( 0x02, bytes[0] ); 302 assertEquals( 0x01, bytes[1] ); 303 304 final ByteArrayOutputStream baos = new ByteArrayOutputStream(2); 305 EndianUtils.writeSwappedShort( baos, (short) 0x0102 ); 306 bytes = baos.toByteArray(); 307 assertEquals( 0x02, bytes[0] ); 308 assertEquals( 0x01, bytes[1] ); 309 } 310 311 } 312