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."); } } }
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."); }
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."); } } }
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); }
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 static void TestEncryptFileWhenDestinationExists() { IRuntimeFileInfo sourceInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath); IRuntimeFileInfo expectedDestinationInfo = OS.Current.FileInfo(AxCryptFile.MakeAxCryptFileName(sourceInfo)); using (Stream stream = expectedDestinationInfo.OpenWrite()) { } FileOperationsController controller = new FileOperationsController(_fileSystemState); string destinationPath = String.Empty; controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.Passphrase = "allan"; }; controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx"); }; 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(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed."); 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."); } } }
public static void TestEncryptFileWhenCanceledDuringQuerySaveAs() { IRuntimeFileInfo sourceInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath); IRuntimeFileInfo expectedDestinationInfo = OS.Current.FileInfo(AxCryptFile.MakeAxCryptFileName(sourceInfo)); using (Stream stream = expectedDestinationInfo.OpenWrite()) { } FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { e.Cancel = true; }; FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath); Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation."); }