Implemented Save-Test

This commit is contained in:
Philipp Crocoll 2013-06-28 20:22:28 +02:00
parent 903de8368a
commit 5a82ed99bf
9 changed files with 746 additions and 707 deletions

View File

@ -33,7 +33,7 @@ namespace keepass2android
public HashSet<PwGroup> Dirty = new HashSet<PwGroup>(new PwGroupEqualityFromIdComparer()); public HashSet<PwGroup> Dirty = new HashSet<PwGroup>(new PwGroupEqualityFromIdComparer());
public PwGroup Root; public PwGroup Root;
public PwDatabase KpDatabase; public PwDatabase KpDatabase;
public IOConnectionInfo Ioc; public IOConnectionInfo Ioc { get { return KpDatabase.IOConnectionInfo; } }
public DateTime LastChangeDate; public DateTime LastChangeDate;
public SearchDbHelper SearchHelper; public SearchDbHelper SearchHelper;
@ -92,8 +92,6 @@ namespace keepass2android
public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, String password, String keyfile, UpdateStatus status) public void LoadData(IKp2aApp app, IOConnectionInfo iocInfo, String password, String keyfile, UpdateStatus status)
{ {
Ioc = iocInfo;
PwDatabase pwDatabase = new PwDatabase(); PwDatabase pwDatabase = new PwDatabase();
CompositeKey compositeKey = new CompositeKey(); CompositeKey compositeKey = new CompositeKey();
@ -212,7 +210,6 @@ namespace keepass2android
Root = null; Root = null;
KpDatabase = null; KpDatabase = null;
Ioc = null;
_loaded = false; _loaded = false;
_locked = false; _locked = false;
_reloadRequested = false; _reloadRequested = false;

View File

@ -54,7 +54,6 @@ namespace keepass2android
// Set Database state // Set Database state
db.Root = db.KpDatabase.RootGroup; db.Root = db.KpDatabase.RootGroup;
db.Ioc = _ioc;
db.Loaded = true; db.Loaded = true;
db.SearchHelper = new SearchDbHelper(_app); db.SearchHelper = new SearchDbHelper(_app);

View File

@ -18,8 +18,8 @@ namespace Kp2aUnitTests
{ {
TestRunner runner = new TestRunner(); TestRunner runner = new TestRunner();
// Run all tests from this assembly // Run all tests from this assembly
//runner.AddTests(Assembly.GetExecutingAssembly()); runner.AddTests(Assembly.GetExecutingAssembly());
runner.AddTests(new List<Type> { typeof(TestLoadDb)}); //runner.AddTests(new List<Type> { typeof(TestSaveDb)});
//runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadWithPasswordOnly"));}} //runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadWithPasswordOnly"));}}
return runner; return runner;
} }

View File

@ -28,12 +28,12 @@ namespace Kp2aUnitTests
item => item =>
{ {
IStructureItem foundItem = db2.RootGroup.FindObject(item.Uuid, true, null); IStructureItem foundItem = db2.RootGroup.FindObject(item.Uuid, true, null);
Assert.IsNotNull(foundItem); Assert.IsNotNull(foundItem, "didn't find item with uuid="+item.Uuid.ToHexString());
Assert.IsTrue(item.ParentGroup.Uuid.EqualsValue(foundItem.ParentGroup.Uuid)); Assert.IsTrue(item.ParentGroup.Uuid.EqualsValue(foundItem.ParentGroup.Uuid), "item.ParentGroup.Uuid ("+item.ParentGroup.Uuid+") != " + foundItem.ParentGroup.Uuid);
} }
); );
Assert.AreEqual(db1.RootGroup.GetObjects(true,null).Count(),db2.RootGroup.GetObjects(true,null).Count()); Assert.AreEqual(db1.RootGroup.GetObjects(true,null).Count(),db2.RootGroup.GetObjects(true,null).Count(), "Wrong Object Count");
} }
protected static string DefaultDirectory protected static string DefaultDirectory
@ -56,19 +56,27 @@ namespace Kp2aUnitTests
get { return "secretpassword!"; } get { return "secretpassword!"; }
} }
protected IKp2aApp LoadDatabase(string defaultFilename, string password, string keyfile) protected string TestDbDirectory
{
get { return DefaultDirectory + "savedWithDesktop/"; }
}
protected IKp2aApp LoadDatabase(string filename, string password, string keyfile)
{ {
IKp2aApp app = new TestKp2aApp(); IKp2aApp app = new TestKp2aApp();
Handler handler = new Handler(Looper.MainLooper); app.CreateNewDatabase();
bool loadSuccesful = false; bool loadSuccesful = false;
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = defaultFilename }, password, keyfile, new ActionOnFinish((success, message) => LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = filename }, password, keyfile, new ActionOnFinish((success, message) =>
{ {
loadSuccesful = success; if (!success) if (!success)
Assert.Fail(message); Android.Util.Log.Debug("KP2ATest", message);
loadSuccesful = success;
}) })
); );
ProgressTask pt = new ProgressTask(app, Application.Context, task, UiStringKey.loading_database); ProgressTask pt = new ProgressTask(app, Application.Context, task, UiStringKey.loading_database);
pt.Run(); pt.Run();
pt.JoinWorkerThread();
Assert.IsTrue(loadSuccesful); Assert.IsTrue(loadSuccesful);
return app; return app;
} }
@ -78,8 +86,7 @@ namespace Kp2aUnitTests
bool saveSuccesful = false; bool saveSuccesful = false;
SaveDb save = new SaveDb(Application.Context, app.GetDb(), new ActionOnFinish((success, message) => SaveDb save = new SaveDb(Application.Context, app.GetDb(), new ActionOnFinish((success, message) =>
{ {
saveSuccesful = success; if (!success) saveSuccesful = success;
Assert.Fail(message);
}), false); }), false);
save.Run(); save.Run();
@ -93,9 +100,12 @@ namespace Kp2aUnitTests
Database db = app.CreateNewDatabase(); Database db = app.CreateNewDatabase();
db.KpDatabase = new PwDatabase(); db.KpDatabase = new PwDatabase();
//Key will be changed/created immediately after creation:
CompositeKey tempKey = new CompositeKey(); CompositeKey compositeKey = new CompositeKey();
db.KpDatabase.New(ioc, tempKey); compositeKey.AddUserKey(new KcpPassword(DefaultPassword));
if (!String.IsNullOrEmpty(DefaultKeyfile))
compositeKey.AddUserKey(new KcpKeyFile(DefaultKeyfile));
db.KpDatabase.New(ioc, compositeKey);
db.KpDatabase.KeyEncryptionRounds = 3; db.KpDatabase.KeyEncryptionRounds = 3;
@ -104,7 +114,6 @@ namespace Kp2aUnitTests
// Set Database state // Set Database state
db.Root = db.KpDatabase.RootGroup; db.Root = db.KpDatabase.RootGroup;
db.Ioc = ioc;
db.Loaded = true; db.Loaded = true;
db.SearchHelper = new SearchDbHelper(app); db.SearchHelper = new SearchDbHelper(app);

