test_ArchiveUtils_countReplacementCharacters.js (2612B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { ArchiveUtils } = ChromeUtils.importESModule( 7 "resource:///modules/backup/ArchiveUtils.sys.mjs" 8 ); 9 10 const string = "🌞🌞🌞🌞"; 11 // F0 9F 8C 9E F0 9F 8C 9E F0 9F 8C 9E F0 9F 8C 9E 12 const buffer = new TextEncoder().encode(string); 13 14 const decoder = new TextDecoder(); 15 16 add_task(async function test_no_issues_decoding_back() { 17 let decoded = decoder.decode(buffer); 18 19 let count = ArchiveUtils.countReplacementCharacters(decoded); 20 21 Assert.equal( 22 decoded, 23 string, 24 "The string should decode back to the original" 25 ); 26 Assert.equal(count, 0, "There should have been no decoding issues"); 27 }); 28 29 add_task(async function test_3_replacements_if_cut_off_1() { 30 let decoded = decoder.decode(buffer.slice(1)); 31 32 let count = ArchiveUtils.countReplacementCharacters(decoded); 33 34 Assert.equal( 35 count, 36 3, 37 "The three bytes of the first character should have been replaced with �" 38 ); 39 Assert.equal( 40 decoded, 41 "\uFFFD\uFFFD\uFFFD🌞🌞🌞", 42 "The string should retain the characters that could be decoded" 43 ); 44 }); 45 46 add_task(async function test_2_replacements_if_cut_off_2() { 47 let decoded = decoder.decode(buffer.slice(2)); 48 49 let count = ArchiveUtils.countReplacementCharacters(decoded); 50 51 Assert.equal( 52 count, 53 2, 54 "The two bytes of the first character should have been replaced with �" 55 ); 56 Assert.equal( 57 decoded, 58 "\uFFFD\uFFFD🌞🌞🌞", 59 "The string should retain the characters that could be decoded" 60 ); 61 }); 62 63 add_task(async function test_1_replacement_if_cut_off_3() { 64 let decoded = decoder.decode(buffer.slice(3)); 65 66 let count = ArchiveUtils.countReplacementCharacters(decoded); 67 68 Assert.equal( 69 count, 70 1, 71 "The last byte of the first character should have been replaced with �" 72 ); 73 Assert.equal( 74 decoded, 75 "\uFFFD🌞🌞🌞", 76 "The string should retain the characters that could be decoded" 77 ); 78 }); 79 80 add_task(async function test_no_issues_cutting_off_whole_character() { 81 let decoded = decoder.decode(buffer.slice(4)); 82 83 let count = ArchiveUtils.countReplacementCharacters(decoded); 84 85 Assert.equal(count, 0, "There should be no replacement characters"); 86 Assert.equal( 87 decoded, 88 "🌞🌞🌞", 89 "The string should retain the characters that could be decoded" 90 ); 91 }); 92 93 add_task(async function test_robust_to_empty_string() { 94 let count = ArchiveUtils.countReplacementCharacters(""); 95 96 Assert.equal(count, 0, "There should be no replacement characters"); 97 });