public static void CheckWatchedFolders(this FileSystemState fileSystemState, ProgressContext progress) { if (fileSystemState == null) { throw new ArgumentNullException("fileSystemState"); } AesKey encryptionKey = fileSystemState.KnownKeys.DefaultEncryptionKey; if (encryptionKey == null) { return; } IEnumerable<IRuntimeFileInfo> files = fileSystemState.DecryptedFilesInWatchedFolders(); progress.NotifyLevelStart(); try { progress.AddTotal(files.Count()); foreach (IRuntimeFileInfo fileInfo in files) { IRuntimeFileInfo destinationFileInfo = fileInfo.CreateEncryptedName(); AxCryptFile.EncryptFileWithBackupAndWipe(fileInfo, destinationFileInfo, encryptionKey, progress); progress.AddCount(1); } } finally { progress.NotifyLevelFinished(); } }
/// <summary> /// Enumerate all files listed as active, checking for status changes and take appropriate actions such as updating status /// in the FileSystemState, re-encrypting or deleting temporary plaintext copies. /// </summary> /// <param name="fileSystemState">The FileSystemState to enumerate and possibly update.</param> /// <param name="mode">Under what circumstances is the FileSystemState.Changed event raised.</param> /// <param name="progress">The ProgressContext to provide visual progress feedback via.</param> public static void CheckActiveFiles(this FileSystemState fileSystemState, ChangedEventMode mode, ProgressContext progress) { progress.NotifyLevelStart(); progress.AddTotal(fileSystemState.ActiveFileCount); fileSystemState.ForEach(mode, (ActiveFile activeFile) => { try { if (FileLock.IsLocked(activeFile.DecryptedFileInfo, activeFile.EncryptedFileInfo)) { return activeFile; } if (OS.Current.UtcNow - activeFile.LastActivityTimeUtc <= new TimeSpan(0, 0, 5)) { return activeFile; } activeFile = fileSystemState.CheckActiveFileActions(activeFile, progress); return activeFile; } finally { progress.AddCount(1); } }); progress.NotifyLevelFinished(); }
public static void DecryptAndOpenFile(IRuntimeFileInfo encryptedDocument, Passphrase passphrase, ProgressContext progress, Action<string, ProgressContext> failure = null) { string tempPath = Path.GetTempPath(); string decryptedFileName; lastUsedKey = passphrase.DerivedPassphrase; if (!TryDecrypt(encryptedDocument, tempPath, lastUsedKey, progress, out decryptedFileName)) { failure("Could not open file", progress); return; } string fullPathToDecryptedFile = Path.Combine(tempPath, decryptedFileName); IRuntimeFileInfo decryptedFile = OS.Current.FileInfo(fullPathToDecryptedFile); NSDictionary userInfo = new NSDictionary(Launcher.TargetFileUserInfoKey, decryptedFile.FullName); NSNotification notification = NSNotification.FromName(Launcher.FileDecryptedNotification, new NSObject(), userInfo); NSNotificationCenter.DefaultCenter.PostNotification(notification); ILauncher launcher = OS.Current.Launch (fullPathToDecryptedFile); launcher.Exited += (sender, e) => { fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext()); }; fileSystemState.Add (new ActiveFile(encryptedDocument, decryptedFile, lastUsedKey, ActiveFileStatus.AssumedOpenAndDecrypted, launcher)); //fileSystemState.Save (); }
/// <summary> /// Decrypt a source file to a destination file, given a passphrase /// </summary> /// <param name="sourceFile">The source file</param> /// <param name="destinationFile">The destination file</param> /// <param name="passphrase">The passphrase</param> /// <returns>true if the passphrase was correct</returns> public static bool Decrypt(IRuntimeFileInfo sourceFile, IRuntimeFileInfo destinationFile, AesKey key, AxCryptOptions options, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationFile == null) { throw new ArgumentNullException("destinationFile"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } using (AxCryptDocument document = Document(sourceFile, key, new ProgressContext())) { if (!document.PassphraseIsValid) { return false; } Decrypt(document, destinationFile, options, progress); } return true; }
public static void TestAddCountAfterFinished() { ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.NotifyLevelFinished(); Assert.Throws<InvalidOperationException>(() => { progress.AddCount(1); }); }
public DecryptionViewController (string sourceFilePath) { this.context = new ProgressContext (); // context.Progressing += (sender, e) => { // SetProgress(e.Percent, "Decrypting ..."); // }; this.sourceFile = OS.Current.FileInfo (sourceFilePath); }
/// <summary> /// Create a thread worker. /// </summary> /// <param name="displayText">A text that may be used in messages as a reference for users.</param> /// <param name="work">A 'work' delegate. Executed on a separate thread, not the GUI thread.</param> /// <param name="complete">A 'complete' delegate. Executed on the original thread, typically the GUI thread.</param> public ThreadWorker(ProgressContext progress) { _worker = new BackgroundWorker(); _worker.DoWork += new DoWorkEventHandler(_worker_DoWork); _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted); _e = new ThreadWorkerEventArgs(progress); }
public static void TestProgress() { ProgressContext progress = new ProgressContext(TimeSpan.Zero); progress.AddTotal(100); bool wasHere = false; progress.Progressing += (object sender, ProgressEventArgs e) => { wasHere = true; }; progress.AddCount(1); Assert.That(wasHere, "The event should be raised."); }
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 ProgressStream(Stream stream, ProgressContext progress) { if (stream == null) { throw new ArgumentNullException("stream"); } if (progress == null) { throw new ArgumentNullException("progress"); } _stream = stream; _progress = progress; }
void InvokeWithProgress(Action<ProgressContext, Action<string, ProgressContext>> action, NSProgressIndicator indicator) { ProgressContext progress = new ProgressContext(); progress.Progressing += (sender, eventArgs) => { if (eventArgs.Percent < 100) { indicator.StartAnimation(this); } else { indicator.StopAnimation(this); } }; action(progress, (message, context) => InvokeOnMainThread((NSAction) delegate { indicator.StopAnimation(this); AppController.OperationFailureHandler(message, context); })); }
public static void TestAddingNegativeCount() { FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current; fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(1000); ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(1000)); int percent = 0; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddTotal(2); Assert.That(percent, Is.EqualTo(0), "No progress yet - should be zero."); progress.AddCount(-100); Assert.That(percent, Is.EqualTo(0), "Nothing should happen adding negative counts."); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(2000); progress.AddCount(1); Assert.That(percent, Is.EqualTo(50), "1 of 2 is 50 percent."); }
public static void TestMultipleAddTotal() { int percent = 0; ProgressContext progress = new ProgressContext(TimeSpan.Zero); progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddTotal(50); progress.AddTotal(50); progress.AddCount(50); Assert.That(percent, Is.EqualTo(50), "The total should be 100, so 50 is 50% and the progressing event should be raised at the first progress."); }
public static void TestItems() { ProgressContext progress = new ProgressContext(); Assert.That(progress.Items, Is.EqualTo(0), "At start there are no item counted."); progress.AddItems(7); Assert.That(progress.Items, Is.EqualTo(7), "There was just 7 added."); progress.AddItems(-1); Assert.That(progress.Items, Is.EqualTo(6), "Items are counted down by adding negative numbers."); }
public static void TestFirstDelay() { FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current; ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(13)); bool wasHere = false; progress.Progressing += (object sender, ProgressEventArgs e) => { wasHere = true; }; progress.AddTotal(100); progress.AddCount(50); Assert.That(wasHere, Is.False, "No progress should be raised, since the first delay time has not elapsed as yet."); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(12); progress.AddCount(1); Assert.That(wasHere, Is.False, "No progress should be raised, since the first delay time has not elapsed as yet."); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(13); progress.AddCount(1); Assert.That(wasHere, Is.True, "Progress should be raised, since the first delay time has now elapsed."); }
public static void TestDoubleNotifyFinished() { ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.NotifyLevelStart(); progress.NotifyLevelFinished(); Assert.DoesNotThrow(() => { progress.NotifyLevelFinished(); }); }
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 TestProgressWithSynchronizationContext() { SynchronizationContext synchronizationContext = new SynchronizationContext(); StateForSynchronizationContext s = new StateForSynchronizationContext(); s.WaitEvent = new ManualResetEvent(false); s.SynchronizationContext = synchronizationContext; synchronizationContext.Post( (object state) => { StateForSynchronizationContext ss = (StateForSynchronizationContext)state; SynchronizationContext.SetSynchronizationContext(ss.SynchronizationContext); ss.SynchronizationContext = SynchronizationContext.Current; ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.Progressing += (object sender, ProgressEventArgs e) => { ss.DidProgress = true; }; progress.NotifyLevelFinished(); ss.WaitEvent.Set(); }, s); bool waitOk = s.WaitEvent.WaitOne(TimeSpan.FromSeconds(10), false); Assert.That(waitOk, "The wait should not time-out"); Assert.That(s.SynchronizationContext, Is.EqualTo(synchronizationContext), "The SynchronizationContext should be current in the code executed."); Assert.That(s.DidProgress, "There should always be one Progressing event after NotifyFinished()."); }
public static void TestProgressTo100AndAboveShouldOnlyReturn99BeforeFinishedPercent() { FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current; fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(1000); ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(1000)); progress.NotifyLevelStart(); int percent = 0; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddTotal(2); Assert.That(percent, Is.EqualTo(0), "No progress yet - should be zero."); progress.AddCount(1); Assert.That(percent, Is.EqualTo(50), "Halfway should be 50 percent."); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(2000); progress.AddCount(1); Assert.That(percent, Is.EqualTo(99), "Even at 100 should report 99 percent."); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(3000); progress.AddCount(1000); Assert.That(percent, Is.EqualTo(99), "Even at very much above 100 should report 99 percent."); progress.NotifyLevelFinished(); Assert.That(percent, Is.EqualTo(100), "Only when NotifyFinished() is called should 100 percent be reported."); }
private static ActiveFile DecryptActiveFileDocument(IRuntimeFileInfo sourceFileInfo, IRuntimeFileInfo destinationFolderInfo, AxCryptDocument document, ProgressContext progress) { string destinationName = document.DocumentHeaders.FileName; string destinationPath = Path.Combine(destinationFolderInfo.FullName, destinationName); IRuntimeFileInfo destinationFileInfo = OS.Current.FileInfo(destinationPath); using (FileLock fileLock = FileLock.Lock(destinationFileInfo)) { AxCryptFile.Decrypt(document, destinationFileInfo, AxCryptOptions.SetFileTimes, progress); } ActiveFile destinationActiveFile = new ActiveFile(sourceFileInfo, destinationFileInfo, document.DocumentHeaders.KeyEncryptingKey, ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.IgnoreChange, null); if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("File decrypted from '{0}' to '{1}'".InvariantFormat(sourceFileInfo.FullName, destinationActiveFile.DecryptedFileInfo.FullName)); } return(destinationActiveFile); }
/// <summary> /// Write a copy of the current encrypted stream. Used to change meta-data /// and encryption key(s) etc. /// </summary> /// <param name="outputStream"></param> public void CopyEncryptedTo(DocumentHeaders outputDocumentHeaders, Stream cipherStream, ProgressContext progress) { if (outputDocumentHeaders == null) { throw new ArgumentNullException("outputDocumentHeaders"); } if (cipherStream == null) { throw new ArgumentNullException("cipherStream"); } if (!cipherStream.CanSeek) { throw new ArgumentException("The output stream must support seek in order to back-track and write the HMAC."); } if (DocumentHeaders == null) { throw new InternalErrorException("Document headers are not loaded"); } using (HmacStream hmacStreamOutput = new HmacStream(outputDocumentHeaders.HmacSubkey.Key, cipherStream)) { outputDocumentHeaders.WriteWithHmac(hmacStreamOutput); using (AxCryptDataStream encryptedDataStream = _reader.CreateEncryptedDataStream(DocumentHeaders.HmacSubkey.Key, DocumentHeaders.CipherTextLength, progress)) { CopyToWithCount(encryptedDataStream, hmacStreamOutput, progress); if (_reader.Hmac != DocumentHeaders.Hmac) { throw new Axantum.AxCrypt.Core.Runtime.InvalidDataException("HMAC validation error in the input stream.", ErrorStatus.HmacValidationError); } } outputDocumentHeaders.Hmac = hmacStreamOutput.HmacResult; // Rewind and rewrite the headers, now with the updated HMAC outputDocumentHeaders.WriteWithoutHmac(cipherStream); cipherStream.Position = cipherStream.Length; } }
private void DecryptEncryptedDataStream(Stream outputPlaintextStream, ICryptoTransform decryptor, AxCryptDataStream encryptedDataStream, ProgressContext progress) { Exception savedExceptionIfCloseCausesCryptographicException = null; try { if (DocumentHeaders.IsCompressed) { using (CryptoStream deflatedPlaintextStream = new CryptoStream(encryptedDataStream, decryptor, CryptoStreamMode.Read)) { using (ZInputStream inflatedPlaintextStream = new ZInputStream(deflatedPlaintextStream)) { try { CopyToWithCount(inflatedPlaintextStream, outputPlaintextStream, encryptedDataStream, progress); } catch (Exception ex) { savedExceptionIfCloseCausesCryptographicException = ex; throw; } } } } else { using (Stream plainStream = new CryptoStream(encryptedDataStream, decryptor, CryptoStreamMode.Read)) { try { CopyToWithCount(plainStream, outputPlaintextStream, encryptedDataStream, progress); } catch (Exception ex) { savedExceptionIfCloseCausesCryptographicException = ex; throw; } } } } catch (CryptographicException) { throw savedExceptionIfCloseCausesCryptographicException; } }
private static void EncryptWithCompressionInternal(DocumentHeaders outputDocumentHeaders, Stream inputStream, CryptoStream encryptingStream, ProgressContext progress) { using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1)) { deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH; CopyToWithCount(inputStream, deflatingStream, progress); deflatingStream.FlushMode = JZlib.Z_FINISH; deflatingStream.Finish(); outputDocumentHeaders.UncompressedLength = deflatingStream.TotalIn; outputDocumentHeaders.PlaintextLength = deflatingStream.TotalOut; } }
private static ActiveFile CheckKeysForAlreadyDecryptedFile(ActiveFile destinationActiveFile, IEnumerable <AesKey> keys, ProgressContext progress) { foreach (AesKey key in keys) { using (AxCryptDocument document = AxCryptFile.Document(destinationActiveFile.EncryptedFileInfo, key, progress)) { if (document.PassphraseIsValid) { if (OS.Log.IsWarningEnabled) { OS.Log.LogWarning("File was already decrypted and the key was known for '{0}' to '{1}'".InvariantFormat(destinationActiveFile.EncryptedFileInfo.FullName, destinationActiveFile.DecryptedFileInfo.FullName)); } return(new ActiveFile(destinationActiveFile, key)); } } } return(null); }
public static void TestPercent() { ProgressContext progress = new ProgressContext(TimeSpan.Zero); int percent = -1; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddTotal(200); progress.AddCount(100); Assert.That(percent, Is.EqualTo(50), "When halfway, the percent should be 50."); }
public static void TestTooManyNotifyFinished() { ProgressContext progress = new ProgressContext(); Assert.Throws<InvalidOperationException>(() => { progress.NotifyLevelFinished(); }); }
public static void TestProgressNoMax() { ProgressContext progress = new ProgressContext(TimeSpan.Zero); int percent = -1; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddCount(100); Assert.That(percent, Is.EqualTo(0), "Since there is no Total set, the percentage should always be zero."); }
private static ActiveFile TryDecrypt(IRuntimeFileInfo sourceFileInfo, IRuntimeFileInfo destinationFolderInfo, IEnumerable <AesKey> keys, ProgressContext progress) { ActiveFile destinationActiveFile = null; foreach (AesKey key in keys) { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Decrypting '{0}'".InvariantFormat(sourceFileInfo.FullName)); } using (FileLock sourceLock = FileLock.Lock(sourceFileInfo)) { using (AxCryptDocument document = AxCryptFile.Document(sourceFileInfo, key, new ProgressContext())) { if (!document.PassphraseIsValid) { continue; } destinationActiveFile = DecryptActiveFileDocument(sourceFileInfo, destinationFolderInfo, document, progress); break; } } } return(destinationActiveFile); }
public static void TestProgressWithoutSynchronizationContext() { bool didProgress = false; SynchronizationContext currentContext = new SynchronizationContext(); Thread thread = new Thread( (object state) => { currentContext = SynchronizationContext.Current; ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.Progressing += (object sender, ProgressEventArgs e) => { didProgress = true; }; progress.NotifyLevelFinished(); } ); thread.Start(); thread.Join(); Assert.That(didProgress, "There should always be one Progressing event after NotifyFinished()."); Assert.That(currentContext, Is.Null, "There should be no SynchronizationContext here."); }
public static void TestCurrentAndMax() { ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); int percent = -1; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.AddTotal(99); progress.NotifyLevelFinished(); Assert.That(percent, Is.EqualTo(100), "After Finished(), Percent should always be 100."); }
public static void TestSeveralCallsToCount() { ProgressContext progress = new ProgressContext(TimeSpan.Zero); int percent = -1; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current; progress.AddTotal(500); progress.AddCount(50); progress.AddCount(50); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); progress.AddCount(50); progress.AddCount(50); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(2); progress.AddCount(50); fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(2); Assert.That(percent, Is.EqualTo(50), "When halfway, the percent should be 50."); }
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 ThreadWorkerEventArgs(ProgressContext progress) { Progress = progress; }
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 TestCancel() { ProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.AddTotal(100); progress.AddCount(10); progress.Cancel = true; Assert.Throws<OperationCanceledException>(() => { progress.NotifyLevelStart(); }); Assert.Throws<OperationCanceledException>(() => { progress.NotifyLevelFinished(); }); Assert.Throws<OperationCanceledException>(() => { progress.AddTotal(50); }); Assert.Throws<OperationCanceledException>(() => { progress.AddCount(10); }); }
/// <summary> /// Create a new instance, reporting progress /// </summary> /// <param name="fileSystemState">The current FileSystemStatem instance</param> /// <param name="progress">The instance of ProgressContext to report progress via</param> public FileOperationsController(FileSystemState fileSystemState, ProgressContext progress) { _eventArgs = new FileOperationEventArgs(); _fileSystemState = fileSystemState; _progress = progress; }