Example #1
0
        public static void TestSimpleEncryptFile()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            string destinationPath = String.Empty;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Passphrase = "allan";
            };
            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };

            FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success.");

            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);

            Assert.That(destinationInfo.Exists, "After encryption the destination file should be created.");
            using (AxCryptDocument document = new AxCryptDocument())
            {
                using (Stream stream = destinationInfo.OpenRead())
                {
                    document.Load(stream, new Passphrase("allan").DerivedPassphrase);
                    Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given.");
                }
            }
        }
Example #2
0
        public static void TestWipeWithConfirmAll()
        {
            ProgressContext          progress   = new ProgressContext();
            FileOperationsController controller = new FileOperationsController(_fileSystemState, progress);
            int confirmationCount = 0;

            controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) =>
            {
                if (confirmationCount++ > 0)
                {
                    throw new InvalidOperationException("The event should not be raised a second time.");
                }
                e.ConfirmAll = true;
            };
            progress.NotifyLevelStart();
            FileOperationStatus status = controller.WipeFile(_helloWorldAxxPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The wipe should indicate success.");

            IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_helloWorldAxxPath);

            Assert.That(!fileInfo.Exists, "The file should not exist after wiping.");

            Assert.DoesNotThrow(() => { status = controller.WipeFile(_davidCopperfieldTxtPath); });
            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The wipe should indicate success.");
            progress.NotifyLevelFinished();

            fileInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath);
            Assert.That(!fileInfo.Exists, "The file should not exist after wiping.");
        }
Example #3
0
        public static void TestEncryptFileThatIsAlreadyEncrypted()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            FileOperationStatus      status     = controller.EncryptFile("test" + OS.Current.AxCryptExtension);

            Assert.That(status, Is.EqualTo(FileOperationStatus.FileAlreadyEncrypted), "The status should indicate that it was already encrypted.");
        }
Example #4
0
        public static void TestSimpleWipeOnThreadWorker()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel     = false;
                e.Skip       = false;
                e.ConfirmAll = false;
            };

            string destinationPath     = String.Empty;
            FileOperationStatus status = FileOperationStatus.Unknown;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
                status          = e.Status;
            };

            using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
            {
                controller.WipeFile(_davidCopperfieldTxtPath, worker);
            }
            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success.");

            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);

            Assert.That(!destinationInfo.Exists, "After wiping the destination file should not exist.");
        }
        public async Task TestSimpleDecryptAndLaunch()
        {
            FakeLauncher launcher = new FakeLauncher();
            bool         called   = false;

            TypeMap.Register.New <ILauncher>(() => { called = true; return(launcher); });

            FileOperationsController controller = new FileOperationsController();

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("a");
                return(Task.FromResult <object>(null));
            };
            FileOperationContext status = await controller.DecryptAndLaunchAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");

            Assert.That(called, Is.True, "There should be a call to launch.");
            Assert.That(Path.GetFileName(launcher.Path), Is.EqualTo("HelloWorld-Key-a.txt"), "The file should be decrypted and the name should be the original from the encrypted headers.");

            IDataStore destinationInfo = New <IDataStore>(launcher.Path);

            Assert.That(destinationInfo.IsAvailable, "After decryption the destination file should be created.");

            string fileContent;

            using (Stream stream = destinationInfo.OpenRead())
            {
                fileContent = new StreamReader(stream).ReadToEnd();
            }

            Assert.That(fileContent.Contains("Hello"), "A file named Hello World should contain that text when decrypted.");
        }