View File

@ -30,8 +30,7 @@ namespace Kp2aUnitTests
bool createSuccesful = false; bool createSuccesful = false;
//create the task: //create the task:
CreateDb createDb = new CreateDb(app, Application.Context, ioc, new ActionOnFinish((success, message) => CreateDb createDb = new CreateDb(app, Application.Context, ioc, new ActionOnFinish((success, message) =>
{ createSuccesful = success; if (!success) { createSuccesful = success;
Assert.Fail(message);
}), false); }), false);
//run it: //run it:
createDb.Run(); createDb.Run();

View File

@ -11,12 +11,6 @@ namespace Kp2aUnitTests
[TestClass] [TestClass]
internal class TestLoadDb : TestBase internal class TestLoadDb : TestBase
{ {
private string TestDbDirectory
{
get { return DefaultDirectory + "savedWithDesktop/"; }
}
private void RunLoadTest(string filenameWithoutDir, string password, string keyfile) private void RunLoadTest(string filenameWithoutDir, string password, string keyfile)
{ {
Android.Util.Log.Debug("KP2ATest", "Starting for " + filenameWithoutDir+" with " + password+"/"+keyfile); Android.Util.Log.Debug("KP2ATest", "Starting for " + filenameWithoutDir+" with " + password+"/"+keyfile);

View File

@ -1,10 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text; using System.Text;
using Android.App; using Android.App;
using Android.OS; using Android.OS;
using KeePassLib; using KeePassLib;
using KeePassLib.Serialization; using KeePassLib.Serialization;
using KeePassLib.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using keepass2android; using keepass2android;
@ -13,6 +16,8 @@ namespace Kp2aUnitTests
[TestClass] [TestClass]
class TestSaveDb: TestBase class TestSaveDb: TestBase
{ {
private string newFilename;
[TestMethod] [TestMethod]
public void TestLoadEditSave() public void TestLoadEditSave()
{ {
@ -24,7 +29,7 @@ namespace Kp2aUnitTests
//modify the database by adding a group: //modify the database by adding a group:
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true); app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
//save the database again: //save the database again:
// -> Ensure Assert below works! SaveDatabase(app, DefaultFilename); SaveDatabase(app);
//load database to a new app instance: //load database to a new app instance:
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
@ -36,7 +41,49 @@ namespace Kp2aUnitTests
[TestMethod] [TestMethod]
public void TestLoadAndSave_TestIdenticalFiles() public void TestLoadAndSave_TestIdenticalFiles()
{ {
Assert.Fail("Todo: implement"); IKp2aApp app = LoadDatabase(DefaultDirectory + "complexDb.kdbx", "test", null);
var kdbxXml = DatabaseToXml(app);
newFilename = TestDbDirectory + "tmp_complexDb.kdbx";
if (File.Exists(newFilename))
File.Delete(newFilename);
app.GetDb().KpDatabase.IOConnectionInfo.Path = newFilename;
app.GetDb().SaveData(Application.Context);
IKp2aApp appReloaded = LoadDatabase(newFilename, "test", null);
var kdbxReloadedXml = DatabaseToXml(appReloaded);
Assert.AreEqual(kdbxXml,kdbxReloadedXml);
}
private class OnCloseToStringMemoryStream : MemoryStream
{
public string Text { get; private set; }
private bool _closed;
public override void Close()
{
if (!_closed)
{
Position = 0;
Text = new StreamReader(this).ReadToEnd();
}
base.Close();
_closed = true;
}
}
private static string DatabaseToXml(IKp2aApp app)
{
KdbxFile kdb = new KdbxFile(app.GetDb().KpDatabase);
var sOutput = new OnCloseToStringMemoryStream();
kdb.Save(sOutput, app.GetDb().KpDatabase.RootGroup, KdbxFormat.PlainXml, null);
return sOutput.Text;
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -143,11 +143,11 @@ namespace MonoDroidUnitTesting {
foreach (TestClass testClass in runner.GetTestClassesSorted(CompareResults)) { foreach (TestClass testClass in runner.GetTestClassesSorted(CompareResults)) {
this.ListAdapter.Add(testClass); this.ListAdapter.Add(testClass);
} }
/*
// Restore previous activity // Restore previous activity
if (!TestMethodResultActivity.RestoreActivity(this)) { if (!TestMethodResultActivity.RestoreActivity(this)) {
TestClassResultActivity.RestoreActivity(this); TestClassResultActivity.RestoreActivity(this);
} }*/
} }
private bool RunOnTestRunEnded() { private bool RunOnTestRunEnded() {