* CachingFileStorage: Added more callbacks to provide user with more information what's going on

* Changed TestCacheSupervisor for easier use of the many callbacks
* Adapted tests for new callbacks

* GroupBaseActivity: Added sync menu command
* Preferences: Added option to enable/disable offline caching
* App: don't lock database when user wants to reload. This is done in PasswordActivity and should be done there after the password was filled into the pw field
* CheckDatabaseForChanges.cs: used when syncing a non-cached database
This commit is contained in:
Philipp Crocoll
2013-08-14 06:05:25 +02:00
parent d169cd3f5b
commit c63302ef5e
23 changed files with 1174 additions and 793 deletions

View File

@@ -19,7 +19,7 @@ namespace Kp2aUnitTests
TestRunner runner = new TestRunner();
// Run all tests from this assembly
runner.AddTests(Assembly.GetExecutingAssembly());
//runner.AddTests(new List<Type> { typeof(TestSaveDbCached) });
//runner.AddTests(new List<Type> { typeof(TestSynchronizeCachedDatabase) });
//runner.AddTests(typeof(TestSaveDbCached).GetMethod("TestLoadEditSaveWhenModified"));
//runner.AddTests(new List<Type> { typeof(TestSaveDb) });

View File

@@ -66,7 +66,7 @@ namespace Kp2aUnitTests
var app = CreateTestKp2aApp();
app.CreateNewDatabase();
bool loadSuccesful = false;
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = filename }, password, keyfile, new ActionOnFinish((success, message) =>
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = filename }, null, password, keyfile, new ActionOnFinish((success, message) =>
{
if (!success)
Kp2aLog.Log(message);

View File

@@ -1,29 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using KeePassLib.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using keepass2android.Io;
namespace Kp2aUnitTests
{
class TestCacheSupervisor: ICacheSupervisor
{
public bool CouldntOpenFromRemoteCalled { get; set; }
public bool CouldntSaveToRemoteCalled { get; set; }
public bool NotifyOpenFromLocalDueToConflictCalled { get; set; }
public const string CouldntOpenFromRemoteId = "CouldntOpenFromRemote";
public const string CouldntSaveToRemoteId = "CouldntSaveToRemote";
public const string NotifyOpenFromLocalDueToConflictId = "CouldntSaveToRemote";
public const string UpdatedCachedFileOnLoadId = "UpdatedCachedFileOnLoad";
public const string LoadedFromRemoteInSyncId = "LoadedFromRemoteInSync";
public const string UpdatedRemoteFileOnLoadId = "UpdatedRemoteFileOnLoad";
private HashSet<string> _callsMade = new HashSet<string>();
public void Reset()
{
_callsMade.Clear();
}
public void AssertNoCall()
{
string allCalls = _callsMade.Aggregate("", (current, s) => current + s + ",");
Assert.AreEqual("", allCalls);
}
public void AssertSingleCall(string id)
{
if ((_callsMade.Count == 1)
&& _callsMade.Single() == id)
{
Reset();
return;
}
Assert.Fail("expected only "+id+", but received: "+_callsMade.Aggregate("", (current, s) => current + s + ","));
}
public void CouldntSaveToRemote(IOConnectionInfo ioc, Exception e)
{
CouldntSaveToRemoteCalled = true;
_callsMade.Add(CouldntSaveToRemoteId);
}
public void CouldntOpenFromRemote(IOConnectionInfo ioc, Exception ex)
{
CouldntOpenFromRemoteCalled = true;
_callsMade.Add(CouldntOpenFromRemoteId);
}
public void UpdatedCachedFileOnLoad(IOConnectionInfo ioc)
{
_callsMade.Add(UpdatedCachedFileOnLoadId);
}
public void UpdatedRemoteFileOnLoad(IOConnectionInfo ioc)
{
_callsMade.Add(UpdatedRemoteFileOnLoadId);
}
public void NotifyOpenFromLocalDueToConflict(IOConnectionInfo ioc)
{
NotifyOpenFromLocalDueToConflictCalled = true;
_callsMade.Add(NotifyOpenFromLocalDueToConflictId);
}
public void LoadedFromRemoteInSync(IOConnectionInfo ioc)
{
_callsMade.Add(LoadedFromRemoteInSyncId);
}
}
}

View File

@@ -30,6 +30,8 @@ namespace Kp2aUnitTests
//read the file once. Should now be in the cache.
MemoryStream fileContents = ReadToMemoryStream(_fileStorage, CachingTestFile);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.UpdatedCachedFileOnLoadId);
//check it's the correct data:
Assert.AreEqual(MemoryStreamToString(fileContents), _defaultCacheFileContents);
@@ -41,8 +43,7 @@ namespace Kp2aUnitTests
AssertEqual(fileContents, fileContents2);
Assert.IsTrue(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
}
@@ -67,8 +68,7 @@ namespace Kp2aUnitTests
//read the file once. Should now be in the cache.
ReadToMemoryStream(_fileStorage, CachingTestFile);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.UpdatedCachedFileOnLoadId);
//let the base file storage go offline:
_testFileStorage.Offline = true;
@@ -77,16 +77,13 @@ namespace Kp2aUnitTests
string newContent = "new content";
WriteContentToCacheFile(newContent);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsTrue(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.CouldntSaveToRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId);
//now try to read the file again:
MemoryStream fileContents2 = ReadToMemoryStream(_fileStorage, CachingTestFile);
Assert.IsTrue(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.CouldntOpenFromRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
//should return the written content:
Assert.AreEqual(MemoryStreamToString(fileContents2), newContent);
@@ -96,8 +93,7 @@ namespace Kp2aUnitTests
MemoryStream fileContents3 = ReadToMemoryStream(_fileStorage, CachingTestFile);
Assert.AreEqual(MemoryStreamToString(fileContents3), newContent);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.UpdatedRemoteFileOnLoadId);
//ensure the data on the remote was synced:
MemoryStream fileContents4 = ReadToMemoryStream(_testFileStorage, CachingTestFile);
@@ -115,8 +111,7 @@ namespace Kp2aUnitTests
//read the file once. Should now be in the cache.
ReadToMemoryStream(_fileStorage, CachingTestFile);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.UpdatedCachedFileOnLoadId);
//let the base file storage go offline:
_testFileStorage.Offline = true;
@@ -125,9 +120,7 @@ namespace Kp2aUnitTests
string newLocalContent = "new local content";
WriteContentToCacheFile(newLocalContent);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsTrue(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.CouldntSaveToRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId);
//write something to the remote file:
File.WriteAllText(CachingTestFile, "new remote content");
@@ -142,12 +135,7 @@ namespace Kp2aUnitTests
Assert.AreEqual(MemoryStreamToString(fileContents2), newLocalContent);
//but a notification about the conflict should be made:
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
Assert.IsTrue(_testCacheSupervisor.NotifyOpenFromLocalDueToConflictCalled);
_testCacheSupervisor.NotifyOpenFromLocalDueToConflictCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.NotifyOpenFromLocalDueToConflictId);
}
@@ -160,13 +148,13 @@ namespace Kp2aUnitTests
//read the file once. Should now be in the cache.
ReadToMemoryStream(_fileStorage, CachingTestFile);
_testCacheSupervisor.Reset();
//write something to the cache:
string newContent = "new content";
WriteContentToCacheFile(newContent);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertNoCall();
Assert.AreEqual(newContent, File.ReadAllText(CachingTestFile));
}
@@ -180,17 +168,19 @@ namespace Kp2aUnitTests
//read the file once. Should now be in the cache.
ReadToMemoryStream(_fileStorage, CachingTestFile);
_testCacheSupervisor.Reset();
//delete remote file:
_testFileStorage.DeleteFile(IocForCacheFile);
//read again. shouldn't throw and give the same result:
var memStream = ReadToMemoryStream(_fileStorage, CachingTestFile);
//check if we received the correct content:
Assert.AreEqual(_defaultCacheFileContents, MemoryStreamToString(memStream));
Assert.AreEqual(_defaultCacheFileContents, MemoryStreamToString(memStream));
Assert.IsTrue(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
}
@@ -216,7 +206,8 @@ namespace Kp2aUnitTests
{
_testFileStorage = new TestFileStorage();
_testCacheSupervisor = new TestCacheSupervisor();
_fileStorage = new CachingFileStorage(_testFileStorage, Application.Context.CacheDir.Path, _testCacheSupervisor);
//_fileStorage = new CachingFileStorage(_testFileStorage, Application.Context.CacheDir.Path, _testCacheSupervisor);
_fileStorage = new CachingFileStorage(_testFileStorage, "/mnt/sdcard/kp2atest_cache", _testCacheSupervisor);
_fileStorage.ClearCache();
File.WriteAllText(CachingTestFile, _defaultCacheFileContents);
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
@@ -29,6 +30,18 @@ namespace Kp2aUnitTests
}
public void LockDatabase(bool allowQuickUnlock = true)
{
throw new NotImplementedException();
}
public void LoadDatabase(IOConnectionInfo ioConnectionInfo, MemoryStream memoryStream, string password, string keyFile,
ProgressDialogStatusLogger statusLogger)
{
_db.LoadData(this, ioConnectionInfo, memoryStream, password, keyFile, statusLogger);
}
public Database GetDb()
{
return _db;

View File

@@ -21,7 +21,7 @@ namespace Kp2aUnitTests
IKp2aApp app = new TestKp2aApp();
app.CreateNewDatabase();
bool loadSuccesful = false;
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = TestDbDirectory+filenameWithoutDir },
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = TestDbDirectory+filenameWithoutDir }, null,
password, keyfile, new ActionOnFinish((success, message) =>
{
if (!success)
@@ -78,7 +78,7 @@ namespace Kp2aUnitTests
app.CreateNewDatabase();
bool loadSuccesful = false;
LoadDb task = new LoadDb(app, ioc, "a", null, new ActionOnFinish((success, message) =>
LoadDb task = new LoadDb(app, ioc, null, "a", null, new ActionOnFinish((success, message) =>
{
if (!success)
Android.Util.Log.Debug("KP2ATest", "error loading db: " + message);
@@ -102,7 +102,7 @@ namespace Kp2aUnitTests
app.CreateNewDatabase();
bool loadSuccesful = false;
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
LoadDb task = new LoadDb(app, ioc, null, "test", null, new ActionOnFinish((success, message) =>
{
if (!success)
Android.Util.Log.Debug("KP2ATest", "error loading db: " + message);
@@ -128,7 +128,7 @@ namespace Kp2aUnitTests
bool loadSuccesful = false;
bool gotError = false;
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
LoadDb task = new LoadDb(app, ioc, null, "test", null, new ActionOnFinish((success, message) =>
{
if (!success)
{
@@ -156,7 +156,7 @@ namespace Kp2aUnitTests
bool loadSuccesful = false;
bool gotError = false;
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
LoadDb task = new LoadDb(app, ioc, null, "test", null, new ActionOnFinish((success, message) =>
{
if (!success)
{

View File

@@ -39,14 +39,15 @@ namespace Kp2aUnitTests
IOConnection.DeleteFile(new IOConnectionInfo { Path = DefaultFilename });
//save it and reload it so we have a base version
SaveDatabase(app);
_testCacheSupervisor.AssertNoCall();
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
//modify the database by adding a group:
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
//save the database again:
SaveDatabase(app);
Assert.IsNull(((TestKp2aApp)app).LastYesNoCancelQuestionTitle);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertNoCall();
//load database to a new app instance:
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
@@ -71,10 +72,10 @@ namespace Kp2aUnitTests
//modify the database by adding a group:
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
//save the database again:
_testCacheSupervisor.Reset();
SaveDatabase(app);
Assert.IsNull(((TestKp2aApp) app).LastYesNoCancelQuestionTitle);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertNoCall();
//load database to a new app instance:
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
@@ -109,11 +110,12 @@ namespace Kp2aUnitTests
SaveDatabase(app2);
_testCacheSupervisor.Reset();
foreach (var group in app.GetDb().KpDatabase.RootGroup.Groups)
Kp2aLog.Log("app d: " + group.Name);
Assert.IsNull(((TestKp2aApp)app).LastYesNoCancelQuestionTitle);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertNoCall();
//modify the database by adding a group:
PwGroup group1 = new PwGroup(true, true, "TestGroup", PwIcon.Apple);
@@ -124,10 +126,10 @@ namespace Kp2aUnitTests
//save the database again:
_testCacheSupervisor.Reset();
SaveDatabase(app);
Assert.AreEqual(((TestKp2aApp)app).LastYesNoCancelQuestionTitle, UiStringKey.TitleSyncQuestion);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.AssertNoCall();
//load database to a new app instance:

View File

@@ -47,6 +47,7 @@ namespace Kp2aUnitTests
//save it and reload it so we have a base version ("remote" and in the cache)
SaveDatabase(app);
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
string resultMessage;
bool wasSuccessful;
@@ -68,8 +69,8 @@ namespace Kp2aUnitTests
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
//save the database again (will be saved locally only)
SaveDatabase(app);
Assert.IsTrue(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.CouldntSaveToRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId);
//go online again:
_testFileStorage.Offline = false;
@@ -82,10 +83,10 @@ namespace Kp2aUnitTests
//ensure both files are identical and up to date now:
_testFileStorage.Offline = true;
var appOfflineLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
_testCacheSupervisor.CouldntOpenFromRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId);
_testFileStorage.Offline = false;
var appRemoteLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
AssertDatabasesAreEqual(app.GetDb().KpDatabase, appOfflineLoaded.GetDb().KpDatabase);
AssertDatabasesAreEqual(app.GetDb().KpDatabase, appRemoteLoaded.GetDb().KpDatabase);
@@ -101,6 +102,7 @@ namespace Kp2aUnitTests
//save it and reload it so we have a base version ("remote" and in the cache)
SaveDatabase(app);
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
//delete remote:
IOConnection.DeleteFile(new IOConnectionInfo { Path = DefaultFilename });
@@ -128,8 +130,11 @@ namespace Kp2aUnitTests
//save it and reload it so we have a base version ("remote" and in the cache)
SaveDatabase(app);
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
var app2 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
app2.FileStorage = _testFileStorage; //give app2 direct access to the remote file
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId);
//go offline:
_testFileStorage.Offline = true;
@@ -145,8 +150,7 @@ namespace Kp2aUnitTests
app2.GetDb().KpDatabase.RootGroup.AddGroup(newGroup2, true);
//save the database again (will be saved locally only for "app")
SaveDatabase(app);
Assert.IsTrue(_testCacheSupervisor.CouldntSaveToRemoteCalled);
_testCacheSupervisor.CouldntSaveToRemoteCalled = false;
_testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId);
//go online again:
_testFileStorage.Offline = false;