filer/tests/spec/fs.xattr.spec.js

395 lines
11 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var Filer = require('../..');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.xattr', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function () {
var fs = util.fs();
expect(fs.setxattr).to.be.a('function');
expect(fs.getxattr).to.be.a('function');
expect(fs.removexattr).to.be.a('function');
expect(fs.fsetxattr).to.be.a('function');
expect(fs.fgetxattr).to.be.a('function');
});
2014-05-23 20:53:50 +00:00
it('should error when setting with a name that is not a string', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 89, 'testvalue', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when setting with a name that is null', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', null, 'testvalue', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when setting with an invalid flag', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 'value', 'InvalidFlag', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when when setting an extended attribute which exists with XATTR_CREATE flag', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 'value', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 'othervalue', 'CREATE', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EEXIST');
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when setting an extended attribute which does not exist with XATTR_REPLACE flag', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 'value', 'REPLACE', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOATTR');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when getting an attribute with a name that is empty', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', '', function(error, value) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when getting an attribute where the name is not a string', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 89, function(error, value) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when getting an attribute that does not exist', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function(error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function(error, value) {
expect(error).to.exist;
expect(error.code).to.equal('ENOATTR');
done();
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when file descriptor is invalid', function(done) {
var fs = util.fs();
var completeSet, completeGet, completeRemove;
var _value;
2014-05-23 20:53:50 +00:00
completeSet = completeGet = completeRemove = false;
2014-05-23 20:53:50 +00:00
function maybeDone() {
if(completeSet && completeGet && completeRemove) {
done();
}
2014-05-23 20:53:50 +00:00
}
2014-05-23 20:53:50 +00:00
fs.fsetxattr(1, 'test', 'value', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
completeSet = true;
maybeDone();
});
2014-05-23 20:53:50 +00:00
fs.fgetxattr(1, 'test', function(error, value) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
expect(value).not.to.exist;
completeGet = true;
maybeDone();
});
2014-05-23 20:53:50 +00:00
fs.fremovexattr(1, 'test', function(error, value) {
expect(error).to.exist;
expect(error.code).to.equal('EBADF');
completeRemove = true;
maybeDone();
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should set and get an extended attribute of a path', function(done) {
var fs = util.fs();
var name = 'test';
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', name, 'somevalue', function(error) {
expect(error).not.to.exist;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', name, function(error, value) {
expect(error).not.to.exist;
expect(value).to.equal('somevalue');
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should error when attempting to remove a non-existing attribute', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.removexattr('/testfile', 'testenoattr', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOATTR');
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should set and get an empty string as a value', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', '', function (error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
expect(error).not.to.exist;
expect(value).to.equal('');
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should set and get an extended attribute for a valid file descriptor', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.open('/testfile', 'w', function (error, ofd) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.fsetxattr(ofd, 'test', 'value', function (error) {
expect(error).not.to.exist;
2014-05-23 20:53:50 +00:00
fs.fgetxattr(ofd, 'test', function (error, value) {
expect(error).not.to.exist;
expect(value).to.equal('value');
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should set and get an object to an extended attribute', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', { key1: 'test', key2: 'value', key3: 87 }, function (error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
expect(error).not.to.exist;
expect(value).to.deep.equal({ key1: 'test', key2: 'value', key3: 87 });
done();
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should update/overwrite an existing extended attribute', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 'value', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
expect(value).to.equal('value');
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', { o: 'object', t: 'test' }, function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
expect(value).to.deep.equal({ o: 'object', t: 'test' });
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', 100, 'REPLACE', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
expect(value).to.equal(100);
done();
});
});
});
});
2014-05-23 20:53:50 +00:00
});
})
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should set multiple extended attributes for a path', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
fs.setxattr('/testfile', 'test', 89, function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'other', 'attribute', function (error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
expect(value).to.equal(89);
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'other', function (error, value) {
expect(error).not.to.exist;
expect(value).to.equal('attribute');
done();
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should remove an extended attribute from a path', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
fs.setxattr('/testfile', 'test', 'somevalue', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
expect(value).to.equal('somevalue');
2014-05-23 20:53:50 +00:00
fs.removexattr('/testfile', 'test', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOATTR');
done();
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should remove an extended attribute from a valid file descriptor', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.open('/testfile', 'w', function (error, ofd) {
if (error) throw error;
fs.fsetxattr(ofd, 'test', 'somevalue', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.fgetxattr(ofd, 'test', function (error, value) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
expect(value).to.equal('somevalue');
2014-05-23 20:53:50 +00:00
fs.fremovexattr(ofd, 'test', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.fgetxattr(ofd, 'test', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOATTR');
done();
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-05-23 20:53:50 +00:00
it('should allow setting with a null value', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.setxattr('/testfile', 'test', null, function (error) {
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.getxattr('/testfile', 'test', function (error, value) {
expect(error).not.to.exist;
expect(value).to.be.null;
done();
});
});
});
});
});