2014-01-14 15:56:36 +00:00
|
|
|
define(["Filer"], function(Filer) {
|
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
|
2013-12-01 21:41:04 +00:00
|
|
|
// adapter, and uses a Memory() provider internally.
|
2013-11-29 20:39:42 +00:00
|
|
|
|
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-14 15:56:36 +00:00
|
|
|
var memoryProvider = new Filer.FileSystem.providers.Memory();
|
2013-12-03 20:14:20 +00:00
|
|
|
return buildAdapter(memoryProvider);
|
2013-11-29 20:39:42 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 15:56:36 +00:00
|
|
|
describe("Filer.FileSystem.adapters." + adapterName, function() {
|
2013-11-29 20:39:42 +00:00
|
|
|
it("is supported -- if it isn't, none of these tests can run.", function() {
|
2013-12-04 17:14:16 +00:00
|
|
|
// Allow for combined adapters (e.g., 'AES+Zlib') joined by '+'
|
|
|
|
adapterName.split('+').forEach(function(name) {
|
2014-01-14 15:56:36 +00:00
|
|
|
expect(Filer.FileSystem.adapters[name].isSupported()).toEqual(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();
|
|
|
|
expect(typeof provider.open).toEqual('function');
|
|
|
|
expect(typeof provider.getReadOnlyContext).toEqual('function');
|
|
|
|
expect(typeof provider.getReadWriteContext).toEqual('function');
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
|
2013-12-01 21:41:04 +00:00
|
|
|
describe("open a Memory provider with an " + adapterName + " adapter", function() {
|
|
|
|
it("should open a new database", function() {
|
2013-11-29 20:39:42 +00:00
|
|
|
var complete = false;
|
|
|
|
var _error, _result;
|
|
|
|
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(err, firstAccess) {
|
|
|
|
_error = err;
|
|
|
|
_result = firstAccess;
|
|
|
|
complete = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function() {
|
|
|
|
return complete;
|
|
|
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(_error).toEqual(null);
|
|
|
|
expect(_result).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-04 17:14:16 +00:00
|
|
|
describe("Read/Write operations on a Memory provider with an " + adapterName + " adapter", function() {
|
2013-11-29 20:39:42 +00:00
|
|
|
it("should allow put() and get()", function() {
|
|
|
|
var complete = false;
|
|
|
|
var _error, _result;
|
|
|
|
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(err, firstAccess) {
|
|
|
|
_error = err;
|
|
|
|
|
|
|
|
var context = provider.getReadWriteContext();
|
2013-12-03 20:14:20 +00:00
|
|
|
context.put("key", valueBuffer, function(err, result) {
|
2013-11-29 20:39:42 +00:00
|
|
|
_error = _error || err;
|
|
|
|
context.get("key", function(err, result) {
|
|
|
|
_error = _error || err;
|
|
|
|
_result = result;
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function() {
|
|
|
|
return complete;
|
|
|
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(_error).toEqual(null);
|
2013-12-03 20:14:20 +00:00
|
|
|
expect(_result).toEqual(valueBuffer);
|
2013-11-29 20:39:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow delete()", function() {
|
|
|
|
var complete = false;
|
|
|
|
var _error, _result;
|
|
|
|
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(err, firstAccess) {
|
|
|
|
_error = err;
|
|
|
|
|
|
|
|
var context = provider.getReadWriteContext();
|
2013-12-03 20:14:20 +00:00
|
|
|
context.put("key", valueBuffer, function(err, result) {
|
2013-11-29 20:39:42 +00:00
|
|
|
_error = _error || err;
|
|
|
|
context.delete("key", function(err, result) {
|
|
|
|
_error = _error || err;
|
|
|
|
context.get("key", function(err, result) {
|
|
|
|
_error = _error || err;
|
|
|
|
_result = result;
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function() {
|
|
|
|
return complete;
|
|
|
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(_error).toEqual(null);
|
|
|
|
expect(_result).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow clear()", function() {
|
|
|
|
var complete = false;
|
|
|
|
var _error, _result1, _result2;
|
|
|
|
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(err, firstAccess) {
|
|
|
|
_error = err;
|
|
|
|
|
|
|
|
var context = provider.getReadWriteContext();
|
2013-12-03 20:14:20 +00:00
|
|
|
context.put("key1", value1Buffer, function(err, result) {
|
2013-11-29 20:39:42 +00:00
|
|
|
_error = _error || err;
|
2013-12-03 20:14:20 +00:00
|
|
|
context.put("key2", value2Buffer, function(err, result) {
|
2013-11-29 20:39:42 +00:00
|
|
|
_error = _error || err;
|
|
|
|
|
|
|
|
context.clear(function(err) {
|
|
|
|
_error = _error || err;
|
|
|
|
|
|
|
|
context.get("key1", function(err, result) {
|
|
|
|
_error = _error || err;
|
|
|
|
_result1 = result;
|
|
|
|
|
|
|
|
context.get("key2", function(err, result) {
|
|
|
|
_error = _error || err;
|
|
|
|
_result2 = result;
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function() {
|
|
|
|
return complete;
|
|
|
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(_error).toEqual(null);
|
|
|
|
expect(_result1).toEqual(null);
|
|
|
|
expect(_result2).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail when trying to write on ReadOnlyContext", function() {
|
|
|
|
var complete = false;
|
|
|
|
var _error, _result;
|
|
|
|
|
|
|
|
var provider = createProvider();
|
|
|
|
provider.open(function(err, firstAccess) {
|
|
|
|
_error = err;
|
|
|
|
|
|
|
|
var context = provider.getReadOnlyContext();
|
2013-12-03 20:14:20 +00:00
|
|
|
context.put("key1", value1Buffer, function(err, result) {
|
2013-11-29 20:39:42 +00:00
|
|
|
_error = _error || err;
|
|
|
|
_result = result;
|
|
|
|
|
|
|
|
complete = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function() {
|
|
|
|
return complete;
|
|
|
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(_error).toBeDefined();
|
|
|
|
expect(_result).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|