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."); }
public void TestDecryptWithCancel() { IDataStore sourceFileInfo = New <IDataStore>(_helloWorldAxxPath); Passphrase passphrase = new Passphrase("a"); IProgressContext progress = new CancelProgressContext(new ProgressContext(new TimeSpan(0, 0, 0, 0, 100))); progress.Progressing += (object sender, ProgressEventArgs e) => { progress.Cancel = true; }; Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(new ProgressStream(sourceFileInfo.OpenRead(), progress))); using (IAxCryptDocument document = AxCryptReaderBase.Document(reader)) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); IDataStore destinationInfo = New <IDataStore>(_rootPath.PathCombine("Destination", "Decrypted.txt")); FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; environment.CurrentTiming.CurrentTiming = new TimeSpan(0, 0, 0, 0, 100); using (FileLock destinationFileLock = New <FileLocker>().Acquire(destinationInfo)) { Assert.Throws <OperationCanceledException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, destinationFileLock, AxCryptOptions.None, progress); })); } } }
public static void TestProgress() { FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; int progressCalls = 0; ProgressContext progress = new ProgressContext(); using (ThreadWorker worker = new ThreadWorker(progress)) { worker.Work += (object sender, ThreadWorkerEventArgs e) => { environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); e.Result = FileOperationStatus.Success; }; progress.Progressing += (object sender, ProgressEventArgs e) => { ++progressCalls; }; worker.Run(); worker.Join(); } Assert.That(progressCalls, Is.EqualTo(1), "The Progressing event should be raised exactly one time."); }
public static void TestProgress() { FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; int progressCalls = 0; ProgressContext progress = new ProgressContext(); using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestProgress), progress, false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); e.Result = new FileOperationContext(String.Empty, ErrorStatus.Success); return(Task.FromResult <object>(null)); }; progress.Progressing += (object sender, ProgressEventArgs e) => { ++progressCalls; }; worker.Run(); worker.Join(); } Assert.That(progressCalls, Is.EqualTo(1), "The Progressing event should be raised exactly one time."); }
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 void TestDecryptCompressedWithCancel() { Passphrase passphrase = new Passphrase("Å ä Ö"); using (V1AxCryptDocument document = new V1AxCryptDocument()) { IProgressContext progress = new CancelProgressContext(new ProgressContext(new TimeSpan(0, 0, 0, 0, 100))); bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, new ProgressStream(FakeDataStore.ExpandableMemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt), progress)); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); using (MemoryStream plaintextStream = new MemoryStream()) { progress.Progressing += (object sender, ProgressEventArgs e) => { progress.Cancel = true; }; FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; environment.CurrentTiming.CurrentTiming = new TimeSpan(0, 0, 0, 0, 100); Assert.Throws <OperationCanceledException>(() => { document.DecryptTo(plaintextStream); }); } } }
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 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 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 void TestCancellationByRequest() { bool wasCanceled = false; FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; using (ThreadWorker worker = new ThreadWorker(new ProgressContext())) { worker.Work += (object sender, ThreadWorkerEventArgs e) => { e.Progress.Cancel = true; environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { wasCanceled = e.Result == FileOperationStatus.Canceled; }; worker.Run(); worker.Join(); } Assert.That(wasCanceled, Is.True, "The operation was canceled and should return status as such."); }
public static void TestCancellationByRequest() { bool wasCanceled = false; FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestCancellationByRequest), new CancelProgressContext(new ProgressContext()), false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { e.Progress.Cancel = true; environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); return(Task.FromResult <object>(null)); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { wasCanceled = e.Result.ErrorStatus == ErrorStatus.Canceled; }; worker.Run(); worker.Join(); } Assert.That(wasCanceled, Is.True, "The operation was canceled and should return status as such."); }