Skip to content

Commit

Permalink
test: add tests for integrity-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 16, 2018
1 parent 0fb45e7 commit e3845d5
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/integrity-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

const test = require('tap').test

const ssri = require('..')

test('generates integrity', t => {
const TARGET = ssri.fromData('foo')
const stream = ssri.integrityStream()
stream.write('foo')
let collected = ''
stream.on('data', d => { collected += d.toString() })
let integrity
stream.on('integrity', i => { integrity = i })
stream.on('end', () => {
t.equal(collected, 'foo', 'stream output is complete')
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
t.done()
})
stream.end()
})

test('optional algorithms option', t => {
const TARGET = ssri.fromData('foo', {algorithms: ['sha1', 'sha256']})
const stream = ssri.integrityStream({algorithms: ['sha1', 'sha256']})
stream.write('foo')
stream.on('data', () => {})
let integrity
stream.on('integrity', i => { integrity = i })
stream.on('end', () => {
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
t.done()
})
stream.end()
})

test('verification for correct data succeeds', t => {
const TARGET = ssri.fromData('foo')
const stream = ssri.integrityStream({
integrity: TARGET
})
stream.write('foo')
let collected = ''
stream.on('data', d => { collected += d.toString() })
let integrity
stream.on('integrity', i => { integrity = i })
stream.on('end', () => {
t.equal(collected, 'foo', 'stream output is complete')
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
t.done()
})
stream.end()
})

test('verification for wrong data fails', t => {
const stream = ssri.integrityStream({
integrity: ssri.fromData('bar')
})
stream.write('foo')
stream.on('data', () => {})
stream.on('error', err => {
t.equal(err.code, 'EINTEGRITY', 'errors with EINTEGRITY on mismatch')
t.done()
})
stream.end()
})

0 comments on commit e3845d5

Please sign in to comment.