2018-10-09 20:39:45 +00:00
|
|
|
/*globals after, before, describe, it*/
|
|
|
|
const {assert} = require('chai');
|
|
|
|
const os = require('os');
|
|
|
|
|
2018-10-30 16:25:57 +00:00
|
|
|
const underlyingFs = require('fs-extra');
|
2018-10-09 20:39:45 +00:00
|
|
|
const fs = require('../lib/core/fs');
|
|
|
|
|
|
|
|
describe('fs', () => {
|
2018-10-30 16:25:57 +00:00
|
|
|
let fsMethods = {};
|
|
|
|
|
2018-10-09 20:39:45 +00:00
|
|
|
before(() => {
|
|
|
|
this.oldProcessExit = process.exit;
|
|
|
|
process.exit = function() {};
|
2018-10-30 16:25:57 +00:00
|
|
|
|
|
|
|
for(const method in underlyingFs) {
|
|
|
|
fsMethods[method] = underlyingFs[method];
|
|
|
|
underlyingFs[method] = function() {};
|
|
|
|
}
|
2018-10-09 20:39:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
process.exit = this.oldProcessExit;
|
2018-10-30 16:25:57 +00:00
|
|
|
|
|
|
|
for(const method in underlyingFs) {
|
|
|
|
underlyingFs[method] = fsMethods[method];
|
|
|
|
}
|
2018-10-09 20:39:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const helperFunctions = [
|
|
|
|
'dappPath',
|
|
|
|
'embarkPath',
|
2018-10-29 16:55:17 +00:00
|
|
|
'pkgPath',
|
2018-10-09 20:39:45 +00:00
|
|
|
'tmpDir'
|
|
|
|
];
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'/etc',
|
|
|
|
'/home/testuser/src',
|
|
|
|
'/usr',
|
|
|
|
'../'
|
|
|
|
];
|
|
|
|
|
|
|
|
for(let func in fs) {
|
|
|
|
if(helperFunctions.includes(func)) continue;
|
|
|
|
|
|
|
|
describe(`fs.${func}`, () => {
|
|
|
|
it('should throw exceptions on paths outside the DApp root', (done) => {
|
|
|
|
paths.forEach(path => {
|
|
|
|
assert.throws(() => {
|
|
|
|
fs[func](path);
|
|
|
|
}, /EPERM: Operation not permitted/);
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw exceptions on paths inside the temporary dir root', (done) => {
|
|
|
|
assert.doesNotThrow(async () => {
|
|
|
|
try {
|
|
|
|
await fs[func](os.tmpdir() + '/foo');
|
|
|
|
} catch(e) {
|
|
|
|
if(e.message.indexOf('EPERM') === 0) throw e;
|
|
|
|
}
|
|
|
|
}, /EPERM: Operation not permitted/);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|