Skip to content

Commit e3845d5

Browse files
committed
test: add tests for integrity-stream
1 parent 0fb45e7 commit e3845d5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/integrity-stream.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
5+
const ssri = require('..')
6+
7+
test('generates integrity', t => {
8+
const TARGET = ssri.fromData('foo')
9+
const stream = ssri.integrityStream()
10+
stream.write('foo')
11+
let collected = ''
12+
stream.on('data', d => { collected += d.toString() })
13+
let integrity
14+
stream.on('integrity', i => { integrity = i })
15+
stream.on('end', () => {
16+
t.equal(collected, 'foo', 'stream output is complete')
17+
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
18+
t.done()
19+
})
20+
stream.end()
21+
})
22+
23+
test('optional algorithms option', t => {
24+
const TARGET = ssri.fromData('foo', {algorithms: ['sha1', 'sha256']})
25+
const stream = ssri.integrityStream({algorithms: ['sha1', 'sha256']})
26+
stream.write('foo')
27+
stream.on('data', () => {})
28+
let integrity
29+
stream.on('integrity', i => { integrity = i })
30+
stream.on('end', () => {
31+
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
32+
t.done()
33+
})
34+
stream.end()
35+
})
36+
37+
test('verification for correct data succeeds', t => {
38+
const TARGET = ssri.fromData('foo')
39+
const stream = ssri.integrityStream({
40+
integrity: TARGET
41+
})
42+
stream.write('foo')
43+
let collected = ''
44+
stream.on('data', d => { collected += d.toString() })
45+
let integrity
46+
stream.on('integrity', i => { integrity = i })
47+
stream.on('end', () => {
48+
t.equal(collected, 'foo', 'stream output is complete')
49+
t.deepEqual(integrity, TARGET, 'matching integrity emitted')
50+
t.done()
51+
})
52+
stream.end()
53+
})
54+
55+
test('verification for wrong data fails', t => {
56+
const stream = ssri.integrityStream({
57+
integrity: ssri.fromData('bar')
58+
})
59+
stream.write('foo')
60+
stream.on('data', () => {})
61+
stream.on('error', err => {
62+
t.equal(err.code, 'EINTEGRITY', 'errors with EINTEGRITY on mismatch')
63+
t.done()
64+
})
65+
stream.end()
66+
})

0 commit comments

Comments
 (0)