Added tests and functionality to ensure that caching and syncing works when the remote file is removed.
Added UI strings for sync and cache functionality
This commit is contained in:
@@ -20,6 +20,8 @@ namespace Kp2aUnitTests
|
||||
// Run all tests from this assembly
|
||||
runner.AddTests(Assembly.GetExecutingAssembly());
|
||||
//runner.AddTests(new List<Type> { typeof(TestSynchronizeCachedDatabase) });
|
||||
//runner.AddTests(new List<Type> { typeof(TestLoadDb) });}}
|
||||
//runner.AddTests(new List<Type> { typeof(TestCachingFileStorage) });
|
||||
//runner.AddTests(typeof(TestCachingFileStorage).GetMethod("TestSaveToRemote"));
|
||||
//runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadKdbpWithPasswordOnly"));
|
||||
//runner.AddTests(typeof(TestSaveDb).GetMethod("TestLoadKdbxAndSaveKdbp_TestIdenticalFiles"));
|
||||
|
||||
@@ -168,7 +168,32 @@ namespace Kp2aUnitTests
|
||||
Assert.IsFalse(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
|
||||
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
|
||||
|
||||
Assert.AreEqual(newContent, File.ReadAllText(CachingTestFile));
|
||||
Assert.AreEqual(newContent, File.ReadAllText(CachingTestFile));
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadFromRemoteWhenRemoteDeleted()
|
||||
{
|
||||
SetupFileStorage();
|
||||
|
||||
//read the file once. Should now be in the cache.
|
||||
ReadToMemoryStream(_fileStorage, CachingTestFile);
|
||||
|
||||
//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.IsTrue(_testCacheSupervisor.CouldntOpenFromRemoteCalled);
|
||||
Assert.IsFalse(_testCacheSupervisor.CouldntSaveToRemoteCalled);
|
||||
Assert.IsFalse(_testCacheSupervisor.RestoredRemoteCalled);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void WriteContentToCacheFile(string newContent)
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Android.App;
|
||||
using Android.OS;
|
||||
using KeePassLib.Serialization;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using keepass2android;
|
||||
using keepass2android.Io;
|
||||
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
@@ -91,6 +94,135 @@ namespace Kp2aUnitTests
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void LoadFromRemote1and1()
|
||||
{
|
||||
var ioc = RemoteIoc1and1; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
app.CreateNewDatabase();
|
||||
|
||||
bool loadSuccesful = false;
|
||||
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
|
||||
{
|
||||
if (!success)
|
||||
Android.Util.Log.Debug("KP2ATest", "error loading db: " + message);
|
||||
loadSuccesful = success;
|
||||
})
|
||||
);
|
||||
ProgressTask pt = new ProgressTask(app, Application.Context, task);
|
||||
Android.Util.Log.Debug("KP2ATest", "Running ProgressTask");
|
||||
pt.Run();
|
||||
pt.JoinWorkerThread();
|
||||
Android.Util.Log.Debug("KP2ATest", "PT.run finished");
|
||||
Assert.IsTrue(loadSuccesful, "didn't succesfully load database :-(");
|
||||
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void LoadFromRemote1and1NonExisting()
|
||||
{
|
||||
var ioc = RemoteIoc1and1NonExisting; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
app.CreateNewDatabase();
|
||||
|
||||
bool loadSuccesful = false;
|
||||
bool gotError = false;
|
||||
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
Android.Util.Log.Debug("KP2ATest", "error loading db: " + message);
|
||||
gotError = true;
|
||||
}
|
||||
loadSuccesful = success;
|
||||
})
|
||||
);
|
||||
ProgressTask pt = new ProgressTask(app, Application.Context, task);
|
||||
Android.Util.Log.Debug("KP2ATest", "Running ProgressTask");
|
||||
pt.Run();
|
||||
pt.JoinWorkerThread();
|
||||
Android.Util.Log.Debug("KP2ATest", "PT.run finished");
|
||||
Assert.IsFalse(loadSuccesful);
|
||||
Assert.IsTrue(gotError);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void LoadFromRemote1and1WrongCredentials()
|
||||
{
|
||||
var ioc = RemoteIoc1and1WrongCredentials; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
app.CreateNewDatabase();
|
||||
|
||||
bool loadSuccesful = false;
|
||||
bool gotError = false;
|
||||
LoadDb task = new LoadDb(app, ioc, "test", null, new ActionOnFinish((success, message) =>
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
Android.Util.Log.Debug("KP2ATest", "error loading db: " + message);
|
||||
gotError = true;
|
||||
}
|
||||
loadSuccesful = success;
|
||||
})
|
||||
);
|
||||
ProgressTask pt = new ProgressTask(app, Application.Context, task);
|
||||
Android.Util.Log.Debug("KP2ATest", "Running ProgressTask");
|
||||
pt.Run();
|
||||
pt.JoinWorkerThread();
|
||||
Android.Util.Log.Debug("KP2ATest", "PT.run finished");
|
||||
Assert.IsFalse(loadSuccesful);
|
||||
Assert.IsTrue(gotError);
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FileNotFoundExceptionWithWebDav()
|
||||
{
|
||||
var fileStorage = new BuiltInFileStorage();
|
||||
|
||||
//should work:
|
||||
using (var stream = fileStorage.OpenFileForRead(RemoteIoc1and1))
|
||||
{
|
||||
stream.CopyTo(new MemoryStream());
|
||||
}
|
||||
|
||||
//shouldn't give FileNotFound:
|
||||
bool gotException = false;
|
||||
try
|
||||
{
|
||||
using (var stream = fileStorage.OpenFileForRead(RemoteIoc1and1WrongCredentials))
|
||||
{
|
||||
stream.CopyTo(new MemoryStream());
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
Assert.Fail("shouldn't get FileNotFound with wrong credentials");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Kp2aLog.Log("received "+e);
|
||||
gotException = true;
|
||||
}
|
||||
Assert.IsTrue(gotException);
|
||||
//should give FileNotFound:
|
||||
gotException = false;
|
||||
try
|
||||
{
|
||||
using (var stream = fileStorage.OpenFileForRead(RemoteIoc1and1NonExisting))
|
||||
{
|
||||
stream.CopyTo(new MemoryStream());
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
gotException = true;
|
||||
}
|
||||
Assert.IsTrue(gotException);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadKdbpWithPasswordOnly()
|
||||
{
|
||||
|
||||
@@ -22,15 +22,6 @@ namespace Kp2aUnitTests
|
||||
private TestCacheSupervisor _testCacheSupervisor = new TestCacheSupervisor();
|
||||
private TestFileStorage _testFileStorage = new TestFileStorage();
|
||||
|
||||
[TestMethod]
|
||||
public void TestTodos()
|
||||
{
|
||||
Assert.IsFalse(true, "Wird immer ManagedTransform benutzt??");
|
||||
Assert.IsFalse(true, "TODOs in SyncDb");
|
||||
Assert.IsFalse(true, "FileNotFound");
|
||||
Assert.IsFalse(true, "Test merge files");
|
||||
}
|
||||
|
||||
protected override TestKp2aApp CreateTestKp2aApp()
|
||||
{
|
||||
TestKp2aApp app = base.CreateTestKp2aApp();
|
||||
@@ -100,6 +91,87 @@ namespace Kp2aUnitTests
|
||||
AssertDatabasesAreEqual(app.GetDb().KpDatabase, appRemoteLoaded.GetDb().KpDatabase);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSyncWhenRemoteDeleted()
|
||||
{
|
||||
//create the default database:
|
||||
TestKp2aApp app = SetupAppWithDefaultDatabase();
|
||||
|
||||
IOConnection.DeleteFile(new IOConnectionInfo {Path = DefaultFilename});
|
||||
//save it and reload it so we have a base version ("remote" and in the cache)
|
||||
SaveDatabase(app);
|
||||
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
|
||||
//delete remote:
|
||||
IOConnection.DeleteFile(new IOConnectionInfo { Path = DefaultFilename });
|
||||
|
||||
string resultMessage;
|
||||
bool wasSuccessful;
|
||||
|
||||
//sync:
|
||||
Synchronize(app, out wasSuccessful, out resultMessage);
|
||||
Assert.IsTrue(wasSuccessful);
|
||||
Assert.AreEqual(resultMessage, app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully));
|
||||
|
||||
//ensure the file is back here:
|
||||
var app2 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
AssertDatabasesAreEqual(app.GetDb().KpDatabase, app2.GetDb().KpDatabase);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSyncWhenConflict()
|
||||
{
|
||||
//create the default database:
|
||||
TestKp2aApp app = SetupAppWithDefaultDatabase();
|
||||
|
||||
IOConnection.DeleteFile(new IOConnectionInfo {Path = DefaultFilename});
|
||||
//save it and reload it so we have a base version ("remote" and in the cache)
|
||||
SaveDatabase(app);
|
||||
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
var app2 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
app2.FileStorage = _testFileStorage; //give app2 direct access to the remote file
|
||||
|
||||
//go offline:
|
||||
_testFileStorage.Offline = true;
|
||||
|
||||
|
||||
string resultMessage;
|
||||
bool wasSuccessful;
|
||||
|
||||
//modify the database by adding a group in both apps:
|
||||
PwGroup newGroup1 = new PwGroup(true, true, "TestGroup", PwIcon.Apple);
|
||||
app.GetDb().KpDatabase.RootGroup.AddGroup(newGroup1, true);
|
||||
PwGroup newGroup2 = new PwGroup(true, true, "TestGroupApp2", PwIcon.Apple);
|
||||
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;
|
||||
|
||||
//go online again:
|
||||
_testFileStorage.Offline = false;
|
||||
|
||||
//...and remote only for "app2":
|
||||
SaveDatabase(app2);
|
||||
|
||||
//try to sync:
|
||||
Synchronize(app, out wasSuccessful, out resultMessage);
|
||||
|
||||
Assert.IsTrue(wasSuccessful);
|
||||
Assert.AreEqual(UiStringKey.SynchronizedDatabaseSuccessfully.ToString(), resultMessage);
|
||||
|
||||
//build app2 with the newGroup1:
|
||||
app2.GetDb().KpDatabase.RootGroup.AddGroup(newGroup1, true);
|
||||
|
||||
var app3 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
|
||||
AssertDatabasesAreEqual(app.GetDb().KpDatabase, app2.GetDb().KpDatabase);
|
||||
AssertDatabasesAreEqual(app.GetDb().KpDatabase, app3.GetDb().KpDatabase);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Synchronize(TestKp2aApp app, out bool wasSuccessful, out string resultMessage)
|
||||
{
|
||||
bool success = false;
|
||||
@@ -110,6 +182,7 @@ namespace Kp2aUnitTests
|
||||
result = _result;
|
||||
}));
|
||||
sync.Run();
|
||||
sync.JoinWorkerThread();
|
||||
wasSuccessful = success;
|
||||
resultMessage = result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user