OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library utf8_test; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'dart:convert'; |
| 8 import 'dart:codec'; |
| 9 |
| 10 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); |
| 11 String decodeAllowMalformed(List<int> bytes) { |
| 12 return new Utf8Decoder(allowMalformed: true).convert(bytes); |
| 13 } |
| 14 |
| 15 String decode2(List<int> bytes) => UTF8.decode(bytes); |
| 16 String decodeAllowMalformed2(List<int> bytes) { |
| 17 return UTF8.decode(bytes, allowMalformed: true); |
| 18 } |
| 19 |
| 20 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); |
| 21 String decodeAllowMalformed3(List<int> bytes) { |
| 22 return new Utf8Codec(allowMalformed: true).decode(bytes); |
| 23 } |
| 24 |
| 25 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); |
| 26 String decodeAllowMalformed4(List<int> bytes) { |
| 27 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); |
| 28 } |
| 29 |
| 30 final TESTS = [ |
| 31 // Unfinished UTF-8 sequences. |
| 32 [ 0xc3 ], |
| 33 [ 0xE2, 0x82 ], |
| 34 [ 0xF0, 0xA4, 0xAD ], |
| 35 // Overlong encoding of euro-sign. |
| 36 [ 0xF0, 0x82, 0x82, 0xAC ], |
| 37 // Other overlong/unfinished sequences. |
| 38 [ 0xC0 ], |
| 39 [ 0xC1 ], |
| 40 [ 0xF5 ], |
| 41 [ 0xF6 ], |
| 42 [ 0xF7 ], |
| 43 [ 0xF8 ], |
| 44 [ 0xF9 ], |
| 45 [ 0xFA ], |
| 46 [ 0xFB ], |
| 47 [ 0xFC ], |
| 48 [ 0xFD ], |
| 49 [ 0xFE ], |
| 50 [ 0xFF ], |
| 51 [ 0xC0, 0x80 ], |
| 52 [ 0xC1, 0x80 ], |
| 53 // Outside valid range. |
| 54 [ 0xF4, 0xBF, 0xBF, 0xBF ]]; |
| 55 |
| 56 final TESTS2 = [ |
| 57 // Test that 0xC0|1, 0x80 does not eat the next character. |
| 58 [[ 0xC0, 0x80, 0x61 ], "Xa" ], |
| 59 [[ 0xC1, 0x80, 0x61 ], "Xa" ], |
| 60 // 0xF5 .. 0xFF never appear in valid UTF-8 sequences. |
| 61 [[ 0xF5, 0x80 ], "XX" ], |
| 62 [[ 0xF6, 0x80 ], "XX" ], |
| 63 [[ 0xF7, 0x80 ], "XX" ], |
| 64 [[ 0xF8, 0x80 ], "XX" ], |
| 65 [[ 0xF9, 0x80 ], "XX" ], |
| 66 [[ 0xFA, 0x80 ], "XX" ], |
| 67 [[ 0xFB, 0x80 ], "XX" ], |
| 68 [[ 0xFC, 0x80 ], "XX" ], |
| 69 [[ 0xFD, 0x80 ], "XX" ], |
| 70 [[ 0xFE, 0x80 ], "XX" ], |
| 71 [[ 0xFF, 0x80 ], "XX" ], |
| 72 [[ 0xF5, 0x80, 0x61 ], "XXa" ], |
| 73 [[ 0xF6, 0x80, 0x61 ], "XXa" ], |
| 74 [[ 0xF7, 0x80, 0x61 ], "XXa" ], |
| 75 [[ 0xF8, 0x80, 0x61 ], "XXa" ], |
| 76 [[ 0xF9, 0x80, 0x61 ], "XXa" ], |
| 77 [[ 0xFA, 0x80, 0x61 ], "XXa" ], |
| 78 [[ 0xFB, 0x80, 0x61 ], "XXa" ], |
| 79 [[ 0xFC, 0x80, 0x61 ], "XXa" ], |
| 80 [[ 0xFD, 0x80, 0x61 ], "XXa" ], |
| 81 [[ 0xFE, 0x80, 0x61 ], "XXa" ], |
| 82 [[ 0xFF, 0x80, 0x61 ], "XXa" ], |
| 83 // Characters outside the valid range. |
| 84 [[ 0xF5, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 85 [[ 0xF6, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 86 [[ 0xF7, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 87 [[ 0xF8, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 88 [[ 0xF9, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 89 [[ 0xFA, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 90 [[ 0xFB, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 91 [[ 0xFC, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 92 [[ 0xFD, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 93 [[ 0xFE, 0x80, 0x80, 0x61 ], "XXXa" ], |
| 94 [[ 0xFF, 0x80, 0x80, 0x61 ], "XXXa" ]]; |
| 95 |
| 96 main() { |
| 97 var allTests = TESTS.expand((test) { |
| 98 // Pairs of test and expected string output when malformed strings are |
| 99 // allowed. Replacement character: U+FFFD |
| 100 return [[ test, "\u{FFFD}" ], |
| 101 [ new List.from([0x61])..addAll(test), "a\u{FFFD}" ], |
| 102 [ new List.from([0x61])..addAll(test)..add(0x61), "a\u{FFFD}a" ], |
| 103 [ new List.from(test)..add(0x61), "\u{FFFD}a" ], |
| 104 [ new List.from(test)..addAll(test), "\u{FFFD}\u{FFFD}" ], |
| 105 [ new List.from(test)..add(0x61)..addAll(test), |
| 106 "\u{FFFD}a\u{FFFD}" ], |
| 107 [ new List.from([0xc3, 0xa5])..addAll(test), "å\u{FFFD}" ], |
| 108 [ new List.from([0xc3, 0xa5])..addAll(test)..addAll([0xc3, 0xa5]), |
| 109 "å\u{FFFD}å" ], |
| 110 [ new List.from(test)..addAll([0xc3, 0xa5]), "\u{FFFD}å" ], |
| 111 [ new List.from(test)..addAll([0xc3, 0xa5])..addAll(test), |
| 112 "\u{FFFD}å\u{FFFD}" ]]; |
| 113 }); |
| 114 |
| 115 var allTests2 = TESTS2.map((test) { |
| 116 // Pairs of test and expected string output when malformed strings are |
| 117 // allowed. Replacement character: U+FFFD |
| 118 String expected = test[1].replaceAll("X", "\u{FFFD}"); |
| 119 return [test[0], expected]; |
| 120 }); |
| 121 |
| 122 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
| 123 List<int> bytes = test[0]; |
| 124 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
| 125 Expect.throws(() => decode2(bytes), (e) => e is FormatException); |
| 126 Expect.throws(() => decode3(bytes), (e) => e is FormatException); |
| 127 Expect.throws(() => decode4(bytes), (e) => e is FormatException); |
| 128 |
| 129 String expected = test[1]; |
| 130 Expect.equals(expected, decodeAllowMalformed(bytes)); |
| 131 Expect.equals(expected, decodeAllowMalformed2(bytes)); |
| 132 Expect.equals(expected, decodeAllowMalformed3(bytes)); |
| 133 Expect.equals(expected, decodeAllowMalformed4(bytes)); |
| 134 } |
| 135 } |
OLD | NEW |