예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }