Add test for randombytes in web worker context

This commit is contained in:
Nick Peihl 2017-11-07 09:45:56 -08:00
parent b883b2a8df
commit 95bcdb89ea
3 changed files with 28 additions and 2 deletions

View File

@ -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",

10
test-worker.js Normal file
View File

@ -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()
})
}

17
test.js
View File

@ -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])
})
}