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

ZipEncodingTest.java 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * http://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. package org.apache.tools.zip;
  20. import java.io.IOException;
  21. import java.nio.ByteBuffer;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertFalse;
  25. import static org.junit.Assert.assertTrue;
  26. /**
  27. * Test zip encodings.
  28. */
  29. public class ZipEncodingTest {
  30. private static final String UNENC_STRING = "\u2016";
  31. // stress test for internal grow method.
  32. private static final String BAD_STRING =
  33. "\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016";
  34. private static final String BAD_STRING_ENC =
  35. "%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016";
  36. @Test
  37. public void testSimpleCp437Encoding() throws IOException {
  38. doSimpleEncodingTest("Cp437", null);
  39. }
  40. @Test
  41. public void testSimpleCp850Encoding() throws IOException {
  42. doSimpleEncodingTest("Cp850", null);
  43. }
  44. @Test
  45. public void testNioCp1252Encoding() throws IOException {
  46. // CP1252 has some undefined code points, these are
  47. // the defined ones
  48. // retrieved by
  49. // awk '/^0x/ && NF>2 {print $1;}' CP1252.TXT
  50. byte[] b = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  52. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  53. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  54. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  55. 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
  56. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  57. 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  58. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  59. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  60. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  61. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
  62. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  63. 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
  64. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  65. 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
  66. (byte) 0x80, (byte) 0x82, (byte) 0x83, (byte) 0x84,
  67. (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88,
  68. (byte) 0x89, (byte) 0x8A, (byte) 0x8B, (byte) 0x8C,
  69. (byte) 0x8E, (byte) 0x91, (byte) 0x92, (byte) 0x93,
  70. (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97,
  71. (byte) 0x98, (byte) 0x99, (byte) 0x9A, (byte) 0x9B,
  72. (byte) 0x9C, (byte) 0x9E, (byte) 0x9F, (byte) 0xA0,
  73. (byte) 0xA1, (byte) 0xA2, (byte) 0xA3, (byte) 0xA4,
  74. (byte) 0xA5, (byte) 0xA6, (byte) 0xA7, (byte) 0xA8,
  75. (byte) 0xA9, (byte) 0xAA, (byte) 0xAB, (byte) 0xAC,
  76. (byte) 0xAD, (byte) 0xAE, (byte) 0xAF, (byte) 0xB0,
  77. (byte) 0xB1, (byte) 0xB2, (byte) 0xB3, (byte) 0xB4,
  78. (byte) 0xB5, (byte) 0xB6, (byte) 0xB7, (byte) 0xB8,
  79. (byte) 0xB9, (byte) 0xBA, (byte) 0xBB, (byte) 0xBC,
  80. (byte) 0xBD, (byte) 0xBE, (byte) 0xBF, (byte) 0xC0,
  81. (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4,
  82. (byte) 0xC5, (byte) 0xC6, (byte) 0xC7, (byte) 0xC8,
  83. (byte) 0xC9, (byte) 0xCA, (byte) 0xCB, (byte) 0xCC,
  84. (byte) 0xCD, (byte) 0xCE, (byte) 0xCF, (byte) 0xD0,
  85. (byte) 0xD1, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4,
  86. (byte) 0xD5, (byte) 0xD6, (byte) 0xD7, (byte) 0xD8,
  87. (byte) 0xD9, (byte) 0xDA, (byte) 0xDB, (byte) 0xDC,
  88. (byte) 0xDD, (byte) 0xDE, (byte) 0xDF, (byte) 0xE0,
  89. (byte) 0xE1, (byte) 0xE2, (byte) 0xE3, (byte) 0xE4,
  90. (byte) 0xE5, (byte) 0xE6, (byte) 0xE7, (byte) 0xE8,
  91. (byte) 0xE9, (byte) 0xEA, (byte) 0xEB, (byte) 0xEC,
  92. (byte) 0xED, (byte) 0xEE, (byte) 0xEF, (byte) 0xF0,
  93. (byte) 0xF1, (byte) 0xF2, (byte) 0xF3, (byte) 0xF4,
  94. (byte) 0xF5, (byte) 0xF6, (byte) 0xF7, (byte) 0xF8,
  95. (byte) 0xF9, (byte) 0xFA, (byte) 0xFB, (byte) 0xFC,
  96. (byte) 0xFD, (byte) 0xFE, (byte) 0xFF};
  97. doSimpleEncodingTest("Cp1252", b);
  98. }
  99. private static void assertByteEquals(byte[] expected, ByteBuffer actual) {
  100. assertEquals(expected.length, actual.limit());
  101. for (byte expectedByte : expected) {
  102. assertEquals(expectedByte, actual.get());
  103. }
  104. }
  105. private void doSimpleEncodingTest(String name, byte[] testBytes)
  106. throws IOException {
  107. ZipEncoding enc = ZipEncodingHelper.getZipEncoding(name);
  108. if (testBytes == null) {
  109. testBytes = new byte[256];
  110. for (int i = 0; i < 256; ++i) {
  111. testBytes[i] = (byte) i;
  112. }
  113. }
  114. String decoded = enc.decode(testBytes);
  115. assertTrue(enc.canEncode(decoded));
  116. ByteBuffer encoded = enc.encode(decoded);
  117. assertByteEquals(testBytes, encoded);
  118. assertFalse(enc.canEncode(UNENC_STRING));
  119. assertByteEquals("%U2016".getBytes("US-ASCII"), enc.encode(UNENC_STRING));
  120. assertFalse(enc.canEncode(BAD_STRING));
  121. assertByteEquals(BAD_STRING_ENC.getBytes("US-ASCII"),
  122. enc.encode(BAD_STRING));
  123. }
  124. }