2015-04-21 21:10:50 +03:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-02 14:14:00 +03:00
|
|
|
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
2015-04-21 21:10:50 +03:00
|
|
|
|
|
|
|
const userAgentID = 'b2546987-4f63-49b1-99f7-739cd3c40e44';
|
|
|
|
const channelID = '35a820f7-d7dd-43b3-af21-d65352212ae3';
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
do_get_profile();
|
|
|
|
setPrefs({
|
|
|
|
userAgentID,
|
|
|
|
requestTimeout: 1000,
|
|
|
|
retryBaseInterval: 150
|
|
|
|
});
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2017-06-22 13:51:42 +03:00
|
|
|
add_task(async function test_register_rollback() {
|
2015-06-02 14:14:00 +03:00
|
|
|
let db = PushServiceWebSocket.newPushDB();
|
2017-12-21 13:10:23 +03:00
|
|
|
registerCleanupFunction(() => {return db.drop().then(_ => db.close());});
|
2015-04-21 21:10:50 +03:00
|
|
|
|
|
|
|
let handshakes = 0;
|
|
|
|
let registers = 0;
|
2015-08-15 03:26:07 +03:00
|
|
|
let unregisterDone;
|
|
|
|
let unregisterPromise = new Promise(resolve => unregisterDone = resolve);
|
2015-06-02 14:14:00 +03:00
|
|
|
PushServiceWebSocket._generateID = () => channelID;
|
2015-04-21 21:10:50 +03:00
|
|
|
PushService.init({
|
2015-06-02 14:14:00 +03:00
|
|
|
serverURI: "wss://push.example.org/",
|
2015-04-21 21:10:50 +03:00
|
|
|
db: makeStub(db, {
|
2015-05-12 19:08:00 +03:00
|
|
|
put(prev, record) {
|
|
|
|
return Promise.reject('universe has imploded');
|
2015-04-21 21:10:50 +03:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
makeWebSocket(uri) {
|
|
|
|
return new MockWebSocket(uri, {
|
|
|
|
onHello(request) {
|
|
|
|
handshakes++;
|
|
|
|
equal(request.uaid, userAgentID, 'Handshake: wrong device ID');
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'hello',
|
|
|
|
status: 200,
|
|
|
|
uaid: userAgentID
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
onRegister(request) {
|
|
|
|
equal(request.channelID, channelID, 'Register: wrong channel ID');
|
|
|
|
registers++;
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'register',
|
|
|
|
status: 200,
|
|
|
|
uaid: userAgentID,
|
|
|
|
channelID,
|
|
|
|
pushEndpoint: 'https://example.com/update/rollback'
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
onUnregister(request) {
|
|
|
|
equal(request.channelID, channelID, 'Unregister: wrong channel ID');
|
2016-03-28 22:29:25 +03:00
|
|
|
equal(request.code, 200, 'Expected manual unregister reason');
|
2015-04-21 21:10:50 +03:00
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'unregister',
|
|
|
|
status: 200,
|
|
|
|
channelID
|
|
|
|
}));
|
2015-08-15 03:26:07 +03:00
|
|
|
unregisterDone();
|
2015-04-21 21:10:50 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Should return a rejected promise if storage fails.
|
2017-06-22 13:51:42 +03:00
|
|
|
await rejects(
|
2015-12-08 23:41:57 +03:00
|
|
|
PushService.register({
|
|
|
|
scope: 'https://example.com/storage-error',
|
|
|
|
originAttributes: ChromeUtils.originAttributesToSuffix(
|
2016-02-05 04:42:44 +03:00
|
|
|
{ appId: Ci.nsIScriptSecurityManager.NO_APP_ID, inIsolatedMozBrowser: false }),
|
2015-12-08 23:41:57 +03:00
|
|
|
}),
|
2015-11-10 00:58:50 +03:00
|
|
|
'Expected error for unregister database failure'
|
2015-04-21 21:10:50 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Should send an out-of-band unregister request.
|
2017-06-22 13:51:42 +03:00
|
|
|
await unregisterPromise;
|
2015-04-21 21:10:50 +03:00
|
|
|
equal(handshakes, 1, 'Wrong handshake count');
|
|
|
|
equal(registers, 1, 'Wrong register count');
|
|
|
|
});
|