/* Copyright 2008-2011 Clipperz Srl This file is part of Clipperz Community Edition. Clipperz Community Edition is an online password manager. For further information about its features and functionalities please refer to http://www.clipperz.com. * Clipperz Community Edition is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * Clipperz Community Edition is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License along with Clipperz Community Edition. If not, see . */ Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose(); var tests = { //------------------------------------------------------------------------- 'core_tests': function (someTestArgs) { // var deferredResult; // deferredResult = new Clipperz.Async.Deferred("core_tests", someTestArgs); // deferredResult.addCallback(function() { var byteArray; byteArray = new Clipperz.ByteArray(); try { byteArray.checkByteValue(512); is(false, true, "a value greater that a byte (0x200) should have raised an exception - NO Exception"); } catch(e) { // is( e.name, // "Clipperz.ByteArray.exception.InvalidValue", // "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") }; // }); // deferredResult.callback(); // return deferredResult; }, //------------------------------------------------------------------------- 'basic_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("simple_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var byteArray2; var byteArrayIterator; var nextBlock; byteArray = new Clipperz.ByteArray(); is(byteArray.length(), 0, "before adding any element the length is 0"); byteArray.appendByte(10); is(byteArray.length(), 1, "adding a single byte the length == 1"); is(byteArray.byteAtIndex(0), 10, "the first element is correct"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(8/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.constructor, Array, "ByteArrayIterator.nextBlock returns an array of byte values"); is(nextBlock.length, 1, "as the block size is 8bit, the returned array has only one element"); is(nextBlock[0], 10, "the element of the returned block is correct"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock, null, "after the last element, the nextBlock returns null"); byteArray = new Clipperz.ByteArray(); byteArray.appendBytes(10, 20, 45, 38); is(byteArray.length(), 4, "Appending more bytes, returns the right length"); is(byteArray.byteAtIndex(0), 10, "and all the elements are right [0]"); is(byteArray.byteAtIndex(1), 20, "and all the elements are right [1]"); is(byteArray.byteAtIndex(2), 45, "and all the elements are right [2]"); is(byteArray.byteAtIndex(3), 38, "and all the elements are right [3]"); byteArray2 = new Clipperz.ByteArray(); byteArray2.appendBytes([10, 20, 45, 38]); is(byteArray.equals(byteArray2), true, "equals method tested with a byteArray created with the same values"); byteArray2 = new Clipperz.ByteArray(); byteArray2.appendBytes([20, 11, 3, 22]); is(byteArray.equals(byteArray2), false, "equals method tested with a byteArray created with different values"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(8/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 1, "the size of the block returned by the byteArrayIterator match with the configured blockedSize"); is(nextBlock[0], 10, "the values returned by nextBlock are right [1]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock[0], 20, "the values returned by nextBlock are right [2]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock[0], 45, "the values returned by nextBlock are right [3]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock[0], 38, "the values returned by nextBlock are right [4]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock, null, "after the last block the nextBlock method returns null"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(16/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 2, "on the same data, using a different block size, returns the right length"); is(nextBlock[0], 10, "and also the data are fine [1][0]"); is(nextBlock[1], 20, "and also the data are fine [1][1]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 2, "also the second block size is right"); is(nextBlock[0], 45, "and also the data are fine [2][0]"); is(nextBlock[1], 38, "and also the data are fine [2][1]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock, null, "even with a bigger blockSize, at the end the returned value is null"); byteArray = new Clipperz.ByteArray(11,22,33,44,55,66,77,88,99); is(byteArray.length(), 9, ""); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(16/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); nextBlock = byteArrayIterator.nextBlock(); nextBlock = byteArrayIterator.nextBlock(); nextBlock = byteArrayIterator.nextBlock(); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 2, "the last block of an odd byte array has always the same size"); is(nextBlock[0], 99, "the last element is returned"); is(nextBlock[1], 0, "and a 0 is return for the extra values"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock, null, "even with odd blockSize, after returning all the values, null is returned"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(32/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock[0], 11, "32 bit blockSize [1][0]"); is(nextBlock[1], 22, "32 bit blockSize [1][1]"); is(nextBlock[2], 33, "32 bit blockSize [1][2]"); is(nextBlock[3], 44, "32 bit blockSize [1][3]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock[0], 55, "32 bit blockSize [2][0]"); is(nextBlock[1], 66, "32 bit blockSize [2][1]"); is(nextBlock[2], 77, "32 bit blockSize [2][2]"); is(nextBlock[3], 88, "32 bit blockSize [2][3]"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 4, "the last block of an odd byte array has always the same size"); is(nextBlock[0], 99, "the last element is returned (2)"); is(nextBlock[1], 0, "and a 0 is return for the extra values (2.1)"); is(nextBlock[2], 0, "and a 0 is return for the extra values (2.2)"); is(nextBlock[3], 0, "and a 0 is return for the extra values (2.3)"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock, null, "even with odd blockSize, after returning all the values, null is returned (2)"); byteArray = new Clipperz.ByteArray([11,22,33,44,55,66,77,88,99,100, 111, 122, 133, 144, 155, 166, 177]); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(128/8), finalPadding:true}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 16, "using a blockSize of 128 bit, the nextBlock return a 16 elements array"); byteArray = new Clipperz.ByteArray(); try { byteArray.appendByte(512); is(false, true, "appending a value greater that a byte (0x200) should have raised an exception - NO Exception"); } catch(e) { // is( e.name, // "Clipperz.ByteArray.exception.InvalidValue", // "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") }; try { byteArray.appendByte(256); is(false, true, "appending a value greater that a byte (0x100) should have raised an exception - NO Exception"); } catch(e) { // is( e.name, // "Clipperz.ByteArray.exception.InvalidValue", // "appending a value greater that a byte (0x100) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x100) should have raised an exception - EXCEPTION HANDLER") }; byteArray.appendByte(255); is(byteArray.length(), 1, "appending a value that feets a single byte does not raise any exception"); byteArray = new Clipperz.ByteArray(10, 20, 30, 40, 50); byteArray2 = new Clipperz.ByteArray("0x0a141e2832"); is(byteArray.equals(byteArray2), true, "the hex constructor works fine"); byteArray2 = byteArray.clone(); is(byteArray.equals(byteArray2), true, "the clone method works fine"); }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'compare_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("compare_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray1; var byteArray2; var result; byteArray1 = new Clipperz.ByteArray("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 16); byteArray2 = new Clipperz.ByteArray("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 16); result = byteArray1.compare(byteArray2); is(result, 0, "equal compare"); byteArray1 = new Clipperz.ByteArray("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 16); byteArray2 = new Clipperz.ByteArray("0x05", 16); result = byteArray1.compare(byteArray2); is(result, 1, "'OxXXXXXXX'.compare('0x05')"); result = byteArray2.compare(byteArray1); is(result, -1, "'0x05'.compare('OxXXXXXXX')"); byteArray1 = new Clipperz.ByteArray("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 16); byteArray2 = new Clipperz.ByteArray("0xff", 16); result = byteArray1.compare(byteArray2); is(result, 1, "'OxXXXXXXX'.compare('0xff')"); result = byteArray2.compare(byteArray1); is(result, -1, "'0xff'.compare('OxXXXXXXX')"); }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'shiftLeft_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("shiftLeft_tests", someTestArgs); deferredResult.addCallback(function() { }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'nextBlockArray_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("nextBlockArray_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var byteArrayIterator; var nextBlock; byteArray = new Clipperz.ByteArray("0x8cf53d90077df9a043bf8d10b470b144784411c93a4d504556834dae3ea4a5bb"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(128/8), finalPadding:false}); nextBlock = byteArrayIterator.nextBlockArray(); is(nextBlock.toHexString(), "0x8cf53d90077df9a043bf8d10b470b144", "nextBlockArray first iteration"); nextBlock = byteArrayIterator.nextBlockArray(); is(nextBlock.toHexString(), "0x784411c93a4d504556834dae3ea4a5bb", "nextBlockArray second iteration"); byteArray = new Clipperz.ByteArray("0x8cf53d90077df9a043bf8d10b470b144784411c93a4d504556834dae3ea4a5bb"); byteArrayIterator = new Clipperz.ByteArrayIterator({byteArray:byteArray, blockSize:(128/8), finalPadding:false}); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 16, "nextBlock first iteration length"); nextBlock = byteArrayIterator.nextBlock(); is(nextBlock.length, 16, "nextBlock second iteration length"); }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'xor_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("xor_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var byteArray2; byteArray = new Clipperz.ByteArray("0x55555555555555555555555555555555"); byteArray2 = new Clipperz.ByteArray("0x00ff000000ff000000ff000000ff0000"); is(byteArray.xorMergeWithBlock(byteArray2).toHexString(), "0x55aa555555aa555555aa555555aa5555", "the xorMergeWithBlock method works fine"); byteArray = new Clipperz.ByteArray("0x555555555555555555"); byteArray2 = new Clipperz.ByteArray("0x00ff000000ff000000ff000000ff0000"); is(byteArray.xorMergeWithBlock(byteArray2, 'left', 'truncate').toHexString(), "0x55aa555555aa555555", "the xorMergeWithBlock (left, truncate) method works fine"); byteArray = new Clipperz.ByteArray("0x555555555555555555"); byteArray2 = new Clipperz.ByteArray("0x00ff000000ff000000ff000000ff0000"); is(byteArray.xorMergeWithBlock(byteArray2, 'right', 'truncate').toHexString(), "0x5555aa555555aa5555", "the xorMergeWithBlock (right, truncate) method works fine"); }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'increment_tests': function (someTestArgs) { var deferredResult; deferredResult = new Clipperz.Async.Deferred("increment_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var number; var i,c; number = new Clipperz.Crypto.BigInt("55555555555555555555555555555555", 16); byteArray = new Clipperz.ByteArray("0x" + number.asString(16)); c = 2048; for (i=0; i