Example #6
0
        public static void TestDecryptFileWithExceptionBeforeStartingDecryption()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Passphrase = "a";
            };
            controller.KnownKeyAdded += (object sender, FileOperationEventArgs e) =>
            {
                throw new FileNotFoundException("Just kidding, but we're faking...", e.OpenFileFullName);
            };
            string destinationPath = String.Empty;

            controller.KnownKeyAdded += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };
            FileOperationStatus status = FileOperationStatus.Unknown;

            Assert.DoesNotThrow(() => { status = controller.DecryptFile(_helloWorldAxxPath); });

            Assert.That(status, Is.EqualTo(FileOperationStatus.FileDoesNotExist), "The status should indicate an exception occurred.");
            Assert.That(String.IsNullOrEmpty(destinationPath), "Since an exception occurred, the destination file should not be created.");
        }
        public async Task TestEncryptFileThatIsAlreadyEncrypted()
        {
            FileOperationsController controller = new FileOperationsController();
            FileOperationContext     status     = await controller.EncryptFileAsync(New <IDataStore>("test" + OS.Current.AxCryptExtension));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.FileAlreadyEncrypted), "The status should indicate that it was already encrypted.");
        }
        public void TestDecryptFileWithExceptionBeforeStartingDecryption()
        {
            FileOperationsController controller = new FileOperationsController();

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("a");
                return(Task.FromResult <object>(null));
            };
            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>((FileOperationEventArgs e) =>
            {
                throw new FileNotFoundException("Just kidding, but we're faking...", e.OpenFileFullName);
            });
            string destinationPath = String.Empty;
            AsyncDelegateAction <FileOperationEventArgs> previous = controller.KnownKeyAdded;

            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>(async(FileOperationEventArgs e) =>
            {
                await previous.ExecuteAsync(e);
                destinationPath = e.SaveFileFullName;
            });
            FileOperationContext status = new FileOperationContext(String.Empty, ErrorStatus.Unknown);

            Assert.DoesNotThrowAsync(async() => { status = await controller.DecryptFileAsync(New <IDataStore>(_helloWorldAxxPath)); });

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.FileDoesNotExist), "The status should indicate an exception occurred.");
            Assert.That(String.IsNullOrEmpty(destinationPath), "Since an exception occurred, the destination file should not be created.");
        }
        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));
        }
