Bug 1369285 - update how we build the default sync device name. r?tcsc draft
authorMark Hammond <mhammond@skippinet.com.au>
Thu, 01 Jun 2017 16:57:02 +1000
changeset 588823 04db32c04e852d4f05708051a804ac5cf99239bb
parent 588783 8a3aa1701537ea6b8334f432cd030d260d492fa3
child 631693 91b64540dda2600a766b987f57b1fdee3acc102a
push id62171
push userbmo:markh@mozilla.com
push dateMon, 05 Jun 2017 07:05:06 +0000
reviewerstcsc
bugs1369285
milestone55.0a1
Bug 1369285 - update how we build the default sync device name. r?tcsc MozReview-Commit-ID: 6eY8IzKg9kd
services/sync/modules/util.js
services/sync/tests/unit/head_helpers.js
services/sync/tests/unit/test_utils_misc.js
services/sync/tests/unit/xpcshell.ini
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -587,17 +587,17 @@ this.Utils = {
     let brand = Services.strings.createBundle(
       "chrome://branding/locale/brand.properties");
     let brandName = brand.GetStringFromName("brandShortName");
 
     let system =
       // '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;
 
     let syncStrings = Services.strings.createBundle("chrome://weave/locale/sync.properties");
     return syncStrings.formatStringFromName("client.name2", [user, brandName, system], 3);
   },
 
   getDeviceName() {
--- a/services/sync/tests/unit/head_helpers.js
+++ b/services/sync/tests/unit/head_helpers.js
@@ -485,16 +485,18 @@ function promiseStopServer(server) {
 function promiseNextTick() {
   return new Promise(resolve => {
     Utils.nextTick(resolve);
   });
 }
 // 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";
 };
 
 function registerRotaryEngine() {
   let {RotaryEngine} =
     Cu.import("resource://testing-common/services/sync/rotaryengine.js", {});
   Service.engineManager.clear();
new file mode 100644
--- /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();
+});
--- a/services/sync/tests/unit/xpcshell.ini
+++ b/services/sync/tests/unit/xpcshell.ini
@@ -25,16 +25,17 @@ support-files =
 # util contains a bunch of functionality used throughout.
 [test_utils_catch.js]
 [test_utils_deepEquals.js]
 [test_utils_deferGetSet.js]
 [test_utils_keyEncoding.js]
 [test_utils_json.js]
 [test_utils_lock.js]
 [test_utils_makeGUID.js]
+[test_utils_misc.js]
 [test_utils_notify.js]
 [test_utils_passphrase.js]
 
 # We have a number of other libraries that are pretty much standalone.
 [test_addon_utils.js]
 run-sequentially = Restarts server, can't change pref.
 tags = addons
 [test_httpd_sync_server.js]