Exemplo n.º 1
0
module.exports = function() {
  var filepath = path.join(common.fixturesDir, 'x.txt'),
      expected = 'xyz\n',
      bufferAsync = new Buffer(expected.length),
      bufferSync = new Buffer(expected.length),
      readCalled = 0,
      rootFS = fs.getRootFS();

  fs.open(filepath, 'r', function(err, fd) {
    if (err) throw err;
    fs.read(fd, bufferAsync, 0, expected.length, 0, function(err, bytesRead) {
      readCalled++;

      assert.equal(bytesRead, expected.length);
      assert.equal(bufferAsync.toString(), new Buffer(expected).toString());
    });

    if (rootFS.supportsSynch()) {
      var r = fs.readSync(fd, bufferSync, 0, expected.length, 0);
      assert.equal(bufferSync.toString(), new Buffer(expected).toString());
      assert.equal(r, expected.length);
    }
  });

  process.on('exit', function() {
    assert.equal(readCalled, 1);
  });
};
Exemplo n.º 2
0
module.exports = function() {
  var rootFS = fs.getRootFS(),
    fses = rootFS.getOverlayedFileSystems(),
    // XXX: Make these proper API calls.
    readable = fses.readable,
    writable = fses.writable;

  // Ensure no files are doubled.
  var seenMap = [];
  fs.readdirSync('/test/fixtures/files/node').forEach(function(file) {
    assert(seenMap.indexOf(file) === -1, "File " + file + " cannot exist multiple times.");
    seenMap.push(file);
    fs.unlinkSync('/test/fixtures/files/node/' + file);
  });

  fs.rmdirSync('/test/fixtures/files/node');

  assert(fs.existsSync('/test/fixtures/files/node') === false, 'Directory must be deleted');
  assert(fs.readdirSync('/test/fixtures/files').length === 0, 'File system must be empty.');

  var newCombined = new BrowserFS.FileSystem.OverlayFS(writable, readable);
  newCombined.initialize(function() {
    assert(newCombined.existsSync('/test/fixtures/files/node') === false, 'Directory must still be deleted.');
    assert(newCombined.readdirSync('/test/fixtures/files').length === 0, "File system must still be empty.");

    var newFs = new BrowserFS.FileSystem.OverlayFS(new BrowserFS.FileSystem.InMemory(), readable);
    newFs.initialize(function() {
      BrowserFS.initialize(newFs);
      assert(fs.existsSync('/test/fixtures/files/node') === true, "Directory must be back");
      assert(fs.readdirSync('/test/fixtures/files').length > 0, "Directory must be back.");
      // XXX: Remake the tmpdir.
      fs.mkdirSync(common.tmpDir);
    });
  });
};
Exemplo n.º 3
0
module.exports = function() {
  if (!fs.getRootFS().isReadOnly()) {
    var pathname1 = common.tmpDir + '/mkdir-test1';

    fs.mkdir(pathname1, function(err) {
      assert.equal(err, null,
          'fs.mkdir(' + pathname1 + ') reports non-null error: ' + err);
      fs.exists(pathname1, function(y){
        assert.equal(y, true,
          'Got null error from fs.mkdir, but fs.exists reports false for ' + pathname1);
      });
    });

    var pathname2 = common.tmpDir + '/mkdir-test2';

    fs.mkdir(pathname2, 511 /*=0777*/, function(err) {
      assert.equal(err, null,
          'fs.mkdir(' + pathname2 + ') reports non-null error: ' + err);
      fs.exists(pathname2, function(y){
        assert.equal(y, true,
          'Got null error from fs.mkdir, but fs.exists reports false for ' + pathname2);
      });
    });

    // Shouldn't be able to make multi-level dirs.
    var pathname3 = common.tmpDir + '/mkdir-test3/again';
    fs.mkdir(pathname3, 511 /*=0777*/, function(err) {
      assert.notEqual(err, null, 'fs.mkdir(' + pathname3 + ') reports null error');
    });
  }
};
Exemplo n.º 4
0
module.exports = function() {
  // Read a file and check its binary bytes.
  fs.readFile(path.join(common.fixturesDir, 'elipses.txt'), function(err, buff) {
    if (err) throw err;
    assert(buff.readUInt16LE(0) === 32994);
  });
  // Same, but synchronous.
  var rootFS = fs.getRootFS();
  if (rootFS.supportsSynch()) {
    var buff = fs.readFileSync(path.join(common.fixturesDir, 'elipses.txt'));
    assert(buff.readUInt16LE(0) === 32994);
  }
};
Exemplo n.º 5
0
module.exports = function() {
  var fn = path.join(common.fixturesDir, 'empty.txt');
  var rootFS = fs.getRootFS();

  fs.readFile(fn, function(err, data) {
    assert.ok(data);
  });

  fs.readFile(fn, 'utf8', function(err, data) {
    assert.strictEqual('', data);
  });

  if (rootFS.supportsSynch()) {
    assert.ok(fs.readFileSync(fn));
    assert.strictEqual('', fs.readFileSync(fn, 'utf8'));
  }
};
Exemplo n.º 6
0
module.exports = function() {
  var filename = path.join(common.fixturesDir, 'a.js');

  var caughtException = false;
  // Only run if the FS supports sync ops.
  var rootFS = fs.getRootFS();
  if (rootFS.supportsSynch()) {
    try {
      // should throw ENOENT, not EBADF
      // see https://github.com/joyent/node/pull/1228
      fs.openSync('/path/to/file/that/does/not/exist', 'r');
    }
    catch (e) {
      assert.equal(e.code, 'ENOENT');
      caughtException = true;
    }
    assert.ok(caughtException);
  }

  fs.open('/path/to/file/that/does/not/exist', 'r', function(e) {
    assert(e != null);
    assert.equal(e.code, 'ENOENT');
  });

  fs.open(filename, 'r', function(err, fd) {
    if (err) {
      throw err;
    }
    assert.ok(fd, 'failed to open with mode `r`: ' + filename);
  });

  fs.open(filename, 'rs', function(err, fd) {
    if (err) {
      throw err;
    }
    assert.ok(fd, 'failed to open with mode `rs`: ' + filename);
  });
};
Exemplo n.º 7
0
module.exports = function() {
  var got_error = false;
  var success_count = 0;
  var existing_dir = common.fixturesDir;
  var existing_file = path.join(common.fixturesDir, 'x.txt');

  // Empty string is not a valid file path.
  fs.stat('', function(err, stats) {
    if (err) {
      success_count++;
    } else {
      got_error = true;
    }
  });

  fs.stat(existing_dir, function(err, stats) {
    if (err) {
      got_error = true;
    } else {
      assert.ok(stats.mtime instanceof Date);
      success_count++;
    }
  });

  fs.lstat(existing_dir, function(err, stats) {
    if (err) {
      got_error = true;
    } else {
      assert.ok(stats.mtime instanceof Date);
      success_count++;
    }
  });

  // fstat
  fs.open(existing_file, 'r', undefined, function(err, fd) {
    assert.ok(!err);
    assert.ok(fd);

    fs.fstat(fd, function(err, stats) {
      if (err) {
        got_error = true;
      } else {
        assert.ok(stats.mtime instanceof Date);
        success_count++;
        fs.close(fd);
      }
    });
  });

  if (fs.getRootFS().supportsSynch()) {
    // fstatSync
    fs.open(existing_file, 'r', undefined, function(err, fd) {
      var stats;
      try {
        stats = fs.fstatSync(fd);
      } catch (e) {
        got_error = true;
      }
      if (stats) {
        assert.ok(stats.mtime instanceof Date);
        success_count++;
      }
      fs.close(fd);
    });
  }

  fs.stat(existing_file, function(err, s) {
    if (err) {
      got_error = true;
    } else {
      success_count++;
      assert.equal(false, s.isDirectory());
      assert.equal(true, s.isFile());
      assert.equal(false, s.isSocket());
      //assert.equal(false, s.isBlockDevice());
      assert.equal(false, s.isCharacterDevice());
      assert.equal(false, s.isFIFO());
      assert.equal(false, s.isSymbolicLink());

      assert.ok(s.mtime instanceof Date);
    }
  });

  process.on('exit', function() {
    var expected_success = 5;
    if (fs.getRootFS().supportsSynch()) expected_success++;
    assert.equal(expected_success, success_count);
    assert.equal(false, got_error);
  });
};
Exemplo n.º 8
0
 process.on('exit', function() {
   var expected_success = 5;
   if (fs.getRootFS().supportsSynch()) expected_success++;
   assert.equal(expected_success, success_count);
   assert.equal(false, got_error);
 });
