Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for randombytes in web worker context #10

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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('./tests/webworker.js'))
w.addEventListener('message', function (e) {
var arr = e.data[0]
t.notEqual(arrConst, arr, 'Array should contain random bytes')
t.end()
window.close()
})
var arr = new Uint8Array(16)
w.postMessage([arr])
})
}
10 changes: 10 additions & 0 deletions tests/webworker.js
Original file line number Diff line number Diff line change
@@ -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()
})
}