Bug 743413 - Move some utility functions from sync to common; r=rnewman

--HG--
rename : services/sync/tests/unit/test_utils_atob.js => services/common/tests/unit/test_utils_atob.js
rename : services/sync/tests/unit/test_utils_utf8.js => services/common/tests/unit/test_utils_utf8.js
This commit is contained in:
Gregory Szorc 2012-04-09 15:39:59 -07:00
parent 49412510e0
commit 889eca9a49
8 changed files with 63 additions and 45 deletions

View File

@ -0,0 +1,11 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://services-common/utils.js");
function run_test() {
let data = ["Zm9vYmE=", "Zm9vYmE==", "Zm9vYmE==="];
for (let d in data) {
do_check_eq(CommonUtils.safeAtoB(data[d]), "fooba");
}
}

View File

@ -0,0 +1,11 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://services-common/utils.js");
function run_test() {
let str = "Umlaute: \u00FC \u00E4\n"; // Umlaute: ü ä
let encoded = CommonUtils.encodeUTF8(str);
let decoded = CommonUtils.decodeUTF8(encoded);
do_check_eq(decoded, str);
}

View File

@ -5,9 +5,11 @@ tail =
# Test load modules first so syntax failures are caught early.
[test_load_modules.js]
[test_utils_atob.js]
[test_utils_makeURI.js]
[test_utils_namedTimer.js]
[test_utils_stackTrace.js]
[test_utils_utf8.js]
[test_async_chain.js]
[test_async_querySpinningly.js]

View File

@ -7,6 +7,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const EXPORTED_SYMBOLS = ["CommonUtils"];
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://services-common/log4moz.js");
let CommonUtils = {
@ -122,4 +123,39 @@ let CommonUtils = {
return thisObj[name] = timer;
},
encodeUTF8: function encodeUTF8(str) {
try {
str = this._utf8Converter.ConvertFromUnicode(str);
return str + this._utf8Converter.Finish();
} catch (ex) {
return null;
}
},
decodeUTF8: function decodeUTF8(str) {
try {
str = this._utf8Converter.ConvertToUnicode(str);
return str + this._utf8Converter.Finish();
} catch (ex) {
return null;
}
},
/**
* Trim excess padding from a Base64 string and atob().
*
* See bug 562431 comment 4.
*/
safeAtoB: function safeAtoB(b64) {
let len = b64.length;
let over = len % 4;
return over ? atob(b64.substr(0, len - over)) : atob(b64);
},
};
XPCOMUtils.defineLazyGetter(CommonUtils, "_utf8Converter", function() {
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
return converter;
});

View File

@ -65,6 +65,9 @@ let Utils = {
exceptionStr: CommonUtils.exceptionStr,
stackTrace: CommonUtils.stackTrace,
makeURI: CommonUtils.makeURI,
encodeUTF8: CommonUtils.encodeUTF8,
decodeUTF8: CommonUtils.decodeUTF8,
safeAtoB: CommonUtils.safeAtoB,
/**
* Wrap a function to catch all exceptions and log them
@ -887,24 +890,6 @@ let Utils = {
return Str.errors.get("error.reason.unknown");
},
encodeUTF8: function(str) {
try {
str = this._utf8Converter.ConvertFromUnicode(str);
return str + this._utf8Converter.Finish();
} catch(ex) {
return null;
}
},
decodeUTF8: function(str) {
try {
str = this._utf8Converter.ConvertToUnicode(str);
return str + this._utf8Converter.Finish();
} catch(ex) {
return null;
}
},
/**
* Generate 26 characters.
*/
@ -1005,15 +990,6 @@ let Utils = {
return acc.trim();
},
// WeaveCrypto returns bad base64 strings. Truncate excess padding
// and decode.
// See Bug 562431, comment 4.
safeAtoB: function safeAtoB(b64) {
let len = b64.length;
let over = len % 4;
return over ? atob(b64.substr(0, len - over)) : atob(b64);
},
/**
* Create an array like the first but without elements of the second. Reuse
* arrays if possible.

View File

@ -1,8 +0,0 @@
Cu.import("resource://services-sync/util.js");
function run_test() {
let data = ["Zm9vYmE=", "Zm9vYmE==", "Zm9vYmE==="];
for (let d in data) {
do_check_eq(Utils.safeAtoB(data[d]), "fooba");
}
}

View File

@ -1,8 +0,0 @@
Cu.import("resource://services-sync/util.js");
function run_test() {
let str = "Umlaute: \u00FC \u00E4\n"; // Umlaute: ü ä
let encoded = Utils.encodeUTF8(str);
let decoded = Utils.decodeUTF8(encoded);
do_check_eq(decoded, str);
}

View File

@ -10,7 +10,6 @@ tail =
[test_load_modules.js]
# util contains a bunch of functionality used throughout.
[test_utils_atob.js]
[test_utils_catch.js]
[test_utils_deepCopy.js]
[test_utils_deepEquals.js]
@ -29,7 +28,6 @@ tail =
[test_utils_passphrase.js]
[test_utils_pbkdf2.js]
[test_utils_sha1.js]
[test_utils_utf8.js]
# We have a number of other libraries that are pretty much standalone.
[test_httpd_sync_server.js]