2014-01-21 21:25:09 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2013-12-03 20:14:20 +00:00
|
|
|
// We reuse the same set of tests for all adapters.
|
|
|
|
// buildTestsFor() creates a set of tests bound to an
|
2014-01-21 21:25:09 +00:00
|
|
|
// adapter, and uses the provider set on the query string
|
2014-03-04 00:11:31 +00:00
|
|
|
// (defaults to best available/supported provider, see test-utils.js).
|
2013-12-03 20:14:20 +00:00
|
|
|
function buildTestsFor(adapterName, buildAdapter) {
|
|
|
|
function encode(str) {
|
2014-01-14 15:56:36 +00:00
|
|
|
// TextEncoder is either native, or shimmed by Filer
|
2013-12-03 20:14:20 +00:00
|
|
|
return (new TextEncoder("utf-8")).encode(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make some string + binary buffer versions of things we'll need
|
|
|
|
var valueStr = "value", valueBuffer = encode(valueStr);
|
|
|
|
var value1Str = "value1", value1Buffer = encode(value1Str);
|
|
|
|
var value2Str = "value2", value2Buffer = encode(value2Str);
|
2013-11-29 20:39:42 +00:00
|
|
|
|
|
|
|
function createProvider() {
|
2014-01-21 21:25:09 +00:00
|
|
|
return buildAdapter(util.provider().provider);
|
2013-11-29 20:39:42 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 15:56:36 +00:00
|
|
|
describe("Filer.FileSystem.adapters." + adapterName, function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
2013-11-29 20:39:42 +00:00
|
|
|
it("is supported -- if it isn't, none of these tests can run.", function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
// Allow for combined adapters (e.g., 'Encryption+Compression') joined by '+'
|
2013-12-04 17:14:16 +00:00
|
|
|
adapterName.split('+').forEach(function(name) {
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(Filer.FileSystem.adapters[name].isSupported()).to.be.true;
|
2013-12-04 17:14:16 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("has open, getReadOnlyContext, and getReadWriteContext instance methods", function() {
|
2013-12-01 21:41:04 +00:00
|
|
|
var provider = createProvider();
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(provider.open).to.be.a('function');
|
|
|
|
expect(provider.getReadOnlyContext).to.be.a('function');
|
|
|
|
expect(provider.getReadWriteContext).to.be.a('function');
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-03-04 00:11:31 +00:00
|
|
|
describe("open a provider with an " + adapterName + " adapter", function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it("should open a new database", function(done) {
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(error, firstAccess) {
|
|
|
|
expect(error).not.to.exist;
|
2014-03-04 00:11:31 +00:00
|
|
|
// NOTE: we test firstAccess logic in the individual provider tests
|
|
|
|
// (see tests/spec/providers/*) but can't easily/actually test it here,
|
|
|
|
// since the provider-agnostic code in test-utils pre-creates a
|
|
|
|
// FileSystem object, thus eating the first access info.
|
|
|
|
// See https://github.com/js-platform/filer/issues/127
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-03-04 00:11:31 +00:00
|
|
|
describe("Read/Write operations on a provider with an " + adapterName + " adapter", function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it("should allow put() and get()", function(done) {
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(error, firstAccess) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
var context = provider.getReadWriteContext();
|
|
|
|
context.put("key", valueBuffer, function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.get("key", function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
2014-02-19 17:53:05 +00:00
|
|
|
expect(util.typedArrayEqual(result, valueBuffer)).to.be.true;
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it("should allow delete()", function(done) {
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(error, firstAccess) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
var context = provider.getReadWriteContext();
|
|
|
|
context.put("key", valueBuffer, function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.delete("key", function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.get("key", function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).not.to.exist;
|
|
|
|
done();
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it("should allow clear()", function(done) {
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(error, firstAccess) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
var context = provider.getReadWriteContext();
|
|
|
|
context.put("key1", value1Buffer, function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.put("key2", value2Buffer, function(error, result) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.clear(function(err) {
|
|
|
|
if(error) throw error;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.get("key1", function(error, result) {
|
|
|
|
if(error) throw error;
|
|
|
|
expect(result).not.to.exist;
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
context.get("key2", function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).not.to.exist;
|
|
|
|
done();
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it("should fail when trying to write on ReadOnlyContext", function(done) {
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(error, firstAccess) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
var context = provider.getReadOnlyContext();
|
|
|
|
context.put("key1", value1Buffer, function(error, result) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(result).not.to.exist;
|
|
|
|
done();
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-03 20:14:20 +00:00
|
|
|
// Encryption
|
2014-01-16 19:46:33 +00:00
|
|
|
buildTestsFor('Encryption', function buildAdapter(provider) {
|
2013-12-03 20:14:20 +00:00
|
|
|
var passphrase = '' + Date.now();
|
2014-01-16 19:46:33 +00:00
|
|
|
return new Filer.FileSystem.adapters.Encryption(passphrase, provider);
|
2013-12-03 20:14:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Compression
|
2014-01-16 19:46:33 +00:00
|
|
|
buildTestsFor('Compression', function buildAdapter(provider) {
|
|
|
|
return new Filer.FileSystem.adapters.Compression(provider);
|
2013-12-03 20:14:20 +00:00
|
|
|
});
|
2013-11-29 20:39:42 +00:00
|
|
|
|
2014-01-16 19:46:33 +00:00
|
|
|
// Encryption + Compression together
|
|
|
|
buildTestsFor('Encryption+Compression', function buildAdapter(provider) {
|
2013-12-04 17:14:16 +00:00
|
|
|
var passphrase = '' + Date.now();
|
2014-01-16 19:46:33 +00:00
|
|
|
var compression = new Filer.FileSystem.adapters.Compression(provider);
|
|
|
|
var encryptionWithCompression = new Filer.FileSystem.adapters.Encryption(passphrase, compression);
|
|
|
|
return encryptionWithCompression;
|
2013-12-04 17:14:16 +00:00
|
|
|
});
|
|
|
|
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|