Bug 1225968 - Refactor data delivery tests to support addition of new tests, r=kitcambridge
authorMartin Thomson <martin.thomson@gmail.com>
Fri, 20 Nov 2015 14:21:24 -0800
changeset 310065 065e03c7e41618123c31c49271f61c145544352d
parent 310064 99dd62654b77b3ca88987caad6fbd829c725a6f6
child 310066 9d619f94515405f77fac0994233c57fe9ca91a56
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskitcambridge
bugs1225968
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1225968 - Refactor data delivery tests to support addition of new tests, r=kitcambridge
dom/push/test/xpcshell/test_notification_data.js
--- a/dom/push/test/xpcshell/test_notification_data.js
+++ b/dom/push/test/xpcshell/test_notification_data.js
@@ -25,17 +25,19 @@ function putRecord(channelID, scope, pub
     lastPush: 0,
     originAttributes: '',
     quota: Infinity,
     p256dhPublicKey: publicKey,
     p256dhPrivateKey: privateKey,
   });
 }
 
-add_task(function* test_notification_ack_data() {
+let ackDone;
+let server;
+add_task(function* test_notification_ack_data_setup() {
   db = PushServiceWebSocket.newPushDB();
   do_register_cleanup(() => {return db.drop().then(_ => db.close());});
 
   yield putRecord(
     'subscription1',
     'https://example.com/page/1',
     'BPCd4gNQkjwRah61LpdALdzZKLLnU5UAwDztQ5_h0QsT26jk0IFbqcK6-JxhHAm-rsHEwy0CyVJjtnfOcqc1tgA',
     {
@@ -72,122 +74,127 @@ add_task(function* test_notification_ack
       ext: true,
       key_ops: ["deriveBits"],
       kty: 'EC',
       x: 'OFQchNJ5WtZjJsWdvvKVVMIMMs91BYyl_yBeFxbC9po',
       y: 'Ja6n3YH8TOcH8narDF6t8mKVvg2ioLW-8MH5O4dzGcI'
     }
   );
 
-  let updates = [];
-  let notifyPromise = promiseObserverNotification('push-notification', function(subject, data) {
-    let notification = subject.QueryInterface(Ci.nsIPushObserverNotification);
-    updates.push({
-      scope: data,
-      data: notification.data,
-    });
-    return updates.length == 3;
-  });
+  let setupDone;
+  let setupDonePromise = new Promise(r => setupDone = r);
 
-  let acks = 0;
-  let ackDone;
-  let ackPromise = new Promise(resolve => ackDone = resolve);
   PushService.init({
     serverURI: "wss://push.example.org/",
     networkInfo: new MockDesktopNetworkInfo(),
     db,
     makeWebSocket(uri) {
       return new MockWebSocket(uri, {
         onHello(request) {
           equal(request.uaid, userAgentID,
-            'Should send matching device IDs in handshake');
+                'Should send matching device IDs in handshake');
           this.serverSendMsg(JSON.stringify({
             messageType: 'hello',
             uaid: userAgentID,
             status: 200,
             use_webpush: true,
           }));
-          // subscription1 will send a message with no rs and padding
-          // length 1.
-          this.serverSendMsg(JSON.stringify({
-            messageType: 'notification',
-            channelID: 'subscription1',
-            headers: {
-              encryption_key: 'keyid="notification1"; dh="BO_tgGm-yvYAGLeRe16AvhzaUcpYRiqgsGOlXpt0DRWDRGGdzVLGlEVJMygqAUECarLnxCiAOHTP_znkedrlWoU"',
-              encryption: 'keyid="notification1";salt="uAZaiXpOSfOLJxtOCZ09dA"',
-            },
-            data: 'NwrrOWPxLE8Sv5Rr0Kep7n0-r_j3rsYrUw_CXPo',
-            version: 'v1',
-          }));
+          server = this;
+          setupDone();
         },
         onACK(request) {
-          switch (++acks) {
-          case 1:
-            deepEqual([{
-              channelID: 'subscription1',
-              version: 'v1',
-            }], request.updates, 'Wrong updates for acknowledgement 1');
-            // subscription2 will send a message with no rs and padding
-            // length 16.
-            this.serverSendMsg(JSON.stringify({
-              messageType: 'notification',
-              channelID: 'subscription2',
-              headers: {
-                encryption_key: 'keyid="notification2"; dh="BKVdQcgfncpNyNWsGrbecX0zq3eHIlHu5XbCGmVcxPnRSbhjrA6GyBIeGdqsUL69j5Z2CvbZd-9z1UBH0akUnGQ"',
-                encryption: 'keyid="notification2";salt="vFn3t3M_k42zHBdpch3VRw"',
-              },
-              data: 'Zt9dEdqgHlyAL_l83385aEtb98ZBilz5tgnGgmwEsl5AOCNgesUUJ4p9qUU',
-              version: 'v2',
-            }));
-            break;
-
-          case 2:
-            deepEqual([{
-              channelID: 'subscription2',
-              version: 'v2',
-            }], request.updates, 'Wrong updates for acknowledgement 2');
-            // subscription3 will send a message with rs equal 24 and
-            // padding length 16.
-            this.serverSendMsg(JSON.stringify({
-              messageType: 'notification',
-              channelID: 'subscription3',
-              headers: {
-                encryption_key: 'keyid="notification3";dh="BD3xV_ACT8r6hdIYES3BJj1qhz9wyv7MBrG9vM2UCnjPzwE_YFVpkD-SGqE-BR2--0M-Yf31wctwNsO1qjBUeMg"',
-                encryption: 'keyid="notification3"; salt="DFq188piWU7osPBgqn4Nlg"; rs=24',
-              },
-              data: 'LKru3ZzxBZuAxYtsaCfaj_fehkrIvqbVd1iSwnwAUgnL-cTeDD-83blxHXTq7r0z9ydTdMtC3UjAcWi8LMnfY-BFzi0qJAjGYIikDA',
-              version: 'v3',
-            }));
-            break;
-
-          case 3:
-            deepEqual([{
-              channelID: 'subscription3',
-              version: 'v3',
-            }], request.updates, 'Wrong updates for acknowledgement 3');
-            ackDone();
-            break;
-
-          default:
-            ok(false, 'Unexpected acknowledgement ' + acks);
+          if (ackDone) {
+            ackDone(request.updates);
           }
         }
       });
     }
   });
-
-  yield waitForPromise(notifyPromise, DEFAULT_TIMEOUT,
-    'Timed out waiting for notifications');
-  yield waitForPromise(ackPromise, DEFAULT_TIMEOUT,
-    'Timed out waiting for multiple acknowledgements');
+  yield waitForPromise(setupDonePromise, DEFAULT_TIMEOUT,
+                       'Timed out waiting for notifications');
+});
 
-  updates.sort((a, b) => a.scope < b.scope ? -1 : a.scope > b.scope ? 1 : 0);
-  deepEqual([{
-    scope: 'https://example.com/page/1',
-    data: 'Some message',
-  }, {
-    scope: 'https://example.com/page/2',
-    data: 'Some message',
-  }, {
-    scope: 'https://example.com/page/3',
-    data: 'Some message',
-  }], updates, 'Wrong data for notifications');
+add_task(function* test_notification_ack_data() {
+  let allTestData = [
+    {
+      channelID: 'subscription1',
+      version: 'v1',
+      send: {
+        headers: {
+          encryption_key: 'keyid="notification1"; dh="BO_tgGm-yvYAGLeRe16AvhzaUcpYRiqgsGOlXpt0DRWDRGGdzVLGlEVJMygqAUECarLnxCiAOHTP_znkedrlWoU"',
+          encryption: 'keyid="notification1";salt="uAZaiXpOSfOLJxtOCZ09dA"',
+        },
+        data: 'NwrrOWPxLE8Sv5Rr0Kep7n0-r_j3rsYrUw_CXPo',
+        version: 'v1',
+      },
+      receive: {
+        scope: 'https://example.com/page/1',
+        data: 'Some message'
+      }
+    },
+    {
+      channelID: 'subscription2',
+      version: 'v2',
+      send: {
+        headers: {
+          encryption_key: 'keyid="notification2"; dh="BKVdQcgfncpNyNWsGrbecX0zq3eHIlHu5XbCGmVcxPnRSbhjrA6GyBIeGdqsUL69j5Z2CvbZd-9z1UBH0akUnGQ"',
+          encryption: 'keyid="notification2";salt="vFn3t3M_k42zHBdpch3VRw"',
+        },
+        data: 'Zt9dEdqgHlyAL_l83385aEtb98ZBilz5tgnGgmwEsl5AOCNgesUUJ4p9qUU',
+      },
+      receive: {
+        scope: 'https://example.com/page/2',
+        data: 'Some message'
+      }
+    },
+    {
+      channelID: 'subscription3',
+      version: 'v3',
+      send: {
+        headers: {
+          encryption_key: 'keyid="notification3";dh="BD3xV_ACT8r6hdIYES3BJj1qhz9wyv7MBrG9vM2UCnjPzwE_YFVpkD-SGqE-BR2--0M-Yf31wctwNsO1qjBUeMg"',
+          encryption: 'keyid="notification3"; salt="DFq188piWU7osPBgqn4Nlg"; rs=24',
+        },
+        data: 'LKru3ZzxBZuAxYtsaCfaj_fehkrIvqbVd1iSwnwAUgnL-cTeDD-83blxHXTq7r0z9ydTdMtC3UjAcWi8LMnfY-BFzi0qJAjGYIikDA',
+      },
+      receive: {
+        scope: 'https://example.com/page/3',
+        data: 'Some message'
+      }
+    }
+  ];
+
+  let sendAndReceive = testData => {
+    let messageReceived = promiseObserverNotification('push-notification', (subject, data) => {
+      dump("push-notification\n");
+      let notification = subject.QueryInterface(Ci.nsIPushObserverNotification);
+      equal(notification.data, testData.receive.data,
+            'Wrong data for notification ' + testData.version);
+      equal(data, testData.receive.scope,
+            'Wrong scope for notification ' + testData.version);
+      return true;
+    });
+
+    let ackReceived = new Promise(resolve => ackDone = resolve)
+        .then(ackData => {
+          dump("push-ack\n");
+          deepEqual([{
+            channelID: testData.channelID,
+            version: testData.version
+          }], ackData, 'Wrong updates for acknowledgment ' + testData.version);
+        });
+
+    let msg = JSON.parse(JSON.stringify(testData.send));
+    msg.messageType = 'notification';
+    msg.channelID = testData.channelID;
+    msg.version = testData.version;
+    server.serverSendMsg(JSON.stringify(msg));
+
+    return Promise.all([messageReceived, ackReceived]);
+  };
+
+  yield waitForPromise(
+    allTestData.reduce((p, testData) => {
+      return p.then(_ => sendAndReceive(testData));
+    }, Promise.resolve()),
+    DEFAULT_TIMEOUT,
+    'Timed out waiting for message exchange to complete');
 });