test_utils_convert_string.js (4039B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // A wise line of Greek verse, and the utf-8 byte encoding. 7 // N.b., Greek begins at utf-8 ce 91 8 const TEST_STR = "πόλλ' οἶδ' ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα"; 9 const TEST_HEX = h( 10 "cf 80 cf 8c ce bb ce bb 27 20 ce bf e1 bc b6 ce" + 11 "b4 27 20 e1 bc 80 ce bb cf 8e cf 80 ce b7 ce be" + 12 "2c 20 e1 bc 80 ce bb ce bb 27 20 e1 bc 90 cf 87" + 13 "e1 bf 96 ce bd ce bf cf 82 20 e1 bc 93 ce bd 20" + 14 "ce bc ce ad ce b3 ce b1" 15 ); 16 // Integer byte values for the above 17 const TEST_BYTES = [ 18 207, 128, 207, 140, 206, 187, 206, 187, 39, 32, 206, 191, 225, 188, 182, 206, 19 180, 39, 32, 225, 188, 128, 206, 187, 207, 142, 207, 128, 206, 183, 206, 190, 20 44, 32, 225, 188, 128, 206, 187, 206, 187, 39, 32, 225, 188, 144, 207, 135, 21 225, 191, 150, 206, 189, 206, 191, 207, 130, 32, 225, 188, 147, 206, 189, 32, 22 206, 188, 206, 173, 206, 179, 206, 177, 23 ]; 24 25 add_test(function test_compress_string() { 26 const INPUT = "hello"; 27 28 let result = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); 29 Assert.equal(result.length, 13); 30 31 let result2 = CommonUtils.convertString(INPUT, "uncompressed", "deflate"); 32 Assert.equal(result, result2); 33 34 let result3 = CommonUtils.convertString(result, "deflate", "uncompressed"); 35 Assert.equal(result3, INPUT); 36 37 run_next_test(); 38 }); 39 40 add_test(function test_compress_utf8() { 41 const INPUT = 42 "Árvíztűrő tükörfúrógép いろはにほへとちりぬるを Pijamalı hasta, yağız şoföre çabucak güvendi."; 43 let inputUTF8 = CommonUtils.encodeUTF8(INPUT); 44 45 let compressed = CommonUtils.convertString( 46 inputUTF8, 47 "uncompressed", 48 "deflate" 49 ); 50 let uncompressed = CommonUtils.convertString( 51 compressed, 52 "deflate", 53 "uncompressed" 54 ); 55 56 Assert.equal(uncompressed, inputUTF8); 57 58 let outputUTF8 = CommonUtils.decodeUTF8(uncompressed); 59 Assert.equal(outputUTF8, INPUT); 60 61 run_next_test(); 62 }); 63 64 add_test(function test_bad_argument() { 65 let failed = false; 66 try { 67 CommonUtils.convertString(null, "uncompressed", "deflate"); 68 } catch (ex) { 69 failed = true; 70 Assert.ok(ex.message.startsWith("Input string must be defined")); 71 } finally { 72 Assert.ok(failed); 73 } 74 75 run_next_test(); 76 }); 77 78 add_task(function test_stringAsHex() { 79 Assert.equal(TEST_HEX, CommonUtils.stringAsHex(TEST_STR)); 80 }); 81 82 add_task(function test_hexAsString() { 83 Assert.equal(TEST_STR, CommonUtils.hexAsString(TEST_HEX)); 84 }); 85 86 add_task(function test_hexToBytes() { 87 let bytes = CommonUtils.hexToBytes(TEST_HEX); 88 Assert.equal(TEST_BYTES.length, bytes.length); 89 // Ensure that the decimal values of each byte are correct 90 Assert.ok(arraysEqual(TEST_BYTES, CommonUtils.stringToByteArray(bytes))); 91 }); 92 93 add_task(function test_bytesToHex() { 94 // Create a list of our character bytes from the reference int values 95 let bytes = CommonUtils.byteArrayToString(TEST_BYTES); 96 Assert.equal(TEST_HEX, CommonUtils.bytesAsHex(bytes)); 97 }); 98 99 add_task(function test_stringToBytes() { 100 Assert.ok( 101 arraysEqual( 102 TEST_BYTES, 103 CommonUtils.stringToByteArray(CommonUtils.stringToBytes(TEST_STR)) 104 ) 105 ); 106 }); 107 108 add_task(function test_stringRoundTrip() { 109 Assert.equal( 110 TEST_STR, 111 CommonUtils.hexAsString(CommonUtils.stringAsHex(TEST_STR)) 112 ); 113 }); 114 115 add_task(function test_hexRoundTrip() { 116 Assert.equal( 117 TEST_HEX, 118 CommonUtils.stringAsHex(CommonUtils.hexAsString(TEST_HEX)) 119 ); 120 }); 121 122 add_task(function test_byteArrayRoundTrip() { 123 Assert.ok( 124 arraysEqual( 125 TEST_BYTES, 126 CommonUtils.stringToByteArray(CommonUtils.byteArrayToString(TEST_BYTES)) 127 ) 128 ); 129 }); 130 131 // turn formatted test vectors into normal hex strings 132 function h(hexStr) { 133 return hexStr.replace(/\s+/g, ""); 134 } 135 136 function arraysEqual(a1, a2) { 137 if (a1.length !== a2.length) { 138 return false; 139 } 140 for (let i = 0; i < a1.length; i++) { 141 if (a1[i] !== a2[i]) { 142 return false; 143 } 144 } 145 return true; 146 }