-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
const crypto = require('crypto') | ||
const fs = require('fs') | ||
const test = require('tap').test | ||
|
||
const ssri = require('..') | ||
|
||
const TEST_DATA = fs.readFileSync(__filename) | ||
|
||
function hash (data, algorithm) { | ||
return crypto.createHash(algorithm).update(data).digest('base64') | ||
} | ||
|
||
test('hashes should match when valid', t => { | ||
const sha = hash(TEST_DATA, 'sha512') | ||
const integrity = `sha512-${sha}` | ||
const parsed = ssri.parse(integrity, { single: true }) | ||
t.same( | ||
parsed.match(integrity, { single: true }), | ||
parsed, | ||
'should return the same algo when digest is equal (single option)' | ||
) | ||
t.same( | ||
parsed.match('sha-233', { single: true }), | ||
false, | ||
'invalid integrity should not match (single option)' | ||
) | ||
t.same( | ||
parsed.match(null, { single: true }), | ||
false, | ||
'null integrity just returns false (single option)' | ||
) | ||
|
||
t.same( | ||
parsed.match(integrity), | ||
parsed, | ||
'should return the same algo when digest is equal' | ||
) | ||
t.same( | ||
parsed.match('sha-233'), | ||
false, | ||
'invalid integrity should not match' | ||
) | ||
t.same( | ||
parsed.match(null), | ||
false, | ||
'null integrity just returns false' | ||
) | ||
t.end() | ||
}) |
This comment was marked as spam.
Sorry, something went wrong.