diff --git a/services/sync/modules/util.js b/services/sync/modules/util.js index 4dfcfb762513..3dae9ac6e247 100644 --- a/services/sync/modules/util.js +++ b/services/sync/modules/util.js @@ -592,7 +592,7 @@ this.Utils = { // 'device' is defined on unix systems Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("device") || // hostname of the system, usually assigned by the user or admin - Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("host") || + Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService).myHostName || // fall back on ua info string Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler).oscpu; diff --git a/services/sync/tests/unit/head_helpers.js b/services/sync/tests/unit/head_helpers.js index df0013a69204..ebb297e90d1e 100644 --- a/services/sync/tests/unit/head_helpers.js +++ b/services/sync/tests/unit/head_helpers.js @@ -490,6 +490,8 @@ function promiseNextTick() { // Avoid an issue where `client.name2` containing unicode characters causes // a number of tests to fail, due to them assuming that we do not need to utf-8 // encode or decode data sent through the mocked server (see bug 1268912). +// We stash away the original implementation so test_utils_misc.js can test it. +Utils._orig_getDefaultDeviceName = Utils.getDefaultDeviceName; Utils.getDefaultDeviceName = function() { return "Test device name"; }; diff --git a/services/sync/tests/unit/test_utils_misc.js b/services/sync/tests/unit/test_utils_misc.js new file mode 100644 index 000000000000..e14fa0e547e0 --- /dev/null +++ b/services/sync/tests/unit/test_utils_misc.js @@ -0,0 +1,29 @@ +_("Misc tests for utils.js"); + +add_test(function test_default_device_name() { + // Note that head_helpers overrides getDefaultDeviceName - this test is + // really just to ensure the actual implementation is sane - we can't + // really check the value it uses is correct. + // We are just hoping to avoid a repeat of bug 1369285. + let def = Utils._orig_getDefaultDeviceName(); // make sure it doesn't throw. + _("default value is " + def); + ok(def.length > 0); + + // This is obviously tied to the implementation, but we want early warning + // if any of these things fail. + // We really want one of these 2 to provide a value. + let hostname = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("device") || + Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService).myHostName; + _("hostname is " + hostname); + ok(hostname.length > 0); + // the hostname should be in the default. + ok(def.includes(hostname)); + // We expect the following to work as a fallback to the above. + let fallback = Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler).oscpu; + _("UA fallback is " + fallback); + ok(fallback.length > 0); + // the fallback should not be in the default + ok(!def.includes(fallback)); + + run_next_test(); +}); diff --git a/services/sync/tests/unit/xpcshell.ini b/services/sync/tests/unit/xpcshell.ini index 8612834708e7..369751c9de97 100644 --- a/services/sync/tests/unit/xpcshell.ini +++ b/services/sync/tests/unit/xpcshell.ini @@ -30,6 +30,7 @@ support-files = [test_utils_json.js] [test_utils_lock.js] [test_utils_makeGUID.js] +[test_utils_misc.js] [test_utils_notify.js] [test_utils_passphrase.js]