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 namespace KeePassLib.Serialization
{ {
internal class KdbpFile public class KdbpFile
{ {
public const string FileNameExtension = "kdbp"; public const string FileNameExtension = "kdbp";

View File

@ -175,7 +175,7 @@ namespace keepass2android
pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep(); pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
pwImp.MasterKey = pwDatabase.MasterKey; pwImp.MasterKey = pwDatabase.MasterKey;
KdbxFile kdbx = new KdbxFile(pwImp); 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); pwDatabase.MergeIn(pwImp, PwMergeMethod.Synchronize, null);

View File

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

View File

@ -105,13 +105,21 @@ namespace Kp2aUnitTests
} }
protected IKp2aApp SetupAppWithDefaultDatabase() protected IKp2aApp SetupAppWithDefaultDatabase()
{
string filename = DefaultFilename;
return SetupAppWithDatabase(filename);
}
protected IKp2aApp SetupAppWithDatabase(string filename)
{ {
IKp2aApp app = new TestKp2aApp(); IKp2aApp app = new TestKp2aApp();
IOConnectionInfo ioc = new IOConnectionInfo {Path = DefaultFilename};
IOConnectionInfo ioc = new IOConnectionInfo {Path = filename};
Database db = app.CreateNewDatabase(); Database db = app.CreateNewDatabase();
db.KpDatabase = new PwDatabase(); db.KpDatabase = new PwDatabase();
CompositeKey compositeKey = new CompositeKey(); CompositeKey compositeKey = new CompositeKey();
compositeKey.AddUserKey(new KcpPassword(DefaultPassword)); compositeKey.AddUserKey(new KcpPassword(DefaultPassword));
if (!String.IsNullOrEmpty(DefaultKeyfile)) if (!String.IsNullOrEmpty(DefaultKeyfile))
@ -130,18 +138,18 @@ namespace Kp2aUnitTests
// Add a couple default groups // Add a couple default groups
db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Internet", PwIcon.Key), true); db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Internet", PwIcon.Key), true);
mailGroup = new PwGroup(true, true, "eMail", PwIcon.UserCommunication); mailGroup = new PwGroup(true, true, "eMail", PwIcon.UserCommunication);
db.KpDatabase.RootGroup.AddGroup(mailGroup, true); 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 = new PwEntry(true, true);
mailEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString( mailEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString(
true, "me@there.com")); true, "me@there.com"));
mailEntry.Strings.Set(PwDefs.TitleField, new ProtectedString( mailEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(
true, "me@there.com Entry")); 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); db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "eMail2", PwIcon.UserCommunication), true);

View File

@ -44,13 +44,25 @@ namespace Kp2aUnitTests
[TestMethod] [TestMethod]
public void TestLoadEditSaveWithSync() public void TestLoadEditSaveWithSync()
{ {
//create the default database: TestSync(DefaultFilename);
IKp2aApp app = SetupAppWithDefaultDatabase(); }
[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 //save it and reload it so we have a base version
SaveDatabase(app); SaveDatabase(app);
app = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); app = LoadDatabase(filename, DefaultPassword, DefaultKeyfile);
//load it once again: //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: //modify the database by adding a group in both databases:
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);
@ -59,23 +71,22 @@ namespace Kp2aUnitTests
//save the database from app 1: //save the database from app 1:
SaveDatabase(app); 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: //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); SaveDatabase(app2);
//make sure the right question was asked //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: //add group 2 to app 1:
app.GetDb().KpDatabase.RootGroup.AddGroup(group2, true); app.GetDb().KpDatabase.RootGroup.AddGroup(group2, true);
//load database to a new app instance: //load database to a new app instance:
IKp2aApp resultApp = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); IKp2aApp resultApp = LoadDatabase(filename, DefaultPassword, DefaultKeyfile);
//ensure the sync was successful: //ensure the sync was successful:
AssertDatabasesAreEqual(app.GetDb().KpDatabase, resultApp.GetDb().KpDatabase); AssertDatabasesAreEqual(app.GetDb().KpDatabase, resultApp.GetDb().KpDatabase);
} }