1// META: title=Encoding API: Invalid UTF-16 surrogates with UTF-8 encoding 2 3var badStrings = [ 4 { 5 input: 'abc123', 6 expected: [0x61, 0x62, 0x63, 0x31, 0x32, 0x33], 7 decoded: 'abc123', 8 name: 'Sanity check' 9 }, 10 { 11 input: '\uD800', 12 expected: [0xef, 0xbf, 0xbd], 13 decoded: '\uFFFD', 14 name: 'Surrogate half (low)' 15 }, 16 { 17 input: '\uDC00', 18 expected: [0xef, 0xbf, 0xbd], 19 decoded: '\uFFFD', 20 name: 'Surrogate half (high)' 21 }, 22 { 23 input: 'abc\uD800123', 24 expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 25 decoded: 'abc\uFFFD123', 26 name: 'Surrogate half (low), in a string' 27 }, 28 { 29 input: 'abc\uDC00123', 30 expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 31 decoded: 'abc\uFFFD123', 32 name: 'Surrogate half (high), in a string' 33 }, 34 { 35 input: '\uDC00\uD800', 36 expected: [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd], 37 decoded: '\uFFFD\uFFFD', 38 name: 'Wrong order' 39 } 40]; 41 42badStrings.forEach(function(t) { 43 test(function() { 44 var encoded = new TextEncoder().encode(t.input); 45 assert_array_equals([].slice.call(encoded), t.expected); 46 assert_equals(new TextDecoder('utf-8').decode(encoded), t.decoded); 47 }, 'Invalid surrogates encoded into UTF-8: ' + t.name); 48}); 49