Example #1
0
        public async Task Initialize()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            MethodInfo testMethod = GetType().GetRuntimeMethod(
                TestContext.TestName, new Type[0]
                );

            var specAttr = testMethod.GetCustomAttribute <DetailsForAttribute>();
            var dataAttr = testMethod.GetCustomAttribute <TestDataAttribute>();

            Assert.IsTrue(specAttr != null || dataAttr != null);

            try
            {
                Utils.DatabaseInfo databaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);

                KdbxReader reader = new KdbxReader();

                using (IRandomAccessStream stream = await databaseInfo.Database.AsIStorageFile.OpenReadAsync())
                {
                    Assert.IsFalse((await reader.ReadHeaderAsync(stream, cts.Token)).IsError);
                    KdbxDecryptionResult decryption = await reader.DecryptFileAsync(stream, databaseInfo.Password, databaseInfo.Keyfile, cts.Token);

                    Assert.IsFalse(decryption.Result.IsError);
                    this.document = decryption.GetDocument();

                    if (specAttr != null && (dataAttr == null || !dataAttr.SkipInitialization))
                    {
                        IDatabaseNavigationViewModel navVm = new DatabaseNavigationViewModel();
                        navVm.SetGroup(this.document.Root.DatabaseGroup);

                        IDatabasePersistenceService persistenceService = new DummyPersistenceService();

                        this.instantiationTime = DateTime.Now;
                        if (specAttr.IsNew)
                        {
                            this.expectedParent = this.document.Root.DatabaseGroup;
                            this.viewModel      = GetNewViewModel(navVm, persistenceService, this.document, this.expectedParent);
                        }
                        else
                        {
                            this.expectedParent = this.document.Root.DatabaseGroup;
                            this.viewModel      = GetExistingViewModel(
                                navVm,
                                persistenceService,
                                this.document,
                                specAttr.IsOpenedReadOnly
                                );
                        }
                    }
                    else
                    {
                        this.expectedParent = null;
                        Assert.IsTrue(dataAttr.SkipInitialization);
                    }
                }
            }
            catch (InvalidOperationException) { }
        }
        public async Task Initialize()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            this.clipboardService = new SensitiveClipboardService(this.clipboard);

            try
            {
                Utils.DatabaseInfo databaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);

                KdbxReader reader = new KdbxReader();

                using (IRandomAccessStream stream = await databaseInfo.Database.AsIStorageFile.OpenReadAsync())
                {
                    Assert.IsFalse((await reader.ReadHeaderAsync(stream, cts.Token)).IsError);
                    KdbxDecryptionResult decryption = await reader.DecryptFileAsync(stream, databaseInfo.Password, databaseInfo.Keyfile, cts.Token);

                    Assert.IsFalse(decryption.Result.IsError);
                    this.viewModel = new DatabaseViewModel(
                        decryption.GetDocument(),
                        new MockResourceProvider(),
                        reader.HeaderData.GenerateRng(),
                        new DatabaseNavigationViewModel(),
                        new DummyPersistenceService(),
                        new AppSettingsService(new InMemorySettingsProvider()),
                        this.clipboardService
                        );

                    await this.viewModel.ActivateAsync();
                }
            }
            catch (InvalidOperationException) { }
        }
        public async Task DbUnlockViewModelReadOnly()
        {
            Utils.DatabaseInfo databaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);

            Assert.IsTrue(databaseInfo.Database.AsIStorageItem.Attributes.HasFlag(FileAttributes.ReadOnly), "Database file should be read-only");

            IFileProxyProvider proxyProvider            = new MockFileProxyProvider();
            StorageFileDatabaseCandidateFactory factory = new StorageFileDatabaseCandidateFactory(proxyProvider);

            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            ICredentialStorageProvider  credentialStorage = new MockCredentialProvider();
            IDatabaseUnlockViewModel    viewModel         = new DatabaseUnlockViewModel(
                new MockSyncContext(),
                await factory.AssembleAsync(databaseInfo.Database),
                false,
                new MockStorageItemAccessList(),
                new KdbxReader(),
                proxyProvider,
                factory,
                new MasterKeyChangeViewModelFactory(new DatabaseCredentialProviderFactory(credentialStorage), new MockFileService()),
                new TaskNotificationService(),
                new MockIdentityVerifier(),
                credentialStorage,
                new MockCredentialStorageViewModelFactory()
                );

            await viewModel.ActivateAsync();

            Assert.IsTrue(viewModel.IsReadOnly, "DatabaseUnlockViewModel should be read-only for a read-only file");

            await viewModel.UpdateCandidateFileAsync(await factory.AssembleAsync(await Utils.GetDatabaseByName("StructureTesting.kdbx")));

            Assert.IsFalse(viewModel.IsReadOnly, "DatabaseUnlockViewModel should not be read-only for a writable file");
        }
