You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestHexDump.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertThrows;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import java.io.IOException;
  21. import java.io.PrintStream;
  22. import java.io.UnsupportedEncodingException;
  23. import org.apache.commons.io.output.NullPrintStream;
  24. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  25. import org.junit.jupiter.api.AfterAll;
  26. import org.junit.jupiter.api.BeforeAll;
  27. import org.junit.jupiter.api.Test;
  28. import org.junit.jupiter.params.ParameterizedTest;
  29. import org.junit.jupiter.params.provider.ValueSource;
  30. class TestHexDump {
  31. private static PrintStream SYSTEM_OUT;
  32. @BeforeAll
  33. public static void setUp() throws UnsupportedEncodingException {
  34. SYSTEM_OUT = System.out;
  35. System.setOut(new NullPrintStream());
  36. }
  37. @AfterAll
  38. public static void tearDown() {
  39. System.setOut(SYSTEM_OUT);
  40. }
  41. @Test
  42. void testDump() throws IOException {
  43. byte[] testArray = testArray();
  44. UnsynchronizedByteArrayOutputStream streamAct = new UnsynchronizedByteArrayOutputStream();
  45. HexDump.dump(testArray, 0, streamAct, 0);
  46. byte[] bytesAct = streamAct.toByteArray();
  47. byte[] bytesExp = toHexDump(0, 0);
  48. assertEquals(bytesExp.length, bytesAct.length, "array size mismatch");
  49. assertArrayEquals(bytesExp, bytesAct, "array mismatch");
  50. // verify proper behavior with non-zero offset
  51. streamAct.reset();
  52. HexDump.dump(testArray, 0x10000000L, streamAct, 0);
  53. bytesAct = streamAct.toByteArray();
  54. bytesExp = toHexDump(0x10000000L,0);
  55. assertEquals(bytesExp.length, bytesAct.length, "array size mismatch");
  56. assertArrayEquals(bytesExp, bytesAct, "array mismatch");
  57. // verify proper behavior with negative offset
  58. streamAct.reset();
  59. HexDump.dump(testArray, 0xFF000000L, streamAct, 0);
  60. bytesAct = streamAct.toByteArray();
  61. bytesExp = toHexDump(0xFF000000L,0);
  62. assertEquals(bytesExp.length, bytesAct.length, "array size mismatch");
  63. assertArrayEquals(bytesExp, bytesAct, "array mismatch");
  64. // verify proper behavior with non-zero index
  65. streamAct.reset();
  66. HexDump.dump(testArray, 0xFF000000L, streamAct, 0x81);
  67. bytesAct = streamAct.toByteArray();
  68. bytesExp = toHexDump(0xFF000000L,0x81);
  69. assertEquals(bytesExp.length, bytesAct.length, "array size mismatch");
  70. assertArrayEquals(bytesExp, bytesAct, "array mismatch");
  71. // verify proper behavior with negative index
  72. streamAct.reset();
  73. assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000L, streamAct, -1));
  74. // verify proper behavior with index that is too large
  75. streamAct.reset();
  76. assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000L, streamAct, testArray.length));
  77. // verify proper behavior with null stream
  78. assertThrows(IllegalArgumentException.class, () -> HexDump.dump(testArray, 0x10000000L, null, 0));
  79. // verify proper behaviour with empty byte array
  80. streamAct.reset();
  81. HexDump.dump( new byte[0], 0, streamAct, 0 );
  82. assertEquals( "No Data" + System.getProperty( "line.separator"), streamAct.toString(LocaleUtil.CHARSET_1252.name()) );
  83. }
  84. private byte[] toHexDump(long offset, int index) {
  85. StringBuilder strExp = new StringBuilder(), chrs = new StringBuilder();
  86. Object[] obj = new Object[33];
  87. StringBuilder format = new StringBuilder();
  88. for (int j = 0; j < 16 && (index + j*16) < 256; j++) {
  89. obj[0] = offset+index+j*16L;
  90. chrs.setLength(0);
  91. format.setLength(0);
  92. format.append("%08X ");
  93. for (int k = 0; k < 16; k++) {
  94. if (index+j*16+k < 256){
  95. obj[k+1] = index+j*16+k;
  96. chrs.append(HexDump.toAscii(index+j*16+k));
  97. format.append("%02X ");
  98. } else {
  99. format.append(" ");
  100. }
  101. }
  102. obj[17] = chrs.toString();
  103. format.append("%18$s").append(HexDump.EOL);
  104. String str = String.format(LocaleUtil.getUserLocale(), format.toString(), obj);
  105. strExp.append(str);
  106. }
  107. return strExp.toString().getBytes(HexDump.UTF8);
  108. }
  109. @Test
  110. void testToHex() {
  111. assertEquals("000A", HexDump.toHex((short)0xA));
  112. assertEquals("0A", HexDump.toHex((byte)0xA));
  113. assertEquals("0000000A", HexDump.toHex(0xA));
  114. assertEquals("[]", HexDump.toHex(new byte[] { }));
  115. assertEquals("[0A]", HexDump.toHex(new byte[] { 0xA }));
  116. assertEquals("[0A, 0B]", HexDump.toHex(new byte[] { 0xA, 0xB }));
  117. assertEquals("FFFF", HexDump.toHex((short)0xFFFF));
  118. assertEquals("00000000000004D2", HexDump.toHex(1234L));
  119. assertEquals("0xFE", HexDump.byteToHex(-2));
  120. assertEquals("0x25", HexDump.byteToHex(37));
  121. assertEquals("0xFFFE", HexDump.shortToHex(-2));
  122. assertEquals("0x0005", HexDump.shortToHex(5));
  123. assertEquals("0xFFFFFF9C", HexDump.intToHex(-100));
  124. assertEquals("0x00001001", HexDump.intToHex(4097));
  125. assertEquals("0xFFFFFFFFFFFF0006", HexDump.longToHex(-65530));
  126. assertEquals("0x0000000000003FCD", HexDump.longToHex(16333));
  127. }
  128. @Test
  129. void testDumpToString() {
  130. byte[] testArray = testArray();
  131. String dump = HexDump.dump(testArray, 0, 0);
  132. //System.out.println("Hex: \n" + dump);
  133. assertTrue(dump.contains("0123456789:;<=>?"), "Had: \n" + dump);
  134. dump = HexDump.dump(testArray, 2, 1);
  135. //System.out.println("Hex: \n" + dump);
  136. assertTrue(dump.contains("123456789:;<=>?@"), "Had: \n" + dump);
  137. }
  138. @ParameterizedTest
  139. @ValueSource(ints = {-1, 2, 1})
  140. void testDumpToStringOutOfIndex1(int index) {
  141. assertThrows(ArrayIndexOutOfBoundsException.class, () ->
  142. HexDump.dump(new byte[1], 0, index));
  143. }
  144. @ParameterizedTest
  145. @ValueSource(ints = {0, 1})
  146. void testDumpToStringNoDataEOL(int index) {
  147. String s = HexDump.dump(new byte[0], 0, index);
  148. assertEquals("No Data", s.trim());
  149. }
  150. private static byte[] testArray() {
  151. byte[] testArray = new byte[ 256 ];
  152. for (int j = 0; j < 256; j++) {
  153. testArray[ j ] = ( byte ) j;
  154. }
  155. return testArray;
  156. }
  157. }