@@ -4,6 +4,8 @@ var crypto = require('crypto');
44var ece = require ( './ece.js' ) ;
55var base64 = require ( 'urlsafe-base64' ) ;
66var assert = require ( 'assert' ) ;
7+ var fs = require ( 'fs' ) ;
8+
79
810// Usage: node test.js [args]
911// If args contains a version (e.g., aes128gcm), filter on versions.
@@ -35,6 +37,7 @@ if (process.argv.length >= 3) {
3537 } else {
3638 plaintext = new Buffer ( process . argv [ 2 ] , 'ascii' ) ;
3739 }
40+ dump = ( process . argv . indexOf ( 'dump' ) != - 1 )
3841}
3942function filterTests ( fullList ) {
4043 var filtered = fullList . filter ( function ( t ) {
@@ -59,12 +62,16 @@ function logbuf(msg, buf) {
5962 }
6063}
6164
65+ // Validate that the encryption function only accepts Buffers
6266function validate ( ) {
6367 [ 'hello' , null , 1 , NaN , [ ] , { } ] . forEach ( function ( v ) {
6468 try {
65- encrypt ( 'hello' , { } ) ;
66- throw new Error ( 'should insist on a buffer' ) ;
67- } catch ( e ) { }
69+ ece . encrypt ( v , { } ) ;
70+ } catch ( e ) {
71+ if ( e . toString ( ) != "Error: buffer argument must be a Buffer" ) {
72+ throw new Error ( "encrypt failed to reject " + JSON . stringify ( v ) ) ;
73+ }
74+ }
6875 } ) ;
6976}
7077
@@ -104,6 +111,21 @@ function encryptDecrypt(input, encryptParams, decryptParams) {
104111 logbuf ( 'Encrypted' , encrypted ) ;
105112 var decrypted = ece . decrypt ( encrypted , decryptParams ) ;
106113 logbuf ( 'Decrypted' , decrypted ) ;
114+ if ( dump ) {
115+ var data = {
116+ version : version ,
117+ input : base64 . encode ( input ) ,
118+ encrypted : base64 . encode ( encrypted ) ,
119+ params : {
120+ encrypted : encryptParams ,
121+ decrypt : decryptParams ,
122+ }
123+ } ;
124+ if ( keyData ) {
125+ data . keys = keyData ;
126+ }
127+ dumpData ( data ) ;
128+ }
107129 assert . equal ( Buffer . compare ( input , decrypted ) , 0 ) ;
108130 log ( '----- OK' ) ;
109131}
@@ -155,7 +177,7 @@ function detectTruncation(version) {
155177 logbuf ( 'Encrypted' , encrypted ) ;
156178 var ok = false ;
157179 try {
158- ece . decrypt ( encrypted , params ) ;
180+ ece . decrypt ( encrypted , params , params , version ) ;
159181 } catch ( e ) {
160182 log ( '----- OK: ' + e ) ;
161183 ok = true ;
@@ -282,4 +304,4 @@ filterTests([ 'aesgcm128', 'aesgcm', 'aes128gcm' ])
282304 } ) ;
283305checkExamples ( ) ;
284306
285- log ( 'All tests passed.' ) ;
307+ log ( 'All tests passed.' ) ;
0 commit comments