Commit 4a4b4a5b authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Adding Crypted Storage + Qunit tests.

Load, GetList, and Remove document do not work.
parent f089c138
define ('SJCL',['SJCLAPI'], function () {
return sjcl;
});
\ No newline at end of file
/**
* Adds 4 storages to JIO.
* Adds 5 storages to JIO.
* - LocalStorage ('local')
* - DAVStorage ('dav')
* - ReplicateStorage ('replicate')
* - IndexedStorage ('indexed')
* - CryptedStorage ('crypted')
*
* @module JIOStorages
*/
(function () {
var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
var jio_storage_loader =
function ( LocalOrCookieStorage, $, Base64, sjcl, Jio) {
////////////////////////////////////////////////////////////////////////////
// Tools
......@@ -1016,7 +1018,6 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
newjob = that.cloneJob();
newjob.storage = that.getSecondStorage();
newjob.callback = loadcallback;
console.log (newjob);
that.addJob ( newjob );
},
settings = that.cloneOptionObject();
......@@ -1077,19 +1078,194 @@ var jio_storage_loader = function ( LocalOrCookieStorage, Base64, Jio, $) {
// end Indexed Storage
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Crypted Storage
/**
* JIO Crypted Storage. Type = 'crypted'.
* It will encrypt the file and its metadata stringified by JSON.
*/
newCryptedStorage = function ( spec, my ) {
// CryptedStorage constructor
var that = Jio.newBaseStorage( spec, my ), priv = {};
priv.encrypt = function (data,callback,index) {
// end with a callback in order to improve encrypt to an
// asynchronous encryption.
var tmp = sjcl.encrypt (that.getStorageUserName()+':'+
that.getStoragePassword(), data);
callback(tmp,index);
};
priv.decrypt = function (data,callback,index) {
var tmp = sjcl.decrypt (that.getStorageUserName()+':'+
that.getStoragePassword(), data);
callback(tmp,index);
};
/**
* Checks the availability of a user name set in the job.
* @method checkNameAvailability
*/
that.checkNameAvailability = function () {
var newjob = that.cloneJob();
newjob.storage = that.getSecondStorage();
newjob.callback = function (result) {
if (result.status === 'done') {
that.done(result.return_value);
} else {
that.fail(result.error);
}
};
that.addJob( newjob );
}; // end checkNameAvailability
/**
* Saves a document.
* @method saveDocument
*/
that.saveDocument = function () {
var newjob, newfilename, newfilecontent,
_1 = function () {
priv.encrypt(that.getFileName(),function(res) {
newfilename = res;
_2();
});
},
_2 = function () {
priv.encrypt(
JSON.stringify({
fileName: that.getFileName(),
fileContent:that.getFileContent()
}),
function(res) {
newfilecontent = res;
_3();
});
},
_3 = function () {
newjob = that.cloneJob();
newjob.fileName = newfilename;
newjob.fileContent = newfilecontent;
newjob.storage = that.getSecondStorage();
newjob.callback = function (result) {
if (result.status === 'done') {
that.done();
} else {
that.fail(result.error);
}
};
that.addJob ( newjob );
};
_1();
}; // end saveDocument
/**
* Loads a document.
* job.options.metadata_only {boolean}
* job.options.content_only {boolean}
* @method loadDocument
*/
that.loadDocument = function () {
var newjob, newfilename,
_1 = function () {
priv.encrypt(that.getFileName(),function(res) {
newfilename = res;
_2();
});
},
_2 = function () {
newjob = that.cloneJob();
newjob.fileName = newfilename;
newjob.storage = that.getSecondStorage();
newjob.callback = loadcallback;
console.log (newjob);
that.addJob ( newjob );
},
loadcallback = function (result) {
if (result.status === 'done') {
priv.decrypt (result.return_value.fileContent,function(res){
that.done(JSON.parse(res));
});
} else {
that.fail(result.error);
}
};
_1();
}; // end loadDocument
/**
* Gets a document list.
* @method getDocumentList
*/
that.getDocumentList = function () {
var newjob, i, l, cpt = 0, array,
_1 = function () {
newjob = that.cloneJob();
newjob.storage = that.getSecondStorage();
newjob.callback = getListCallback;
that.addJob ( newjob );
},
getListCallback = function (result) {
if (result.status === 'done') {
array = result.return_value;
for (i = 0, l = array.length; i < l; i+= 1) {
priv.decrypt (array[i],
lastCallback,i);
}
} else {
that.fail(result.error);
}
},
lastCallback = function (res,index) {
var tmp;
cpt++;
tmp = JSON.parse(res);
array[index] = res.fileName;
array[index] = res.fileContent;
if (cpt === l) {
// this is the last callback
that.done(array);
}
};
_1();
}; // end getDocumentList
/**
* Removes a document.
* @method removeDocument
*/
that.removeDocument = function () {
var newjob = that.cloneJob();
newjob.storage = that.getSecondStorage();
newjob.callback = function (result) {
if (result.status === 'done') {
that.done();
} else {
that.fail(result.error);
}
};
that.addJob(newjob);
};
return that;
};
// end Crypted Storage
////////////////////////////////////////////////////////////////////////////
// add key to storageObjectType of global jio
Jio.addStorageType('local', newLocalStorage);
Jio.addStorageType('dav', newDAVStorage);
Jio.addStorageType('replicate', newReplicateStorage);
Jio.addStorageType('indexed', newIndexedStorage);
Jio.addStorageType('crypted', newCryptedStorage);
};
if (window.requirejs) {
define ('JIOStorages',
['LocalOrCookieStorage','Base64','JIO','jQuery'],
['LocalOrCookieStorage','jQuery','Base64','SJCL','JIO'],
jio_storage_loader);
} else {
jio_storage_loader ( LocalOrCookieStorage, Base64, JIO, jQuery );
jio_storage_loader ( LocalOrCookieStorage, jQuery, Base64, sjcl, JIO);
}
}());
(function () { var thisfun = function(loader) {
var JIO = loader.JIO,
LocalOrCookieStorage = loader.LocalOrCookieStorage,
sjcl = loader.sjcl,
Base64 = loader.Base64,
$ = loader.jQuery;
......@@ -391,6 +392,8 @@ test ('Document load', function () {
// re-load file after saving it manually
doc = {'fileName':'file','fileContent':'content',
'lastModified':1234,'creationDate':1000};
LocalOrCookieStorage.setItem ('jio/localfilenamearray/MrLoadName/jiotests',
['file']);
LocalOrCookieStorage.setItem ('jio/local/MrLoadName/jiotests/file',doc);
mytest ('return_value',doc);
......@@ -1001,6 +1004,74 @@ test ('Remove document', function () {
o.jio.stop();
});
module ('Jio CryptedStorage');
test ('Check name availability' , function () {
var o = {}, clock = this.sandbox.useFakeTimers();
o.jio=JIO.createNew({type:'crypted',
storage:{type:'local',
userName:'cryptcheck'}},
{ID:'jiotests'});
o.f = function (result) {
deepEqual (result.return_value,true,'name must be available');
};
this.spy(o,'f');
o.jio.checkNameAvailability({userName:'cryptcheck',
maxtries:1,callback:o.f});
clock.tick(1000);
if (!o.f.calledOnce) {
ok (false, 'no response / too much results');
}
o.jio.stop();
});
test ('Document save' , function () {
var o = {}, clock = this.sandbox.useFakeTimers();
o.jio=JIO.createNew({type:'crypted',
password:'mypwd',
storage:{type:'local',
userName:'cryptsave'}},
{ID:'jiotests'});
o.f = function (result) {
deepEqual (result.status,'done','save ok');
};
this.spy(o,'f');
o.jio.saveDocument({fileName:'testsave',fileContent:'contentoftest',
maxtries:1,callback:o.f});
clock.tick(1000);
if (!o.f.calledOnce) {
ok (false, 'no response / too much results');
}
o.jio.stop();
});
test ('Document Load' , function () {
var o = {}, clock = this.sandbox.useFakeTimers();
o.jio=JIO.createNew({type:'crypted',
password:'mypwd',
storage:{type:'local',
userName:'cryptload'}},
{ID:'jiotests'});
o.f = function (result) {
if (result.status === 'done') {
deepEqual (result.return_value,{fileName:'testload',
fileContent:'contentoftest',
lastModified:500,
creationDate:500}
,'load ok');
} else {
ok (false ,'cannot load');
}
};
this.spy(o,'f');
o.jio.loadDocument({fileName:'testload',
maxtries:1,callback:o.f});
clock.tick(1000);
if (!o.f.calledOnce) {
ok (false, 'no response / too much results');
}
o.jio.stop();
});
// end require
}; // end thisfun
......@@ -1016,13 +1087,16 @@ if (window.requirejs) {
Base64API: '../lib/base64/base64',
Base64: '../js/base64.requirejs_module',
JIODummyStorages: '../src/jio.dummystorages',
JIOStorages: '../src/jio.storage'
JIOStorages: '../src/jio.storage',
SJCLAPI:'../lib/sjcl/sjcl',
SJCL:'../js/sjcl.requirejs_module'
}
});
require(['jiotestsloader'],thisfun);
} else {
thisfun ({LocalOrCookieStorage:LocalOrCookieStorage,
JIO:JIO,
sjcl:sjcl,
Base64:Base64,
jQuery:jQuery});
}
......
define ('jiotestsloader',[
'LocalOrCookieStorage','JIO','Base64',
'LocalOrCookieStorage','JIO','Base64','SJCL',
'jQuery','JIODummyStorages','JIOStorages'],
function (LocalOrCookieStorage,JIO,Base64,jQuery) {
function (LocalOrCookieStorage,JIO,Base64,sjcl,jQuery) {
return {
LocalOrCookieStorage: LocalOrCookieStorage,
JIO: JIO,
sjcl: sjcl,
Base64: Base64,
jQuery: jQuery
};
......
......@@ -15,6 +15,7 @@
</script>
<script type="text/javascript" src="../src/jio.js"></script>
<script type="text/javascript" src="../lib/base64/base64.js"></script>
<script type="text/javascript" src="../lib/sjcl/sjcl.js"></script>
<script type="text/javascript" src="../src/jio.dummystorages.js"></script>
<script type="text/javascript" src="../src/jio.storage.js"></script>
<script type="text/javascript" src="jiotests.js"></script>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment