test_notification_incomplete.js (4317B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const userAgentID = "1ca1cf66-eeb4-4df7-87c1-d5c92906ab90"; 7 8 function run_test() { 9 do_get_profile(); 10 setPrefs({ 11 userAgentID, 12 }); 13 run_next_test(); 14 } 15 16 add_task(async function test_notification_incomplete() { 17 let db = PushServiceWebSocket.newPushDB(); 18 registerCleanupFunction(() => { 19 return db.drop().then(_ => db.close()); 20 }); 21 let records = [ 22 { 23 channelID: "123", 24 pushEndpoint: "https://example.org/update/1", 25 scope: "https://example.com/page/1", 26 version: 1, 27 originAttributes: "", 28 quota: Infinity, 29 }, 30 { 31 channelID: "3ad1ed95-d37a-4d88-950f-22cbe2e240d7", 32 pushEndpoint: "https://example.org/update/2", 33 scope: "https://example.com/page/2", 34 version: 1, 35 originAttributes: "", 36 quota: Infinity, 37 }, 38 { 39 channelID: "d239498b-1c85-4486-b99b-205866e82d1f", 40 pushEndpoint: "https://example.org/update/3", 41 scope: "https://example.com/page/3", 42 version: 3, 43 originAttributes: "", 44 quota: Infinity, 45 }, 46 { 47 channelID: "a50de97d-b496-43ce-8b53-05522feb78db", 48 pushEndpoint: "https://example.org/update/4", 49 scope: "https://example.com/page/4", 50 version: 10, 51 originAttributes: "", 52 quota: Infinity, 53 }, 54 ]; 55 for (let record of records) { 56 await db.put(record); 57 } 58 59 function observeMessage() { 60 ok(false, "Should not deliver malformed updates"); 61 } 62 registerCleanupFunction(() => 63 Services.obs.removeObserver(observeMessage, PushServiceComponent.pushTopic) 64 ); 65 Services.obs.addObserver(observeMessage, PushServiceComponent.pushTopic); 66 67 let notificationDone; 68 let notificationPromise = new Promise( 69 resolve => (notificationDone = after(2, resolve)) 70 ); 71 let prevHandler = PushServiceWebSocket._handleNotificationReply; 72 PushServiceWebSocket._handleNotificationReply = 73 function _handleNotificationReply() { 74 notificationDone(); 75 return prevHandler.apply(this, arguments); 76 }; 77 PushService.init({ 78 serverURI: "wss://push.example.org/", 79 db, 80 makeWebSocket(uri) { 81 return new MockWebSocket(uri, { 82 onHello() { 83 this.serverSendMsg( 84 JSON.stringify({ 85 messageType: "hello", 86 status: 200, 87 uaid: userAgentID, 88 }) 89 ); 90 this.serverSendMsg( 91 JSON.stringify({ 92 // Missing "updates" field; should ignore message. 93 messageType: "notification", 94 }) 95 ); 96 this.serverSendMsg( 97 JSON.stringify({ 98 messageType: "notification", 99 updates: [ 100 { 101 // Wrong channel ID field type. 102 channelID: 123, 103 version: 3, 104 }, 105 { 106 // Missing version field. 107 channelID: "3ad1ed95-d37a-4d88-950f-22cbe2e240d7", 108 }, 109 { 110 // Wrong version field type. 111 channelID: "d239498b-1c85-4486-b99b-205866e82d1f", 112 version: true, 113 }, 114 { 115 // Negative versions should be ignored. 116 channelID: "a50de97d-b496-43ce-8b53-05522feb78db", 117 version: -5, 118 }, 119 ], 120 }) 121 ); 122 }, 123 onACK() { 124 ok(false, "Should not acknowledge malformed updates"); 125 }, 126 }); 127 }, 128 }); 129 130 await notificationPromise; 131 132 let storeRecords = await db.getAllKeyIDs(); 133 storeRecords.sort(({ pushEndpoint: a }, { pushEndpoint: b }) => 134 compareAscending(a, b) 135 ); 136 recordsAreEqual(records, storeRecords); 137 }); 138 139 function recordIsEqual(a, b) { 140 strictEqual(a.channelID, b.channelID, "Wrong channel ID in record"); 141 strictEqual(a.pushEndpoint, b.pushEndpoint, "Wrong push endpoint in record"); 142 strictEqual(a.scope, b.scope, "Wrong scope in record"); 143 strictEqual(a.version, b.version, "Wrong version in record"); 144 } 145 146 function recordsAreEqual(a, b) { 147 equal(a.length, b.length, "Mismatched record count"); 148 for (let i = 0; i < a.length; i++) { 149 recordIsEqual(a[i], b[i]); 150 } 151 }