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); }
public async Task MultiEdit_Degenerate() { StorageFileDatabaseCandidateFactory factory = new StorageFileDatabaseCandidateFactory(new MockFileProxyProvider { ScopeValue = true }); StorageFolder work = await Utils.GetWorkFolder(); IDatabaseCandidate workDb = await factory.AssembleAsync( (await this.thisTestInfo.Database.AsIStorageFile.CopyAsync(work, "Work.kdbx", NameCollisionOption.ReplaceExisting)) .AsWrapper() ); IKdbxWriter writer; KdbxDocument doc; var reader = new KdbxReader(); using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { ReaderResult headerResult = await reader.ReadHeaderAsync(stream, CancellationToken.None); Assert.AreEqual(headerResult, ReaderResult.Success); } KdbxDecryptionResult bodyResult = null; using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { bodyResult = await reader.DecryptFileAsync(stream, this.thisTestInfo.Password, this.thisTestInfo.Keyfile, CancellationToken.None); Assert.AreEqual(bodyResult.Result, ReaderResult.Success); } writer = reader.GetWriter(); doc = bodyResult.GetDocument(); IDatabasePersistenceService persistor = new DefaultFilePersistenceService(writer, writer, workDb, new MockSyncContext(), await workDb.File.CheckWritableAsync()); Assert.IsTrue(persistor.CanSave); Assert.IsTrue(await persistor.Save(doc)); // Remove the last group doc.Root.DatabaseGroup.Children.RemoveAt( doc.Root.DatabaseGroup.Children.IndexOf( doc.Root.DatabaseGroup.Children.Last(node => node is IKeePassGroup) ) ); Assert.IsTrue(await persistor.Save(doc)); reader = new KdbxReader(); using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { ReaderResult headerResult = await reader.ReadHeaderAsync(stream, CancellationToken.None); Assert.AreEqual(headerResult, ReaderResult.Success); } using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { bodyResult = await reader.DecryptFileAsync(stream, this.thisTestInfo.Password, this.thisTestInfo.Keyfile, CancellationToken.None); Assert.AreEqual(bodyResult.Result, ReaderResult.Success); } writer = reader.GetWriter(); doc = bodyResult.GetDocument(); doc.Root.DatabaseGroup.Children.RemoveAt( doc.Root.DatabaseGroup.Children.IndexOf( doc.Root.DatabaseGroup.Children.Last(node => node is IKeePassGroup) ) ); Assert.IsTrue(await persistor.Save(doc)); reader = new KdbxReader(); using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { ReaderResult headerResult = await reader.ReadHeaderAsync(stream, CancellationToken.None); Assert.AreEqual(headerResult, ReaderResult.Success); } using (IRandomAccessStream stream = await workDb.GetRandomReadAccessStreamAsync()) { bodyResult = await reader.DecryptFileAsync(stream, this.thisTestInfo.Password, this.thisTestInfo.Keyfile, CancellationToken.None); Assert.AreEqual(bodyResult.Result, ReaderResult.Success); } writer = reader.GetWriter(); doc = bodyResult.GetDocument(); }