blob: e8cc9f64c6607baa384469f3363b1c9f9f8ea3eb [file] [log] [blame]
Joshua Bell7f567fa2015-01-13 17:46:571<!DOCTYPE html>
2<title>Encoding API: Fatal flag</title>
3<script src="/resources/testharness.js"></script>
4<script src="/resources/testharnessreport.js"></script>
Joshua Bell7f567fa2015-01-13 17:46:575<script>
6
7var bad = [
8 { encoding: 'utf-8', input: [0xFF], name: 'invalid code' },
9 { encoding: 'utf-8', input: [0xC0], name: 'ends early' },
10 { encoding: 'utf-8', input: [0xE0], name: 'ends early 2' },
11 { encoding: 'utf-8', input: [0xC0, 0x00], name: 'invalid trail' },
12 { encoding: 'utf-8', input: [0xC0, 0xC0], name: 'invalid trail 2' },
13 { encoding: 'utf-8', input: [0xE0, 0x00], name: 'invalid trail 3' },
14 { encoding: 'utf-8', input: [0xE0, 0xC0], name: 'invalid trail 4' },
15 { encoding: 'utf-8', input: [0xE0, 0x80, 0x00], name: 'invalid trail 5' },
16 { encoding: 'utf-8', input: [0xE0, 0x80, 0xC0], name: 'invalid trail 6' },
17 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: '> 0x10FFFF' },
18 { encoding: 'utf-8', input: [0xFE, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'obsolete lead byte' },
19
20 // Overlong encodings
21 { encoding: 'utf-8', input: [0xC0, 0x80], name: 'overlong U+0000 - 2 bytes' },
22 { encoding: 'utf-8', input: [0xE0, 0x80, 0x80], name: 'overlong U+0000 - 3 bytes' },
23 { encoding: 'utf-8', input: [0xF0, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 4 bytes' },
24 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 5 bytes' },
25 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x80, 0x80], name: 'overlong U+0000 - 6 bytes' },
26
27 { encoding: 'utf-8', input: [0xC1, 0xBF], name: 'overlong U+007F - 2 bytes' },
28 { encoding: 'utf-8', input: [0xE0, 0x81, 0xBF], name: 'overlong U+007F - 3 bytes' },
29 { encoding: 'utf-8', input: [0xF0, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 4 bytes' },
30 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 5 bytes' },
31 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x81, 0xBF], name: 'overlong U+007F - 6 bytes' },
32
33 { encoding: 'utf-8', input: [0xE0, 0x9F, 0xBF], name: 'overlong U+07FF - 3 bytes' },
34 { encoding: 'utf-8', input: [0xF0, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 4 bytes' },
35 { encoding: 'utf-8', input: [0xF8, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 5 bytes' },
36 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x80, 0x9F, 0xBF], name: 'overlong U+07FF - 6 bytes' },
37
38 { encoding: 'utf-8', input: [0xF0, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 4 bytes' },
39 { encoding: 'utf-8', input: [0xF8, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 5 bytes' },
40 { encoding: 'utf-8', input: [0xFC, 0x80, 0x80, 0x8F, 0xBF, 0xBF], name: 'overlong U+FFFF - 6 bytes' },
41
42 { encoding: 'utf-8', input: [0xF8, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 5 bytes' },
43 { encoding: 'utf-8', input: [0xFC, 0x80, 0x84, 0x8F, 0xBF, 0xBF], name: 'overlong U+10FFFF - 6 bytes' },
44
45 // UTF-16 surrogates encoded as code points in UTF-8
46 { encoding: 'utf-8', input: [0xED, 0xA0, 0x80], name: 'lead surrogate' },
47 { encoding: 'utf-8', input: [0xED, 0xB0, 0x80], name: 'trail surrogate' },
48 { encoding: 'utf-8', input: [0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80], name: 'surrogate pair' },
49
50 { encoding: 'utf-16le', input: [0x00], name: 'truncated code unit' },
51 // Mismatched UTF-16 surrogates are exercised in utf16-surrogates.html
52
53 // FIXME: Add legacy encoding cases
54];
55
56bad.forEach(function(t) {
57 test(function() {
Joshua Belld74324b2015-01-14 17:18:2058 assert_throws(new TypeError(), function() {
Joshua Bell7f567fa2015-01-13 17:46:5759 new TextDecoder(t.encoding, {fatal: true}).decode(new Uint8Array(t.input))
60 });
61 }, 'Fatal flag: ' + t.encoding + ' - ' + t.name);
62});
63
64test(function() {
65 assert_true('fatal' in new TextDecoder(), 'The fatal attribute should exist on TextDecoder.');
66 assert_equals(typeof new TextDecoder().fatal, 'boolean', 'The type of the fatal attribute should be boolean.');
67 assert_false(new TextDecoder().fatal, 'The fatal attribute should default to false.');
68 assert_true(new TextDecoder('utf-8', {fatal: true}).fatal, 'The fatal attribute can be set using an option.');
69
70}, 'The fatal attribute of TextDecoder');
71
72</script>