Skip to content

Commit

Permalink
fixed random,hash tests and upgraded chai
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed Jan 23, 2024
1 parent 8c1e93d commit 068cf78
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 308 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@react-navigation/native": "^6.1.6",
"@react-navigation/native-stack": "^6.9.12",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"chai": "^5.0.0",
"crypto-browserify": "^3.12.0",
"eslint": "^8.4.1",
"events": "^3.3.0",
Expand Down
5 changes: 1 addition & 4 deletions example/src/components/TestItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ const styles = StyleSheet.create({
alignContent: 'center',
alignItems: 'center',
justifyContent: 'space-evenly',
marginTop: 10,
gap: 20,
borderBottomWidth: 1,
borderBottomColor: '#ccc',
},
label: {
flex: 1,
},
count: {
alignSelf: 'flex-end',
},
count: {},
});
7 changes: 4 additions & 3 deletions example/src/hooks/useRunTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ export const useRunTests = (
[results]
);

// this resets results to be empty, causing the useEffect to fire and run tests
const runTests = () => {
setResults([]);
};

console.log({ results });
// console.log({ results });
return [results, runTests];
};

Expand Down Expand Up @@ -91,13 +92,13 @@ const run = (
type: 'incorrect',
errorMsg: err.message,
});
console.error(
console.log(
`${indent()}fail: ${test.fullTitle()} - error: ${err.message}`
);
})
.once(EVENT_RUN_END, () => {});

testRegistrators.map((register) => {
testRegistrators.forEach((register) => {
register();
});
runner.run();
Expand Down
4 changes: 1 addition & 3 deletions example/src/testing/Tests/CipherTests/CipherTestFirst.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// copied from https://github.com/nodejs/node/blob/master/test/parallel/test-crypto-hash.js
import { Buffer } from '@craftzdog/react-native-buffer';
import chai from 'chai';
import { assert } from 'chai';
import crypto from 'react-native-quick-crypto';
import { it } from '../../MochaRNAdapter';

const assert = chai.assert;

export function registerCipherTests1() {
'use strict';
function testCipher1(key: Buffer | string) {
Expand Down
4 changes: 1 addition & 3 deletions example/src/testing/Tests/CipherTests/CipherTestSecond.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// copied from https://github.com/nodejs/node/blob/master/test/parallel/test-crypto-hash.js
import crypto from 'react-native-quick-crypto';
import { it } from '../../MochaRNAdapter';
import chai from 'chai';
import { assert } from 'chai';
import { Buffer } from '@craftzdog/react-native-buffer';

const assert = chai.assert;

export function registerCipherTests2() {
'use strict';

Expand Down
50 changes: 25 additions & 25 deletions example/src/testing/Tests/CipherTests/GenerateKeyPairTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chai from 'chai';
import { assert, expect } from 'chai';
import type { Buffer } from '@craftzdog/react-native-buffer';
import { it } from '../../MochaRNAdapter';
import crypto from 'react-native-quick-crypto';
Expand All @@ -19,11 +19,11 @@ function assertApproximateSize(key: 'string' | Buffer, expectedSize: number) {
const u = typeof key === 'string' ? 'chars' : 'bytes';
const min = Math.floor(0.9 * expectedSize);
const max = Math.ceil(1.1 * expectedSize);
chai.assert(
assert(
key.length >= min,
`Key (${key.length} ${u}) is shorter than expected (${min} ${u})`
);
chai.assert(
assert(
key.length <= max,
`Key (${key.length} ${u}) is longer than expected (${max} ${u})`
);
Expand Down Expand Up @@ -54,16 +54,16 @@ export function registerGenerateKeyPairTests() {
},
});

chai.assert.strictEqual(Object.keys(ret).length, 2);
assert.strictEqual(Object.keys(ret).length, 2);
const { publicKey, privateKey } = ret;
chai.expect(!!publicKey).to.equal(true);
chai.expect(!!privateKey).to.equal(true);
expect(!!publicKey).to.equal(true);
expect(!!privateKey).to.equal(true);

chai.assert.strictEqual(typeof publicKey, 'string');
chai.assert.match(publicKey as any, spkiExp);
assert.strictEqual(typeof publicKey, 'string');
assert.match(publicKey as any, spkiExp);
assertApproximateSize(publicKey, 800);
chai.assert.strictEqual(typeof privateKey, 'string');
chai.assert.match(privateKey as any, pkcs8EncExp);
assert.strictEqual(typeof privateKey, 'string');
assert.match(privateKey as any, pkcs8EncExp);
assertApproximateSize(privateKey, 3434);
});

Expand All @@ -83,14 +83,14 @@ export function registerGenerateKeyPairTests() {
},
});

chai.assert.strictEqual(Object.keys(ret).length, 2);
assert.strictEqual(Object.keys(ret).length, 2);
const { publicKey, privateKey } = ret;

chai.assert.strictEqual(typeof publicKey, 'string');
chai.assert.match(publicKey, pkcs1PubExp);
assert.strictEqual(typeof publicKey, 'string');
assert.match(publicKey, pkcs1PubExp);
assertApproximateSize(publicKey, 162);
chai.assert.strictEqual(typeof privateKey, 'string');
chai.assert.match(privateKey, pkcs8Exp);
assert.strictEqual(typeof privateKey, 'string');
assert.match(privateKey, pkcs8Exp);
assertApproximateSize(privateKey, 512);
});

