Skip to content

Commit e689066

Browse files
committed
crypto: add new tests for the lazy-transform
1 parent 41d63aa commit e689066

File tree

4 files changed

+71
-41
lines changed

4 files changed

+71
-41
lines changed

lib/crypto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const timingSafeEqual = binding.timingSafeEqual;
2121
const Buffer = require('buffer').Buffer;
2222
const stream = require('stream');
2323
const util = require('util');
24-
const LazyTransform = require('internal/streams/lazy_transform');
24+
const LazyTransform = require('internal/crypto/lazy_transform');
2525

2626
const DH_GENERATOR = 2;
2727

node.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
'lib/internal/util.js',
9393
'lib/internal/v8_prof_polyfill.js',
9494
'lib/internal/v8_prof_processor.js',
95-
'lib/internal/streams/lazy_transform.js',
95+
'lib/internal/crypto/lazy_transform.js',
9696
'lib/internal/streams/BufferList.js',
9797
'deps/v8/tools/splaytree.js',
9898
'deps/v8/tools/codemap.js',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
if (!common.hasCrypto) {
6+
common.skip('missing crypto');
7+
return;
8+
}
9+
10+
const crypto = require('crypto');
11+
12+
const UTF_INPUT = 'öäü';
13+
const TEST_CASES = [
14+
{
15+
options: undefined,
16+
// Hash of "öäü" in default format (utf8)
17+
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
18+
},
19+
{
20+
options: {},
21+
// Hash of "öäü" in default format (utf8)
22+
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
23+
},
24+
{
25+
options: {
26+
defaultEncoding: 'latin1',
27+
},
28+
// Hash of "öäü" in latin1 format
29+
output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830',
30+
},
31+
{
32+
options: {
33+
defaultEncoding: 'utf8',
34+
},
35+
// Hash of "öäü" in utf8 format
36+
output: '4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c',
37+
},
38+
{
39+
options: {
40+
defaultEncoding: 'ascii',
41+
},
42+
// Hash of "öäü" in ascii format
43+
output: 'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830',
44+
},
45+
];
46+
47+
for (const test of TEST_CASES) {
48+
const hashValue = crypto.createHash('sha256')
49+
.update(UTF_INPUT, test.options && test.options.defaultEncoding)
50+
.digest('hex');
51+
52+
assert.equal(hashValue, test.output);
53+
}
54+
55+
for (const test of TEST_CASES) {
56+
const hash = crypto.createHash('sha256', test.options);
57+
let hashValue = '';
58+
59+
hash.on('data', (data) => {
60+
hashValue += data.toString('hex');
61+
});
62+
63+
hash.on('end', common.mustCall(() => {
64+
assert.equal(hashValue, test.output);
65+
}));
66+
67+
hash.write(UTF_INPUT);
68+
hash.end();
69+
}

test/parallel/test-crypto.js

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -140,42 +140,3 @@ assert.throws(function() {
140140

141141
// Make sure memory isn't released before being returned
142142
console.log(crypto.randomBytes(16));
143-
144-
/**
145-
* Check if the stream function uses utf8 as a default encoding.
146-
**/
147-
148-
function testEncoding(options, assertionHash) {
149-
const hash = crypto.createHash('sha256', options);
150-
let hashValue = '';
151-
152-
hash.on('data', (data) => {
153-
hashValue += data.toString('hex');
154-
});
155-
156-
hash.on('end', () => {
157-
assert.equal(hashValue, assertionHash);
158-
});
159-
160-
hash.write('öäü');
161-
hash.end();
162-
}
163-
164-
// Hash of "öäü" in utf8 format
165-
const assertionHashUtf8 =
166-
'4f53d15bee524f082380e6d7247cc541e7cb0d10c64efdcc935ceeb1e7ea345c';
167-
168-
// Hash of "öäü" in ascii format
169-
const assertionHashAscii =
170-
'cd37bccd5786e2e76d9b18c871e919e6eb11cc12d868f5ae41c40ccff8e44830';
171-
172-
testEncoding(undefined, assertionHashUtf8);
173-
testEncoding({}, assertionHashUtf8);
174-
175-
testEncoding({
176-
defaultEncoding: 'utf8'
177-
}, assertionHashUtf8);
178-
179-
testEncoding({
180-
defaultEncoding: 'ascii'
181-
}, assertionHashAscii);

0 commit comments

Comments
 (0)