/*
* test_TextDecoderBOMEncoding . js
* bug 764234 tests
*/
/* eslint-env mozilla/testharness */
function runTextDecoderBOMEnoding() {
test(testDecodeValidBOMUTF16, "testDecodeValidBOMUTF16" );
test(testBOMEncodingUTF8, "testBOMEncodingUTF8" );
test(testMoreBOMEncoding, "testMoreBOMEncoding" );
}
function testDecodeValidBOMUTF16() {
var expectedString =
'"\u0412\u0441\u0435 \u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u044B\u0435 \u0441\u0435\u043C\u044C\u0438 \u043F\u043E\u0445\u043E\u0436\u0438 \u0434\u0440\u0443\u0433 \u043D\u0430 \u0434\u0440\u0443\u0433\u0430, \u043A\u0430\u0436\u0434\u0430\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430\u044F \u0441\u0435\u043C\u044C\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430 \u043F\u043E-\u0441\u0432\u043E\u0435\u043C\u0443."' ;
// Testing UTF-16BE
var data = [
0 xfe, 0 xff, 0 x00, 0 x22, 0 x04, 0 x12, 0 x04, 0 x41, 0 x04, 0 x35, 0 x00, 0 x20,
0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b,
0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x4b, 0 x04, 0 x35, 0 x00, 0 x20, 0 x04, 0 x41,
0 x04, 0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x38, 0 x00, 0 x20, 0 x04, 0 x3f,
0 x04, 0 x3e, 0 x04, 0 x45, 0 x04, 0 x3e, 0 x04, 0 x36, 0 x04, 0 x38, 0 x00, 0 x20,
0 x04, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x00, 0 x20, 0 x04, 0 x3d,
0 x04, 0 x30, 0 x00, 0 x20, 0 x04, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33,
0 x04, 0 x30, 0 x00, 0 x2c, 0 x00, 0 x20, 0 x04, 0 x3a, 0 x04, 0 x30, 0 x04, 0 x36,
0 x04, 0 x34, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x3d, 0 x04, 0 x35,
0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b,
0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x41,
0 x04, 0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x3d,
0 x04, 0 x35, 0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42,
0 x04, 0 x3b, 0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x00, 0 x20, 0 x04, 0 x3f,
0 x04, 0 x3e, 0 x00, 0 x2d, 0 x04, 0 x41, 0 x04, 0 x32, 0 x04, 0 x3e, 0 x04, 0 x35,
0 x04, 0 x3c, 0 x04, 0 x43, 0 x00, 0 x2e, 0 x00, 0 x22,
];
testBOMCharset({
encoding: "utf-16be" ,
data,
expected: expectedString,
msg: "decoder valid UTF-16BE test." ,
});
}
function testBOMEncodingUTF8() {
// basic utf-8 test with valid encoding and byte stream. no byte om provided.
var data = [0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27];
var expectedString = " !\" #$%&'";
testBOMCharset({
encoding: "utf-8" ,
data,
expected: expectedString,
msg: "utf-8 encoding." ,
});
// test valid encoding provided with valid byte OM also provided.
data = [0 xef, 0 xbb, 0 xbf, 0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27];
expectedString = " !\" #$%&'";
testBOMCharset({
encoding: "utf-8" ,
data,
expected: expectedString,
msg: "valid utf-8 encoding provided with VALID utf-8 BOM test." ,
});
// test valid encoding provided with invalid byte OM also provided.
data = [0 xff, 0 xfe, 0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27];
testBOMCharset({
encoding: "utf-8" ,
fatal: true ,
data,
error: "TypeError" ,
msg: "valid utf-8 encoding provided with invalid utf-8 fatal BOM test." ,
});
// test valid encoding provided with invalid byte OM also provided.
data = [0 xff, 0 xfe, 0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27];
expectedString = "\ufffd\ufffd !\" #$%&'";
testBOMCharset({
encoding: "utf-8" ,
data,
expected: expectedString,
msg: "valid utf-8 encoding provided with invalid utf-8 BOM test." ,
});
// test empty encoding provided with invalid byte OM also provided.
data = [0 xff, 0 xfe, 0 x20, 0 x21, 0 x22, 0 x23, 0 x24, 0 x25, 0 x26, 0 x27];
testBOMCharset({
encoding: "" ,
data,
error: "RangeError" ,
msg: "empty encoding provided with invalid utf-8 BOM test." ,
});
}
function testMoreBOMEncoding() {
var expectedString =
'"\u0412\u0441\u0435 \u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u044B\u0435 \u0441\u0435\u043C\u044C\u0438 \u043F\u043E\u0445\u043E\u0436\u0438 \u0434\u0440\u0443\u0433 \u043D\u0430 \u0434\u0440\u0443\u0433\u0430, \u043A\u0430\u0436\u0434\u0430\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430\u044F \u0441\u0435\u043C\u044C\u044F \u043D\u0435\u0441\u0447\u0430\u0441\u0442\u043B\u0438\u0432\u0430 \u043F\u043E-\u0441\u0432\u043E\u0435\u043C\u0443."' ;
// Testing user provided encoding is UTF-16BE & bom encoding is utf-16le
var data = [
0 xff, 0 xfe, 0 x00, 0 x22, 0 x04, 0 x12, 0 x04, 0 x41, 0 x04, 0 x35, 0 x00, 0 x20,
0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b,
0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x4b, 0 x04, 0 x35, 0 x00, 0 x20, 0 x04, 0 x41,
0 x04, 0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x38, 0 x00, 0 x20, 0 x04, 0 x3f,
0 x04, 0 x3e, 0 x04, 0 x45, 0 x04, 0 x3e, 0 x04, 0 x36, 0 x04, 0 x38, 0 x00, 0 x20,
0 x04, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x00, 0 x20, 0 x04, 0 x3d,
0 x04, 0 x30, 0 x00, 0 x20, 0 x04, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33,
0 x04, 0 x30, 0 x00, 0 x2c, 0 x00, 0 x20, 0 x04, 0 x3a, 0 x04, 0 x30, 0 x04, 0 x36,
0 x04, 0 x34, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x3d, 0 x04, 0 x35,
0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b,
0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x41,
0 x04, 0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x4f, 0 x00, 0 x20, 0 x04, 0 x3d,
0 x04, 0 x35, 0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42,
0 x04, 0 x3b, 0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x00, 0 x20, 0 x04, 0 x3f,
0 x04, 0 x3e, 0 x00, 0 x2d, 0 x04, 0 x41, 0 x04, 0 x32, 0 x04, 0 x3e, 0 x04, 0 x35,
0 x04, 0 x3c, 0 x04, 0 x43, 0 x00, 0 x2e, 0 x00, 0 x22,
];
testBOMCharset({
encoding: "utf-16be" ,
fatal: true ,
data,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16be fatal." ,
});
testBOMCharset({
encoding: "utf-16be" ,
data,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16be." ,
});
// Testing user provided encoding is UTF-16LE & bom encoding is utf-16be
var dataUTF16 = [
0 xfe, 0 xff, 0 x22, 0 x00, 0 x12, 0 x04, 0 x41, 0 x04, 0 x35, 0 x04, 0 x20, 0 x00,
0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b, 0 x04,
0 x38, 0 x04, 0 x32, 0 x04, 0 x4b, 0 x04, 0 x35, 0 x04, 0 x20, 0 x00, 0 x41, 0 x04,
0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x38, 0 x04, 0 x20, 0 x00, 0 x3f, 0 x04,
0 x3e, 0 x04, 0 x45, 0 x04, 0 x3e, 0 x04, 0 x36, 0 x04, 0 x38, 0 x04, 0 x20, 0 x00,
0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04,
0 x30, 0 x04, 0 x20, 0 x00, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x04,
0 x30, 0 x04, 0 x2c, 0 x00, 0 x20, 0 x00, 0 x3a, 0 x04, 0 x30, 0 x04, 0 x36, 0 x04,
0 x34, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04, 0 x35, 0 x04,
0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b, 0 x04,
0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x41, 0 x04,
0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04,
0 x35, 0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04,
0 x3b, 0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x20, 0 x00, 0 x3f, 0 x04,
0 x3e, 0 x04, 0 x2d, 0 x00, 0 x41, 0 x04, 0 x32, 0 x04, 0 x3e, 0 x04, 0 x35, 0 x04,
0 x3c, 0 x04, 0 x43, 0 x04, 0 x2e, 0 x00, 0 x22, 0 x00,
];
testBOMCharset({
encoding: "utf-16le" ,
fatal: true ,
data: dataUTF16,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16le fatal." ,
});
testBOMCharset({
encoding: "utf-16le" ,
data: dataUTF16,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16le." ,
});
// Testing user provided encoding is UTF-16 & bom encoding is utf-16be
testBOMCharset({
encoding: "utf-16" ,
fatal: true ,
data: dataUTF16,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16 fatal." ,
});
testBOMCharset({
encoding: "utf-16" ,
data: dataUTF16,
expected: "\ufffe" + expectedString,
msg: "test decoder invalid BOM encoding for utf-16." ,
});
// Testing user provided encoding is UTF-16 & bom encoding is utf-16le
dataUTF16 = [
0 xff, 0 xfe, 0 x22, 0 x00, 0 x12, 0 x04, 0 x41, 0 x04, 0 x35, 0 x04, 0 x20, 0 x00,
0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b, 0 x04,
0 x38, 0 x04, 0 x32, 0 x04, 0 x4b, 0 x04, 0 x35, 0 x04, 0 x20, 0 x00, 0 x41, 0 x04,
0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x38, 0 x04, 0 x20, 0 x00, 0 x3f, 0 x04,
0 x3e, 0 x04, 0 x45, 0 x04, 0 x3e, 0 x04, 0 x36, 0 x04, 0 x38, 0 x04, 0 x20, 0 x00,
0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04,
0 x30, 0 x04, 0 x20, 0 x00, 0 x34, 0 x04, 0 x40, 0 x04, 0 x43, 0 x04, 0 x33, 0 x04,
0 x30, 0 x04, 0 x2c, 0 x00, 0 x20, 0 x00, 0 x3a, 0 x04, 0 x30, 0 x04, 0 x36, 0 x04,
0 x34, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04, 0 x35, 0 x04,
0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04, 0 x3b, 0 x04,
0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x41, 0 x04,
0 x35, 0 x04, 0 x3c, 0 x04, 0 x4c, 0 x04, 0 x4f, 0 x04, 0 x20, 0 x00, 0 x3d, 0 x04,
0 x35, 0 x04, 0 x41, 0 x04, 0 x47, 0 x04, 0 x30, 0 x04, 0 x41, 0 x04, 0 x42, 0 x04,
0 x3b, 0 x04, 0 x38, 0 x04, 0 x32, 0 x04, 0 x30, 0 x04, 0 x20, 0 x00, 0 x3f, 0 x04,
0 x3e, 0 x04, 0 x2d, 0 x00, 0 x41, 0 x04, 0 x32, 0 x04, 0 x3e, 0 x04, 0 x35, 0 x04,
0 x3c, 0 x04, 0 x43, 0 x04, 0 x2e, 0 x00, 0 x22, 0 x00,
];
testBOMCharset({
encoding: "utf-16" ,
fatal: true ,
data: dataUTF16,
expected: expectedString,
msg: "test decoder BOM encoding for utf-16 fatal." ,
});
testBOMCharset({
encoding: "utf-16" ,
data: dataUTF16,
expected: expectedString,
msg: "test dec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82,
0 xd0, 0 xbb, 0 xd0, 0 xb8, 0 xd0, 0 xb2, 0 xd0, 0 xb0, 0 x20, 0 xd0, 0 xbf, 0 xd0,
0 xbe, 0 x2d, 0 xd1, 0 x81, 0 xd0, 0 xb2, 0 xd0, 0 xbe, 0 xd0, 0 xb5, 0 xd0, 0 xbc,
0 xd1, 0 x83, 0 x2e, 0 x22,
];
testBOMCharset({
encoding: "utf-8" ,
fatal: true ,
data,
error: "TypeError" ,
msg: "test decoder invalid BOM encoding for valid utf-8 fatal provided label." ,
});
testBOMCharset({
encoding: "utf-8" ,
data,
expected: "\ufffd\ufffd" + expectedString,
msg: "test decoder invalid BOM encoding for valid utf-8 provided label." ,
});
// Testing user provided encoding is non-UTF & bom encoding is utf-16be
data = [
0 xfe, 0 xff, 0 xa2, 0 xa3, 0 xa4, 0 xa5, 0 xa6, 0 xa7, 0 xa8, 0 xa9, 0 xaa, 0 xab,
0 xac, 0 xad, 0 xaf, 0 xb0, 0 xb1, 0 xb2, 0 xb3, 0 xb4, 0 xb5, 0 xb6, 0 xb7, 0 xb8,
0 xb9, 0 xba, 0 xbb, 0 xbc, 0 xbd, 0 xbe, 0 xbf, 0 xc0, 0 xc1, 0 xc2, 0 xc3, 0 xc4,
0 xc5, 0 xc6, 0 xc7, 0 xc8, 0 xc9, 0 xca, 0 xcb, 0 xcc, 0 xcd, 0 xce, 0 xcf, 0 xd0,
0 xd1, 0 xd3, 0 xd4, 0 xd5, 0 xd6, 0 xd7, 0 xd8, 0 xd9, 0 xda, 0 xdb, 0 xdc, 0 xdd,
0 xde, 0 xdf, 0 xe0, 0 xe1, 0 xe2, 0 xe3, 0 xe4, 0 xe5, 0 xe6, 0 xe7, 0 xe8, 0 xe9,
0 xea, 0 xeb, 0 xec, 0 xed, 0 xee, 0 xef, 0 xf0, 0 xf1, 0 xf2, 0 xf3, 0 xf4, 0 xf5,
0 xf6, 0 xf7, 0 xf8, 0 xf9, 0 xfa, 0 xfb, 0 xfc, 0 xfd, 0 xfe,
];
expectedString =
"\u03CE\uFFFD\u2019\xA3\u20AC\u20AF\xA6\xA7\xA8\xA9\u037A\xAB\xAC\xAD\u2015" +
"\xB0\xB1\xB2\xB3\u0384\u0385\u0386\xB7\u0388\u0389\u038A\xBB\u038C\xBD\u038E\u038F" +
"\u0390\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039A\u039B\u039C\u039D\u039E\u039F" +
"\u03A0\u03A1\u03A3\u03A4\u03A5\u03A6\u03A7\u03A8\u03A9\u03AA\u03AB\u03AC\u03AD\u03AE\u03AF" +
"\u03B0\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB\u03BC\u03BD\u03BE\u03BF" +
"\u03C0\u03C1\u03C2\u03C3\u03C4\u03C5\u03C6\u03C7\u03C8\u03C9\u03CA\u03CB\u03CC\u03CD\u03CE" ;
testBOMCharset({
encoding: "greek" ,
fatal: true ,
data,
error: "TypeError" ,
msg: "test decoder encoding provided with invalid BOM encoding for greek." ,
});
testBOMCharset({
encoding: "greek" ,
data,
expected: expectedString,
msg: "test decoder encoding provided with invalid BOM encoding for greek." ,
});
}
function testBOMCharset(test) {
var outText;
try {
var decoder =
"fatal" in test
? new TextDecoder(test.encoding, { fatal: test.fatal })
: new TextDecoder(test.encoding);
outText = decoder.decode(new Uint8Array(test.data));
} catch (e) {
assert_equals(e.name, test.error, test.msg);
return ;
}
assert_true(!test.error, test.msg);
if (outText !== test.expected) {
assert_equals(
escape(outText),
escape(test.expected),
test.msg + " Code points do not match expected code points."
);
}
}
Messung V0.5 in Prozent C=96 H=100 G=97