Fix atob() base64-encode in testsuite. Bug 534781, r=jcranmer, sr=standard8
authorBen Bucksch <ben.bucksch@beonex.com>
Mon, 01 Feb 2010 16:58:59 +0100
changeset 4795 69c6658a1102be19d0df171a17533e284ae72162
parent 4794 701e06745f4440215be0ff00732d164ef80e9aa8
child 4797 49e4963153a2e7e25a0a1534b8d078c4c7505c37
push idunknown
push userunknown
push dateunknown
reviewersjcranmer, standard8
bugs534781
Fix atob() base64-encode in testsuite. Bug 534781, r=jcranmer, sr=standard8
mailnews/base/test/unit/test_testsuite_base64.js
mailnews/test/resources/mailTestUtils.js
new file mode 100644
--- /dev/null
+++ b/mailnews/base/test/unit/test_testsuite_base64.js
@@ -0,0 +1,23 @@
+/**
+ * Tests functions atob() and btoa() in mailnews/test/resources/mailTestUtils.js .
+ *
+ * Note:
+ * btoa() = base64 encode
+ * atob() = base64 decode
+ * (i.e. "binary" = plain, and "ascii" = encoded)
+ */
+
+function run_test()
+{
+  var plain = "testtesttest";
+  var encoded = "dGVzdHRlc3R0ZXN0";
+
+  // correct encoding according to spec
+  do_check_eq(btoa(plain), encoded); // encode
+  do_check_eq(atob(encoded), plain); // decode
+
+  // roundtrip works
+  do_check_eq(atob(btoa(plain)), plain);
+  do_check_eq(btoa(atob(encoded)), encoded);
+  return true;
+};
--- a/mailnews/test/resources/mailTestUtils.js
+++ b/mailnews/test/resources/mailTestUtils.js
@@ -64,17 +64,19 @@ function loadLocalMailAccount()
   // a local inbox should have a Mail flag!
   gLocalInboxFolder.setFlag(Ci.nsMsgFolderFlags.Mail);
 
   // Force an initialization of the Inbox folder database.
   var folderName = gLocalInboxFolder.prettiestName;
 }
 
 /**
- * Converts a base64-encoded to a string with the octet data.
+ * atob() = base64 decode
+ * Converts a base64-encoded string to a string with the octet data.
+ * @see RFC 4648
  *
  * The extra parameters are optional arguments that are used to override the
  * official base64 characters for values 62 and 63. If not specified, they
  * default to '+' and '/'.
  *
  * No unicode translation is performed during the conversion.
  *
  * @param str    A string argument representing the encoded data
@@ -92,19 +94,19 @@ function atob(str, c62, c63) {
     let val = 0;
     if (65 <= c && c <= 90) // A-Z
       val = c-65;
     else if (97 <= c && c <= 122) // a-z
       val = c-97+26;
     else if (48 <= c && c <= 57) // 0-9
       val = c-48+52;
     else if (c == c62)
-      value = 62;
+      val = 62;
     else if (c == c63)
-      value = 63;
+      val = 63;
     else if (c == 61) {
       for (var q=i+1;q<str.length;q++)
         if (str[q] != '=')
           throw "Character after =: "+str[q];
       break;
     } else
       throw "Illegal character in input: "+c;
     bits.push((val >> 5) & 1);
@@ -113,21 +115,23 @@ function atob(str, c62, c63) {
     bits.push((val >> 2) & 1);
     bits.push((val >> 1) & 1);
     bits.push((val >> 0) & 1);
     if (bits.length >= 8)
       result.push(bits.splice(0, 8).reduce(function (form, bit) {
         return (form << 1) | bit;
       }, 0));
   }
-  return result.reduce(function (str, c) { return str+c }, "");
+  return result.reduce(function (str, c) { return str + String.fromCharCode(c); }, "");
 }
 
 /**
+ * btoa() = base64 encode
  * Converts a string or array of octets to a base64-encoded string.
+ * @see RFC 4648
  *
  * The extra parameters are optional arguments that are used to override the
  * official base64 characters for values 62 and 63. If not specified, they
  * default to '+' and '/'.
  *
  * Data is treated as if it were modulo 256.
  *
  * @param str    A string or array with the data to be encoded