make-tests.js (3272B)
1 // make-tests.js - Create test files automatically. 2 3 var Node = { 4 fs: require('fs'), 5 path: require('path'), 6 zlib: require('zlib') 7 }; 8 var assert = require('assert'); 9 10 function set(options, key, value) { 11 assert(typeof key === 'string'); 12 assert(value !== undefined); 13 assert(Number.isInteger(value) || Buffer.isBuffer(value)); 14 if (!options.hasOwnProperty(key)) options[key] = value; 15 } 16 17 function EOCDR(options = {}) { 18 set(options, 'signature', Buffer.from('504b0506', 'hex')); 19 set(options, 'disk', 0); 20 set(options, 'cd_disk', options.disk); 21 set(options, 'cd_records', 0); 22 set(options, 'cd_disk_records', options.cd_records); 23 set(options, 'cd_size', 0); 24 set(options, 'cd_offset', 0); 25 set(options, 'comment', Buffer.alloc(0)); 26 set(options, 'comment_length', options.comment.length); 27 var buffer = Buffer.alloc(22 + options.comment.length); 28 var offset = 0; 29 offset += options.signature.copy(buffer, offset); 30 offset = buffer.writeUInt16LE(options.disk, offset); 31 offset = buffer.writeUInt16LE(options.cd_disk, offset); 32 offset = buffer.writeUInt16LE(options.cd_disk_records, offset); 33 offset = buffer.writeUInt16LE(options.cd_records, offset); 34 offset = buffer.writeUInt32LE(options.cd_size, offset); 35 offset = buffer.writeUInt32LE(options.cd_offset, offset); 36 offset = buffer.writeUInt16LE(options.comment_length, offset); 37 offset += options.comment.copy(buffer, offset); 38 assert(offset === buffer.length); 39 return buffer; 40 } 41 42 if (!Node.fs.existsSync('tests')) Node.fs.mkdirSync('tests'); 43 44 [ 45 [ 46 'PURE_E_OK', 47 EOCDR() 48 ], 49 [ 50 'PURE_E_ZIP_TOO_SMALL', 51 EOCDR().slice(0, 21) 52 ], 53 [ 54 'PURE_E_ZIP_RAR', 55 Buffer.from('526172211a0700000000000000000000000000000000', 'hex') 56 ], 57 [ 58 'PURE_E_ZIP_TAR', 59 Buffer.from('75737461720000000000000000000000000000000000', 'hex') 60 ], 61 [ 62 'PURE_E_ZIP_XAR', 63 Buffer.from('78617221000000000000000000000000000000000000', 'hex') 64 ], 65 [ 66 'PURE_E_ZIP_EOCDR_NOT_FOUND', 67 EOCDR({ signature: Buffer.from('504b0505', 'hex') }) 68 ], 69 [ 70 'PURE_E_ZIP_EOCDR_SIZE_OVERFLOW', 71 EOCDR({ cd_records: 2, cd_size: 46 * 2 - 1 }) 72 ], 73 [ 74 'PURE_E_ZIP_EOCDR_SIZE_UNDERFLOW', 75 EOCDR({ cd_records: 0, cd_size: 46 }) 76 ], 77 [ 78 'PURE_E_ZIP_CD_EOCDR_OVERFLOW', 79 EOCDR({ cd_offset: 0, cd_records: 1, cd_size: 46 }) 80 ], 81 [ 82 'PURE_E_ZIP_EOCDR_RECORDS', 83 EOCDR({ cd_disk_records: 1, cd_records: 0 }) 84 ], 85 [ 86 'PURE_E_ZIP_MULTIPLE_DISKS', 87 EOCDR({ cd_disk: 1 }) 88 ], 89 [ 90 'PURE_E_ZIP_APPENDED_DATA_ZEROED', 91 Buffer.concat([EOCDR(), Buffer.alloc(13, 0)]) 92 ], 93 [ 94 'PURE_E_ZIP_APPENDED_DATA_BUFFER_BLEED', 95 Buffer.concat([EOCDR(), Buffer.alloc(97, 1)]) 96 ], 97 [ 98 'PURE_E_ZIP_PREPENDED_DATA', 99 Buffer.concat([Buffer.alloc(2048, 1), EOCDR()]) 100 ], 101 [ 102 'PURE_E_ZIP_PREPENDED_DATA_BUFFER_BLEED', 103 Buffer.concat([Buffer.alloc(1, 1), EOCDR()]) 104 ], 105 [ 106 'PURE_E_ZIP_PREPENDED_DATA_ZEROED', 107 Buffer.concat([Buffer.alloc(1, 0), EOCDR()]) 108 ] 109 ].forEach( 110 function(test, index) { 111 var code = test[0]; 112 var buffer = test[1]; 113 var name = index.toString().padStart(3, '0') + '_' + code + '.zip'; 114 var path = Node.path.join('tests', name); 115 console.log('Creating ./' + path); 116 Node.fs.writeFileSync(path, buffer); 117 } 118 ); 119