summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_notification_incomplete.js
blob: 48aba51132383723c812f5152840c107bb90d2cc (plain)
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const userAgentID = "1ca1cf66-eeb4-4df7-87c1-d5c92906ab90";

function run_test() {
  do_get_profile();
  setPrefs({
    userAgentID,
  });
  run_next_test();
}

add_task(async function test_notification_incomplete() {
  let db = PushServiceWebSocket.newPushDB();
  registerCleanupFunction(() => {
    return db.drop().then(_ => db.close());
  });
  let records = [
    {
      channelID: "123",
      pushEndpoint: "https://example.org/update/1",
      scope: "https://example.com/page/1",
      version: 1,
      originAttributes: "",
      quota: Infinity,
    },
    {
      channelID: "3ad1ed95-d37a-4d88-950f-22cbe2e240d7",
      pushEndpoint: "https://example.org/update/2",
      scope: "https://example.com/page/2",
      version: 1,
      originAttributes: "",
      quota: Infinity,
    },
    {
      channelID: "d239498b-1c85-4486-b99b-205866e82d1f",
      pushEndpoint: "https://example.org/update/3",
      scope: "https://example.com/page/3",
      version: 3,
      originAttributes: "",
      quota: Infinity,
    },
    {
      channelID: "a50de97d-b496-43ce-8b53-05522feb78db",
      pushEndpoint: "https://example.org/update/4",
      scope: "https://example.com/page/4",
      version: 10,
      originAttributes: "",
      quota: Infinity,
    },
  ];
  for (let record of records) {
    await db.put(record);
  }

  function observeMessage(subject, topic, data) {
    ok(false, "Should not deliver malformed updates");
  }
  registerCleanupFunction(() =>
    Services.obs.removeObserver(observeMessage, PushServiceComponent.pushTopic)
  );
  Services.obs.addObserver(observeMessage, PushServiceComponent.pushTopic);

  let notificationDone;
  let notificationPromise = new Promise(
    resolve => (notificationDone = after(2, resolve))
  );
  let prevHandler = PushServiceWebSocket._handleNotificationReply;
  PushServiceWebSocket._handleNotificationReply =
    function _handleNotificationReply() {
      notificationDone();
      return prevHandler.apply(this, arguments);
    };
  PushService.init({
    serverURI: "wss://push.example.org/",
    db,
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          this.serverSendMsg(
            JSON.stringify({
              messageType: "hello",
              status: 200,
              uaid: userAgentID,
            })
          );
          this.serverSendMsg(
            JSON.stringify({
              // Missing "updates" field; should ignore message.
              messageType: "notification",
            })
          );
          this.serverSendMsg(
            JSON.stringify({
              messageType: "notification",
              updates: [
                {
                  // Wrong channel ID field type.
                  channelID: 123,
                  version: 3,
                },
                {
                  // Missing version field.
                  channelID: "3ad1ed95-d37a-4d88-950f-22cbe2e240d7",
                },
                {
                  // Wrong version field type.
                  channelID: "d239498b-1c85-4486-b99b-205866e82d1f",
                  version: true,
                },
                {
                  // Negative versions should be ignored.
                  channelID: "a50de97d-b496-43ce-8b53-05522feb78db",
                  version: -5,
                },
              ],
            })
          );
        },
        onACK() {
          ok(false, "Should not acknowledge malformed updates");
        },
      });
    },
  });

  await notificationPromise;

  let storeRecords = await db.getAllKeyIDs();
  storeRecords.sort(({ pushEndpoint: a }, { pushEndpoint: b }) =>
    compareAscending(a, b)
  );
  recordsAreEqual(records, storeRecords);
});

function recordIsEqual(a, b) {
  strictEqual(a.channelID, b.channelID, "Wrong channel ID in record");
  strictEqual(a.pushEndpoint, b.pushEndpoint, "Wrong push endpoint in record");
  strictEqual(a.scope, b.scope, "Wrong scope in record");
  strictEqual(a.version, b.version, "Wrong version in record");
}

function recordsAreEqual(a, b) {
  equal(a.length, b.length, "Mismatched record count");
  for (let i = 0; i < a.length; i++) {
    recordIsEqual(a[i], b[i]);
  }
}