test_crypto.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/ */
  3. function run_test() {
  4. let Cu = Components.utils;
  5. let sb = new Cu.Sandbox('https://www.example.com',
  6. { wantGlobalProperties:
  7. ["crypto", "TextEncoder", "TextDecoder"]
  8. });
  9. sb.ok = ok;
  10. Cu.evalInSandbox('ok(this.crypto);', sb);
  11. Cu.evalInSandbox('ok(this.crypto.subtle);', sb);
  12. sb.do_check_eq = do_check_eq;
  13. let innerPromise = new Promise(r => (sb.test_done = r));
  14. Cu.evalInSandbox('crypto.subtle.digest("SHA-256", ' +
  15. ' new TextEncoder("utf-8").encode("abc"))' +
  16. ' .then(h => do_check_eq(new Uint16Array(h)[0], 30906))' +
  17. ' .then(test_done);', sb);
  18. Cu.importGlobalProperties(["crypto"]);
  19. ok(crypto);
  20. ok(crypto.subtle);
  21. let outerPromise = crypto.subtle.digest("SHA-256", new TextEncoder("utf-8").encode("abc"))
  22. .then(h => do_check_eq(new Uint16Array(h)[0], 30906));
  23. do_test_pending();
  24. Promise.all([innerPromise, outerPromise]).then(() => do_test_finished());
  25. }