Example #10
0
        public static void TestEncryptFileWithDefaultEncryptionKey()
        {
            _fileSystemState.KnownKeys.DefaultEncryptionKey = new Passphrase("default").DerivedPassphrase;
            FileOperationsController controller     = new FileOperationsController(_fileSystemState);
            bool queryEncryptionPassphraseWasCalled = false;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                queryEncryptionPassphraseWasCalled = true;
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };

            FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success.");
            Assert.That(!queryEncryptionPassphraseWasCalled, "No query of encryption passphrase should be needed since there is a default set.");

            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);

            Assert.That(destinationInfo.Exists, "After encryption the destination file should be created.");
            using (AxCryptDocument document = new AxCryptDocument())
            {
                using (Stream stream = destinationInfo.OpenRead())
                {
                    document.Load(stream, new Passphrase("default").DerivedPassphrase);
                    Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the default passphrase given.");
                }
            }
        }
        public void TestSimpleWipeOnThreadWorker()
        {
            FileOperationsController controller = new FileOperationsController();

            controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel     = false;
                e.Skip       = false;
                e.ConfirmAll = false;
            };

            string destinationPath      = String.Empty;
            FileOperationContext status = new FileOperationContext(String.Empty, ErrorStatus.Unknown);

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
                status          = e.Status;
            };

            controller.WipeFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));
            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");

            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(!destinationInfo.IsAvailable, "After wiping the destination file should not exist.");
        }
        private Task <FileOperationContext> OpenEncryptedWorkAsync(IDataStore file, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase = HandleQueryOpenPassphraseEventAsync;

            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.SetConvertLegacyOptionCommandAsync = async() =>
            {
                if (Resolve.UserSettings.EncryptionUpgradeMode != EncryptionUpgradeMode.NotDecided)
                {
                    return;
                }
                if (!New <LicensePolicy>().Capabilities.Has(LicenseCapability.EditExistingFiles))
                {
                    return;
                }

                bool autoConvert = await New <IPopup>().ShowAsync(PopupButtons.OkCancel, Texts.OptionsConvertMenuItemText, Texts.LegacyOpenMessage) == PopupButtons.Ok;

                autoConvert = autoConvert && New <IVerifySignInPassword>().Verify(Texts.LegacyConversionVerificationPrompt);
                New <UserSettings>().EncryptionUpgradeMode = autoConvert ? EncryptionUpgradeMode.AutoUpgrade : EncryptionUpgradeMode.RetainWithoutUpgrade;
            };
            return(operationsController.DecryptAndLaunchAsync(file));
        }
        public async Task TestWipeWithConfirmAll()
        {
            ProgressContext          progress   = new ProgressContext();
            FileOperationsController controller = new FileOperationsController(progress);
            int confirmationCount = 0;

            controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) =>
            {
                if (confirmationCount++ > 0)
                {
                    throw new InvalidOperationException("The event should not be raised a second time.");
                }
                e.ConfirmAll = true;
            };
            progress.NotifyLevelStart();
            FileOperationContext status = await controller.WipeFileAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The wipe should indicate success.");

            IDataStore fileInfo = New <IDataStore>(_helloWorldAxxPath);

            Assert.That(!fileInfo.IsAvailable, "The file should not exist after wiping.");

            Assert.DoesNotThrowAsync(async() => { status = await controller.WipeFileAsync(New <IDataStore>(_davidCopperfieldTxtPath)); });
            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The wipe should indicate success.");
            progress.NotifyLevelFinished();

            fileInfo = New <IDataStore>(_davidCopperfieldTxtPath);
            Assert.That(!fileInfo.IsAvailable, "The file should not exist after wiping.");
        }
        private Task <FileOperationContext> WipeFileWorkAsync(IDataStore file, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) =>
            {
                FileSelectionEventArgs fileSelectionArgs = new FileSelectionEventArgs(new string[] { file.FullName, })
                {
                    FileSelectionType = FileSelectionType.WipeConfirm,
                };
                OnSelectingFiles(fileSelectionArgs);
                e.Cancel           = fileSelectionArgs.Cancel;
                e.Skip             = fileSelectionArgs.Skip;
                e.ConfirmAll       = fileSelectionArgs.ConfirmAll;
                e.SaveFileFullName = fileSelectionArgs.SelectedFiles.FirstOrDefault() ?? String.Empty;
            };

            operationsController.Completed += async(object sender, FileOperationEventArgs e) =>
            {
                if (e.Skip)
                {
                    return;
                }
                if (e.Status.ErrorStatus == ErrorStatus.Success)
                {
                    await New <ActiveFileAction>().RemoveRecentFiles(new IDataStore[] { New <IDataStore>(e.SaveFileFullName) }, progress);
                }
            };

            return(operationsController.WipeFileAsync(file));
        }
        public async Task TestSimpleEncryptFile()
        {
            FileOperationsController controller = new FileOperationsController();
            string destinationPath = String.Empty;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("allan");
            };
            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");

            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created.");
            using (V2AxCryptDocument document = new V2AxCryptDocument())
            {
                using (Stream stream = destinationInfo.OpenRead())
                {
                    document.Load(new Passphrase("allan"), new V2Aes256CryptoFactory().CryptoId, stream);
                    Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given.");
                }
            }
        }
