-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.test.js
More file actions
172 lines (146 loc) · 6.71 KB
/
index.test.js
File metadata and controls
172 lines (146 loc) · 6.71 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2017 ExcitableAardvark <excitableaardvark@tutanota.de>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* eslint-env jest */
const cryptonight = require('./')
test('async hash of empty string', done => {
return cryptonight.asyncHash(Buffer.from(''), data => {
expect(data.toString('hex'))
.toBe('eb14e8a833fac6fe9a43b57b336789c46ffe93f2868452240720607b14387e11')
done()
})
})
test('async hash of empty string with variant 0', done => {
return cryptonight.asyncHash(Buffer.from(''), 0, data => {
expect(data.toString('hex'))
.toBe('eb14e8a833fac6fe9a43b57b336789c46ffe93f2868452240720607b14387e11')
done()
})
})
test('async hash of empty string with variant 1', () => {
expect(() => cryptonight.asyncHash(Buffer.from(''), 1, data => {}))
.toThrow(/43/)
})
test('async hash of test string', done => {
return cryptonight.asyncHash(Buffer.from('This is a test'), data => {
expect(data.toString('hex'))
.toBe('a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605')
done()
})
})
test('async hash of test string with variant 1', done => {
return cryptonight.asyncHash(Buffer.from('This is a test which as at least 43 bytes ...'), 1, data => {
expect(data.toString('hex'))
.toBe('bf1b87e049bfe1c668c44f2dc1bb689abcc729a704fc8088917cfbca202fc3cb')
done()
})
})
test('async hash of test string with variant 2', done => {
return cryptonight.asyncHash(Buffer.from('This is a test This is a test This is a test'), 2, data => {
expect(data.toString('hex'))
.toBe('353fdc068fd47b03c04b9431e005e00b68c2168a3cc7335c8b9b308156591a4f')
done()
})
})
test('sync hash of test string with variant 4 (monero test vector)', done => {
return cryptonight.asyncHash(Buffer.from('73756e7420696e2063756c706120717569206f666669636961206465736572756e74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e', 'hex'), 4, 1806269, data => {
expect(data.toString('hex'))
.toBe('75c6f2ae49a20521de97285b431e717125847fb8935ed84a61e7f8d36a2c3d8e')
done()
})
})
test('async hash of test string with variant 0', done => {
return cryptonight.asyncHash(Buffer.from('This is a test'), 0, data => {
expect(data.toString('hex'))
.toBe('a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605')
done()
})
})
test('async invalid argument throws exception', () => {
expect(() => cryptonight.asyncHash('not a buffer'))
.toThrow(/buffer/)
})
test('async no argument throws exception', () => {
expect(() => cryptonight.asyncHash())
.toThrow(/buffer/)
})
test('invalid arguments throws exception', () => {
expect(() => cryptonight.asyncHash(Buffer.from(''), { invalid: true }, () => {}))
.toThrow(/number/)
})
test('invalid argument order throws exception', () => {
expect(() => cryptonight.asyncHash(Buffer.from(''), 0, 0, 0))
.toThrow(/Fourth argument must be a callback/)
})
test('extra arguments throws exception', () => {
expect(() => cryptonight.asyncHash(Buffer.from(''), 0, 0, () => {}, 1))
.toThrow(/Invalid/)
})
test('sync hash of empty string', () => {
expect(cryptonight.hash(Buffer.from('')).toString('hex'))
.toBe('eb14e8a833fac6fe9a43b57b336789c46ffe93f2868452240720607b14387e11')
})
test('sync hash of empty string with variant 0', () => {
expect(cryptonight.hash(Buffer.from(''), 0).toString('hex'))
.toBe('eb14e8a833fac6fe9a43b57b336789c46ffe93f2868452240720607b14387e11')
})
test('sync hash of empty string with variant 1', () => {
expect(() => cryptonight.hash(Buffer.from(''), 1))
.toThrow(/43/)
})
test('sync hash of test string with variant 1', () => {
expect(cryptonight.hash(Buffer.from('This is a test which as at least 43 bytes ...'), 1).toString('hex'))
.toBe('bf1b87e049bfe1c668c44f2dc1bb689abcc729a704fc8088917cfbca202fc3cb')
})
test('sync hash of test string with variant 2', () => {
expect(cryptonight.hash(Buffer.from('This is a test This is a test This is a test'), 2).toString('hex'))
.toBe('353fdc068fd47b03c04b9431e005e00b68c2168a3cc7335c8b9b308156591a4f')
})
test('sync hash of test string with variant 2 (monero test vector)', () => {
expect(cryptonight.hash(Buffer.from('73756e7420696e2063756c706120717569206f666669636961206465736572756e74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e', 'hex'), 2).toString('hex'))
.toBe('2659ff95fc74b6215c1dc741e85b7a9710101b30620212f80eb59c3c55993f9d')
})
test('sync hash of test string with variant 4 (monero test vector)', () => {
expect(cryptonight.hash(Buffer.from('73756e7420696e2063756c706120717569206f666669636961206465736572756e74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e', 'hex'), 4, 1806269).toString('hex'))
.toBe('75c6f2ae49a20521de97285b431e717125847fb8935ed84a61e7f8d36a2c3d8e')
})
test('sync invalid argument throws exception', () => {
expect(() => cryptonight.hash('not a buffer'))
.toThrow(/buffer/)
})
test('sync no argument throws exception', () => {
expect(() => cryptonight.hash())
.toThrow(/buffer/)
})
test('sync extra arguments throws exception', () => {
expect(() => cryptonight.hash(Buffer.from(''), { invalid: true }))
.toThrow(/number/)
})
test('sync extra arguments throws exception', () => {
expect(() => cryptonight.hash(Buffer.from(''), 0, { invalid: true }))
.toThrow(/Third argument must be a number/)
})