-
Notifications
You must be signed in to change notification settings - Fork 21
/
integrity.js
98 lines (91 loc) · 2.33 KB
/
integrity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
const test = require('tap').test
const ssri = require('..')
test('toString()', t => {
const sri = ssri.parse('sha512-foo sha256-bar!')
t.equal(
sri.toString(),
'sha512-foo sha256-bar!',
'integrity objects from ssri.parse() can use toString()'
)
t.equal(
sri.toString({strict: true}),
'sha512-foo',
'accepts strict mode option'
)
t.equal(
sri.toString({sep: '\n'}),
'sha512-foo\nsha256-bar!',
'accepts separator option'
)
t.done()
})
test('concat()', t => {
const sri = ssri.parse('sha512-foo')
t.equal(
sri.concat('sha512-bar').toString(),
'sha512-foo sha512-bar',
'concatenates with a string'
)
t.equal(
sri.concat({digest: 'bar', algorithm: 'sha384'}).toString(),
'sha512-foo sha384-bar',
'concatenates with an IntegrityMetadata-like'
)
t.equal(
sri.concat({
'sha384': [{digest: 'bar', algorithm: 'sha384'}],
'sha1': [{digest: 'baz', algorithm: 'sha1'}]
}).toString(),
'sha512-foo sha384-bar sha1-baz',
'concatenates with an Integrity-like'
)
t.equal(
sri.concat(
{digest: 'bar', algorithm: 'sha1'}
).concat(
'sha1-baz'
).concat(
'sha512-quux'
).toString(),
'sha512-foo sha512-quux sha1-bar sha1-baz',
'preserves relative order for algorithms between different concatenations'
)
t.equal(
sri.concat('sha1-bar!', {strict: true}).toString(),
'sha512-foo',
'accepts strict mode option'
)
t.done()
})
test('pickAlgorithm()', t => {
const sri = ssri.parse('sha1-foo sha512-bar sha384-baz')
t.equal(sri.pickAlgorithm(), 'sha512', 'picked best algorithm')
t.equal(
ssri.parse('unknown-deadbeef uncertain-bada55').pickAlgorithm(),
'unknown',
'unrecognized algorithm returned if none others known'
)
t.equal(
sri.pickAlgorithm({
pickAlgorithm: (a, b) => 'sha384'
}),
'sha384',
'custom pickAlgorithm function accepted'
)
t.done()
})
test('IntegrityMetadata.hexDigest()', t => {
const sri = ssri.parse('sha512-bar', {single: true})
t.equal(
sri.hexDigest(),
(
Buffer.from ? Buffer.from('bar', 'base64') : new Buffer('bar', 'base64')
).toString('hex'),
'returned hex version of base64 digest')
t.done()
})
test('semi-private', t => {
t.equal(ssri.Integrity, undefined, 'Integrity class is module-private.')
t.done()
})