Example #16
0
        private void EncryptFile(string file, IThreadWorker worker, ProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress);

            operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Title            = Resources.EncryptFileSaveAsDialogTitle;
                    sfd.AddExtension     = true;
                    sfd.ValidateNames    = true;
                    sfd.CheckPathExists  = true;
                    sfd.DefaultExt       = OS.Current.AxCryptExtension;
                    sfd.FileName         = e.SaveFileFullName;
                    sfd.Filter           = Resources.EncryptedFileDialogFilterPattern.InvariantFormat(OS.Current.AxCryptExtension);
                    sfd.InitialDirectory = Path.GetDirectoryName(e.SaveFileFullName);
                    sfd.ValidateNames    = true;
                    DialogResult saveAsResult = sfd.ShowDialog();
                    if (saveAsResult != DialogResult.OK)
                    {
                        e.Cancel = true;
                        return;
                    }
                    e.SaveFileFullName = sfd.FileName;
                }
            };

            operationsController.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                string passphrase = AskForEncryptionPassphrase();
                if (String.IsNullOrEmpty(passphrase))
                {
                    e.Cancel = true;
                    return;
                }
                e.Passphrase = passphrase;
                AesKey defaultEncryptionKey = new Passphrase(e.Passphrase).DerivedPassphrase;
                persistentState.Current.KnownKeys.DefaultEncryptionKey = defaultEncryptionKey;
            };

            operationsController.Completed += (object sender, FileOperationEventArgs e) =>
            {
                if (e.Status == FileOperationStatus.FileAlreadyEncrypted)
                {
                    e.Status = FileOperationStatus.Success;
                    return;
                }
                if (CheckStatusAndShowMessage(e.Status, e.OpenFileFullName))
                {
                    IRuntimeFileInfo encryptedInfo = OS.Current.FileInfo(e.SaveFileFullName);
                    IRuntimeFileInfo decryptedInfo = OS.Current.FileInfo(FileOperation.GetTemporaryDestinationName(e.OpenFileFullName));
                    ActiveFile       activeFile    = new ActiveFile(encryptedInfo, decryptedInfo, e.Key, ActiveFileStatus.NotDecrypted, null);
                    persistentState.Current.Add(activeFile);
                    persistentState.Current.Save();
                }
            };

            operationsController.EncryptFile(file, worker);
        }
        private Task <FileOperationContext> VerifyFileIntegrityAsync(IDataStore dataStore, LogOnIdentity identity, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase = HandleQueryDecryptionPassphraseEventAsync;

            return(operationsController.VerifyFileIntegrityAsync(dataStore));
        }
        public async Task TestDecryptFileWithRepeatedPassphraseQueries()
        {
            FileOperationsController controller = new FileOperationsController();
            int passphraseTry = 0;

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                switch (++passphraseTry)
                {
                case 1:
                    e.LogOnIdentity = new LogOnIdentity("b");
                    break;

                case 2:
                    e.LogOnIdentity = new LogOnIdentity("d");
                    break;

                case 3:
                    e.LogOnIdentity = new LogOnIdentity("a");
                    break;

                case 4:
                    e.LogOnIdentity = new LogOnIdentity("e");
                    break;
                }
                ;
                return(Task.FromResult <object>(null));
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };
            bool knownKeyWasAdded = false;

            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>((FileOperationEventArgs e) =>
            {
                knownKeyWasAdded = e.LogOnIdentity.Equals(new LogOnIdentity("a"));
                return(Constant.CompletedTask);
            });
            FileOperationContext status = await controller.DecryptFileAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");
            Assert.That(knownKeyWasAdded, "A new known key was used, so the KnownKeyAdded event should have been raised.");
            Assert.That(passphraseTry, Is.EqualTo(3), "The third key was the correct one.");
            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After decryption the destination file should be created.");

            string fileContent;

            using (Stream stream = destinationInfo.OpenRead())
            {
                fileContent = new StreamReader(stream).ReadToEnd();
            }
            Assert.That(fileContent.Contains("Hello"), "A file named Hello World should contain that text when decrypted.");
        }
