2010-08-05 09:24:45 -07:00
Cu . import ( "resource://services-sync/util.js" ) ;
Cu . import ( "resource://services-sync/service.js" ) ;
Cu . import ( "resource://services-sync/base_records/crypto.js" ) ;
Cu . import ( "resource://services-sync/base_records/keys.js" ) ;
Cu . import ( "resource://services-sync/resource.js" ) ;
function FakeCollection ( ) {
this . deleted = false ;
}
FakeCollection . prototype = {
handler : function ( ) {
let self = this ;
return function ( request , response ) {
let body = "" ;
if ( request . method == "DELETE" ) {
body = JSON . stringify ( Date . now ( ) / 1000 ) ;
self . deleted = true ;
}
response . setStatusLine ( request . httpVersion , 200 , "OK" ) ;
response . bodyOutputStream . write ( body , body . length ) ;
} ;
}
} ;
function serviceUnavailable ( request , response ) {
let body = "Service Unavailable" ;
response . setStatusLine ( request . httpVersion , 503 , "Service Unavailable" ) ;
response . bodyOutputStream . write ( body , body . length ) ;
}
function createAndUploadKeypair ( ) {
let keys = PubKeys . createKeypair ( ID . get ( "WeaveCryptoID" ) ,
PubKeys . defaultKeyUri ,
PrivKeys . defaultKeyUri ) ;
PubKeys . uploadKeypair ( keys ) ;
}
function createAndUploadSymKey ( url ) {
let symkey = Svc . Crypto . generateRandomKey ( ) ;
let pubkey = PubKeys . getDefaultKey ( ) ;
let meta = new CryptoMeta ( url ) ;
meta . addUnwrappedKey ( pubkey , symkey ) ;
let res = new Resource ( meta . uri ) ;
res . put ( meta ) ;
CryptoMetas . set ( url , meta ) ;
}
function setUpTestFixtures ( ) {
let cryptoService = new FakeCryptoService ( ) ;
2010-08-25 15:49:45 -07:00
Service . clusterURL = "http://localhost:8080/" ;
Service . username = "johndoe" ;
Service . passphrase = "secret" ;
2010-08-05 09:24:45 -07:00
createAndUploadKeypair ( ) ;
createAndUploadSymKey ( "http://localhost:8080/1.0/johndoe/storage/crypto/steam" ) ;
createAndUploadSymKey ( "http://localhost:8080/1.0/johndoe/storage/crypto/petrol" ) ;
createAndUploadSymKey ( "http://localhost:8080/1.0/johndoe/storage/crypto/diesel" ) ;
}
function test _withCollectionList _failOnCrypto ( ) {
2010-08-25 15:49:45 -07:00
_ ( "Service.wipeServer() deletes collections given as argument and aborts if a collection delete fails." ) ;
2010-08-05 09:24:45 -07:00
let steam _coll = new FakeCollection ( ) ;
let petrol _coll = new FakeCollection ( ) ;
let diesel _coll = new FakeCollection ( ) ;
let crypto _steam = new ServerWBO ( 'steam' ) ;
let crypto _diesel = new ServerWBO ( 'diesel' ) ;
let server = httpd _setup ( {
"/1.0/johndoe/storage/keys/pubkey" : ( new ServerWBO ( 'pubkey' ) ) . handler ( ) ,
"/1.0/johndoe/storage/keys/privkey" : ( new ServerWBO ( 'privkey' ) ) . handler ( ) ,
"/1.0/johndoe/storage/steam" : steam _coll . handler ( ) ,
"/1.0/johndoe/storage/petrol" : petrol _coll . handler ( ) ,
"/1.0/johndoe/storage/diesel" : diesel _coll . handler ( ) ,
"/1.0/johndoe/storage/crypto/steam" : crypto _steam . handler ( ) ,
"/1.0/johndoe/storage/crypto/petrol" : serviceUnavailable ,
"/1.0/johndoe/storage/crypto/diesel" : crypto _diesel . handler ( )
} ) ;
do _test _pending ( ) ;
try {
setUpTestFixtures ( ) ;
_ ( "Confirm initial environment." ) ;
do _check _false ( steam _coll . deleted ) ;
do _check _false ( petrol _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
do _check _true ( crypto _steam . payload != undefined ) ;
do _check _true ( crypto _diesel . payload != undefined ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/steam" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/petrol" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/diesel" ) ) ;
_ ( "wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection's symkey." ) ;
let error ;
try {
2010-08-25 15:49:45 -07:00
Service . wipeServer ( [ "non-existent" , "steam" , "petrol" , "diesel" ] ) ;
2010-08-05 09:24:45 -07:00
} catch ( ex ) {
error = ex ;
}
_ ( "wipeServer() threw this exception: " + error ) ;
do _check _true ( error != undefined ) ;
_ ( "wipeServer stopped deleting after encountering an error with the 'petrol' collection's symkey, thus only 'steam' and 'petrol' have been deleted." ) ;
do _check _true ( steam _coll . deleted ) ;
do _check _true ( petrol _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
do _check _true ( crypto _steam . payload == undefined ) ;
do _check _true ( crypto _diesel . payload != undefined ) ;
do _check _false ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/steam" ) ) ;
do _check _false ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/petrol" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/diesel" ) ) ;
} finally {
server . stop ( do _test _finished ) ;
Svc . Prefs . resetBranch ( "" ) ;
CryptoMetas . clearCache ( ) ;
}
}
function test _withCollectionList _failOnCollection ( ) {
2010-08-25 15:49:45 -07:00
_ ( "Service.wipeServer() deletes collections given as argument." ) ;
2010-08-05 09:24:45 -07:00
let steam _coll = new FakeCollection ( ) ;
let diesel _coll = new FakeCollection ( ) ;
let crypto _steam = new ServerWBO ( 'steam' ) ;
let crypto _petrol = new ServerWBO ( 'petrol' ) ;
let crypto _diesel = new ServerWBO ( 'diesel' ) ;
let server = httpd _setup ( {
"/1.0/johndoe/storage/keys/pubkey" : ( new ServerWBO ( 'pubkey' ) ) . handler ( ) ,
"/1.0/johndoe/storage/keys/privkey" : ( new ServerWBO ( 'privkey' ) ) . handler ( ) ,
"/1.0/johndoe/storage/steam" : steam _coll . handler ( ) ,
"/1.0/johndoe/storage/petrol" : serviceUnavailable ,
"/1.0/johndoe/storage/diesel" : diesel _coll . handler ( ) ,
"/1.0/johndoe/storage/crypto/steam" : crypto _steam . handler ( ) ,
"/1.0/johndoe/storage/crypto/petrol" : crypto _petrol . handler ( ) ,
"/1.0/johndoe/storage/crypto/diesel" : crypto _diesel . handler ( )
} ) ;
do _test _pending ( ) ;
try {
setUpTestFixtures ( ) ;
_ ( "Confirm initial environment." ) ;
do _check _false ( steam _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
do _check _true ( crypto _steam . payload != undefined ) ;
do _check _true ( crypto _petrol . payload != undefined ) ;
do _check _true ( crypto _diesel . payload != undefined ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/steam" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/petrol" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/diesel" ) ) ;
_ ( "wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection." ) ;
let error ;
try {
2010-08-25 15:49:45 -07:00
Service . wipeServer ( [ "non-existent" , "steam" , "petrol" , "diesel" ] ) ;
2010-08-05 09:24:45 -07:00
} catch ( ex ) {
error = ex ;
}
_ ( "wipeServer() threw this exception: " + error ) ;
do _check _true ( error != undefined ) ;
_ ( "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted." ) ;
do _check _true ( steam _coll . deleted ) ;
do _check _false ( diesel _coll . deleted ) ;
do _check _true ( crypto _steam . payload == undefined ) ;
do _check _true ( crypto _petrol . payload != undefined ) ;
do _check _true ( crypto _diesel . payload != undefined ) ;
do _check _false ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/steam" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/petrol" ) ) ;
do _check _true ( CryptoMetas . contains ( "http://localhost:8080/1.0/johndoe/storage/crypto/diesel" ) ) ;
} finally {
server . stop ( do _test _finished ) ;
Svc . Prefs . resetBranch ( "" ) ;
CryptoMetas . clearCache ( ) ;
}
}
function run _test ( ) {
test _withCollectionList _failOnCollection ( ) ;
test _withCollectionList _failOnCrypto ( ) ;
}