2017-10-20 20:36:54 +00:00
|
|
|
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
2013-10-29 18:50:54 +00:00
|
|
|
/* vim: set ts=2: */
|
2017-10-20 20:36:54 +00:00
|
|
|
/*jshint mocha:true */
|
2018-02-12 07:30:44 +00:00
|
|
|
/* eslint-env mocha */
|
2017-10-20 20:36:54 +00:00
|
|
|
/*global process, require */
|
|
|
|
/*::
|
|
|
|
declare type EmptyFunc = (() => void) | null;
|
|
|
|
declare type DescribeIt = { (desc:string, test:EmptyFunc):void; skip(desc:string, test:EmptyFunc):void; };
|
|
|
|
declare var describe : DescribeIt;
|
|
|
|
declare var it: DescribeIt;
|
|
|
|
declare var before:(test:EmptyFunc)=>void;
|
|
|
|
*/
|
2013-10-26 20:20:19 +00:00
|
|
|
var CFB;
|
|
|
|
var fs = require('fs');
|
2018-09-04 07:14:20 +00:00
|
|
|
describe('source', function() { it('should load', function() { CFB = require('./'); if(zlibify) CFB.utils.use_zlib(require("zlib")); }); });
|
2017-10-20 20:36:54 +00:00
|
|
|
var CRC32 = require('crc-32');
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
var WTF = !!process.env.WTF;
|
|
|
|
var zlibify = !!process.env.ZLIB;
|
2014-11-03 04:02:42 +00:00
|
|
|
var ex = [".xls",".doc",".ppt"];
|
|
|
|
if(process.env.FMTS) ex=process.env.FMTS.split(":").map(function(x){return x[0]==="."?x:"."+x;});
|
|
|
|
if(process.env.FMTS === "full") process.env.FMTS = ex.join(":");
|
|
|
|
if(process.env.FMTS) ex=process.env.FMTS.split(":").map(function(x){return x[0]==="."?x:"."+x;});
|
|
|
|
|
|
|
|
var ffunc = function(x){return (ex.indexOf(x.substr(-4))>=0 || ex.indexOf(x.substr(-3))>=0) && fails.indexOf(x) === -1;};
|
2014-05-20 17:43:03 +00:00
|
|
|
var fails = fs.existsSync('./fails.lst') ? fs.readFileSync('./fails.lst', 'utf-8').split("\n") : [];
|
2014-06-24 04:00:39 +00:00
|
|
|
var files = fs.readdirSync('test_files').filter(ffunc);
|
|
|
|
var f2011 = fs.readdirSync('test_files/2011').filter(ffunc);
|
|
|
|
var f2013 = fs.readdirSync('test_files/2013').filter(ffunc);
|
2014-11-03 04:02:42 +00:00
|
|
|
var fpres = fs.readdirSync('test_files_pres').filter(ffunc);
|
2018-09-04 07:14:20 +00:00
|
|
|
var fxlsx = fs.readdirSync('test_files').filter(function(x) { return x.slice(-5) == ".xlsx" && fails.indexOf(x) === -1; });
|
2014-05-20 17:43:03 +00:00
|
|
|
|
|
|
|
var dir = "./test_files/";
|
2017-09-14 21:14:22 +00:00
|
|
|
var TYPE = "buffer";
|
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
var names/*:Array<Array<string>>*/ = [
|
2017-09-14 21:14:22 +00:00
|
|
|
["!DocumentSummaryInformation", "\u0005"],
|
|
|
|
["!SummaryInformation", "\u0005"],
|
|
|
|
["!CompObj", "\u0001"],
|
2018-03-05 03:49:40 +00:00
|
|
|
["/!DataSpaces/Version", "\u0006"],
|
2017-09-14 21:14:22 +00:00
|
|
|
["!DRMContent", "\u0009"],
|
|
|
|
["!DRMViewerContent", "\u0009"],
|
|
|
|
["!Ole", "\u0001"]
|
|
|
|
].map(function(x) { return [x[0], x[0].replace("!", x[1])]; });
|
2013-10-29 18:50:54 +00:00
|
|
|
|
2018-09-04 07:14:20 +00:00
|
|
|
/* [ rel, abs ] */
|
|
|
|
var ENTRIES/*:Array<Array<string>>*/ = [
|
|
|
|
/* DOC */
|
|
|
|
["WordDocument", "/WordDocument"],
|
|
|
|
/* PPT */
|
|
|
|
["PowerPoint Document", "/PowerPoint Document"],
|
|
|
|
/* XLS */
|
|
|
|
["Workbook", "/Workbook"],
|
|
|
|
["Book", "/Book"],
|
|
|
|
/* OPC */
|
|
|
|
["[Content_Types].xml", "/[Content_Types].xml"],
|
|
|
|
/* ODF */
|
|
|
|
["content.xml", "/content.xml"],
|
|
|
|
/* XLSX */
|
|
|
|
["workbook.xml", "/xl/workbook.xml"],
|
|
|
|
/* Encrypted */
|
|
|
|
["EncryptedPackage", "/EncryptedPackage"],
|
|
|
|
["EncryptionInfo", "/EncryptionInfo"]
|
|
|
|
];
|
|
|
|
|
|
|
|
var REL_FILES = ENTRIES.map(function(e) { return e[0]; });
|
|
|
|
var ABS_FILES = ENTRIES.map(function(e) { return e[1]; });
|
|
|
|
|
|
|
|
function has_file(cfb, files/*:Array<string>*/)/*:string*/ {
|
|
|
|
for(var i = 0; i < files.length; ++i) if(CFB.find(cfb, files[i])) return files[i];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function zero_dates(cfb) {
|
|
|
|
cfb.FileIndex.forEach(function(f) { delete f.mt; delete f.ct; });
|
|
|
|
}
|
|
|
|
|
2013-10-29 18:50:54 +00:00
|
|
|
function parsetest(x, cfb) {
|
|
|
|
describe(x + ' should have basic parts', function() {
|
2017-09-14 21:14:22 +00:00
|
|
|
it('should find relative path', function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
if(!has_file(cfb, REL_FILES)) throw new Error("Cannot find content for " + x);
|
2017-08-09 06:50:59 +00:00
|
|
|
});
|
2017-09-14 21:14:22 +00:00
|
|
|
it('should find absolute path', function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
if(!has_file(cfb, ABS_FILES)) throw new Error("Cannot find content for " + x);
|
2017-08-09 06:50:59 +00:00
|
|
|
});
|
2017-09-14 21:14:22 +00:00
|
|
|
it('should handle "!" aliases', function() {
|
|
|
|
names.forEach(function(n) { if(CFB.find(cfb,n[0]) != CFB.find(cfb,n[1])) throw new Error("Bad name: " + n.join(" != ")); });
|
|
|
|
});
|
2018-02-12 07:30:44 +00:00
|
|
|
it('should handle size < 0', function() {
|
|
|
|
cfb.FileIndex.forEach(function(p, i) { if(p.size < 0) throw new Error(cfb.FullPaths[i] + " size=" + p.size); });
|
|
|
|
});
|
2017-09-14 21:14:22 +00:00
|
|
|
});
|
|
|
|
describe(x + ' should roundtrip', function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
var datacfb, newcfb;
|
|
|
|
var datazip, newzip;
|
|
|
|
var datacmp, newcmp;
|
|
|
|
before(function() {
|
|
|
|
/* cfb */
|
|
|
|
zero_dates(cfb);
|
|
|
|
datacfb = CFB.write(cfb, {type:TYPE});
|
|
|
|
newcfb = CFB.read(datacfb, {type:TYPE});
|
|
|
|
zero_dates(newcfb);
|
|
|
|
|
|
|
|
/* zip */
|
|
|
|
zero_dates(cfb);
|
|
|
|
datazip = CFB.write(cfb, {type:TYPE, fileType:"zip"});
|
|
|
|
newzip = CFB.read(datazip, {type:TYPE});
|
|
|
|
zero_dates(newzip);
|
|
|
|
|
|
|
|
/* zip with compression */
|
|
|
|
zero_dates(cfb);
|
|
|
|
datacmp = CFB.write(cfb, {type:TYPE, fileType:"zip", compression:1});
|
|
|
|
newcmp = CFB.read(datacmp, {type:TYPE});
|
|
|
|
zero_dates(newcmp);
|
2017-09-14 21:14:22 +00:00
|
|
|
});
|
2018-09-04 07:14:20 +00:00
|
|
|
it('should preserve content', function() {
|
|
|
|
var path = has_file(cfb, REL_FILES);
|
|
|
|
var _old = CFB.find(cfb, path);
|
|
|
|
var _cfb = CFB.find(newcfb, path);
|
|
|
|
var _zip = CFB.find(newzip, path);
|
|
|
|
var _cmp = CFB.find(newcmp, path);
|
|
|
|
/*:: if(!_old || !_cfb || !_zip || !_cmp) throw "unreachable"; */
|
|
|
|
var c1 = CRC32.buf(_old.content);
|
|
|
|
var c2 = CRC32.buf(_cfb.content);
|
|
|
|
var c3 = CRC32.buf(_zip.content);
|
|
|
|
var c4 = CRC32.buf(_cmp.content);
|
|
|
|
if(c1 != c2) throw new Error(x + " failed CFB roundtrip test");
|
|
|
|
if(c1 != c3) throw new Error(x + " failed ZIP roundtrip test");
|
|
|
|
if(c1 != c4) throw new Error(x + " failed ZIP compression roundtrip test");
|
2017-09-14 21:14:22 +00:00
|
|
|
});
|
|
|
|
it('should be idempotent', function() {
|
|
|
|
var dat2 = CFB.write(newcfb, {type:TYPE});
|
2018-09-04 07:14:20 +00:00
|
|
|
if(CRC32.buf(datacfb) != CRC32.buf(dat2)) throw new Error(x + " failed CFB idempotent test");
|
|
|
|
var dat2zip = CFB.write(newzip, {type:TYPE, fileType:"zip"});
|
|
|
|
if(CRC32.buf(datazip) != CRC32.buf(dat2zip)) throw new Error(x + " failed ZIP idempotent test");
|
|
|
|
var dat2cmp = CFB.write(newcmp, {type:TYPE, fileType:"zip", compression:1});
|
|
|
|
if(CRC32.buf(datacmp) != CRC32.buf(dat2cmp)) throw new Error(x + " failed ZIP idempotent test");
|
2017-09-14 21:14:22 +00:00
|
|
|
});
|
2013-10-29 18:50:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('should parse test files', function() {
|
|
|
|
files.forEach(function(x) {
|
2013-10-26 20:20:19 +00:00
|
|
|
it('should parse ' + x, function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
var cfb = CFB.read('./test_files/' + x, {type: "file", WTF: WTF});
|
2013-10-29 18:50:54 +00:00
|
|
|
parsetest(x, cfb);
|
2013-10-26 20:20:19 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-03 04:02:42 +00:00
|
|
|
fpres.forEach(function(x) {
|
|
|
|
it('should parse ' + x, function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
var cfb = CFB.read('./test_files_pres/' + x, {type: "file", WTF: WTF});
|
2014-11-03 04:02:42 +00:00
|
|
|
parsetest(x, cfb);
|
|
|
|
});
|
|
|
|
});
|
2014-05-20 17:43:03 +00:00
|
|
|
f2011.forEach(function(x) {
|
|
|
|
it('should parse ' + x, function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
var cfb = CFB.read('./test_files/2011/' + x, {type: "file", WTF: WTF});
|
2014-05-20 17:43:03 +00:00
|
|
|
parsetest(x, cfb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
f2013.forEach(function(x) {
|
|
|
|
it('should parse ' + x, function() {
|
2018-09-04 07:14:20 +00:00
|
|
|
var cfb = CFB.read('./test_files/2013/' + x, {type: "file", WTF: WTF});
|
2014-05-20 17:43:03 +00:00
|
|
|
parsetest(x, cfb);
|
|
|
|
});
|
|
|
|
});
|
2018-03-05 03:49:40 +00:00
|
|
|
fxlsx.forEach(function(x) {
|
|
|
|
it('should parse ' + x, function() {
|
|
|
|
try {
|
2018-09-04 07:14:20 +00:00
|
|
|
var cfb = CFB.read('./test_files/' + x, {type: "file", WTF: WTF});
|
2018-03-05 03:49:40 +00:00
|
|
|
parsetest(x, cfb);
|
|
|
|
} catch(e) {
|
|
|
|
if(e.message.match(/CFB file size /)) return;
|
|
|
|
if(!e.message.match(/Header Signature: Expected d0cf11e0a1b11ae1 saw /)) throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should recognize correct magic number', function() {
|
|
|
|
var cfb = CFB.read('./test_files/AutoFilter.xls', {type: "file"});
|
|
|
|
if(!CFB.find(cfb, '!CompObj')) throw new Error("Could not find !CompObj");
|
|
|
|
if(!CFB.find(cfb, '\u0001CompObj')) throw new Error("Could not find 1CompObj");
|
|
|
|
if(CFB.find(cfb, '\u0005CompObj')) throw new Error("Found 5CompObj");
|
|
|
|
|
|
|
|
if(!CFB.find(cfb, '!DocumentSummaryInformation')) throw new Error("Could not find !DSI");
|
|
|
|
if(!CFB.find(cfb, '\u0005DocumentSummaryInformation')) throw new Error("Could not find 5DSI");
|
|
|
|
if(CFB.find(cfb, '\u0001DocumentSummaryInformation')) throw new Error("Found 1DSI");
|
|
|
|
});
|
2014-05-20 17:43:03 +00:00
|
|
|
});
|
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
var cp = 'custom_properties.xls';
|
2018-09-04 07:14:20 +00:00
|
|
|
var xl = 'custom_properties.xlsx';
|
2014-05-20 17:43:03 +00:00
|
|
|
describe('input formats', function() {
|
|
|
|
it('should read binary strings', function() {
|
|
|
|
CFB.read(fs.readFileSync(dir + '/' + cp, 'binary'), {type: 'binary'});
|
2018-09-04 07:14:20 +00:00
|
|
|
CFB.read(fs.readFileSync(dir + '/' + xl, 'binary'), {type: 'binary'});
|
2014-05-20 17:43:03 +00:00
|
|
|
});
|
|
|
|
it('should read base64 strings', function() {
|
|
|
|
CFB.read(fs.readFileSync(dir + '/' + cp, 'base64'), {type: 'base64'});
|
2018-09-04 07:14:20 +00:00
|
|
|
CFB.read(fs.readFileSync(dir + '/' + xl, 'base64'), {type: 'base64'});
|
2014-05-20 17:43:03 +00:00
|
|
|
});
|
2014-06-24 04:00:39 +00:00
|
|
|
it('should read buffers', function() {
|
|
|
|
CFB.read(fs.readFileSync(dir + '/' + cp), {type: 'buffer'});
|
2018-09-04 07:14:20 +00:00
|
|
|
CFB.read(fs.readFileSync(dir + '/' + xl), {type: 'buffer'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('output formats', function() {
|
|
|
|
it('should write binary strings', function() {
|
|
|
|
var t = [
|
|
|
|
[ "CFB", CFB.write(CFB.read(fs.readFileSync(dir + '/' + cp, 'binary'), {type: 'binary'}), {type: 'binary'})],
|
|
|
|
[ "ZIP", CFB.write(CFB.read(fs.readFileSync(dir + '/' + xl, 'binary'), {type: 'binary'}), {type: 'binary', fileType: 'zip'})]
|
|
|
|
];
|
|
|
|
t.forEach(function(r) {
|
|
|
|
if(typeof r[1] != "string") throw new Error(r[0] + " binary write failed");
|
|
|
|
var good = false;
|
|
|
|
for(var i = 0; i < r[1].length; ++i) {
|
|
|
|
if(r[1].charCodeAt(i) == 0x00) good = true;
|
|
|
|
else if(r[1].charCodeAt(i) > 0xFF) { good = false; break; }
|
|
|
|
}
|
|
|
|
if(!good) throw new Error(r[0] + " binary write failed");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should write base64 strings', function() {
|
|
|
|
var t = [
|
|
|
|
[ "CFB", CFB.write(CFB.read(fs.readFileSync(dir + '/' + cp, 'base64'), {type: 'base64'}), {type: 'base64'})],
|
|
|
|
[ "ZIP", CFB.write(CFB.read(fs.readFileSync(dir + '/' + xl, 'base64'), {type: 'base64'}), {type: 'base64', fileType: 'zip'})]
|
|
|
|
];
|
|
|
|
t.forEach(function(r) {
|
|
|
|
if(typeof r[1] != "string") throw new Error(r[0] + " base64 write failed");
|
|
|
|
var good = false;
|
|
|
|
if(r[1].match(/[^a-zA-Z0-9+\/\+\.=]/)) throw new Error(r[0] + " base64 write failed");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should write buffers', function() {
|
|
|
|
var t1 = CFB.write(CFB.read(fs.readFileSync(dir + '/' + cp), {type: 'buffer'}), {type: 'buffer'});
|
|
|
|
var t2 = CFB.write(CFB.read(fs.readFileSync(dir + '/' + xl), {type: 'buffer'}), {type: 'buffer'});
|
|
|
|
if(!Buffer.isBuffer(t1)) throw new Error("CFB buffer write failed");
|
|
|
|
if(!Buffer.isBuffer(t2)) throw new Error("ZIP buffer write failed");
|
2014-06-24 04:00:39 +00:00
|
|
|
});
|
2013-10-26 20:20:19 +00:00
|
|
|
});
|
2017-11-27 05:41:39 +00:00
|
|
|
|
|
|
|
describe('api', function() {
|
|
|
|
it('should generate a file with custom root', function() {
|
|
|
|
var cfb = CFB.utils.cfb_new({root:'SheetJS'});
|
|
|
|
if(cfb.FileIndex[0].name != 'SheetJS') throw new Error("Bad root name");
|
|
|
|
var newcfb = CFB.read(CFB.write(cfb, {type:'base64'}), {type:'base64'});
|
|
|
|
if(newcfb.FileIndex[0].name != 'SheetJS') throw new Error("Bad root name");
|
|
|
|
});
|
|
|
|
it('should be able to delete a file', function() {
|
|
|
|
var cfb = CFB.read(fs.readFileSync(dir + '/' + cp, 'binary'), {type: 'binary'});
|
|
|
|
if(!CFB.find(cfb, '/Workbook')) throw new Error("Cannot find /Workbook");
|
|
|
|
CFB.utils.cfb_del(cfb, '/Workbook');
|
|
|
|
if(CFB.utils.cfb_del(cfb, '/Workbook')) throw new Error("Found /Workbook");
|
|
|
|
if(CFB.find(cfb, '/Workbook')) throw new Error("Failed deleting /Workbook");
|
|
|
|
var newcfb = CFB.read(CFB.write(cfb, {type:'binary'}), {type:'binary'});
|
|
|
|
if(CFB.find(newcfb, '/Workbook')) throw new Error("Found /Workbook");
|
|
|
|
});
|
|
|
|
it('should be able to move a file', function() {
|
|
|
|
var cfb = CFB.read(fs.readFileSync(dir + '/' + cp, 'binary'), {type: 'binary'});
|
|
|
|
if(!CFB.find(cfb, '/Workbook')) throw new Error("Cannot find /Workbook");
|
|
|
|
CFB.utils.cfb_mov(cfb, '/Workbook', '/Book');
|
|
|
|
if(CFB.utils.cfb_mov(cfb, '/Workbook', '/Work')) throw new Error("Found /Workbook");
|
|
|
|
if(CFB.find(cfb, '/Workbook')) throw new Error("Failed deleting /Workbook");
|
|
|
|
var newcfb = CFB.read(CFB.write(cfb, {type:'binary'}), {type:'binary'});
|
|
|
|
if(CFB.find(newcfb, '/Workbook')) throw new Error("Found /Workbook");
|
|
|
|
});
|
|
|
|
it('should be able to add a file', function() {
|
|
|
|
var cfb = CFB.read(fs.readFileSync(dir + '/' + cp, 'binary'), {type: 'binary'});
|
|
|
|
CFB.utils.cfb_add(cfb, '/dafuq', [1,2,3]);
|
|
|
|
var newcfb = CFB.read(CFB.write(cfb, {type:'binary'}), {type:'binary'});
|
|
|
|
var file = CFB.find(cfb, '/dafuq');
|
|
|
|
if(!file || !file.content) throw new Error("Cannot find /dafuq");
|
|
|
|
if(file.content.length != 3) throw new Error("Bad content length " + file.content.length);
|
2018-02-12 07:30:44 +00:00
|
|
|
file = CFB.find(newcfb, '/dafuq');
|
|
|
|
if(!file || !file.content) throw new Error("Cannot find /dafuq");
|
|
|
|
if(file.content.length != 3) throw new Error("Bad content length " + file.content.length);
|
2017-11-27 05:41:39 +00:00
|
|
|
});
|
|
|
|
});
|