/src/test/java/com/alibaba/json/bvt/parser/TestUTF8_3.java

https://github.com/flydream/fastjson · Java · 221 lines · 167 code · 53 blank · 1 comment · 5 complexity · f730e32135ab5b8aff96d5fbb1f3319b MD5 · raw file

  1. package com.alibaba.json.bvt.parser;
  2. import java.nio.ByteBuffer;
  3. import java.nio.CharBuffer;
  4. import junit.framework.Assert;
  5. import junit.framework.TestCase;
  6. import com.alibaba.fastjson.util.IOUtils;
  7. import com.alibaba.fastjson.util.ThreadLocalCache;
  8. import com.alibaba.fastjson.util.UTF8Decoder;
  9. public class TestUTF8_3 extends TestCase {
  10. public void test_error_0() throws Exception {
  11. byte[] bytes = decodeHex("C2FF".toCharArray());
  12. UTF8Decoder charsetDecoder = new UTF8Decoder();
  13. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  14. char[] chars = ThreadLocalCache.getChars(scaleLength);
  15. CharBuffer charBuffer = CharBuffer.wrap(chars);
  16. Exception error = null;
  17. try {
  18. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  19. } catch (Exception ex) {
  20. error = ex;
  21. }
  22. Assert.assertNotNull(error);
  23. }
  24. public void test_error_2() throws Exception {
  25. byte[] bytes = decodeHex("F0A4ADFF".toCharArray());
  26. UTF8Decoder charsetDecoder = new UTF8Decoder();
  27. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  28. char[] chars = ThreadLocalCache.getChars(scaleLength);
  29. CharBuffer charBuffer = CharBuffer.wrap(chars);
  30. Exception error = null;
  31. try {
  32. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  33. } catch (Exception ex) {
  34. error = ex;
  35. }
  36. Assert.assertNotNull(error);
  37. }
  38. public void test_error_3() throws Exception {
  39. byte[] bytes = decodeHex("E280FF".toCharArray());
  40. UTF8Decoder charsetDecoder = new UTF8Decoder();
  41. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  42. char[] chars = ThreadLocalCache.getChars(scaleLength);
  43. CharBuffer charBuffer = CharBuffer.wrap(chars);
  44. Exception error = null;
  45. try {
  46. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  47. } catch (Exception ex) {
  48. error = ex;
  49. }
  50. Assert.assertNotNull(error);
  51. }
  52. public void test_error_4() throws Exception {
  53. byte[] bytes = decodeHex("80".toCharArray());
  54. UTF8Decoder charsetDecoder = new UTF8Decoder();
  55. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  56. char[] chars = ThreadLocalCache.getChars(scaleLength);
  57. CharBuffer charBuffer = CharBuffer.wrap(chars);
  58. Exception error = null;
  59. try {
  60. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  61. } catch (Exception ex) {
  62. error = ex;
  63. }
  64. Assert.assertNotNull(error);
  65. }
  66. public void test_error_5() throws Exception {
  67. byte[] bytes = decodeHex("FBBBF0".toCharArray());
  68. UTF8Decoder charsetDecoder = new UTF8Decoder();
  69. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  70. char[] chars = ThreadLocalCache.getChars(scaleLength);
  71. CharBuffer charBuffer = CharBuffer.wrap(chars);
  72. Exception error = null;
  73. try {
  74. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  75. } catch (Exception ex) {
  76. error = ex;
  77. }
  78. Assert.assertNotNull(error);
  79. }
  80. public void test_error_6() throws Exception {
  81. byte[] bytes = decodeHex("FCBBBF".toCharArray());
  82. UTF8Decoder charsetDecoder = new UTF8Decoder();
  83. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  84. char[] chars = ThreadLocalCache.getChars(scaleLength);
  85. CharBuffer charBuffer = CharBuffer.wrap(chars);
  86. Exception error = null;
  87. try {
  88. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  89. } catch (Exception ex) {
  90. error = ex;
  91. }
  92. Assert.assertNotNull(error);
  93. }
  94. public void test_error_7() throws Exception {
  95. byte[] bytes = decodeHex("F1808080".toCharArray());
  96. UTF8Decoder charsetDecoder = new UTF8Decoder();
  97. char[] chars = new char[1];
  98. CharBuffer charBuffer = CharBuffer.wrap(chars);
  99. Exception error = null;
  100. try {
  101. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  102. } catch (Exception ex) {
  103. error = ex;
  104. }
  105. Assert.assertNotNull(error);
  106. }
  107. public void test_error_8() throws Exception {
  108. byte[] bytes = decodeHex("E0B0B0E0B0B0".toCharArray());
  109. UTF8Decoder charsetDecoder = new UTF8Decoder();
  110. char[] chars = new char[1];
  111. CharBuffer charBuffer = CharBuffer.wrap(chars);
  112. Exception error = null;
  113. try {
  114. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  115. } catch (Exception ex) {
  116. error = ex;
  117. }
  118. Assert.assertNotNull(error);
  119. }
  120. public void test_error_9() throws Exception {
  121. byte[] bytes = decodeHex("C0B0".toCharArray());
  122. UTF8Decoder charsetDecoder = new UTF8Decoder();
  123. char[] chars = new char[1];
  124. CharBuffer charBuffer = CharBuffer.wrap(chars);
  125. Exception error = null;
  126. try {
  127. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  128. } catch (Exception ex) {
  129. error = ex;
  130. }
  131. Assert.assertNotNull(error);
  132. }
  133. public void f_test_utf_1() throws Exception {
  134. byte[] bytes = decodeHex("C2FF".toCharArray());
  135. String content = new String(bytes, "UTF-8");
  136. UTF8Decoder charsetDecoder = new UTF8Decoder();
  137. int scaleLength = (int) (bytes.length * (double) charsetDecoder.maxCharsPerByte());
  138. char[] chars = ThreadLocalCache.getChars(scaleLength);
  139. CharBuffer charBuffer = CharBuffer.wrap(chars);
  140. IOUtils.decode(charsetDecoder, ByteBuffer.wrap(bytes), charBuffer);
  141. charsetDecoder.decode(ByteBuffer.wrap(bytes), charBuffer, true);
  142. int position = charBuffer.position();
  143. Assert.assertEquals(content, new String(chars, 0, position));
  144. }
  145. public static byte[] decodeHex(char[] data) throws Exception {
  146. int len = data.length;
  147. if ((len & 0x01) != 0) {
  148. throw new Exception("Odd number of characters.");
  149. }
  150. byte[] out = new byte[len >> 1];
  151. // two characters form the hex value.
  152. for (int i = 0, j = 0; j < len; i++) {
  153. int f = toDigit(data[j], j) << 4;
  154. j++;
  155. f = f | toDigit(data[j], j);
  156. j++;
  157. out[i] = (byte) (f & 0xFF);
  158. }
  159. return out;
  160. }
  161. protected static int toDigit(char ch, int index) throws Exception {
  162. int digit = Character.digit(ch, 16);
  163. if (digit == -1) {
  164. throw new Exception("Illegal hexadecimal character " + ch + " at index " + index);
  165. }
  166. return digit;
  167. }
  168. }