Expand All @@ -112,20 +112,20 @@ export function registerGenerateKeyPairTests() {
},
(err, publicKey, privateKey) => {
if (err) {
chai.assert.fail((err as any).toString());
assert.fail((err as any).toString());
}
chai.expect(!!publicKey).to.equal(true);
chai.expect(!!privateKey).to.equal(true);
expect(!!publicKey).to.equal(true);
expect(!!privateKey).to.equal(true);

// chai.assert.strictEqual(Object.keys(ret).length, 2);
// assert.strictEqual(Object.keys(ret).length, 2);
// const { publicKey, privateKey } = ret;

chai.assert.strictEqual(typeof publicKey, 'string');
chai.assert.match(publicKey as any, spkiExp);
// chai.assertApproximateSize(publicKey, 162);
chai.assert.strictEqual(typeof privateKey, 'string');
chai.assert.match(privateKey as any, pkcs8EncExp);
// chai.assertApproximateSize(privateKey, 512);
assert.strictEqual(typeof publicKey, 'string');
assert.match(publicKey as any, spkiExp);
// assertApproximateSize(publicKey, 162);
assert.strictEqual(typeof privateKey, 'string');
assert.match(privateKey as any, pkcs8EncExp);
// assertApproximateSize(privateKey, 512);

done();
}
Expand Down
12 changes: 6 additions & 6 deletions example/src/testing/Tests/CipherTests/PublicCipherTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chai from 'chai';
import { assert, expect } from 'chai';
import { Buffer } from '@craftzdog/react-native-buffer';
import { it } from '../../MochaRNAdapter';
import crypto from 'react-native-quick-crypto';
Expand All @@ -11,7 +11,7 @@ function testEncryptDecrypt(publicKey: any, privateKey: any) {
for (const key of [publicKey, privateKey]) {
const ciphertext = crypto.publicEncrypt(key, plaintext);
const received = crypto.privateDecrypt(privateKey, ciphertext);
chai.assert.strictEqual(received.toString('utf8'), message);
assert.strictEqual(received.toString('utf8'), message);
}
}

Expand Down Expand Up @@ -48,9 +48,9 @@ export function registerPublicCipherTests() {
const privateKey = await PrivateKey.generate(1024);
const encrypted = privateKey.encrypt(Buffer.from(clearText) as any);
const decrypted = privateKey.decrypt(encrypted);
chai.expect(decrypted.toString('utf-8')).to.equal(clearText);
expect(decrypted.toString('utf-8')).to.equal(clearText);
} catch (e) {
chai.assert.fail();
assert.fail();
}
});

Expand Down Expand Up @@ -110,7 +110,7 @@ export function registerPublicCipherTests() {
ciphertext
);

chai.expect(decrypted.toString('utf-8')).to.equal(message);
expect(decrypted.toString('utf-8')).to.equal(message);
});

