More refactoring and introduction of further abstractions
Added further tests Fixed test loading with keyfile only
This commit is contained in:
@@ -46,11 +46,16 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ProgressDialogStub.cs" />
|
||||
<Compile Include="TestBase.cs" />
|
||||
<Compile Include="TestDrawableFactory.cs" />
|
||||
<Compile Include="TestCreateDb.cs" />
|
||||
<Compile Include="MainActivity.cs" />
|
||||
<Compile Include="Resources\Resource.Designer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestKp2aApp.cs" />
|
||||
<Compile Include="TestLoadDb.cs" />
|
||||
<Compile Include="TestSaveDb.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\AboutResources.txt" />
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.Runtime;
|
||||
@@ -18,7 +18,9 @@ namespace Kp2aUnitTests
|
||||
{
|
||||
TestRunner runner = new TestRunner();
|
||||
// Run all tests from this assembly
|
||||
runner.AddTests(Assembly.GetExecutingAssembly());
|
||||
//runner.AddTests(Assembly.GetExecutingAssembly());
|
||||
runner.AddTests(new List<Type> { typeof(TestLoadDb)});
|
||||
//runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadWithPasswordOnly"));}}
|
||||
return runner;
|
||||
}
|
||||
}
|
||||
|
||||
34
src/Kp2aUnitTests/ProgressDialogStub.cs
Normal file
34
src/Kp2aUnitTests/ProgressDialogStub.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using keepass2android;
|
||||
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
class ProgressDialogStub : IProgressDialog
|
||||
{
|
||||
public void SetTitle(string title)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void SetMessage(string getResourceString)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Dismiss()
|
||||
{
|
||||
Dismissed = true;
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
Showed = true;
|
||||
}
|
||||
|
||||
protected bool Showed { get; set; }
|
||||
|
||||
public bool Dismissed { get; set; }
|
||||
}
|
||||
}
|
||||
10
src/Kp2aUnitTests/Resources/Resource.Designer.cs
generated
10
src/Kp2aUnitTests/Resources/Resource.Designer.cs
generated
@@ -93,14 +93,14 @@ namespace Kp2aUnitTests
|
||||
public partial class String
|
||||
{
|
||||
|
||||
// aapt resource value: 0x7f040002
|
||||
public const int ApplicationName = 2130968578;
|
||||
|
||||
// aapt resource value: 0x7f040001
|
||||
public const int ApplicationName = 2130968577;
|
||||
public const int Hello = 2130968577;
|
||||
|
||||
// aapt resource value: 0x7f040000
|
||||
public const int Hello = 2130968576;
|
||||
|
||||
// aapt resource value: 0x7f040002
|
||||
public const int library_name = 2130968578;
|
||||
public const int library_name = 2130968576;
|
||||
|
||||
static String()
|
||||
{
|
||||
|
||||
131
src/Kp2aUnitTests/TestBase.cs
Normal file
131
src/Kp2aUnitTests/TestBase.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Android.App;
|
||||
using Android.OS;
|
||||
using KeePassLib;
|
||||
using KeePassLib.Interfaces;
|
||||
using KeePassLib.Keys;
|
||||
using KeePassLib.Security;
|
||||
using KeePassLib.Serialization;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using keepass2android;
|
||||
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
internal class TestBase
|
||||
{
|
||||
private PwGroup mailGroup;
|
||||
private PwEntry mailEntry;
|
||||
|
||||
/// <summary>
|
||||
/// Compares the two databases. uses Asserts
|
||||
/// TODO: implement this with many more checks or use serialization?
|
||||
/// </summary>
|
||||
protected void AssertDatabasesAreEqual(PwDatabase db1, PwDatabase db2)
|
||||
{
|
||||
db1.RootGroup.GetObjects(true, null)
|
||||
.ForEach(
|
||||
item =>
|
||||
{
|
||||
IStructureItem foundItem = db2.RootGroup.FindObject(item.Uuid, true, null);
|
||||
Assert.IsNotNull(foundItem);
|
||||
Assert.IsTrue(item.ParentGroup.Uuid.EqualsValue(foundItem.ParentGroup.Uuid));
|
||||
}
|
||||
);
|
||||
|
||||
Assert.AreEqual(db1.RootGroup.GetObjects(true,null).Count(),db2.RootGroup.GetObjects(true,null).Count());
|
||||
}
|
||||
|
||||
protected static string DefaultDirectory
|
||||
{
|
||||
get { return "/mnt/sdcard/kp2atest/"; }
|
||||
}
|
||||
|
||||
protected static string DefaultFilename
|
||||
{
|
||||
get { return "/mnt/sdcard/kp2atest/savetest.kdbx"; }
|
||||
}
|
||||
|
||||
protected string DefaultKeyfile
|
||||
{
|
||||
get { return DefaultDirectory + "keyfile.txt"; }
|
||||
}
|
||||
|
||||
protected string DefaultPassword
|
||||
{
|
||||
get { return "secretpassword!"; }
|
||||
}
|
||||
|
||||
protected IKp2aApp LoadDatabase(string defaultFilename, string password, string keyfile)
|
||||
{
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
Handler handler = new Handler(Looper.MainLooper);
|
||||
bool loadSuccesful = false;
|
||||
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = defaultFilename }, password, keyfile, new ActionOnFinish((success, message) =>
|
||||
{
|
||||
loadSuccesful = success; if (!success)
|
||||
Assert.Fail(message);
|
||||
})
|
||||
);
|
||||
ProgressTask pt = new ProgressTask(app, Application.Context, task, UiStringKey.loading_database);
|
||||
pt.Run();
|
||||
Assert.IsTrue(loadSuccesful);
|
||||
return app;
|
||||
}
|
||||
|
||||
protected void SaveDatabase(IKp2aApp app)
|
||||
{
|
||||
bool saveSuccesful = false;
|
||||
SaveDb save = new SaveDb(Application.Context, app.GetDb(), new ActionOnFinish((success, message) =>
|
||||
{
|
||||
saveSuccesful = success; if (!success)
|
||||
Assert.Fail(message);
|
||||
}), false);
|
||||
save.Run();
|
||||
|
||||
Assert.IsTrue(saveSuccesful);
|
||||
}
|
||||
|
||||
protected IKp2aApp SetupAppWithDefaultDatabase()
|
||||
{
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
IOConnectionInfo ioc = new IOConnectionInfo {Path = DefaultFilename};
|
||||
Database db = app.CreateNewDatabase();
|
||||
|
||||
db.KpDatabase = new PwDatabase();
|
||||
//Key will be changed/created immediately after creation:
|
||||
CompositeKey tempKey = new CompositeKey();
|
||||
db.KpDatabase.New(ioc, tempKey);
|
||||
|
||||
|
||||
db.KpDatabase.KeyEncryptionRounds = 3;
|
||||
db.KpDatabase.Name = "Keepass2Android Testing Password Database";
|
||||
|
||||
|
||||
// Set Database state
|
||||
db.Root = db.KpDatabase.RootGroup;
|
||||
db.Ioc = ioc;
|
||||
db.Loaded = true;
|
||||
db.SearchHelper = new SearchDbHelper(app);
|
||||
|
||||
// Add a couple default groups
|
||||
db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Internet", PwIcon.Key), true);
|
||||
|
||||
mailGroup = new PwGroup(true, true, "eMail", PwIcon.UserCommunication);
|
||||
db.KpDatabase.RootGroup.AddGroup(mailGroup, true);
|
||||
|
||||
mailGroup.AddGroup(new PwGroup(true, true, "business", PwIcon.BlackBerry), true );
|
||||
|
||||
mailEntry = new PwEntry(true, true);
|
||||
mailEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString(
|
||||
true, "me@there.com"));
|
||||
mailEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(
|
||||
true, "me@there.com Entry"));
|
||||
mailGroup.AddEntry(mailEntry , true);
|
||||
|
||||
db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "eMail2", PwIcon.UserCommunication), true);
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Android.App;
|
||||
using Java.IO;
|
||||
using KeePassLib;
|
||||
using KeePassLib.Interfaces;
|
||||
using KeePassLib.Keys;
|
||||
using KeePassLib.Serialization;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -12,15 +13,15 @@ using keepass2android;
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
class TestCreateDb
|
||||
class TestCreateDb: TestBase
|
||||
{
|
||||
[TestMethod]
|
||||
public void CreateAndSaveLocal()
|
||||
{
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
IOConnectionInfo ioc = new IOConnectionInfo {Path = "/mnt/sdcard/kp2atest/savetest.kdbx"};
|
||||
IOConnectionInfo ioc = new IOConnectionInfo {Path = DefaultFilename};
|
||||
|
||||
File outputDir = new File("/mnt/sdcard/kp2atest/");
|
||||
File outputDir = new File(DefaultDirectory);
|
||||
outputDir.Mkdirs();
|
||||
File targetFile = new File(ioc.Path);
|
||||
if (targetFile.Exists())
|
||||
@@ -45,6 +46,10 @@ namespace Kp2aUnitTests
|
||||
PwDatabase loadedDb = new PwDatabase();
|
||||
loadedDb.Open(ioc, new CompositeKey(), null);
|
||||
|
||||
//Check whether the databases are equal
|
||||
AssertDatabasesAreEqual(loadedDb, app.GetDb().KpDatabase);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using KeePassLib.Serialization;
|
||||
using keepass2android;
|
||||
|
||||
@@ -50,5 +51,13 @@ namespace Kp2aUnitTests
|
||||
{
|
||||
yesHandler(null, null);
|
||||
}
|
||||
|
||||
public Handler UiThreadHandler {
|
||||
get { return null; } //ensure everything runs in the same thread. Otherwise the OnFinish-callback would run after the test has already finished (with failure)
|
||||
}
|
||||
public IProgressDialog CreateProgressDialog(Context ctx)
|
||||
{
|
||||
return new ProgressDialogStub();
|
||||
}
|
||||
}
|
||||
}
|
||||
78
src/Kp2aUnitTests/TestLoadDb.cs
Normal file
78
src/Kp2aUnitTests/TestLoadDb.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Android.App;
|
||||
using Android.OS;
|
||||
using KeePassLib.Serialization;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using keepass2android;
|
||||
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
internal class TestLoadDb : TestBase
|
||||
{
|
||||
private string TestDbDirectory
|
||||
{
|
||||
get { return DefaultDirectory + "savedWithDesktop/"; }
|
||||
}
|
||||
|
||||
|
||||
private void RunLoadTest(string filenameWithoutDir, string password, string keyfile)
|
||||
{
|
||||
Android.Util.Log.Debug("KP2ATest", "Starting for " + filenameWithoutDir+" with " + password+"/"+keyfile);
|
||||
|
||||
IKp2aApp app = new TestKp2aApp();
|
||||
app.CreateNewDatabase();
|
||||
bool loadSuccesful = false;
|
||||
LoadDb task = new LoadDb(app, new IOConnectionInfo() { Path = TestDbDirectory+filenameWithoutDir },
|
||||
password, keyfile, 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, UiStringKey.loading_database);
|
||||
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 :-(");
|
||||
|
||||
Assert.AreEqual(6,app.GetDb().KpDatabase.RootGroup.Groups.Count());
|
||||
Assert.AreEqual(2,app.GetDb().KpDatabase.RootGroup.Entries.Count());
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadWithPasswordOnly()
|
||||
{
|
||||
RunLoadTest("passwordonly.kdbx", DefaultPassword, "");
|
||||
}
|
||||
[TestMethod]
|
||||
public void TestLoadWithKeyfileOnly()
|
||||
{
|
||||
RunLoadTest("keyfileonly.kdbx", "", TestDbDirectory + "keyfile.txt");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadWithPasswordAndKeyfile()
|
||||
{
|
||||
RunLoadTest("PasswordAndKeyfile.kdbx", DefaultPassword, TestDbDirectory + "keyfile.txt");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadWithEmptyPassword()
|
||||
{
|
||||
RunLoadTest("EmptyPasswordAndKeyfile.kdbx", "", TestDbDirectory + "keyfile.txt");
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadWithEmptyPasswordOnly()
|
||||
{
|
||||
RunLoadTest("EmptyPassword.kdbx", "", "");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
42
src/Kp2aUnitTests/TestSaveDb.cs
Normal file
42
src/Kp2aUnitTests/TestSaveDb.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Android.App;
|
||||
using Android.OS;
|
||||
using KeePassLib;
|
||||
using KeePassLib.Serialization;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using keepass2android;
|
||||
|
||||
namespace Kp2aUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
class TestSaveDb: TestBase
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestLoadEditSave()
|
||||
{
|
||||
//create the default database:
|
||||
IKp2aApp app = SetupAppWithDefaultDatabase();
|
||||
//save it and reload it so we have a base version
|
||||
SaveDatabase(app);
|
||||
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
//modify the database by adding a group:
|
||||
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
|
||||
//save the database again:
|
||||
// -> Ensure Assert below works! SaveDatabase(app, DefaultFilename);
|
||||
|
||||
//load database to a new app instance:
|
||||
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
|
||||
|
||||
//ensure the change was saved:
|
||||
AssertDatabasesAreEqual(app.GetDb().KpDatabase, resultApp.GetDb().KpDatabase);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLoadAndSave_TestIdenticalFiles()
|
||||
{
|
||||
Assert.Fail("Todo: implement");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user