Exemplo n.º 9
0
module.exports = function() {
  var oldmfs = fs.getRootFS();

  var rootForMfs = new BrowserFS.FileSystem.InMemory();
  BrowserFS.initialize(rootForMfs);
  fs.mkdirSync('/home');
  fs.mkdirSync('/home/anotherFolder');

  var newmfs = new BrowserFS.FileSystem.MountableFileSystem();
  // double mount, for funsies.
  newmfs.mount('/root', rootForMfs);
  // second mount is subdir of subdirectory that already exists in mount point.
  // also stresses our recursive mkdir code.
  newmfs.mount('/root/home/secondRoot', rootForMfs);
  newmfs.mount('/root/anotherRoot', rootForMfs);
  BrowserFS.initialize(newmfs);

  assert.equal(fs.readdirSync('/')[0], 'root', 'Invariant fail: Can query root directory.');

  var t1text = 'Invariant fail: Can query folder that contains items and a mount point.';
  var expectedHomeListing = ['anotherFolder', 'secondRoot'];
  var homeListing = fs.readdirSync('/root/home').sort();
  assert.deepEqual(homeListing, expectedHomeListing, t1text);

  fs.readdir('/root/home', function(err, files) {
    assert(!err, t1text);
    assert.deepEqual(files.sort(), expectedHomeListing, t1text);

    var t2text = "Invariant fail: Cannot delete a mount point.";
    codeAssertThrows(function() {
      fs.rmdirSync('/root/home/secondRoot');
    }, t2text);

    fs.rmdir('/root/home/secondRoot', function(err) {
      assert(err, t2text);
      assert(fs.statSync('/root/home').isDirectory(), "Invariant fail: Can stat a mount point.");

      var t4text = "Invariant fail: Cannot move a mount point.";
      codeAssertThrows(function() {
        fs.renameSync('/root/home/secondRoot', '/root/home/anotherFolder');
      }, t4text);

      fs.rename('/root/home/secondRoot', '/root/home/anotherFolder', function(err) {
        assert(err, t4text);

        fs.rmdirSync('/root/home/anotherFolder');

        var t5text = "Invariant fail: Cannot remove parent of mount point, even if empty in owning FS.";
        codeAssertThrows(function() {
          fs.rmdirSync('/root/home');
        }, t5text);

        fs.rmdir('/root/home', function(err) {
          assert(err, t5text);

          assert.deepEqual(fs.readdirSync('/root').sort(), ['anotherRoot', 'home'], "Invariant fail: Directory listings do not contain duplicate items when folder contains mount points w/ same names as existing files/folders.");

          var newRoot = new BrowserFS.FileSystem.InMemory();
          // Let's confuse things and mount something in '/'.
          newmfs.mount('/', newRoot);
          fs.mkdirSync('/home2');
          assert(fs.existsSync('/home2'));
          assert(newmfs.existsSync('/home2'));
          assert(fs.existsSync('/root'));
          newmfs.umount('/');
          assert(!fs.existsSync('/home2'));
        });
      });
    });

  });


  // Restore test FS on test end.
  process.on('exit', function() {
    BrowserFS.initialize(oldmfs);
  });
};
Exemplo n.º 10
0
module.exports = function() {
  var rootFS = fs.getRootFS();

  function check(async, sync) {
    var expected = /Path must be a string without null bytes./;
    var argsSync = Array.prototype.slice.call(arguments, 2);
    var argsAsync = argsSync.concat(function(er) {
      assert(er && er.message.match(expected));
    });

    if (rootFS.supportsSynch() && sync)
      assert.throws(function() {
        sync.apply(null, argsSync);
      }, expected);

    if (async)
      async.apply(null, argsAsync);
  }

  check(fs.appendFile,  fs.appendFileSync,  'foo\u0000bar');
  check(fs.lstat,       fs.lstatSync,       'foo\u0000bar');
  check(fs.mkdir,       fs.mkdirSync,       'foo\u0000bar', '0755');
  check(fs.open,        fs.openSync,        'foo\u0000bar', 'r');
  check(fs.readFile,    fs.readFileSync,    'foo\u0000bar');
  check(fs.readdir,     fs.readdirSync,     'foo\u0000bar');
  check(fs.realpath,    fs.realpathSync,    'foo\u0000bar');
  check(fs.rename,      fs.renameSync,      'foo\u0000bar', 'foobar');
  check(fs.rename,      fs.renameSync,      'foobar', 'foo\u0000bar');
  check(fs.rmdir,       fs.rmdirSync,       'foo\u0000bar');
  check(fs.stat,        fs.statSync,        'foo\u0000bar');
  check(fs.truncate,    fs.truncateSync,    'foo\u0000bar');
  check(fs.unlink,      fs.unlinkSync,      'foo\u0000bar');
  check(fs.writeFile,   fs.writeFileSync,   'foo\u0000bar');

  if (rootFS.supportsLinks()) {
    check(fs.link,        fs.linkSync,        'foo\u0000bar', 'foobar');
    check(fs.link,        fs.linkSync,        'foobar', 'foo\u0000bar');
    check(fs.readlink,    fs.readlinkSync,    'foo\u0000bar');
    check(fs.symlink,     fs.symlinkSync,     'foo\u0000bar', 'foobar');
    check(fs.symlink,     fs.symlinkSync,     'foobar', 'foo\u0000bar');
  }

  if (rootFS.supportsProps()) {
    check(fs.chmod,       fs.chmodSync,       'foo\u0000bar', '0644');
    check(fs.chown,       fs.chownSync,       'foo\u0000bar', 12, 34);
    check(fs.utimes,      fs.utimesSync,      'foo\u0000bar', 0, 0);
  }

  // BFS: We don't support watching files right now.
  //check(null,           fs.unwatchFile,     'foo\u0000bar', assert.fail);
  //check(null,           fs.watch,           'foo\u0000bar', assert.fail);
  //check(null,           fs.watchFile,       'foo\u0000bar', assert.fail);

  // an 'error' for exists means that it doesn't exist.
  // one of many reasons why this file is the absolute worst.
  fs.exists('foo\u0000bar', function(exists) {
    assert(!exists);
  });
  if (rootFS.supportsSynch()) {
    assert(!fs.existsSync('foo\u0000bar'));
  }
};