it('passphrased private key without passphrase should throw', () => {
Expand All @@ -130,7 +130,7 @@ export function registerPublicCipherTests() {

try {
testEncryptDecrypt(publicKey, privateKey);
chai.assert.fail();
assert.fail();
} catch (e) {
// intentionally left blank
}
Expand Down
162 changes: 82 additions & 80 deletions example/src/testing/Tests/ConstantsTests/ConstantsTests.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,86 @@
import chai from 'chai';
import { expect } from 'chai';
import crypto from 'react-native-quick-crypto';
import { it } from '../../MochaRNAdapter';
import { describe, it } from '../../MochaRNAdapter';

export function registerConstantsTests() {
it('crypto constants are there', () => {
chai.expect(crypto.constants).to.eql({
OPENSSL_VERSION_NUMBER: 269488367,
SSL_OP_ALL: 2147485780,
SSL_OP_ALLOW_NO_DHE_KEX: 1024,
SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: 262144,
SSL_OP_CIPHER_SERVER_PREFERENCE: 4194304,
SSL_OP_CISCO_ANYCONNECT: 32768,
SSL_OP_COOKIE_EXCHANGE: 8192,
SSL_OP_CRYPTOPRO_TLSEXT_BUG: 2147483648,
SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: 2048,
SSL_OP_EPHEMERAL_RSA: 0,
SSL_OP_LEGACY_SERVER_CONNECT: 4,
SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: 0,
SSL_OP_MICROSOFT_SESS_ID_BUG: 0,
SSL_OP_MSIE_SSLV2_RSA_PADDING: 0,
SSL_OP_NETSCAPE_CA_DN_BUG: 0,
SSL_OP_NETSCAPE_CHALLENGE_BUG: 0,
SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: 0,
SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: 0,
SSL_OP_NO_COMPRESSION: 131072,
SSL_OP_NO_ENCRYPT_THEN_MAC: 524288,
SSL_OP_NO_QUERY_MTU: 4096,
SSL_OP_NO_RENEGOTIATION: 1073741824,
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: 65536,
SSL_OP_NO_SSLv2: 0,
SSL_OP_NO_SSLv3: 33554432,
SSL_OP_NO_TICKET: 16384,
SSL_OP_NO_TLSv1: 67108864,
SSL_OP_NO_TLSv1_1: 268435456,
SSL_OP_NO_TLSv1_2: 134217728,
SSL_OP_NO_TLSv1_3: 536870912,
SSL_OP_PKCS1_CHECK_1: 0,
SSL_OP_PKCS1_CHECK_2: 0,
SSL_OP_PRIORITIZE_CHACHA: 2097152,
SSL_OP_SINGLE_DH_USE: 0,
SSL_OP_SINGLE_ECDH_USE: 0,
SSL_OP_SSLEAY_080_CLIENT_DH_BUG: 0,
SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: 0,
SSL_OP_TLS_BLOCK_PADDING_BUG: 0,
SSL_OP_TLS_D5_BUG: 0,
SSL_OP_TLS_ROLLBACK_BUG: 8388608,
ENGINE_METHOD_RSA: 1,
ENGINE_METHOD_DSA: 2,
ENGINE_METHOD_DH: 4,
ENGINE_METHOD_RAND: 8,
ENGINE_METHOD_EC: 2048,
ENGINE_METHOD_CIPHERS: 64,
ENGINE_METHOD_DIGESTS: 128,
ENGINE_METHOD_PKEY_METHS: 512,
ENGINE_METHOD_PKEY_ASN1_METHS: 1024,
ENGINE_METHOD_ALL: 65535,
ENGINE_METHOD_NONE: 0,
DH_CHECK_P_NOT_SAFE_PRIME: 2,
DH_CHECK_P_NOT_PRIME: 1,
DH_UNABLE_TO_CHECK_GENERATOR: 4,
DH_NOT_SUITABLE_GENERATOR: 8,
ALPN_ENABLED: 1,
RSA_PKCS1_PADDING: 1,
RSA_SSLV23_PADDING: 2,
RSA_NO_PADDING: 3,
RSA_PKCS1_OAEP_PADDING: 4,
RSA_X931_PADDING: 5,
RSA_PKCS1_PSS_PADDING: 6,
RSA_PSS_SALTLEN_DIGEST: -1,
RSA_PSS_SALTLEN_MAX_SIGN: -2,
RSA_PSS_SALTLEN_AUTO: -2,
defaultCoreCipherList:
'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA256:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!SRP:!CAMELLIA',
TLS1_VERSION: 769,
TLS1_1_VERSION: 770,
TLS1_2_VERSION: 771,
TLS1_3_VERSION: 772,
POINT_CONVERSION_COMPRESSED: 2,
POINT_CONVERSION_UNCOMPRESSED: 4,
POINT_CONVERSION_HYBRID: 6,
export const registerConstantsTests = () => {
describe('constants', () => {
it('crypto constants are there', () => {
expect(crypto.constants).to.eql({
OPENSSL_VERSION_NUMBER: 269488367,
SSL_OP_ALL: 2147485780,
SSL_OP_ALLOW_NO_DHE_KEX: 1024,
SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: 262144,
SSL_OP_CIPHER_SERVER_PREFERENCE: 4194304,
SSL_OP_CISCO_ANYCONNECT: 32768,
SSL_OP_COOKIE_EXCHANGE: 8192,
SSL_OP_CRYPTOPRO_TLSEXT_BUG: 2147483648,
SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: 2048,
SSL_OP_EPHEMERAL_RSA: 0,
SSL_OP_LEGACY_SERVER_CONNECT: 4,
SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: 0,
SSL_OP_MICROSOFT_SESS_ID_BUG: 0,
SSL_OP_MSIE_SSLV2_RSA_PADDING: 0,
SSL_OP_NETSCAPE_CA_DN_BUG: 0,
SSL_OP_NETSCAPE_CHALLENGE_BUG: 0,
SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: 0,
SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: 0,
SSL_OP_NO_COMPRESSION: 131072,
SSL_OP_NO_ENCRYPT_THEN_MAC: 524288,
SSL_OP_NO_QUERY_MTU: 4096,
SSL_OP_NO_RENEGOTIATION: 1073741824,
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: 65536,
SSL_OP_NO_SSLv2: 0,
SSL_OP_NO_SSLv3: 33554432,
SSL_OP_NO_TICKET: 16384,
SSL_OP_NO_TLSv1: 67108864,
SSL_OP_NO_TLSv1_1: 268435456,
SSL_OP_NO_TLSv1_2: 134217728,
SSL_OP_NO_TLSv1_3: 536870912,
SSL_OP_PKCS1_CHECK_1: 0,
SSL_OP_PKCS1_CHECK_2: 0,
SSL_OP_PRIORITIZE_CHACHA: 2097152,
SSL_OP_SINGLE_DH_USE: 0,
SSL_OP_SINGLE_ECDH_USE: 0,
SSL_OP_SSLEAY_080_CLIENT_DH_BUG: 0,
SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: 0,
SSL_OP_TLS_BLOCK_PADDING_BUG: 0,
SSL_OP_TLS_D5_BUG: 0,
SSL_OP_TLS_ROLLBACK_BUG: 8388608,
ENGINE_METHOD_RSA: 1,
ENGINE_METHOD_DSA: 2,
ENGINE_METHOD_DH: 4,
ENGINE_METHOD_RAND: 8,
ENGINE_METHOD_EC: 2048,
ENGINE_METHOD_CIPHERS: 64,
ENGINE_METHOD_DIGESTS: 128,
ENGINE_METHOD_PKEY_METHS: 512,
ENGINE_METHOD_PKEY_ASN1_METHS: 1024,
ENGINE_METHOD_ALL: 65535,
ENGINE_METHOD_NONE: 0,
DH_CHECK_P_NOT_SAFE_PRIME: 2,
DH_CHECK_P_NOT_PRIME: 1,
DH_UNABLE_TO_CHECK_GENERATOR: 4,
DH_NOT_SUITABLE_GENERATOR: 8,
ALPN_ENABLED: 1,
RSA_PKCS1_PADDING: 1,
RSA_SSLV23_PADDING: 2,
RSA_NO_PADDING: 3,
RSA_PKCS1_OAEP_PADDING: 4,
RSA_X931_PADDING: 5,
RSA_PKCS1_PSS_PADDING: 6,
RSA_PSS_SALTLEN_DIGEST: -1,
RSA_PSS_SALTLEN_MAX_SIGN: -2,
RSA_PSS_SALTLEN_AUTO: -2,
defaultCoreCipherList:
'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA256:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!SRP:!CAMELLIA',
TLS1_VERSION: 769,
TLS1_1_VERSION: 770,
TLS1_2_VERSION: 771,
TLS1_3_VERSION: 772,
POINT_CONVERSION_COMPRESSED: 2,
POINT_CONVERSION_UNCOMPRESSED: 4,
POINT_CONVERSION_HYBRID: 6,
});
});
});
}
};
Loading

0 comments on commit 068cf78

Please sign in to comment.