Example #19
0
        public static void TestDecryptFileWithRepeatedPassphraseQueries()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            int passphraseTry = 0;

            controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                switch (++passphraseTry)
                {
                case 1:
                    e.Passphrase = "b";
                    break;

                case 2:
                    e.Passphrase = "d";
                    break;

                case 3:
                    e.Passphrase = "a";
                    break;

                case 4:
                    e.Passphrase = "e";
                    break;
                }
                ;
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };
            bool knownKeyWasAdded = false;

            controller.KnownKeyAdded += (object sender, FileOperationEventArgs e) =>
            {
                knownKeyWasAdded = e.Key == new Passphrase("a").DerivedPassphrase;
            };
            FileOperationStatus status = controller.DecryptFile(_helloWorldAxxPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success.");
            Assert.That(knownKeyWasAdded, "A new known key was used, so the KnownKeyAdded event should have been raised.");
            Assert.That(passphraseTry, Is.EqualTo(3), "The third key was the correct one.");
            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);

            Assert.That(destinationInfo.Exists, "After decryption the destination file should be created.");

            string fileContent;

            using (Stream stream = destinationInfo.OpenRead())
            {
                fileContent = new StreamReader(stream).ReadToEnd();
            }
            Assert.That(fileContent.Contains("Hello"), "A file named Hello World should contain that text when decrypted.");
        }
Example #20
0
        public static void TestCanceledDecryptAndLaunch()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };
            FileOperationStatus status = controller.DecryptAndLaunch(_helloWorldAxxPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation.");
        }
Example #21
0
        private void SelectFileToCompressButton_Click(object sender, EventArgs e)
        {
            FileOperationsController _selectedFileOperations = new FileOperationsController();
            ReadFileController       _readTextFileController = new ReadFileController();

            _selectedFileOperations.selectFileDirectory();
            FileNameTextBox.Text = _selectedFileOperations.getSelectedFileDirectoryName();
            filePathRichBox.Text = _selectedFileOperations.getSelectedFileDirectoryPath().ToString();


            UncompressedByteSizeTextBox.Text = "Bytes: " + _selectedFileOperations.getSelectedFileDirectorySize();
        }
        private Task <FileOperationContext> EncryptionUpgradeWorkAsync(IDataStore store, IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase = HandleQueryDecryptionPassphraseEventAsync;

            operationsController.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>(async(FileOperationEventArgs e) =>
            {
                await _knownIdentities.AddAsync(e.LogOnIdentity);
            });

            return(operationsController.EncryptionUpgradeAsync(store));
        }
Example #23
0
        public static void TestEncryptFileWhenCanceledDuringQueryPassphrase()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };

            FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation.");
        }
        public async Task TestCanceledDecryptAndLaunch()
        {
            FileOperationsController controller = new FileOperationsController();

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                e.Cancel = true;
                return(Task.FromResult <object>(null));
            };
            FileOperationContext status = await controller.DecryptAndLaunchAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Canceled), "The status should indicate cancellation.");
        }
        public async Task TestEncryptFileWhenCanceledDuringQueryPassphrase()
        {
            FileOperationsController controller = new FileOperationsController();

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Canceled), "The status should indicate cancellation.");
        }