Example #4
0
        public async Task Init()
        {
            // Get database from test attributes
            Utils.DatabaseInfo dbInfo = await Utils.GetDatabaseInfoForTest(TestContext);

            this.dbPassword = dbInfo.Password;
            this.dbKeyFile  = dbInfo.Keyfile;

            // Assert that databases named *ReadOnly* are actually readonly after a clone
            if (dbInfo.Database.Name.IndexOf("ReadOnly", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                Assert.IsFalse(
                    await dbInfo.Database.CheckWritableAsync(),
                    $"This file is expected to be read-only; please verify this before testing: {dbInfo.Database.Name}"
                    );
            }

            this.saveFile = (await dbInfo.Database.AsIStorageFile.CopyAsync(
                                 ApplicationData.Current.TemporaryFolder,
                                 $"PersistenceTestDb-{Guid.NewGuid()}.kdbx",
                                 NameCollisionOption.ReplaceExisting
                                 )).AsWrapper();

            // Use a KdbxReader to parse the database and get a corresponding writer
            KdbxReader reader = new KdbxReader();

            using (IRandomAccessStream stream = await this.saveFile.AsIStorageFile.OpenReadAsync())
            {
                await reader.ReadHeaderAsync(stream, CancellationToken.None);

                KdbxDecryptionResult decryption = await reader.DecryptFileAsync(stream, dbInfo.Password, dbInfo.Keyfile, CancellationToken.None);

                Assert.AreEqual(KdbxParserCode.Success, decryption.Result.Code);
                this.document = decryption.GetDocument();
            }

            // Construct services we can use for the test
            this.writer             = reader.GetWriter();
            this.persistenceService = new DefaultFilePersistenceService(
                this.writer,
                this.writer,
                new StorageFileDatabaseCandidate(this.saveFile, true),
                new MockSyncContext(),
                true
                );
            this.credentialStorage = new MockCredentialProvider();
            this.masterKeyVm       = new MasterKeyChangeViewModel(
                this.document,
                this.saveFile,
                new DatabaseCredentialProvider(this.persistenceService, this.credentialStorage),
                new MockFileService()
                );
            this.settingsVm = new DatabaseSettingsViewModel(this.writer);
        }
Example #5
0
        public async Task Initialize()
        {
            Utils.DatabaseInfo databaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);

            KdbxReader reader = new KdbxReader();

            using (IRandomAccessStream stream = await databaseInfo.Database.AsIStorageFile.OpenReadAsync())
            {
                Assert.IsFalse((await reader.ReadHeaderAsync(stream, CancellationToken.None)).IsError);
                KdbxDecryptionResult decryption = await reader.DecryptFileAsync(stream, databaseInfo.Password, databaseInfo.Keyfile, CancellationToken.None);

                Assert.IsFalse(decryption.Result.IsError);
                this.document = decryption.GetDocument();
                this.rng      = reader.HeaderData.GenerateRng();
            }
        }
