Added test for merge with kdbp file and fixed it in SaveDB

This commit is contained in:
Philipp Crocoll 2013-07-13 20:13:45 +02:00
parent 30cdc6136f
commit 99cbe84879
5 changed files with 39 additions and 20 deletions

View File

@ -11,7 +11,7 @@ using ProtoBuf.Meta;
namespace KeePassLib.Serialization
{
internal class KdbpFile
public class KdbpFile
{
public const string FileNameExtension = "kdbp";

View File

@ -175,7 +175,7 @@ namespace keepass2android
pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
pwImp.MasterKey = pwDatabase.MasterKey;
KdbxFile kdbx = new KdbxFile(pwImp);
kdbx.Load(fileStorage.OpenFileForRead(ioc), KdbxFormat.Default, null);
kdbx.Load(fileStorage.OpenFileForRead(ioc), KdbpFile.GetFormatToUse(ioc), null);
pwDatabase.MergeIn(pwImp, PwMergeMethod.Synchronize, null);

View File

@ -18,10 +18,10 @@ 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(TestSaveDb)});
//runner.AddTests(typeof(TestSaveDb).GetMethod("TestLoadEditSaveWithSyncConflict"));
//runner.AddTests(typeof(TestSaveDb).GetMethod("TestLoadEditSave"));
runner.AddTests(typeof(TestSaveDb).GetMethod("TestLoadEditSaveWithSyncKdbp"));
//runner.AddTests(typeof(TestSaveDb).GetMethod("TestLoadKdbxAndSaveKdbp_TestIdenticalFiles"));
return runner;
}
}

View File

@ -105,9 +105,17 @@ namespace Kp2aUnitTests
}
protected IKp2aApp SetupAppWithDefaultDatabase()
{
string filename = DefaultFilename;
return SetupAppWithDatabase(filename);
}
protected IKp2aApp SetupAppWithDatabase(string filename)
{
IKp2aApp app = new TestKp2aApp();
IOConnectionInfo ioc = new IOConnectionInfo {Path = DefaultFilename};
IOConnectionInfo ioc = new IOConnectionInfo {Path = filename};
Database db = app.CreateNewDatabase();
db.KpDatabase = new PwDatabase();
@ -134,14 +142,14 @@ namespace Kp2aUnitTests
mailGroup = new PwGroup(true, true, "eMail", PwIcon.UserCommunication);
db.KpDatabase.RootGroup.AddGroup(mailGroup, true);
mailGroup.AddGroup(new PwGroup(true, true, "business", PwIcon.BlackBerry), 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);
mailGroup.AddEntry(mailEntry, true);
db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "eMail2", PwIcon.UserCommunication), true);

View File

@ -44,13 +44,25 @@ namespace Kp2aUnitTests
[TestMethod]
public void TestLoadEditSaveWithSync()
{
//create the default database:
IKp2aApp app = SetupAppWithDefaultDatabase();
TestSync(DefaultFilename);
}
[TestMethod]
public void TestLoadEditSaveWithSyncKdbp()
{
TestSync(DefaultDirectory+"savetest.kdbp");
}
private void TestSync(string filename)
{
//create the default database:
IKp2aApp app = SetupAppWithDatabase(filename);
//save it and reload it so we have a base version
SaveDatabase(app);
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
app = LoadDatabase(filename, DefaultPassword, DefaultKeyfile);
//load it once again:
IKp2aApp app2 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
IKp2aApp app2 = LoadDatabase(filename, DefaultPassword, DefaultKeyfile);
//modify the database by adding a group in both databases:
app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true);
@ -59,23 +71,22 @@ namespace Kp2aUnitTests
//save the database from app 1:
SaveDatabase(app);
((TestKp2aApp)app2).SetYesNoCancelResult(TestKp2aApp.YesNoCancelResult.Yes);
((TestKp2aApp) app2).SetYesNoCancelResult(TestKp2aApp.YesNoCancelResult.Yes);
//save the database from app 2: This save operation must detect the changes made from app 1 and ask if it should sync:
SaveDatabase(app2);
//make sure the right question was asked
Assert.AreEqual(UiStringKey.TitleSyncQuestion, ((TestKp2aApp)app2).LastYesNoCancelQuestionTitle);
Assert.AreEqual(UiStringKey.TitleSyncQuestion, ((TestKp2aApp) app2).LastYesNoCancelQuestionTitle);
//add group 2 to app 1:
app.GetDb().KpDatabase.RootGroup.AddGroup(group2, true);
//load database to a new app instance:
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile);
IKp2aApp resultApp = LoadDatabase(filename, DefaultPassword, DefaultKeyfile);
//ensure the sync was successful:
AssertDatabasesAreEqual(app.GetDb().KpDatabase, resultApp.GetDb().KpDatabase);
}