Example #26
0
        private void LZWCompressButton_Click_1(object sender, EventArgs e)
        {
            LZWCompress               _lzwCompress             = new LZWCompress(this);
            IReadFileController       _readTextFileController  = new ReadFileController();
            IFileOperationsController _fileOperationController = new FileOperationsController();
            string compressedFilePath = null;


            compressedFilePathRichTextBox.Text = filePathRichBox.Text.Remove(filePathRichBox.Text.Length - 4) + "Compressed.txt";
            _lzwCompress.LZWCompressFile(filePathRichBox.Text, compressedFilePathRichTextBox.Text, Convert.ToInt16(selectedMaxBitSizeTextbox.Text));
            compressedFilePath             = filePathRichBox.Text.Remove(filePathRichBox.Text.Length - 4) + "Compressed.txt";
            compressedByteSizeTextBox.Text = _fileOperationController.getFileByteSize(compressedFilePath);
        }
        public async Task TestEncryptFileWhenDestinationExists()
        {
            IDataStore sourceInfo = New <IDataStore>(_davidCopperfieldTxtPath);
            IDataStore expectedDestinationInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceInfo));

            using (Stream stream = expectedDestinationInfo.OpenWrite())
            {
            }

            FileOperationsController controller = new FileOperationsController();
            string        destinationPath       = String.Empty;
            LogOnIdentity logOnIdentity         = null;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("allan");
            };
            controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx");
            };
            Guid cryptoId = Guid.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
                logOnIdentity   = e.LogOnIdentity;
                cryptoId        = e.CryptoId;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");

            Assert.That(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed.");
            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created.");

            EncryptionParameters encryptionParameters = new EncryptionParameters(cryptoId, logOnIdentity);
            await encryptionParameters.AddAsync(logOnIdentity.PublicKeys);

            Headers           headers = new Headers();
            AxCryptReaderBase reader  = headers.CreateReader(new LookAheadStream(destinationInfo.OpenRead()));

            using (IAxCryptDocument document = AxCryptReaderBase.Document(reader))
            {
                document.Load(logOnIdentity.Passphrase, cryptoId, headers);
                Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given.");
            }
        }
        private static FileOperationsController EncryptFileWorkController(IProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                using (FileLock lockedSave = e.SaveFileFullName.CreateUniqueFile())
                {
                    e.SaveFileFullName = lockedSave.DataStore.FullName;
                    lockedSave.DataStore.Delete();
                }
            };

            return(operationsController);
        }
Example #29
0
        private void LZWDecompressButton_Click_1(object sender, EventArgs e)
        {
            IFileOperationsController _fileOperationController = new FileOperationsController();
            LZWDecompress             _lzwDecompress           = new LZWDecompress(this);


            string decompressedFilePath = compressedFilePathRichTextBox.Text.Remove(compressedFilePathRichTextBox.Text.Length - 14) + "Uncompressed.txt";

            decompressedFileNameTextBox.Text = decompressedFilePath;
            _lzwDecompress.LZWDecompressFile(compressedFilePathRichTextBox.Text, decompressedFilePath, Convert.ToInt16(selectedMaxBitSizeTextbox.Text));
            decompressedByteSizeTextBox.Text = _fileOperationController.getFileByteSize(decompressedFilePath);

            compressedPercentanceTextBox.Text     = _lzwDecompress.getCompressionRatio(double.Parse(compressedByteSizeTextBox.Text), double.Parse(decompressedByteSizeTextBox.Text)) + " %";
            CompressedDataPercentanceTextBox.Text = (100 - Convert.ToDouble(compressedPercentanceTextBox.Text.Remove(compressedPercentanceTextBox.Text.Length - 2))).ToString() + " %";
        }
        public async Task TestDecryptWithKnownKey()
        {
            FileOperationsController controller = new FileOperationsController();
            await Resolve.KnownIdentities.AddAsync(new LogOnIdentity("b"));

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

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

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

            bool passphraseWasQueried = false;

            controller.QueryDecryptionPassphrase = (FileOperationEventArgs e) =>
            {
                passphraseWasQueried = true;
                return(Task.FromResult <object>(null));
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };
            bool knownKeyWasAdded = false;

            controller.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>((FileOperationEventArgs e) =>
            {
                knownKeyWasAdded = true;
                return(Constant.CompletedTask);
            });
            FileOperationContext status = await controller.DecryptFileAsync(New <IDataStore>(_helloWorldAxxPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success.");
            Assert.That(!knownKeyWasAdded, "An already known key was used, so the KnownKeyAdded event should not have been raised.");
            Assert.That(!passphraseWasQueried, "An already known key was used, so the there should be no need to query for a passphrase.");
            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After decryption the destination file should be created.");

            string fileContent;

            using (Stream stream = destinationInfo.OpenRead())
            {
                fileContent = new StreamReader(stream).ReadToEnd();
            }
            Assert.That(fileContent.Contains("Hello"), "A file named Hello World should contain that text when decrypted.");
        }