1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // nsIPrefBranch.{getCharPref,setCharPref} uses Latin-1 string, and
6 // nsIPrefBranch.{getStringPref,setStringPref} uses UTF-8 string.
8 // Mixing them results in unexpected string, but it should perform lossy
9 // conversion, and not throw.
11 const gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
14 // Latin-1 to Latin-1 and UTF-8 to UTF-8 should preserve the string.
15 // Latin-1 to UTF-8 should replace invalid character with REPLACEMENT
17 // UTF-8 to Latin1 should return the raw UTF-8 code units.
19 // UTF-8 code units sequence without the last unit.
21 // input, Latin-1 to UTF-8, UTF-8 to Latin-1
22 ["\xC2", "\uFFFD", "\xC3\x82"],
23 ["\xDF", "\uFFFD", "\xC3\x9F"],
24 ["\xE0\xA0", "\uFFFD", "\xC3\xA0\xC2\xA0"],
25 ["\xF0\x90\x80", "\uFFFD", "\xC3\xB0\xC2\x90\xC2\x80"],
27 // UTF-8 code units sequence with malformed last unit.
29 // input, Latin-1 to UTF-8, UTF-8 to Latin-1
30 ["\xC2 ", "\uFFFD ", "\xC3\x82 "],
31 ["\xDF ", "\uFFFD ", "\xC3\x9F "],
32 ["\xE0\xA0 ", "\uFFFD ", "\xC3\xA0\xC2\xA0 "],
33 ["\xF0\x90\x80 ", "\uFFFD ", "\xC3\xB0\xC2\x90\xC2\x80 "],
35 // UTF-8 code units without the first unit.
37 // input, Latin-1 to UTF-8, UTF-8 to Latin-1
38 ["\x80", "\uFFFD", "\xC2\x80"],
39 ["\xBF", "\uFFFD", "\xC2\xBF"],
40 ["\xA0\x80", "\uFFFD\uFFFD", "\xC2\xA0\xC2\x80"],
41 ["\x8F\x80\x80", "\uFFFD\uFFFD\uFFFD", "\xC2\x8F\xC2\x80\xC2\x80"],
44 add_task(function testLatin1ToLatin1() {
45 for (const [input, ] of tests) {
46 gPrefs.setCharPref("test.malformed_utf8_data", input);
47 const result = gPrefs.getCharPref("test.malformed_utf8_data");
48 Assert.equal(result, input);
52 add_task(function testLatin1ToUTF8() {
53 for (const [input, expected] of tests) {
54 gPrefs.setCharPref("test.malformed_utf8_data", input);
55 const result = gPrefs.getStringPref("test.malformed_utf8_data");
56 Assert.equal(result, expected);
60 add_task(function testUTF8ToLatin1() {
61 for (const [input, , expected] of tests) {
62 gPrefs.setStringPref("test.malformed_utf8_data", input);
63 const result = gPrefs.getCharPref("test.malformed_utf8_data");
64 Assert.equal(result, expected);
68 add_task(function testUTF8ToUTF8() {
69 for (const [input, ] of tests) {
70 gPrefs.setStringPref("test.malformed_utf8_data", input);
71 const result = gPrefs.getStringPref("test.malformed_utf8_data");
72 Assert.equal(result, input);