chromium/third_party/blink/web_tests/http/tests/push_messaging/application-server-key-format-test.html

<!DOCTYPE html>
<html>
  <head>
    <title>
      Subscribing with applicationServerKey should succeed only when the
      applicationServerKey is valid.
    </title>
    <script src="./resources/push-constants.js"></script>
    <script src="./resources/test-helpers.js"></script>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../resources/testharness-helpers.js"></script>
    <script src="../serviceworker/resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Subscribe should fail given a gcm sender id encoded as base64 url safe string.
      promise_test(function(test) {
        return registerAndSubscribePushWithBase64urlString(test, btoa('0123456789'))
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not encoded as base64url without padding'));
            });
      }, 'Subscribing with a gcm sender id encoded as base64 url safe string should fail');

      // Subscribe should succeed given a valid numeric sender ID.
      promise_test(function(test) {
        return registerAndSubscribePushWithString(test, '0123456789')
            .then(function(pushSubscription) {
              assert_true(
                  pushSubscription.endpoint.includes('StandardizedEndpoint'));
            });
      }, 'Subscribing with a valid numeric sender ID should succeed');

      // Subscribe should succeed given a valid p256 key.
      promise_test(function(test) {
        return registerAndSubscribePush(test, new Uint8Array(PUBLIC_KEY))
            .then(function(pushSubscription) {
              assert_true(
                  pushSubscription.endpoint.includes('StandardizedEndpoint'));
            });
      }, 'Subscribing with a valid p256 applicationServerKey should succeed');

      // Subscribe should succeed given a valid base64url encoded key.
      promise_test(function(test) {
        return registerAndSubscribePushWithBase64urlString(test, VALID_BASE64URL_ENCODED_KEY)
            .then(function(pushSubscription) {
              assert_true(
                  pushSubscription.endpoint.includes('StandardizedEndpoint'));
            });
      }, 'Subscribing with a valid base64url encoded key should succeed');

      // Subscribe should fail given a base64 encoded key instead of base64url.
      promise_test(function(test) {
        return registerAndSubscribePushWithBase64urlString(test, INVALID_BASE64_ENCODED_KEY)
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not encoded as base64url without padding'));
            });
      }, 'Subscribing with a base64 encoded string should fail');

      // Subscribe should fail given an invalid base64url encoded string.
      promise_test(function(test) {
        return registerAndSubscribePushWithBase64urlString(test, INVALID_BASE64URL_ENCODED_KEY)
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not encoded as base64url without padding'));
            });
      }, 'Subscribing with an invalid base64url encoded string should fail');

      // Subscribe should fail given a non-numeric sender ID.
      promise_test(function(test) {
        return registerAndSubscribePushWithString(test, '01234a56789')
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not valid.'));
            });
       }, 'Subscribing with a non-numeric sender ID should fail');

      // Subscribe should fail given an empty applicationServerKey.
      promise_test(function(test) {
        return registerAndSubscribePushWithString(test, '')
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not valid.'));
            });
      }, 'Subscribing with an empty applicationServerKey should fail');

      // Subscribe should fail given a too long applicationServerKey.
      promise_test(function(test) {
        const longKey = new Uint8Array(300);
        return registerAndSubscribePush(test, longKey)
            .then(function(pushSubscription) {
              assert_unreached('Subscribe should have failed.');
            })
            .catch (function(e) {
              assert_true(e.message.includes(
                  'The provided applicationServerKey is not valid.'));
            });
      }, 'Subscribing with a too long applicationServerKey should fail');
    </script>
  </body>
</html>