Run tests with randomly generated strings

This commit is contained in:
Maximilian Krambach 2018-05-17 12:36:22 +02:00
parent 7cd2aded63
commit 1eb3902a96
6 changed files with 59 additions and 23 deletions

View File

@ -4,6 +4,7 @@ const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp
const chai = require('chai'); const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
const input = require('./testInputs.js');
const expect = chai.expect; const expect = chai.expect;
@ -222,10 +223,12 @@ describe('Brainpool Cryptography', function () {
const bye = secondKey.key; const bye = secondKey.key;
const pubBye = bye.toPublic(); const pubBye = bye.toPublic();
const testdata = input.createSomeMessage();
const testdata2 = input.createSomeMessage();
return Promise.all([ return Promise.all([
// Signing message // Signing message
openpgp.sign( openpgp.sign(
{ data: 'Hi, this is me, Hi!', privateKeys: hi } { data: testdata, privateKeys: hi }
).then(signed => { ).then(signed => {
const msg = openpgp.cleartext.readArmored(signed.data); const msg = openpgp.cleartext.readArmored(signed.data);
// Verifying signed message // Verifying signed message
@ -235,7 +238,7 @@ describe('Brainpool Cryptography', function () {
).then(output => expect(output.signatures[0].valid).to.be.true), ).then(output => expect(output.signatures[0].valid).to.be.true),
// Verifying detached signature // Verifying detached signature
openpgp.verify( openpgp.verify(
{ message: openpgp.message.fromText('Hi, this is me, Hi!'), { message: openpgp.message.fromText(testdata),
publicKeys: pubHi, publicKeys: pubHi,
signature: openpgp.signature.readArmored(signed.data) } signature: openpgp.signature.readArmored(signed.data) }
).then(output => expect(output.signatures[0].valid).to.be.true) ).then(output => expect(output.signatures[0].valid).to.be.true)
@ -243,7 +246,7 @@ describe('Brainpool Cryptography', function () {
}), }),
// Encrypting and signing // Encrypting and signing
openpgp.encrypt( openpgp.encrypt(
{ data: 'Hi, Hi wrote this but only Bye can read it!', { data: testdata2,
publicKeys: [pubBye], publicKeys: [pubBye],
privateKeys: [hi] } privateKeys: [hi] }
).then(encrypted => { ).then(encrypted => {
@ -254,7 +257,7 @@ describe('Brainpool Cryptography', function () {
privateKeys: bye, privateKeys: bye,
publicKeys: [pubHi] } publicKeys: [pubHi] }
).then(output => { ).then(output => {
expect(output.data).to.equal('Hi, Hi wrote this but only Bye can read it!'); expect(output.data).to.equal(testdata2);
expect(output.signatures[0].valid).to.be.true; expect(output.signatures[0].valid).to.be.true;
}); });
}) })

View File

@ -4,6 +4,7 @@ const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp
const chai = require('chai'); const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
const input = require('./testInputs.js');
const expect = chai.expect; const expect = chai.expect;
@ -238,6 +239,8 @@ describe('Elliptic Curve Cryptography', function () {
function omnibus() { function omnibus() {
it('Omnibus NIST P-256 Test', function () { it('Omnibus NIST P-256 Test', function () {
const options = { userIds: {name: "Hi", email: "hi@hel.lo"}, curve: "p256" }; const options = { userIds: {name: "Hi", email: "hi@hel.lo"}, curve: "p256" };
const testdata = input.createSomeMessage();
const testdata2 = input.createSomeMessage();
return openpgp.generateKey(options).then(function (firstKey) { return openpgp.generateKey(options).then(function (firstKey) {
const hi = firstKey.key; const hi = firstKey.key;
const pubHi = hi.toPublic(); const pubHi = hi.toPublic();
@ -249,8 +252,9 @@ describe('Elliptic Curve Cryptography', function () {
return Promise.all([ return Promise.all([
// Signing message // Signing message
openpgp.sign( openpgp.sign(
{ data: 'Hi, this is me, Hi!', privateKeys: hi } { data: testdata, privateKeys: hi }
).then(signed => { ).then(signed => {
const msg = openpgp.cleartext.readArmored(signed.data); const msg = openpgp.cleartext.readArmored(signed.data);
// Verifying signed message // Verifying signed message
@ -260,7 +264,7 @@ describe('Elliptic Curve Cryptography', function () {
).then(output => expect(output.signatures[0].valid).to.be.true), ).then(output => expect(output.signatures[0].valid).to.be.true),
// Verifying detached signature // Verifying detached signature
openpgp.verify( openpgp.verify(
{ message: openpgp.message.fromText('Hi, this is me, Hi!'), { message: openpgp.message.fromText(testdata),
publicKeys: pubHi, publicKeys: pubHi,
signature: openpgp.signature.readArmored(signed.data) } signature: openpgp.signature.readArmored(signed.data) }
).then(output => expect(output.signatures[0].valid).to.be.true) ).then(output => expect(output.signatures[0].valid).to.be.true)
@ -268,7 +272,7 @@ describe('Elliptic Curve Cryptography', function () {
}), }),
// Encrypting and signing // Encrypting and signing
openpgp.encrypt( openpgp.encrypt(
{ data: 'Hi, Hi wrote this but only Bye can read it!', { data: testdata2,
publicKeys: [pubBye], publicKeys: [pubBye],
privateKeys: [hi] } privateKeys: [hi] }
).then(encrypted => { ).then(encrypted => {
@ -279,7 +283,7 @@ describe('Elliptic Curve Cryptography', function () {
privateKeys: bye, privateKeys: bye,
publicKeys: [pubHi] } publicKeys: [pubHi] }
).then(output => { ).then(output => {
expect(output.data).to.equal('Hi, Hi wrote this but only Bye can read it!'); expect(output.data).to.equal(testdata2);
expect(output.signatures[0].valid).to.be.true; expect(output.signatures[0].valid).to.be.true;
}); });
}) })

View File

@ -4,6 +4,7 @@ const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp
const spy = require('sinon/lib/sinon/spy'); const spy = require('sinon/lib/sinon/spy');
const stub = require('sinon/lib/sinon/stub'); const stub = require('sinon/lib/sinon/stub');
const input = require('./testInputs.js');
const chai = require('chai'); const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
@ -286,7 +287,7 @@ DECl1Qu4QyeXin29uEXWiekMpNlZVsEuc8icCw6ABhIZ
-----END PGP PRIVATE KEY BLOCK-----`; -----END PGP PRIVATE KEY BLOCK-----`;
const passphrase = 'hello world'; const passphrase = 'hello world';
const plaintext = 'short message\nnext line\n한국어/조선말'; const plaintext = input.createSomeMessage() + '\n한국어/조선말';
const password1 = 'I am a password'; const password1 = 'I am a password';
const password2 = 'I am another password'; const password2 = 'I am another password';
const password3 = 'I am a third password'; const password3 = 'I am a third password';
@ -1991,7 +1992,7 @@ describe('OpenPGP.js public api tests', function() {
passwords: password2 passwords: password2
}; };
return openpgp.decrypt(decOpt).then(function (decrypted) { return openpgp.decrypt(decOpt).then(function (decrypted) {
expect(decrypted.data).to.equal(plaintext); expect(decrypted.data).to.equal('short message\nnext line\n한국어/조선말');
expect(decrypted.signatures.length).to.equal(0); expect(decrypted.signatures.length).to.equal(0);
}); });
}); });

View File

@ -5,6 +5,7 @@ const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
const { expect } = chai; const { expect } = chai;
const input = require('./testInputs.js');
function stringify(array) { function stringify(array) {
if(!Uint8Array.prototype.isPrototypeOf(array)) { if(!Uint8Array.prototype.isPrototypeOf(array)) {
@ -58,9 +59,10 @@ describe("Packet", function() {
it('Symmetrically encrypted packet', async function() { it('Symmetrically encrypted packet', async function() {
const message = new openpgp.packet.List(); const message = new openpgp.packet.List();
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
literal.setText('Hello world'); literal.setText(testtext);
const enc = new openpgp.packet.SymmetricallyEncrypted(); const enc = new openpgp.packet.SymmetricallyEncrypted();
message.push(enc); message.push(enc);
@ -81,9 +83,10 @@ describe("Packet", function() {
it('Symmetrically encrypted packet - MDC error for modern cipher', async function() { it('Symmetrically encrypted packet - MDC error for modern cipher', async function() {
const message = new openpgp.packet.List(); const message = new openpgp.packet.List();
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
literal.setText('Hello world'); literal.setText(testtext);
const enc = new openpgp.packet.SymmetricallyEncrypted(); const enc = new openpgp.packet.SymmetricallyEncrypted();
message.push(enc); message.push(enc);
@ -102,13 +105,14 @@ describe("Packet", function() {
it('Sym. encrypted integrity protected packet', async function() { it('Sym. encrypted integrity protected packet', async function() {
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]); const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]);
const algo = 'aes256'; const algo = 'aes256';
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
const enc = new openpgp.packet.SymEncryptedIntegrityProtected(); const enc = new openpgp.packet.SymEncryptedIntegrityProtected();
const msg = new openpgp.packet.List(); const msg = new openpgp.packet.List();
msg.push(enc); msg.push(enc);
literal.setText('Hello world!'); literal.setText(testtext);
enc.packets.push(literal); enc.packets.push(literal);
await enc.encrypt(algo, key); await enc.encrypt(algo, key);
@ -123,13 +127,13 @@ describe("Packet", function() {
it('Sym. encrypted AEAD protected packet', function() { it('Sym. encrypted AEAD protected packet', function() {
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]); const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]);
const algo = 'aes256'; const algo = 'aes256';
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
const enc = new openpgp.packet.SymEncryptedAEADProtected(); const enc = new openpgp.packet.SymEncryptedAEADProtected();
const msg = new openpgp.packet.List(); const msg = new openpgp.packet.List();
msg.push(enc); msg.push(enc);
literal.setText('Hello world!'); literal.setText(testtext);
enc.packets.push(literal); enc.packets.push(literal);
const msg2 = new openpgp.packet.List(); const msg2 = new openpgp.packet.List();
@ -147,6 +151,7 @@ describe("Packet", function() {
let aead_protect_versionVal = openpgp.config.aead_protect_version; let aead_protect_versionVal = openpgp.config.aead_protect_version;
openpgp.config.aead_protect = true; openpgp.config.aead_protect = true;
openpgp.config.aead_protect_version = 4; openpgp.config.aead_protect_version = 4;
const testtext = input.createSomeMessage();
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]); const key = new Uint8Array([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]);
const algo = 'aes256'; const algo = 'aes256';
@ -156,7 +161,7 @@ describe("Packet", function() {
const msg = new openpgp.packet.List(); const msg = new openpgp.packet.List();
msg.push(enc); msg.push(enc);
literal.setText('Hello world!'); literal.setText(testtext);
enc.packets.push(literal); enc.packets.push(literal);
const msg2 = new openpgp.packet.List(); const msg2 = new openpgp.packet.List();
@ -393,6 +398,7 @@ describe("Packet", function() {
it('Sym. encrypted session key reading/writing', async function() { it('Sym. encrypted session key reading/writing', async function() {
const passphrase = 'hello'; const passphrase = 'hello';
const algo = 'aes256'; const algo = 'aes256';
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
const key_enc = new openpgp.packet.SymEncryptedSessionKey(); const key_enc = new openpgp.packet.SymEncryptedSessionKey();
@ -407,7 +413,7 @@ describe("Packet", function() {
const key = key_enc.sessionKey; const key = key_enc.sessionKey;
literal.setText('Hello world!'); literal.setText(testtext);
enc.packets.push(literal); enc.packets.push(literal);
await enc.encrypt(algo, key); await enc.encrypt(algo, key);
@ -430,6 +436,7 @@ describe("Packet", function() {
try { try {
const passphrase = 'hello'; const passphrase = 'hello';
const algo = 'aes256'; const algo = 'aes256';
const testtext = input.createSomeMessage();
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
const key_enc = new openpgp.packet.SymEncryptedSessionKey(); const key_enc = new openpgp.packet.SymEncryptedSessionKey();
@ -444,7 +451,7 @@ describe("Packet", function() {
const key = key_enc.sessionKey; const key = key_enc.sessionKey;
literal.setText('Hello world!'); literal.setText(testtext);
enc.packets.push(literal); enc.packets.push(literal);
await enc.encrypt(algo, key); await enc.encrypt(algo, key);
@ -774,6 +781,7 @@ describe("Packet", function() {
mpi = mpi.map(function(k) { mpi = mpi.map(function(k) {
return new openpgp.MPI(k); return new openpgp.MPI(k);
}); });
const testtext = input.createSomeMessage();
key.params = mpi; key.params = mpi;
key.algorithm = "rsa_sign"; key.algorithm = "rsa_sign";
@ -782,7 +790,7 @@ describe("Packet", function() {
const literal = new openpgp.packet.Literal(); const literal = new openpgp.packet.Literal();
const signature = new openpgp.packet.Signature(); const signature = new openpgp.packet.Signature();
literal.setText('Hello world'); literal.setText(testtext);
signature.hashAlgorithm = 'sha256'; signature.hashAlgorithm = 'sha256';
signature.publicKeyAlgorithm = 'rsa_sign'; signature.publicKeyAlgorithm = 'rsa_sign';

View File

@ -0,0 +1,17 @@
/**
* Generates a 50 character long javascript string out of the whole utf-8 range.
*/
function createSomeMessage(){
const length = 50;
let arr = [];
for (let i= 0; i < length; i++){
arr.push(String.fromCharCode(
Math.floor(Math.random() * 10174) + 1));
}
return arr.join('');
}
module.exports = {
createSomeMessage: createSomeMessage
};

View File

@ -6,6 +6,7 @@ const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
const { expect } = chai; const { expect } = chai;
const input = require('./testInputs');
describe('X25519 Cryptography', function () { describe('X25519 Cryptography', function () {
const data = { const data = {
@ -169,14 +170,15 @@ describe('X25519 Cryptography', function () {
it('Sign message', async function () { it('Sign message', async function () {
const name = 'light'; const name = 'light';
const randomdata = input.createSomeMessage();
const priv = await load_priv_key(name); const priv = await load_priv_key(name);
const signed = await openpgp.sign({ privateKeys: [priv], data: data[name].message + "\n" }); const signed = await openpgp.sign({ privateKeys: [priv], data: randomdata});
const pub = load_pub_key(name); const pub = load_pub_key(name);
const msg = openpgp.cleartext.readArmored(signed.data); const msg = openpgp.cleartext.readArmored(signed.data);
const result = await openpgp.verify({ publicKeys: [pub], message: msg}); const result = await openpgp.verify({ publicKeys: [pub], message: msg});
expect(result).to.exist; expect(result).to.exist;
expect(result.data.trim()).to.equal(data[name].message); expect(result.data.trim()).to.equal(randomdata);
expect(result.signatures).to.have.length(1); expect(result.signatures).to.have.length(1);
expect(result.signatures[0].valid).to.be.true; expect(result.signatures[0].valid).to.be.true;
}); });
@ -197,7 +199,8 @@ describe('X25519 Cryptography', function () {
it('Encrypt and sign message', async function () { it('Encrypt and sign message', async function () {
const nightPublic = load_pub_key('night'); const nightPublic = load_pub_key('night');
const lightPrivate = await load_priv_key('light'); const lightPrivate = await load_priv_key('light');
const encrypted = await openpgp.encrypt({ publicKeys: [nightPublic], privateKeys: [lightPrivate], data: data.light.message + "\n" }); const randomdata = input.createSomeMessage();
const encrypted = await openpgp.encrypt({ publicKeys: [nightPublic], privateKeys: [lightPrivate], data: randomdata });
const message = openpgp.message.readArmored(encrypted.data); const message = openpgp.message.readArmored(encrypted.data);
const lightPublic = load_pub_key('light'); const lightPublic = load_pub_key('light');
@ -205,7 +208,7 @@ describe('X25519 Cryptography', function () {
const result = await openpgp.decrypt({ privateKeys: nightPrivate, publicKeys: [lightPublic], message: message }); const result = await openpgp.decrypt({ privateKeys: nightPrivate, publicKeys: [lightPublic], message: message });
expect(result).to.exist; expect(result).to.exist;
expect(result.data.trim()).to.equal(data.light.message); expect(result.data.trim()).to.equal(randomdata);
expect(result.signatures).to.have.length(1); expect(result.signatures).to.have.length(1);
expect(result.signatures[0].valid).to.be.true; expect(result.signatures[0].valid).to.be.true;
}); });