Example #6
0
        public async Task Initialize()
        {
            try
            {
                CancellationTokenSource cts = new CancellationTokenSource();

                Utils.DatabaseInfo databaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);

                KdbxReader reader = new KdbxReader();

                using (IRandomAccessStream stream = await databaseInfo.Database.AsIStorageFile.OpenReadAsync())
                {
                    Assert.IsFalse((await reader.ReadHeaderAsync(stream, cts.Token)).IsError);
                    KdbxDecryptionResult decryption = await reader.DecryptFileAsync(stream, databaseInfo.Password, databaseInfo.Keyfile, cts.Token);

                    Assert.IsFalse(decryption.Result.IsError);
                    this.document = decryption.GetDocument();
                }
            }
            catch (InvalidOperationException) { }

            this.viewModel = new DatabaseNavigationViewModel();
        }
        public async Task Initialize()
        {
            TestDataAttribute dataAttr = GetTestAttribute <TestDataAttribute>();

            if (dataAttr != null && dataAttr.SkipInitialization)
            {
                return;
            }

            this.testDatabaseInfo = null;
            IDatabaseCandidate databaseValue = null;
            bool sampleValue = false;

            try
            {
                this.testDatabaseInfo = await Utils.GetDatabaseInfoForTest(TestContext);
            }
            catch (InvalidOperationException) { }

            if (dataAttr?.UseRealProxyProvider != true)
            {
                this.proxyProvider = new MockFileProxyProvider
                {
                    ScopeValue = (dataAttr?.InAppScope == true)
                };
            }
            else
            {
                StorageFolder proxyFolder = ApplicationData.Current.TemporaryFolder;
                proxyFolder = await proxyFolder.CreateFolderAsync("Proxies", CreationCollisionOption.OpenIfExists);

                this.proxyProvider = new FileProxyProvider(proxyFolder);
            }

            IDatabaseCandidateFactory candidateFactory = new StorageFileDatabaseCandidateFactory(this.proxyProvider);

            if (this.testDatabaseInfo != null)
            {
                databaseValue = await candidateFactory.AssembleAsync(this.testDatabaseInfo.Database);

                sampleValue = (dataAttr != null && dataAttr.InitSample);
            }

            if (dataAttr != null && !dataAttr.InitDatabase)
            {
                databaseValue = null;
            }

            this.accessList = new MockStorageItemAccessList();

            this.identityService = new MockIdentityVerifier()
            {
                CanVerify = dataAttr?.IdentityVerifierAvailable ?? UserConsentVerifierAvailability.NotConfiguredForUser,
                Verified  = dataAttr?.IdentityVerified ?? false
            };
            this.credentialProvider = new MockCredentialProvider();

            if (dataAttr?.StoredCredentials == true && databaseValue != null && this.testDatabaseInfo != null)
            {
                Assert.IsTrue(
                    await this.credentialProvider.TryStoreRawKeyAsync(
                        databaseValue.File,
                        this.testDatabaseInfo.RawKey
                        )
                    );
            }

            Utils.DatabaseInfo backupDatabase = await Utils.DatabaseMap["StructureTesting"];
            this.alwaysStoredCandidate = await candidateFactory.AssembleAsync(
                backupDatabase.Database
                );

            Assert.IsTrue(
                await this.credentialProvider.TryStoreRawKeyAsync(
                    this.alwaysStoredCandidate.File,
                    backupDatabase.RawKey
                    )
                );

            this.viewModel = new DatabaseUnlockViewModel(
                new MockSyncContext(),
                databaseValue,
                sampleValue,
                this.accessList,
                new KdbxReader(),
                this.proxyProvider,
                candidateFactory,
                new MasterKeyChangeViewModelFactory(new DatabaseCredentialProviderFactory(this.credentialProvider), new MockFileService()),
                new TaskNotificationService(),
                this.identityService,
                this.credentialProvider,
                new MockCredentialStorageViewModelFactory()
                );

            await this.viewModel.ActivateAsync();

            // Set various ViewModel properties if desired
            if (this.testDatabaseInfo != null && dataAttr != null)
            {
                if (dataAttr.SetPassword)
                {
                    this.viewModel.Password = this.testDatabaseInfo.Password;
                }

                if (dataAttr.SetKeyFile)
                {
                    this.viewModel.KeyFile = this.testDatabaseInfo.Keyfile;
                }
            }

            if (databaseValue != null)
            {
                await ViewModelHeaderValidated();
            }
        }