1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
ChromeUtils.defineESModuleGetters(this, {
Service: "resource://services-sync/service.sys.mjs",
extensionStorageSync: "resource://gre/modules/ExtensionStorageSync.sys.mjs",
});
const { ExtensionStorageEngineBridge, ExtensionStorageEngineKinto } =
ChromeUtils.importESModule(
"resource://services-sync/engines/extension-storage.sys.mjs"
);
const { BridgeWrapperXPCOM } = ChromeUtils.importESModule(
"resource://services-sync/bridged_engine.sys.mjs"
);
Services.prefs.setStringPref("webextensions.storage.sync.log.level", "debug");
add_task(async function test_switching_between_kinto_and_bridged() {
function assertUsingKinto(message) {
let kintoEngine = Service.engineManager.get("extension-storage");
Assert.ok(kintoEngine instanceof ExtensionStorageEngineKinto, message);
}
function assertUsingBridged(message) {
let bridgedEngine = Service.engineManager.get("extension-storage");
Assert.ok(bridgedEngine instanceof ExtensionStorageEngineBridge, message);
}
let isUsingKinto = Services.prefs.getBoolPref(
"webextensions.storage.sync.kinto",
false
);
if (isUsingKinto) {
assertUsingKinto("Should use Kinto engine before flipping pref");
} else {
assertUsingBridged("Should use bridged engine before flipping pref");
}
_("Flip pref");
Services.prefs.setBoolPref("webextensions.storage.sync.kinto", !isUsingKinto);
await Service.engineManager.switchAlternatives();
if (isUsingKinto) {
assertUsingBridged("Should use bridged engine after flipping pref");
} else {
assertUsingKinto("Should use Kinto engine after flipping pref");
}
_("Clean up");
Services.prefs.clearUserPref("webextensions.storage.sync.kinto");
await Service.engineManager.switchAlternatives();
});
add_task(async function test_enable() {
const PREF = "services.sync.engine.extension-storage.force";
let addonsEngine = Service.engineManager.get("addons");
let extensionStorageEngine = Service.engineManager.get("extension-storage");
try {
Assert.ok(
addonsEngine.enabled,
"Add-ons engine should be enabled by default"
);
Assert.ok(
extensionStorageEngine.enabled,
"Extension storage engine should be enabled by default"
);
addonsEngine.enabled = false;
Assert.ok(
!extensionStorageEngine.enabled,
"Disabling add-ons should disable extension storage"
);
extensionStorageEngine.enabled = true;
Assert.ok(
!extensionStorageEngine.enabled,
"Enabling extension storage without override pref shouldn't work"
);
Services.prefs.setBoolPref(PREF, true);
Assert.ok(
extensionStorageEngine.enabled,
"Setting override pref should enable extension storage"
);
extensionStorageEngine.enabled = false;
Assert.ok(
!extensionStorageEngine.enabled,
"Disabling extension storage engine with override pref should work"
);
extensionStorageEngine.enabled = true;
Assert.ok(
extensionStorageEngine.enabled,
"Enabling extension storage with override pref should work"
);
} finally {
addonsEngine.enabled = true;
Services.prefs.clearUserPref(PREF);
}
});
add_task(async function test_notifyPendingChanges() {
let engine = new ExtensionStorageEngineBridge(Service);
let extension = { id: "ext-1" };
let expectedChange = {
a: "b",
c: "d",
};
let lastSync = 0;
let syncID = Utils.makeGUID();
let error = null;
engine.component = {
QueryInterface: ChromeUtils.generateQI([
"mozIBridgedSyncEngine",
"mozIExtensionStorageArea",
"mozISyncedExtensionStorageArea",
]),
ensureCurrentSyncId(id, callback) {
if (syncID != id) {
syncID = id;
lastSync = 0;
}
callback.handleSuccess(id);
},
resetSyncId(callback) {
callback.handleSuccess(syncID);
},
syncStarted(callback) {
callback.handleSuccess();
},
getLastSync(callback) {
callback.handleSuccess(lastSync);
},
setLastSync(lastSyncMillis, callback) {
lastSync = lastSyncMillis;
callback.handleSuccess();
},
apply(callback) {
callback.handleSuccess([]);
},
fetchPendingSyncChanges(callback) {
if (error) {
callback.handleError(Cr.NS_ERROR_FAILURE, error.message);
} else {
callback.onChanged(extension.id, JSON.stringify(expectedChange));
callback.handleSuccess();
}
},
setUploaded(modified, ids, callback) {
callback.handleSuccess();
},
syncFinished(callback) {
callback.handleSuccess();
},
takeMigrationInfo(callback) {
callback.handleSuccess(null);
},
};
engine._bridge = new BridgeWrapperXPCOM(engine.component);
let server = await serverForFoo(engine);
let actualChanges = [];
let listener = changes => actualChanges.push(changes);
extensionStorageSync.addOnChangedListener(extension, listener);
try {
await SyncTestingInfrastructure(server);
info("Sync engine; notify about changes");
await sync_engine_and_validate_telem(engine, false);
deepEqual(
actualChanges,
[expectedChange],
"Should notify about changes during sync"
);
error = new Error("oops!");
actualChanges = [];
await sync_engine_and_validate_telem(engine, false);
deepEqual(
actualChanges,
[],
"Should finish syncing even if notifying about changes fails"
);
} finally {
extensionStorageSync.removeOnChangedListener(extension, listener);
await promiseStopServer(server);
await engine.finalize();
}
});
// It's difficult to know what to test - there's already tests for the bridged
// engine etc - so we just try and check that this engine conforms to the
// mozIBridgedSyncEngine interface guarantees.
add_task(async function test_engine() {
// Forcibly set the bridged engine in the engine manager. the reason we do
// this, unlike the other tests where we just create the engine, is so that
// telemetry can get at the engine's `overrideTelemetryName`, which it gets
// through the engine manager.
await Service.engineManager.unregister("extension-storage");
await Service.engineManager.register(ExtensionStorageEngineBridge);
let engine = Service.engineManager.get("extension-storage");
Assert.equal(engine.version, 1);
Assert.deepEqual(await engine.getSyncID(), null);
await engine.resetLocalSyncID();
Assert.notEqual(await engine.getSyncID(), null);
Assert.equal(await engine.getLastSync(), 0);
// lastSync is seconds on this side of the world, but milli-seconds on the other.
await engine.setLastSync(1234.567);
// should have 2 digit precision.
Assert.equal(await engine.getLastSync(), 1234.57);
await engine.setLastSync(0);
// Set some data.
await extensionStorageSync.set({ id: "ext-2" }, { ext_2_key: "ext_2_value" });
// Now do a sync with out regular test server.
let server = await serverForFoo(engine);
try {
await SyncTestingInfrastructure(server);
info("Add server records");
let foo = server.user("foo");
let collection = foo.collection("extension-storage");
let now = new_timestamp();
collection.insert(
"fakeguid0000",
encryptPayload({
id: "fakeguid0000",
extId: "ext-1",
data: JSON.stringify({ foo: "bar" }),
}),
now
);
info("Sync the engine");
let ping = await sync_engine_and_validate_telem(engine, false);
Assert.ok(ping.engines.find(e => e.name == "rust-webext-storage"));
Assert.equal(
ping.engines.find(e => e.name == "extension-storage"),
null
);
// We should have applied the data from the existing collection record.
Assert.deepEqual(await extensionStorageSync.get({ id: "ext-1" }, null), {
foo: "bar",
});
// should now be 2 records on the server.
let payloads = collection.payloads();
Assert.equal(payloads.length, 2);
// find the new one we wrote.
let newPayload =
payloads[0].id == "fakeguid0000" ? payloads[1] : payloads[0];
Assert.equal(newPayload.data, `{"ext_2_key":"ext_2_value"}`);
// should have updated the timestamp.
greater(await engine.getLastSync(), 0, "Should update last sync time");
} finally {
await promiseStopServer(server);
await engine.finalize();
}
});
|