From 95bcdb89eabf001c30dc2d314091c3983fcfb4e0 Mon Sep 17 00:00:00 2001 From: Nick Peihl Date: Tue, 7 Nov 2017 09:45:56 -0800 Subject: [PATCH] Add test for randombytes in web worker context --- package.json | 3 ++- test-worker.js | 10 ++++++++++ test.js | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test-worker.js diff --git a/package.json b/package.json index f39ea47..31c7368 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "devDependencies": { "browser-run": "^4.0.2", "browserify": "^14.1.0", - "sodium-test": "^0.7.0" + "sodium-test": "^0.7.0", + "webworkify": "^1.4.0" }, "scripts": { "browser": "browserify test.js | browser-run", diff --git a/test-worker.js b/test-worker.js new file mode 100644 index 0000000..c60e117 --- /dev/null +++ b/test-worker.js @@ -0,0 +1,10 @@ +var sodium = require('.') + +module.exports = function (self) { + self.addEventListener('message', function (e) { + var arr = e.data[0] + sodium.randombytes_buf(arr) + self.postMessage(arr) + self.close() + }) +} diff --git a/test.js b/test.js index b6c0192..907afa2 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,18 @@ require('sodium-test')(require('.')) -if (typeof window !== 'undefined') window.close() +if (typeof window !== 'undefined') { + var test = require('tape') + var arrConst = new Uint8Array(16) + test('randombytes works in web worker context', function (t) { + var work = require('webworkify') + var w = work(require('./test-worker.js')) + w.addEventListener('message', function (e) { + var arr = e.data[0] + t.notEqual(arrConst, arr, '') + t.end() + window.close() + }) + var arr = new Uint8Array(16) + w.postMessage([arr]) + }) +}