/** * Forge Common Tests * * @author Dave Longley * * Copyright (c) 2009-2012 Digital Bazaar, Inc. All rights reserved. */ jQuery(function($) { // logging category var cat = 'forge.tests.common'; // local alias var forge = window.forge; var tests = []; var passed = 0; var failed = 0; var init = function() { passed = failed = 0; $('.ready,.testing,.pass,.fail') .removeClass('ready testing pass fail'); $('#status') .text('Ready.') .addClass('ready'); $('#total').text(tests.length); $('#pass').text(passed); $('#fail').text(failed); $('.expect').empty(); $('.result').empty(); $('.time').empty(); $('.timePer').empty(); $('#start').attr('disabled', ''); }; var start = function() { $('#start').attr('disabled', 'true'); // meta! use tasks to run the task tests forge.task.start({ type: 'test', run: function(task) { task.next('starting', function(task) { forge.log.debug(cat, 'start'); $('#status') .text('Testing...') .addClass('testing') .removeClass('idle'); }); $.each(tests, function(i, test) { task.next('test', function(task) { var title = $('li:first', test.container); if($('#scroll:checked').length === 1) { $('html,body').animate({scrollTop: title.offset().top}); } title.addClass('testing'); test.run(task, test); }); task.next('test', function(task) { $('li:first', test.container).removeClass('testing'); }); }); task.next('success', function(task) { forge.log.debug(cat, 'done'); if(failed === 0) { $('#status') .text('PASS') .addClass('pass') .removeClass('testing'); } else { // FIXME: should just be hitting failure() below $('#status') .text('FAIL') .addClass('fail') .removeClass('testing'); } }); }, failure: function() { $('#status') .text('FAIL') .addClass('fail') .removeClass('testing'); } }); }; $('#start').click(function() { start(); }); $('#reset').click(function() { init(); }); $('#keygen').click(function() { var bits = $('#bits')[0].value; var keys = forge.pki.rsa.generateKeyPair(bits); forge.log.debug(cat, 'generating ' + bits + '-bit RSA key-pair...'); setTimeout(function() { forge.log.debug(cat, 'private key:', keys.privateKey); forge.log.debug(cat, forge.pki.privateKeyToPem(keys.privateKey)); forge.log.debug(cat, 'public key:', keys.publicKey); forge.log.debug(cat, forge.pki.publicKeyToPem(keys.publicKey)); forge.log.debug(cat, 'testing sign/verify...'); setTimeout(function() { // do sign/verify test try { var md = forge.md.sha1.create(); md.update('foo'); var signature = keys.privateKey.sign(md); keys.publicKey.verify(md.digest().getBytes(), signature); forge.log.debug(cat, 'sign/verify success'); } catch(ex) { forge.log.error(cat, 'sign/verify failure', ex); } }, 0); }, 0); }); $('#certgen').click(function() { var bits = $('#bits')[0].value; forge.log.debug(cat, 'generating ' + bits + '-bit RSA key-pair and certificate...'); setTimeout(function() { try { var keys = forge.pki.rsa.generateKeyPair(bits); var cert = forge.pki.createCertificate(); cert.serialNumber = '01'; cert.validity.notBefore = new Date(); cert.validity.notAfter = new Date(); cert.validity.notAfter.setFullYear( cert.validity.notBefore.getFullYear() + 1); var attrs = [{ name: 'commonName', value: 'mycert' }, { name: 'countryName', value: 'US' }, { shortName: 'ST', value: 'Virginia' }, { name: 'localityName', value: 'Blacksburg' }, { name: 'organizationName', value: 'Test' }, { shortName: 'OU', value: 'Test' }]; cert.setSubject(attrs); cert.setIssuer(attrs); cert.setExtensions([{ name: 'basicConstraints', cA: true }, { name: 'keyUsage', keyCertSign: true }, { name: 'subjectAltName', altNames: [{ type: 6, // URI value: 'http://localhost/dataspace/person/myname#this' }] }]); // FIXME: add subjectKeyIdentifier extension // FIXME: add authorityKeyIdentifier extension cert.publicKey = keys.publicKey; // self-sign certificate cert.sign(keys.privateKey); forge.log.debug(cat, 'certificate:', cert); //forge.log.debug(cat, // forge.asn1.prettyPrint(forge.pki.certificateToAsn1(cert))); forge.log.debug(cat, forge.pki.certificateToPem(cert)); // verify certificate forge.log.debug(cat, 'verified', cert.verify(cert)); } catch(ex) { forge.log.error(cat, ex, ex.message ? ex.message : ''); } }, 0); }); var addTest = function(name, run) { var container = $('