Backed out changeset 93d2d4bbe263 (bug 1575921) for xpcshell failures on test_device.js CLOSED TREE

This commit is contained in:
Bogdan Tara 2019-09-10 04:42:15 +03:00
Родитель 1735c8570e
Коммит 0f5e9e5206
14 изменённых файлов: 233 добавлений и 336 удалений

Просмотреть файл

@ -568,10 +568,7 @@ var gSyncPane = {
_populateComputerName(value) {
let textbox = document.getElementById("fxaSyncComputerName");
if (!textbox.hasAttribute("placeholder")) {
textbox.setAttribute(
"placeholder",
fxAccounts.device.getDefaultLocalName()
);
textbox.setAttribute("placeholder", Weave.Utils.getDefaultDeviceName());
}
textbox.value = value;
},

Просмотреть файл

@ -50,7 +50,6 @@ const {
ON_DEVICE_DISCONNECTED_NOTIFICATION,
ON_NEW_DEVICE_ID,
POLL_SESSION,
PREF_ACCOUNT_ROOT,
PREF_LAST_FXA_USER,
SERVER_ERRNO_TO_ERROR,
SCOPE_OLD_SYNC,
@ -88,21 +87,17 @@ ChromeUtils.defineModuleGetter(
"resource://gre/modules/FxAccountsCommands.js"
);
ChromeUtils.defineModuleGetter(
this,
"FxAccountsDevice",
"resource://gre/modules/FxAccountsDevice.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"FxAccountsProfile",
"resource://gre/modules/FxAccountsProfile.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
Preferences: "resource://gre/modules/Preferences.jsm",
});
ChromeUtils.defineModuleGetter(
this,
"Utils",
"resource://services-sync/util.js"
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
@ -118,9 +113,9 @@ var publicProperties = [
"canGetKeys",
"checkVerificationStatus",
"commands",
"device",
"getAccountsClient",
"getAssertion",
"getDeviceId",
"getDeviceList",
"getKeys",
"authorizeOAuthCode",
@ -523,14 +518,6 @@ FxAccountsInternal.prototype = {
return this._commands;
},
_device: null,
get device() {
if (!this._device) {
this._device = new FxAccountsDevice(this);
}
return this._device;
},
_oauthClient: null,
get oauthClient() {
if (!this._oauthClient) {
@ -736,7 +723,6 @@ FxAccountsInternal.prototype = {
if (!FXA_ENABLED) {
throw new Error("Cannot call setSignedInUser when FxA is disabled.");
}
Preferences.resetBranch(PREF_ACCOUNT_ROOT);
log.debug("setSignedInUser - aborting any existing flows");
const signedInUser = await this.getSignedInUser();
if (signedInUser) {
@ -855,6 +841,33 @@ FxAccountsInternal.prototype = {
return this.currentAccountState.updateUserAccountData({ cert: null });
},
async getDeviceId() {
let data = await this.currentAccountState.getUserAccountData();
if (!data) {
// Without a signed-in user, there can be no device id.
return null;
}
// Try migrating first. Remove this in Firefox 65+.
if (data.deviceId) {
log.info("Migrating from deviceId to device.");
await this.currentAccountState.updateUserAccountData({
deviceId: null,
deviceRegistrationVersion: null,
device: {
id: data.deviceId,
registrationVersion: data.deviceRegistrationVersion,
},
});
data = await this.currentAccountState.getUserAccountData();
}
const { device } = data;
if (await this.checkDeviceUpdateNeeded(device)) {
return this._registerOrUpdateDevice(data);
}
// Return the device id that we already registered with the server.
return device.id;
},
async checkDeviceUpdateNeeded(device) {
// There is no device registered or the device registration is outdated.
// Either way, we should register the device with FxA
@ -1009,7 +1022,6 @@ FxAccountsInternal.prototype = {
},
async _signOutLocal() {
Preferences.resetBranch(PREF_ACCOUNT_ROOT);
await this.currentAccountState.signOut();
// this "aborts" this.currentAccountState but doesn't make a new one.
await this.abortExistingFlow();
@ -2071,7 +2083,7 @@ FxAccountsInternal.prototype = {
try {
const subscription = await this.fxaPushService.registerPushEndpoint();
const deviceName = this.device.getLocalName();
const deviceName = this._getDeviceName();
let deviceOptions = {};
// if we were able to obtain a subscription
@ -2103,7 +2115,7 @@ FxAccountsInternal.prototype = {
device = await this.fxAccountsClient.registerDevice(
sessionToken,
deviceName,
this.device.getLocalType(),
this._getDeviceType(),
deviceOptions
);
Services.obs.notifyObservers(null, ON_NEW_DEVICE_ID);
@ -2125,6 +2137,14 @@ FxAccountsInternal.prototype = {
}
},
_getDeviceName() {
return Utils.getDeviceName();
},
_getDeviceType() {
return Utils.getDeviceType();
},
_handleDeviceError(error, sessionToken) {
return Promise.resolve()
.then(() => {

Просмотреть файл

@ -109,10 +109,6 @@ exports.COMMAND_CHANGE_PASSWORD = "fxaccounts:change_password";
exports.COMMAND_FXA_STATUS = "fxaccounts:fxa_status";
exports.COMMAND_PAIR_PREFERENCES = "fxaccounts:pair_preferences";
// The pref branch where any prefs which relate to a specific account should
// be stored. This branch will be reset on account signout and signin.
exports.PREF_ACCOUNT_ROOT = "identity.fxaccounts.account.";
exports.PREF_LAST_FXA_USER = "identity.fxaccounts.lastSignedInUserHash";
exports.PREF_REMOTE_PAIRING_URI = "identity.fxaccounts.remote.pairing.uri";

Просмотреть файл

@ -1,150 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { log } = ChromeUtils.import(
"resource://gre/modules/FxAccountsCommon.js"
);
const { DEVICE_TYPE_DESKTOP } = ChromeUtils.import(
"resource://services-sync/constants.js"
);
const { PREF_ACCOUNT_ROOT } = ChromeUtils.import(
"resource://gre/modules/FxAccountsCommon.js"
);
const PREF_LOCAL_DEVICE_NAME = PREF_ACCOUNT_ROOT + "device.name";
XPCOMUtils.defineLazyPreferenceGetter(
this,
"pref_localDeviceName",
PREF_LOCAL_DEVICE_NAME,
""
);
const PREF_DEPRECATED_DEVICE_NAME = "services.sync.client.name";
// Everything to do with FxA devices.
// TODO: Move more device stuff from FxAccounts.jsm into here - eg, device
// registration, device lists, etc.
class FxAccountsDevice {
constructor(fxa) {
this._fxa = fxa;
}
async getLocalId() {
let data = await this._fxa.currentAccountState.getUserAccountData();
if (!data) {
// Without a signed-in user, there can be no device id.
return null;
}
const { device } = data;
if (await this._fxa.checkDeviceUpdateNeeded(device)) {
return this._fxa._registerOrUpdateDevice(data);
}
// Return the device id that we already registered with the server.
return device.id;
}
// Generate a client name if we don't have a useful one yet
getDefaultLocalName() {
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
let user = env.get("USER") || env.get("USERNAME");
// Note that we used to fall back to the "services.sync.username" pref here,
// but that's no longer suitable in a world where sync might not be
// configured. However, we almost never *actually* fell back to that, and
// doing so sanely here would mean making this function async, which we don't
// really want to do yet.
// A little hack for people using the the moz-build environment on Windows
// which sets USER to the literal "%USERNAME%" (yes, really)
if (user == "%USERNAME%" && env.get("USERNAME")) {
user = env.get("USERNAME");
}
let brand = Services.strings.createBundle(
"chrome://branding/locale/brand.properties"
);
let brandName;
try {
brandName = brand.GetStringFromName("brandShortName");
} catch (O_o) {
// this only fails in tests and markh can't work out why :(
brandName = Services.appinfo.name;
}
// The DNS service may fail to provide a hostname in edge-cases we don't
// fully understand - bug 1391488.
let hostname;
try {
// hostname of the system, usually assigned by the user or admin
hostname = Cc["@mozilla.org/network/dns-service;1"].getService(
Ci.nsIDNSService
).myHostName;
} catch (ex) {
Cu.reportError(ex);
}
let system =
// 'device' is defined on unix systems
Services.sysinfo.get("device") ||
hostname ||
// fall back on ua info string
Cc["@mozilla.org/network/protocol;1?name=http"].getService(
Ci.nsIHttpProtocolHandler
).oscpu;
// It's a little unfortunate that this string is defined as being weave/sync,
// but it's not worth moving it.
let syncStrings = Services.strings.createBundle(
"chrome://weave/locale/sync.properties"
);
return syncStrings.formatStringFromName("client.name2", [
user,
brandName,
system,
]);
}
getLocalName() {
// We used to store this in services.sync.client.name, but now store it
// under an fxa-specific location.
let deprecated_value = Services.prefs.getStringPref(
PREF_DEPRECATED_DEVICE_NAME,
""
);
if (deprecated_value) {
Services.prefs.setStringPref(PREF_LOCAL_DEVICE_NAME, deprecated_value);
Services.prefs.clearUserPref(PREF_DEPRECATED_DEVICE_NAME);
}
let name = pref_localDeviceName;
if (!name) {
name = this.getDefaultLocalName();
Services.prefs.setStringPref(PREF_LOCAL_DEVICE_NAME, name);
}
return name;
}
setLocalName(newName) {
Services.prefs.clearUserPref(PREF_DEPRECATED_DEVICE_NAME);
Services.prefs.setStringPref(PREF_LOCAL_DEVICE_NAME, newName);
// Update the registration in the background.
this._fxa.updateDeviceRegistration().catch(error => {
log.warn("failed to update fxa device registration", error);
});
}
getLocalType() {
return DEVICE_TYPE_DESKTOP;
}
}
var EXPORTED_SYMBOLS = ["FxAccountsDevice"];

Просмотреть файл

@ -22,7 +22,6 @@ EXTRA_JS_MODULES += [
'FxAccountsCommands.js',
'FxAccountsCommon.js',
'FxAccountsConfig.jsm',
'FxAccountsDevice.jsm',
'FxAccountsOAuthGrantClient.jsm',
'FxAccountsPairing.jsm',
'FxAccountsPairingChannel.js',

Просмотреть файл

@ -111,14 +111,17 @@ MockFxAccountsClient.prototype = {
__proto__: FxAccountsClient.prototype,
};
async function MockFxAccounts(credentials, device = {}) {
let fxa = new FxAccounts({
newAccountState(creds) {
function MockFxAccounts(device = {}) {
return new FxAccounts({
newAccountState(credentials) {
// we use a real accountState but mocked storage.
let storage = new MockStorageManager();
storage.initialize(creds);
storage.initialize(credentials);
return new AccountState(storage);
},
_getDeviceName() {
return device.name || "mock device name";
},
async availableCommands() {
return {};
},
@ -143,12 +146,6 @@ async function MockFxAccounts(credentials, device = {}) {
},
DEVICE_REGISTRATION_VERSION,
});
await fxa.internal.setSignedInUser(credentials);
Services.prefs.setStringPref(
"identity.fxaccounts.account.device.name",
device.name || "mock device name"
);
return fxa;
}
add_task(async function test_updateDeviceRegistration_with_new_device() {
@ -156,7 +153,8 @@ add_task(async function test_updateDeviceRegistration_with_new_device() {
const deviceType = "bar";
const credentials = getTestUser("baz");
const fxa = await MockFxAccounts(credentials, { name: deviceName });
const fxa = new MockFxAccounts({ name: deviceName });
await fxa.internal.setSignedInUser(credentials);
// Remove the current device registration (setSignedInUser does one!).
await fxa.updateUserAccountData({ uid: credentials.uid, device: null });
@ -215,7 +213,8 @@ add_task(async function test_updateDeviceRegistration_with_existing_device() {
const deviceName = "phil's device";
const credentials = getTestUser("pb");
const fxa = await MockFxAccounts(credentials, { name: deviceName });
const fxa = new MockFxAccounts({ name: deviceName });
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({
uid: credentials.uid,
device: {
@ -279,7 +278,8 @@ add_task(
const currentDeviceId = "my device id";
const credentials = getTestUser("baz");
const fxa = await MockFxAccounts(credentials, { name: deviceName });
const fxa = new MockFxAccounts({ name: deviceName });
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({
uid: credentials.uid,
device: {
@ -350,7 +350,8 @@ add_task(
const conflictingDeviceId = "conflicting device id";
const credentials = getTestUser("baz");
const fxa = await MockFxAccounts(credentials, { name: deviceName });
const fxa = new MockFxAccounts({ name: deviceName });
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({
uid: credentials.uid,
device: {
@ -438,7 +439,8 @@ add_task(
const deviceName = "foo";
const credentials = getTestUser("baz");
const fxa = await MockFxAccounts(credentials, { name: deviceName });
const fxa = new MockFxAccounts({ name: deviceName });
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({ uid: credentials.uid, device: null });
const spy = {
@ -484,7 +486,8 @@ add_task(
async function test_getDeviceId_with_no_device_id_invokes_device_registration() {
const credentials = getTestUser("foo");
credentials.verified = true;
const fxa = await MockFxAccounts(credentials);
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({ uid: credentials.uid, device: null });
const spy = { count: 0, args: [] };
@ -499,7 +502,7 @@ add_task(
return Promise.resolve("bar");
};
const result = await fxa.internal.device.getLocalId();
const result = await fxa.internal.getDeviceId();
Assert.equal(spy.count, 1);
Assert.equal(spy.args[0].length, 1);
@ -513,7 +516,8 @@ add_task(
async function test_getDeviceId_with_registration_version_outdated_invokes_device_registration() {
const credentials = getTestUser("foo");
credentials.verified = true;
const fxa = await MockFxAccounts(credentials);
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
const spy = { count: 0, args: [] };
fxa.internal.currentAccountState.getUserAccountData = () =>
@ -530,7 +534,7 @@ add_task(
return Promise.resolve("wibble");
};
const result = await fxa.internal.device.getLocalId();
const result = await fxa.internal.getDeviceId();
Assert.equal(spy.count, 1);
Assert.equal(spy.args[0].length, 1);
@ -543,7 +547,8 @@ add_task(
async function test_getDeviceId_with_device_id_and_uptodate_registration_version_doesnt_invoke_device_registration() {
const credentials = getTestUser("foo");
credentials.verified = true;
const fxa = await MockFxAccounts(credentials);
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
const spy = { count: 0 };
fxa.internal.currentAccountState.getUserAccountData = async () => ({
@ -558,7 +563,7 @@ add_task(
return Promise.resolve("bar");
};
const result = await fxa.internal.device.getLocalId();
const result = await fxa.internal.getDeviceId();
Assert.equal(spy.count, 0);
Assert.equal(result, "foo's device id");
@ -569,7 +574,8 @@ add_task(
async function test_getDeviceId_with_device_id_and_with_no_registration_version_invokes_device_registration() {
const credentials = getTestUser("foo");
credentials.verified = true;
const fxa = await MockFxAccounts(credentials);
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
const spy = { count: 0, args: [] };
fxa.internal.currentAccountState.getUserAccountData = () =>
@ -580,7 +586,7 @@ add_task(
return Promise.resolve("wibble");
};
const result = await fxa.internal.device.getLocalId();
const result = await fxa.internal.getDeviceId();
Assert.equal(spy.count, 1);
Assert.equal(spy.args[0].length, 1);
@ -589,11 +595,38 @@ add_task(
}
);
add_task(async function test_migration_toplevel_deviceId_to_device() {
const credentials = getTestUser("foo");
credentials.verified = true;
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({ uid: credentials.uid, device: null });
// Can't use updateUserAccountData here since it won't accept deprecated fields!
const accountData =
fxa.internal.currentAccountState.storageManager.accountData;
accountData.deviceId = "mydeviceid";
accountData.deviceRegistrationVersion = DEVICE_REGISTRATION_VERSION;
const result = await fxa.internal.getDeviceId();
Assert.equal(result, "mydeviceid");
const state = fxa.internal.currentAccountState;
const data = await state.getUserAccountData();
Assert.deepEqual(data.device, {
id: "mydeviceid",
registrationVersion: DEVICE_REGISTRATION_VERSION,
registeredCommandsKeys: [],
});
Assert.ok(!data.deviceId);
Assert.ok(!data.deviceRegistrationVersion);
});
add_task(async function test_devicelist_pushendpointexpired() {
const deviceId = "mydeviceid";
const credentials = getTestUser("baz");
credentials.verified = true;
const fxa = await MockFxAccounts(credentials);
const fxa = new MockFxAccounts();
await fxa.internal.setSignedInUser(credentials);
await fxa.updateUserAccountData({
uid: credentials.uid,
device: {

Просмотреть файл

@ -1,92 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { fxAccounts } = ChromeUtils.import(
"resource://gre/modules/FxAccounts.jsm"
);
const { PREF_ACCOUNT_ROOT } = ChromeUtils.import(
"resource://gre/modules/FxAccountsCommon.js"
);
_("Misc tests for FxAccounts.device");
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 = fxAccounts.device._getDefaultLocalName(); // 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 =
Services.sysinfo.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();
});
add_test(function test_migration() {
Services.prefs.clearUserPref("identity.fxaccounts.account.device.name");
Services.prefs.setStringPref("services.sync.client.name", "my client name");
// calling getLocalName() should move the name to the new pref and reset the old.
equal(fxAccounts.device.getLocalName(), "my client name");
equal(
Services.prefs.getStringPref("identity.fxaccounts.account.device.name"),
"my client name"
);
ok(!Services.prefs.prefHasUserValue("services.sync.client.name"));
run_next_test();
});
add_test(function test_migration_set_before_get() {
Services.prefs.setStringPref("services.sync.client.name", "old client name");
fxAccounts.device.setLocalName("new client name");
equal(fxAccounts.device.getLocalName(), "new client name");
run_next_test();
});
add_task(async function test_reset() {
// We don't test the client name specifically here because the client name
// is set as part of signing the user in via the attempt to register the
// device.
const testPref = PREF_ACCOUNT_ROOT + "test-pref";
Services.prefs.setStringPref(testPref, "whatever");
let credentials = {
email: "foo@example.com",
uid: "1234@lcip.org",
assertion: "foobar",
sessionToken: "dead",
kSync: "beef",
kXCS: "cafe",
kExtSync: "bacon",
kExtKbHash: "cheese",
verified: true,
};
await fxAccounts.setSignedInUser(credentials);
ok(!Services.prefs.prefHasUserValue(testPref));
// signing the user out should reset the name pref.
const namePref = PREF_ACCOUNT_ROOT + "device.name";
ok(Services.prefs.prefHasUserValue(namePref));
await fxAccounts.signOut(/* remoteOnly = */ true);
ok(!Services.prefs.prefHasUserValue(namePref));
});

Просмотреть файл

@ -11,7 +11,6 @@ support-files =
[test_client.js]
[test_commands.js]
[test_credentials.js]
[test_device.js]
[test_loginmgr_storage.js]
[test_oauth_grant_client.js]
[test_oauth_grant_client_server.js]

Просмотреть файл

@ -234,14 +234,21 @@ ClientEngine.prototype = {
},
get localName() {
return this.fxAccounts.device.getLocalName();
return Utils.getDeviceName();
},
set localName(value) {
this.fxAccounts.device.setLocalName(value);
Svc.Prefs.set("client.name", value);
// Update the registration in the background.
this.fxAccounts.updateDeviceRegistration().catch(error => {
this._log.warn("failed to update fxa device registration", error);
});
},
get localType() {
return this.fxAccounts.device.getLocalType();
return Utils.getDeviceType();
},
set localType(value) {
Svc.Prefs.set("client.type", value);
},
getClientName(id) {
@ -356,7 +363,7 @@ ClientEngine.prototype = {
this._log.debug("Updating the known stale clients");
// _fetchFxADevices side effect updates this._knownStaleFxADeviceIds.
await this._fetchFxADevices();
let localFxADeviceId = await fxAccounts.device.getLocalId();
let localFxADeviceId = await fxAccounts.getDeviceId();
// Process newer records first, so that if we hit a record with a device ID
// we've seen before, we can mark it stale immediately.
let clientList = Object.values(this._store._remoteClients).sort(
@ -445,7 +452,7 @@ ClientEngine.prototype = {
await this._removeRemoteClient(id);
}
}
let localFxADeviceId = await fxAccounts.device.getLocalId();
let localFxADeviceId = await fxAccounts.getDeviceId();
// Bug 1264498: Mobile clients don't remove themselves from the clients
// collection when the user disconnects Sync, so we mark as stale clients
// with the same name that haven't synced in over a week.
@ -615,7 +622,7 @@ ClientEngine.prototype = {
};
let excludedIds = null;
if (!ids) {
const localFxADeviceId = await fxAccounts.device.getLocalId();
const localFxADeviceId = await fxAccounts.getDeviceId();
excludedIds = [localFxADeviceId];
}
try {
@ -1103,7 +1110,7 @@ ClientStore.prototype = {
// Package the individual components into a record for the local client
if (id == this.engine.localID) {
try {
record.fxaDeviceId = await this.engine.fxAccounts.device.getLocalId();
record.fxaDeviceId = await this.engine.fxAccounts.getDeviceId();
} catch (error) {
this._log.warn("failed to get fxa device id", error);
}

Просмотреть файл

@ -47,6 +47,13 @@ XPCOMUtils.defineLazyServiceGetter(
"nsILoginManagerCrypto"
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"localDeviceName",
"services.sync.client.name",
""
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"localDeviceType",
@ -686,6 +693,68 @@ var Utils = {
return result;
},
getDefaultDeviceName() {
// Generate a client name if we don't have a useful one yet
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
let user =
env.get("USER") ||
env.get("USERNAME") ||
Svc.Prefs.get("account") ||
Svc.Prefs.get("username");
// A little hack for people using the the moz-build environment on Windows
// which sets USER to the literal "%USERNAME%" (yes, really)
if (user == "%USERNAME%" && env.get("USERNAME")) {
user = env.get("USERNAME");
}
let brand = Services.strings.createBundle(
"chrome://branding/locale/brand.properties"
);
let brandName = brand.GetStringFromName("brandShortName");
// The DNS service may fail to provide a hostname in edge-cases we don't
// fully understand - bug 1391488.
let hostname;
try {
// hostname of the system, usually assigned by the user or admin
hostname = Cc["@mozilla.org/network/dns-service;1"].getService(
Ci.nsIDNSService
).myHostName;
} catch (ex) {
Cu.reportError(ex);
}
let system =
// 'device' is defined on unix systems
Services.sysinfo.get("device") ||
hostname ||
// 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,
]);
},
getDeviceName() {
let deviceName = localDeviceName;
if (deviceName === "") {
deviceName = this.getDefaultDeviceName();
Svc.Prefs.set("client.name", deviceName);
}
return deviceName;
},
/**
* Helper to implement a more efficient version of fairly common pattern:
*

Просмотреть файл

@ -509,6 +509,15 @@ function promiseOneObserver(topic, callback) {
});
}
// 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";
};
async function registerRotaryEngine() {
let { RotaryEngine } = ChromeUtils.import(
"resource://testing-common/services/sync/rotaryengine.js"

Просмотреть файл

@ -980,16 +980,8 @@ add_task(async function test_clients_not_in_fxa_list() {
notifyDevices() {
return Promise.resolve(true);
},
device: {
getLocalId() {
return fxAccounts.device.getLocalId();
},
getLocalName() {
return fxAccounts.device.getLocalName();
},
getLocalType() {
return fxAccounts.device.getLocalType();
},
getDeviceId() {
return fxAccounts.getDeviceId();
},
getDeviceList() {
return Promise.resolve([{ id: remoteId }]);
@ -1059,16 +1051,8 @@ add_task(async function test_dupe_device_ids() {
notifyDevices() {
return Promise.resolve(true);
},
device: {
getLocalId() {
return fxAccounts.device.getLocalId();
},
getLocalName() {
return fxAccounts.device.getLocalName();
},
getLocalType() {
return fxAccounts.device.getLocalType();
},
getDeviceId() {
return fxAccounts.getDeviceId();
},
getDeviceList() {
return Promise.resolve([{ id: remoteDeviceId }]);
@ -2073,16 +2057,8 @@ add_task(async function test_other_clients_notified_on_first_sync() {
const fxAccounts = engine.fxAccounts;
let calls = 0;
engine.fxAccounts = {
device: {
getLocalId() {
return fxAccounts.device.getLocalId();
},
getLocalName() {
return fxAccounts.device.getLocalName();
},
getLocalType() {
return fxAccounts.device.getLocalType();
},
getDeviceId() {
return fxAccounts.getDeviceId();
},
notifyDevices() {
calls++;

Просмотреть файл

@ -0,0 +1,33 @@
_("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 =
Services.sysinfo.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();
});

Просмотреть файл

@ -29,6 +29,7 @@ support-files =
[test_utils_lock.js]
[test_utils_makeGUID.js]
run-sequentially = Disproportionately slows down full test run, bug 1450316
[test_utils_misc.js]
[test_utils_notify.js]
[test_utils_passphrase.js]