private Task <FileOperationContext> VerifyAndAddActiveWorkAsync(IDataStore fullName, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase += HandleQueryDecryptionPassphraseEventAsync;

            operationsController.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>(async(FileOperationEventArgs e) =>
            {
                if (!_fileSystemState.KnownPassphrases.Any(i => i.Thumbprint == e.LogOnIdentity.Passphrase.Thumbprint))
                {
                    _fileSystemState.KnownPassphrases.Add(e.LogOnIdentity.Passphrase);
                }
                await _knownIdentities.AddAsync(e.LogOnIdentity);
            });

            operationsController.Completed += (object sender, FileOperationEventArgs e) =>
            {
                if (e.Status.ErrorStatus == ErrorStatus.Success)
                {
                    IDataStore encryptedInfo = New <IDataStore>(e.OpenFileFullName);
                    IDataStore decryptedInfo = New <IDataStore>(FileOperation.GetTemporaryDestinationName(e.SaveFileFullName));
                    ActiveFile activeFile    = new ActiveFile(encryptedInfo, decryptedInfo, e.LogOnIdentity, ActiveFileStatus.NotDecrypted, e.CryptoId);
                    _fileSystemState.Add(activeFile);
                }
            };

            return(operationsController.VerifyEncryptedAsync(fullName));
        }
        private Task <FileOperationContext> RestoreRandomRenameInternal(IDataStore file, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase = HandleQueryOpenPassphraseEventAsync;

            operationsController.Completed += (object sender, FileOperationEventArgs e) =>
            {
                if (e.Status.ErrorStatus == ErrorStatus.Success)
                {
                    file.RestoreRandomRename(e.LogOnIdentity);
                }
            };

            return(operationsController.VerifyEncryptedAsync(file));
        }
        public async Task TestVerifyEncrypted()
        {
            FileOperationsController controller = new FileOperationsController();
            bool passphraseWasQueried           = false;

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                passphraseWasQueried = true;
                e.Cancel             = true;
                return(Task.FromResult <object>(null));
            };
            bool knownKeyWasAdded = false;

            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>((FileOperationEventArgs e) =>
            {
                knownKeyWasAdded = true;
                return(Constant.CompletedTask);
            });

            FileOperationContext status = await controller.VerifyEncryptedAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Canceled));
            Assert.That(knownKeyWasAdded, Is.False);
            Assert.That(passphraseWasQueried, Is.True);

            controller = new FileOperationsController();
            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("a");
                return(Task.FromResult <object>(null));
            };
            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>((FileOperationEventArgs e) =>
            {
                knownKeyWasAdded = true;
                return(Constant.CompletedTask);
            });

            await Resolve.KnownIdentities.AddAsync(new LogOnIdentity("b"));

            await Resolve.KnownIdentities.AddAsync(new LogOnIdentity("c"));

            status = await controller.VerifyEncryptedAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success));
            Assert.That(knownKeyWasAdded, Is.True, "A known key should have been added.");
        }