/ test / fs.js
fs.js
 1  /*globals after, before, describe, it*/
 2  const {assert} = require('chai');
 3  const os = require('os');
 4  
 5  process.env.DAPP_PATH = '/home/testuser/src/dapp_path/';
 6  const fs = require('../lib/core/fs');
 7  
 8  describe('fs', () => {
 9    before(() => {
10      this.oldProcessExit = process.exit;
11      process.exit = function() {};
12    });
13  
14  
15    after(() => {
16      process.exit = this.oldProcessExit;
17    });
18  
19    const helperFunctions = [
20      'dappPath',
21      'embarkPath',
22      'tmpDir'
23    ];
24  
25    const paths = [
26      '/etc',
27      '/home/testuser/src',
28      '/usr',
29      '../'
30    ];
31  
32    for(let func in fs) {
33      if(helperFunctions.includes(func)) continue;
34  
35      describe(`fs.${func}`, () => {
36        it('should throw exceptions on paths outside the DApp root', (done) => {
37          paths.forEach(path => {
38            assert.throws(() => {
39              fs[func](path);
40            }, /EPERM: Operation not permitted/);
41          });
42  
43          done();
44        });
45  
46        it('should not throw exceptions on paths inside the temporary dir root', (done) => {
47          assert.doesNotThrow(async () => {
48            try {
49              await fs[func](os.tmpdir() + '/foo');
50            } catch(e) {
51              if(e.message.indexOf('EPERM') === 0) throw e;
52            }
53          }, /EPERM: Operation not permitted/);
54  
55          done();
56        });
57      });
58    }
59  });