22import { Buffer } from 'safe-buffer' ;
33import { expect } from 'chai' ;
44import { test } from '../util' ;
5- import { fixtures , type Fixture } from './fixtures' ;
5+ import { fixtures , type ValidFixture } from './fixtures' ;
66
77import crypto from 'react-native-quick-crypto' ;
8- import type { BinaryLike , HashAlgorithm } from 'react-native-quick-crypto' ;
8+ import type { BinaryLike } from 'react-native-quick-crypto' ;
99
1010type TestFixture = [ string , string , number , number , string ] ;
1111
@@ -27,18 +27,11 @@ const SUITE = 'pbkdf2';
2727 length : number ,
2828 expected : string ,
2929 ) => {
30- crypto . pbkdf2 (
31- pass ,
32- salt ,
33- iterations ,
34- length ,
35- hash as HashAlgorithm ,
36- function ( err , result ) {
37- expect ( err ) . to . be . null ;
38- expect ( result ) . not . to . be . null ;
39- expect ( result ?. toString ( 'hex' ) ) . to . equal ( expected ) ;
40- } ,
41- ) ;
30+ crypto . pbkdf2 ( pass , salt , iterations , length , hash , function ( err , result ) {
31+ expect ( err ) . to . be . null ;
32+ expect ( result ) . not . to . be . null ;
33+ expect ( result ?. toString ( 'hex' ) ) . to . equal ( expected ) ;
34+ } ) ;
4235 } ;
4336
4437 const kTests : TestFixture [ ] = [
@@ -75,7 +68,7 @@ const SUITE = 'pbkdf2';
7568}
7669
7770test ( SUITE , 'handles buffers' , ( ) => {
78- const resultSync = crypto . pbkdf2Sync ( 'password' , 'salt' , 1 , 32 ) ;
71+ const resultSync = crypto . pbkdf2Sync ( 'password' , 'salt' , 1 , 32 , 'sha1' ) ;
7972 expect ( resultSync ?. toString ( 'hex' ) ) . to . equal (
8073 '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164' ,
8174 ) ;
@@ -125,8 +118,7 @@ test(
125118
126119const algos = [ 'sha1' , 'sha224' , 'sha256' , 'sha384' , 'sha512' , 'ripemd160' ] ;
127120algos . forEach ( function ( algorithm ) {
128- fixtures . valid . forEach ( function ( f : Fixture ) {
129- // TODO: check these types once nitro port is done
121+ fixtures . valid . forEach ( function ( f : ValidFixture ) {
130122 let key : BinaryLike , keyType : string , salt : BinaryLike , saltType : string ;
131123 if ( f . keyUint8Array ) {
132124 key = new Uint8Array ( f . keyUint8Array ) ;
@@ -160,7 +152,7 @@ algos.forEach(function (algorithm) {
160152 salt = f . salt as BinaryLike ;
161153 saltType = 'string' ;
162154 }
163- const expected = f . results ? f . results [ algorithm ] : undefined ;
155+ const expected = f . results [ algorithm ] ;
164156 const description =
165157 algorithm +
166158 ' encodes "' +
@@ -180,9 +172,9 @@ algos.forEach(function (algorithm) {
180172 crypto . pbkdf2 (
181173 key ,
182174 salt ,
183- f . iterations as number ,
184- f . dkLen as number ,
185- algorithm as HashAlgorithm ,
175+ f . iterations ,
176+ f . dkLen ,
177+ algorithm ,
186178 function ( err , result ) {
187179 expect ( err ) . to . be . null ;
188180 expect ( result ) . not . to . be . null ;
@@ -195,42 +187,16 @@ algos.forEach(function (algorithm) {
195187 const result = crypto . pbkdf2Sync (
196188 key ,
197189 salt ,
198- f . iterations as number ,
199- f . dkLen as number ,
200- algorithm as HashAlgorithm ,
190+ f . iterations ,
191+ f . dkLen ,
192+ algorithm ,
201193 ) ;
202194 expect ( result ?. toString ( 'hex' ) ) . to . equal ( expected ) ;
203195 } ) ;
204196 } ) ;
205197
206- // // TODO: fix the 'invalid' tests
207- // fixtures.invalid.forEach(function (f: Fixture) {
208- // const description = algorithm + ' should throw ' + f.exception;
209-
210- // test(SUITE, ' async w/ ' + description, function () {
211- // function noop() {}
212- // expect(() => {
213- // crypto.pbkdf2(
214- // f.key as BinaryLike,
215- // f.salt as BinaryLike,
216- // f.iterations as number,
217- // f.dkLen as number,
218- // algorithm as HashAlgorithm,
219- // noop
220- // )
221- // }).to.throw(f.exception);
222- // });
223-
224- // test(SUITE, ' sync w/' + description, function () {
225- // expect(() => {
226- // crypto.pbkdf2Sync(
227- // f.key as BinaryLike,
228- // f.salt as BinaryLike,
229- // f.iterations as number,
230- // f.dkLen as number,
231- // algorithm as HashAlgorithm,
232- // )
233- // }).to.throw(f.exception);
234- // });
235- // });
198+ // TODO(#931): enable invalid fixture tests once JS-side validation is added
199+ // for iterations/keylen. Currently invalid values (negative, NaN, Infinity)
200+ // reach the native C layer and trigger assert()/abort() instead of throwing.
201+ // See: fixtures.invalid
236202} ) ;
0 commit comments