Bug 1305531 - Add test cases to test_sdr.js to ensure recommended way of using encryptString() and decryptString() works. r=keeler draft
authorCykesiopka <cykesiopka.bmo@gmail.com>
Sat, 01 Oct 2016 09:29:29 +0800
changeset 419841 6aba7627e3c0c79ea633933ce7f140c20b16c9d9
parent 419840 514e78f2e1c2cef3b3692656b20daf3b068a4fee
child 532660 2e9146619cc03fd2ac4b39fea663f7daa0061aa1
push id31023
push usercykesiopka.bmo@gmail.com
push dateSat, 01 Oct 2016 01:30:33 +0000
reviewerskeeler
bugs1305531
milestone52.0a1
Bug 1305531 - Add test cases to test_sdr.js to ensure recommended way of using encryptString() and decryptString() works. r=keeler MozReview-Commit-ID: 5vPxUZyzGSz
security/manager/ssl/tests/unit/test_sdr.js
--- a/security/manager/ssl/tests/unit/test_sdr.js
+++ b/security/manager/ssl/tests/unit/test_sdr.js
@@ -29,32 +29,42 @@ function run_test() {
   loginToDBWithDefaultPassword();
 
   let sdr = Cc["@mozilla.org/security/sdr;1"]
               .getService(Ci.nsISecretDecoderRing);
 
   // Test valid inputs for encryptString() and decryptString().
   let inputs = [
     "",
+    " ", // First printable latin1 character (code point 32).
     "foo",
-    "1234567890`~!#$%^&*()-_=+{[}]|\\:;'\",<.>/?",
+    "1234567890`~!@#$%^&*()-_=+{[}]|\\:;'\",<.>/?",
+    "¡äöüÿ", // Misc + last printable latin1 character (code point 255).
+    "aaa 一二三", // Includes Unicode with code points outside [0, 255].
   ];
   for (let input of inputs) {
-    let encrypted = sdr.encryptString(input);
+    let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
+                      .createInstance(Ci.nsIScriptableUnicodeConverter);
+    converter.charset = "UTF-8";
 
-    notEqual(input, encrypted,
-             "Encypted input should not just be the input itself");
+    let convertedInput = converter.ConvertFromUnicode(input);
+    convertedInput += converter.Finish();
+
+    let encrypted = sdr.encryptString(convertedInput);
+
+    notEqual(convertedInput, encrypted,
+             "Encrypted input should not just be the input itself");
 
     try {
       atob(encrypted);
     } catch (e) {
       ok(false, `encryptString() should have returned Base64: ${e}`);
     }
 
-    equal(input, sdr.decryptString(encrypted),
+    equal(convertedInput, sdr.decryptString(encrypted),
           "decryptString(encryptString(input)) should return input");
   }
 
   // Test invalid inputs for decryptString().
   throws(() => sdr.decryptString("*"), /NS_ERROR_ILLEGAL_VALUE/,
          "decryptString() should throw if given non-Base64 input");
 
   // Test calling changePassword() pops up the appropriate dialog.