|
| 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