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 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 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 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."); }
public static void TestExitEvent() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; FakeLauncher launcher = null; SetupAssembly.FakeRuntimeEnvironment.Launcher = ((string path) => { launcher = new FakeLauncher(path); launcher.WasStarted = true; return(launcher); }); FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed."); Assert.That(launcher, Is.Not.Null, "There should be a call to launch to try launching."); 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."); bool changedWasRaised = false; OS.Current.WorkFolderStateChanged += (object sender, EventArgs e) => { changedWasRaised = true; }; Assert.That(changedWasRaised, Is.False, "The global changed event should not have been raised yet."); launcher.RaiseExited(); Assert.That(changedWasRaised, Is.True, "The global changed event should be raised when the process exits."); }
public void SerializeZipOperation() { FileOperationType operationType = FileOperationType.Zip; FileOperationStatus operationStatus = FileOperationStatus.Ok; int filesCompleted = 50; int totalFiles = 100; }
public static void TestSimple() { int workThreadId = -1; FileOperationStatus returnedStatus = FileOperationStatus.UnspecifiedError; bool done = false; using (ThreadWorker worker = new ThreadWorker(new ProgressContext())) { worker.Work += (object sender, ThreadWorkerEventArgs e) => { workThreadId = Thread.CurrentThread.ManagedThreadId; e.Result = FileOperationStatus.Success; }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { returnedStatus = e.Result; done = true; }; worker.Run(); worker.Join(); } Assert.That(returnedStatus, Is.EqualTo(FileOperationStatus.Success), "The status should be returned as successful."); Assert.That(workThreadId, Is.Not.EqualTo(Thread.CurrentThread.ManagedThreadId), "The work should not be performed on the caller thread."); Assert.That(done, Is.True, "The background work must have executed the completed handler now."); }
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 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 TestFileDoesNotExist() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_rootPath.PathCombine("Documents", "HelloWorld-NotThere.axx"), keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.FileDoesNotExist), "The launch should fail with status FileDoesNotExist."); }
public static void TestInvalidKey() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("b").DerivedPassphrase }; FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.InvalidKey), "The key is invalid, so the launch should fail with that status."); }
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."); }
public static FileOperationStatus OpenAndLaunchApplication(this FileSystemState fileSystemState, string file, IEnumerable <AesKey> keys, ProgressContext progress) { if (fileSystemState == null) { throw new ArgumentNullException("fileSystemState"); } if (file == null) { throw new ArgumentNullException("file"); } if (keys == null) { throw new ArgumentNullException("keys"); } if (progress == null) { throw new ArgumentNullException("progress"); } IRuntimeFileInfo fileInfo = OS.Current.FileInfo(file); if (!fileInfo.Exists) { if (OS.Log.IsWarningEnabled) { OS.Log.LogWarning("Tried to open non-existing '{0}'.".InvariantFormat(fileInfo.FullName)); } return(FileOperationStatus.FileDoesNotExist); } ActiveFile destinationActiveFile = fileSystemState.FindEncryptedPath(fileInfo.FullName); if (destinationActiveFile == null || !destinationActiveFile.DecryptedFileInfo.Exists) { IRuntimeFileInfo destinationFolderInfo = GetTemporaryDestinationFolder(destinationActiveFile); destinationActiveFile = TryDecrypt(fileInfo, destinationFolderInfo, keys, progress); } else { destinationActiveFile = CheckKeysForAlreadyDecryptedFile(destinationActiveFile, keys, progress); } if (destinationActiveFile == null) { return(FileOperationStatus.InvalidKey); } fileSystemState.Add(destinationActiveFile); fileSystemState.Save(); FileOperationStatus status = LaunchApplicationForDocument(fileSystemState, destinationActiveFile); return(status); }
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."); }
private bool OpenAxCryptDocument(string fullName, FileOperationEventArgs e) { e.AxCryptDocument = null; try { IRuntimeFileInfo source = OS.Current.FileInfo(fullName); e.OpenFileFullName = source.FullName; foreach (AesKey key in _fileSystemState.KnownKeys.Keys) { e.AxCryptDocument = AxCryptFile.Document(source, key, new ProgressContext()); if (e.AxCryptDocument.PassphraseIsValid) { break; } e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; } Passphrase passphrase; while (e.AxCryptDocument == null) { OnQueryDecryptionPassphrase(e); if (e.Cancel) { e.Status = FileOperationStatus.Canceled; return(false); } passphrase = new Passphrase(e.Passphrase); e.AxCryptDocument = AxCryptFile.Document(source, passphrase.DerivedPassphrase, new ProgressContext()); if (!e.AxCryptDocument.PassphraseIsValid) { e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; continue; } e.Key = passphrase.DerivedPassphrase; OnKnownKeyAdded(e); } } catch (IOException ioex) { if (e.AxCryptDocument != null) { e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; } FileOperationStatus status = ioex is FileNotFoundException ? FileOperationStatus.FileDoesNotExist : FileOperationStatus.Exception; e.Status = status; return(false); } return(true); }
private static bool CheckStatusAndShowMessage(FileOperationStatus status, string displayContext) { switch (status) { case FileOperationStatus.Success: return(true); case FileOperationStatus.UnspecifiedError: Resources.FileOperationFailed.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.FileAlreadyExists: Resources.FileAlreadyExists.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.FileDoesNotExist: Resources.FileDoesNotExist.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.CannotWriteDestination: Resources.CannotWrite.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.CannotStartApplication: Resources.CannotStartApplication.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InconsistentState: Resources.InconsistentState.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InvalidKey: Resources.InvalidKey.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.Canceled: break; case FileOperationStatus.Exception: Resources.Exception.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InvalidPath: Resources.InvalidPath.InvariantFormat(displayContext).ShowWarning(); break; default: Resources.UnrecognizedError.InvariantFormat(displayContext).ShowWarning(); break; } return(false); }
public static void TestWin32Exception() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; SetupAssembly.FakeRuntimeEnvironment.Launcher = ((string path) => { throw new Win32Exception("Fake Win32Exception from Unit Test."); }); FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.CannotStartApplication), "The launch should fail since the launch throws a Win32Exception."); }
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 TestSimpleDecryptAndLaunchOnThreadWorker() { FakeLauncher launcher = null; FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; environment.Launcher = ((string path) => { launcher = new FakeLauncher(path); return(launcher); }); FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.Passphrase = "a"; }; FileOperationStatus status = FileOperationStatus.Unknown; controller.Completed += (object sender, FileOperationEventArgs e) => { status = e.Status; }; using (ThreadWorker worker = new ThreadWorker(new ProgressContext())) { controller.DecryptAndLaunch(_helloWorldAxxPath, worker); worker.Join(); } Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success."); Assert.That(launcher, Is.Not.Null, "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."); IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(launcher.Path); 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."); }
public static void TestWipeWithSkip() { FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) => { e.Skip = true; }; FileOperationStatus status = controller.WipeFile(_helloWorldAxxPath); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The wipe should indicate success even when skipping."); IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_helloWorldAxxPath); Assert.That(fileInfo.Exists, "The file should still exist after wiping that was skipped during confirmation."); }
public static void TestSimpleOpenAndLaunch() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; FakeLauncher launcher = null; SetupAssembly.FakeRuntimeEnvironment.Launcher = ((string path) => { launcher = new FakeLauncher(path); return(launcher); }); FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed."); Assert.That(launcher, Is.Not.Null, "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."); }
public static void TestSimpleWipe() { FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) => { e.Cancel = false; e.Skip = false; e.ConfirmAll = false; }; 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."); }
public static void TestFileContainedByActiveFilesButNotDecrypted() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed."); IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_helloWorldAxxPath); ActiveFile destinationActiveFile = _fileSystemState.FindEncryptedPath(fileInfo.FullName); destinationActiveFile.DecryptedFileInfo.Delete(); destinationActiveFile = new ActiveFile(destinationActiveFile, ActiveFileStatus.NotDecrypted); _fileSystemState.Add(destinationActiveFile); _fileSystemState.Save(); status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should once again succeed."); }
public static void TestDecryptWithKnownKey() { FileOperationsController controller = new FileOperationsController(_fileSystemState); _fileSystemState.KnownKeys.Add(new Passphrase("b").DerivedPassphrase); _fileSystemState.KnownKeys.Add(new Passphrase("c").DerivedPassphrase); _fileSystemState.KnownKeys.Add(new Passphrase("a").DerivedPassphrase); _fileSystemState.KnownKeys.Add(new Passphrase("e").DerivedPassphrase); bool passphraseWasQueried = false; controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) => { passphraseWasQueried = true; }; string destinationPath = String.Empty; controller.Completed += (object sender, FileOperationEventArgs e) => { destinationPath = e.SaveFileFullName; }; bool knownKeyWasAdded = false; controller.KnownKeyAdded += (object sender, FileOperationEventArgs e) => { knownKeyWasAdded = true; }; FileOperationStatus status = controller.DecryptFile(_helloWorldAxxPath); Assert.That(status, Is.EqualTo(FileOperationStatus.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."); 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."); }
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 TestSimpleDecryptFileOnThreadWorker() { FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.Passphrase = "a"; }; bool knownKeyWasAdded = false; controller.KnownKeyAdded += (object sender, FileOperationEventArgs e) => { knownKeyWasAdded = e.Key == new Passphrase("a").DerivedPassphrase; }; 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.DecryptFile(_helloWorldAxxPath, worker); worker.Join(); } 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."); 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."); }
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."); }
public static void TestFileAlreadyDecryptedWithKnownKey() { IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase }; DateTime utcNow = DateTime.UtcNow; SetupAssembly.FakeRuntimeEnvironment.TimeFunction = () => { return(utcNow); }; FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed."); IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_helloWorldAxxPath); ActiveFile destinationActiveFile = _fileSystemState.FindEncryptedPath(fileInfo.FullName); Assert.That(destinationActiveFile.DecryptedFileInfo.LastWriteTimeUtc, Is.Not.EqualTo(utcNow), "The decryption should restore the time stamp of the original file, and this is not now."); destinationActiveFile.DecryptedFileInfo.SetFileTimes(utcNow, utcNow, utcNow); status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext()); Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed this time too."); destinationActiveFile = _fileSystemState.FindEncryptedPath(fileInfo.FullName); Assert.That(destinationActiveFile.DecryptedFileInfo.LastWriteTimeUtc, Is.EqualTo(utcNow), "There should be no decryption again necessary, and thus the time stamp should be as just set."); }
public static FileOperationStatus OpenAndLaunchApplication(this FileSystemState fileSystemState, string file, AxCryptDocument document, ProgressContext progress) { if (fileSystemState == null) { throw new ArgumentNullException("fileSystemState"); } if (file == null) { throw new ArgumentNullException("file"); } if (document == null) { throw new ArgumentNullException("document"); } if (progress == null) { throw new ArgumentNullException("progress"); } IRuntimeFileInfo fileInfo = OS.Current.FileInfo(file); ActiveFile destinationActiveFile = fileSystemState.FindEncryptedPath(fileInfo.FullName); if (destinationActiveFile == null || !destinationActiveFile.DecryptedFileInfo.Exists) { IRuntimeFileInfo destinationFolderInfo = GetTemporaryDestinationFolder(destinationActiveFile); destinationActiveFile = DecryptActiveFileDocument(fileInfo, destinationFolderInfo, document, progress); } else { destinationActiveFile = new ActiveFile(destinationActiveFile, document.DocumentHeaders.KeyEncryptingKey); } fileSystemState.Add(destinationActiveFile); fileSystemState.Save(); FileOperationStatus status = LaunchApplicationForDocument(fileSystemState, destinationActiveFile); return(status); }
public static void TestDecryptWithCancelDuringQuerySaveAs() { IRuntimeFileInfo expectedDestinationInfo = OS.Current.FileInfo(Path.Combine(Path.GetDirectoryName(_helloWorldAxxPath), "HelloWorld-Key-a.txt")); using (Stream stream = expectedDestinationInfo.OpenWrite()) { } FileOperationsController controller = new FileOperationsController(_fileSystemState); controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.Passphrase = "a"; }; controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { e.Cancel = true; }; FileOperationStatus status = controller.DecryptFile(_helloWorldAxxPath); Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation."); }
private static bool CheckStatusAndShowMessage(FileOperationStatus status, string displayContext) { switch (status) { case FileOperationStatus.Success: return true; case FileOperationStatus.UnspecifiedError: Resources.FileOperationFailed.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.FileAlreadyExists: Resources.FileAlreadyExists.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.FileDoesNotExist: Resources.FileDoesNotExist.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.CannotWriteDestination: Resources.CannotWrite.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.CannotStartApplication: Resources.CannotStartApplication.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InconsistentState: Resources.InconsistentState.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InvalidKey: Resources.InvalidKey.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.Canceled: break; case FileOperationStatus.Exception: Resources.Exception.InvariantFormat(displayContext).ShowWarning(); break; case FileOperationStatus.InvalidPath: Resources.InvalidPath.InvariantFormat(displayContext).ShowWarning(); break; default: Resources.UnrecognizedError.InvariantFormat(displayContext).ShowWarning(); break; } return false; }