private IKp2aApp PerformLoad(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; var key = CreateKey(password, keyfile); string loadErrorMessage = ""; LoadDb task = new LoadDb(app, new IOConnectionInfo { Path = TestDbDirectory + filenameWithoutDir }, null, key, keyfile, new ActionOnFinish((success, message) => { loadErrorMessage = 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 :-( " + loadErrorMessage); return(app); }
public void LoadFromRemoteWithDomain() { //warning, looks like credentials are no longer valid var ioc = RemoteDomainIoc; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public! var app = new TestKp2aApp(); app.ServerCertificateErrorResponse = true; //accept invalid cert app.CreateNewDatabase(); bool loadSuccesful = false; LoadDb task = new LoadDb(app, ioc, null, CreateKey("a"), 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 :-("); }
public void LoadWithAcceptedCertificateTrustFailure() { var ioc = RemoteCertFailureIoc; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public! var app = new TestKp2aApp(); app.ServerCertificateErrorResponse = true; app.CreateNewDatabase(); bool loadSuccesful = false; LoadDb task = new LoadDb(app, ioc, null, CreateKey("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, "database should be loaded because invalid certificates are accepted"); }
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); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId); //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); }
protected override TestKp2aApp CreateTestKp2aApp() { TestKp2aApp app = base.CreateTestKp2aApp(); app.FileStorage = new CachingFileStorage(new TestFileStorage(app), "/mnt/sdcard/kp2atest/cache/", _testCacheSupervisor); return(app); }
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, null, CreateKey("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); }
public void CreateAndSaveLocal() { IKp2aApp app = new TestKp2aApp(); IOConnectionInfo ioc = new IOConnectionInfo {Path = DefaultFilename}; File outputDir = new File(DefaultDirectory); outputDir.Mkdirs(); File targetFile = new File(ioc.Path); if (targetFile.Exists()) targetFile.Delete(); bool createSuccesful = false; //create the task: CreateDb createDb = new CreateDb(app, Application.Context, ioc, new ActionOnFinish((success, message) => { createSuccesful = success; if (!success) Android.Util.Log.Debug("KP2A_Test", message); }), false); //run it: createDb.Run(); //check expectations: Assert.IsTrue(createSuccesful); Assert.IsNotNull(app.GetDb()); Assert.IsNotNull(app.GetDb().KpDatabase); //the create task should create two groups: Assert.AreEqual(2, app.GetDb().KpDatabase.RootGroup.Groups.Count()); //ensure the the database can be loaded from file: PwDatabase loadedDb = new PwDatabase(); loadedDb.Open(ioc, new CompositeKey(), null, new KdbxDatabaseFormat(KdbxFormat.Default)); //Check whether the databases are equal AssertDatabasesAreEqual(loadedDb, app.GetDb().KpDatabase); }
public void TestSimpleSyncCases() { //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); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId); string resultMessage; bool wasSuccessful; //sync without changes on any side: Synchronize(app, out wasSuccessful, out resultMessage); Assert.IsTrue(wasSuccessful); Assert.AreEqual(resultMessage, app.GetResourceString(UiStringKey.FilesInSync)); //go offline: TestFileStorage.Offline = true; //sync when offline (->error) Synchronize(app, out wasSuccessful, out resultMessage); Assert.IsFalse(wasSuccessful); Assert.AreEqual(resultMessage, "offline"); //modify the database by adding a group: app.GetDb().KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "TestGroup", PwIcon.Apple), true); //save the database again (will be saved locally only) SaveDatabase(app); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId); //go online again: TestFileStorage.Offline = false; //sync with local changes only (-> upload): Synchronize(app, out wasSuccessful, out resultMessage); Assert.IsTrue(wasSuccessful); Assert.AreEqual(resultMessage, app.GetResourceString(UiStringKey.SynchronizedDatabaseSuccessfully)); //ensure both files are identical and up to date now: TestFileStorage.Offline = true; var appOfflineLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId); TestFileStorage.Offline = false; var appRemoteLoaded = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId); AssertDatabasesAreEqual(app.GetDb().KpDatabase, appOfflineLoaded.GetDb().KpDatabase); AssertDatabasesAreEqual(app.GetDb().KpDatabase, appRemoteLoaded.GetDb().KpDatabase); }
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); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId); var app2 = LoadDatabase(DefaultFilename, DefaultPassword, DefaultKeyfile); app2.FileStorage = app.TestFileStorage; //give app2 direct access to the remote file _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.LoadedFromRemoteInSyncId); //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); _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntSaveToRemoteId); //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); }
protected TestKp2aApp SetupAppWithDatabase(string filename) { TestKp2aApp app = CreateTestKp2aApp(); IOConnectionInfo ioc = new IOConnectionInfo { Path = filename }; Database db = app.CreateNewDatabase(); if (filename.EndsWith(".kdb")) { db.DatabaseFormat = new KdbDatabaseFormat(app); } db.KpDatabase = new PwDatabase(); CompositeKey compositeKey = new CompositeKey(); 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.Name = "Keepass2Android Testing Password Database"; // Set Database state db.Root = db.KpDatabase.RootGroup; 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, "*****@*****.**")); mailEntry.Strings.Set(PwDefs.TitleField, new ProtectedString( true, "[email protected] Entry")); mailGroup.AddEntry(mailEntry, true); db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "eMail2", PwIcon.UserCommunication), true); return(app); }
private void Synchronize(TestKp2aApp app, out bool wasSuccessful, out string resultMessage) { bool success = false; string result = null; var sync = new SynchronizeCachedDatabase(Application.Context, app, new ActionOnFinish((_success, _result) => { success = _success; result = _result; })); sync.Run(); sync.JoinWorkerThread(); wasSuccessful = success; resultMessage = result; }
public void FileNotFoundExceptionWithWebDav() { var app = new TestKp2aApp(); var fileStorage = app.FileStorage; //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); }
public void CreateAndSaveLocal() { IKp2aApp app = new TestKp2aApp(); IOConnectionInfo ioc = new IOConnectionInfo { Path = DefaultFilename }; File outputDir = new File(DefaultDirectory); outputDir.Mkdirs(); File targetFile = new File(ioc.Path); if (targetFile.Exists()) { targetFile.Delete(); } bool createSuccesful = false; //create the task: CreateDb createDb = new CreateDb(app, Application.Context, ioc, new ActionOnFinish((success, message) => { createSuccesful = success; if (!success) { Android.Util.Log.Debug("KP2A_Test", message); } }), false); //run it: createDb.Run(); //check expectations: Assert.IsTrue(createSuccesful); Assert.IsNotNull(app.GetDb()); Assert.IsNotNull(app.GetDb().KpDatabase); //the create task should create two groups: Assert.AreEqual(2, app.GetDb().KpDatabase.RootGroup.Groups.Count()); //ensure the the database can be loaded from file: PwDatabase loadedDb = new PwDatabase(); loadedDb.Open(ioc, new CompositeKey(), null, new KdbxDatabaseFormat(KdbxFormat.Default)); //Check whether the databases are equal AssertDatabasesAreEqual(loadedDb, app.GetDb().KpDatabase); }
public void FileNotFoundExceptionWithWebDav() { var app = new TestKp2aApp(); var fileStorage = app.FileStorage; //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); }
public void LoadAndSaveFromRemote1And1Ftp() { var ioc = RemoteIoc1and1Ftp; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public! var app = new TestKp2aApp(); app.CreateNewDatabase(); bool loadSuccesful = false; LoadDb task = new LoadDb(app, ioc, null, CreateKey("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 :-("); Assert.IsTrue(TrySaveDatabase(app), "didn't successfully save database."); }
private IKp2aApp PerformLoad(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; var key = CreateKey(password, keyfile); string loadErrorMessage = ""; LoadDb task = new LoadDb(app, new IOConnectionInfo {Path = TestDbDirectory + filenameWithoutDir}, null, key, keyfile, new ActionOnFinish((success, message) => { loadErrorMessage = 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 :-( " + loadErrorMessage); return app; }
public void LoadWithAcceptedCertificateTrustFailure() { var ioc = RemoteCertFailureIoc; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public! var app = new TestKp2aApp(); app.ServerCertificateErrorResponse = true; app.CreateNewDatabase(); bool loadSuccesful = false; LoadDb task = new LoadDb(app, ioc, null, CreateKey("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, "database should be loaded because invalid certificates are accepted"); }
public void LoadFromRemoteWithDomain() { //warning, looks like credentials are no longer valid var ioc = RemoteDomainIoc; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public! var app = new TestKp2aApp(); app.ServerCertificateErrorResponse = true; //accept invalid cert app.CreateNewDatabase(); bool loadSuccesful = false; LoadDb task = new LoadDb(app, ioc, null, CreateKey("a"), 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 :-("); }
protected virtual TestKp2aApp CreateTestKp2aApp() { TestKp2aApp app = new TestKp2aApp(); return(app); }
protected virtual TestKp2aApp CreateTestKp2aApp() { TestKp2aApp app = new TestKp2aApp(); return app; }