Esempio n. 1
0
        public static void TestCancellationByException()
        {
            bool wasCanceled = false;
            using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
            {
                worker.Work += (object sender, ThreadWorkerEventArgs e) =>
                    {
                        throw new OperationCanceledException();
                    };
                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.");
        }
Esempio n. 2
0
        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 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 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 TestDecryptWithCancelDuringQueryDecryptionPassphraseOnThreadWorker()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) =>
                {
                    e.Cancel = true;
                };
            FileOperationStatus status = FileOperationStatus.Unknown;
            controller.Completed += (object sender, FileOperationEventArgs e) =>
                {
                    status = e.Status;
                };

            using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
            {
                controller.DecryptFile(_helloWorldAxxPath, worker);
                worker.Join();
            }

            Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation.");
        }
Esempio n. 6
0
 /// <summary>
 /// Create a ThreadWorker for background work. Concurrency limitations are effective, so this call may block.
 /// </summary>
 /// <remarks>
 /// Since this call may block, it should not be called from the GUI thread if there is a risk of blocking.
 /// </remarks>
 /// <returns></returns>
 public IThreadWorker CreateWorker()
 {
     if (_disposed)
     {
         throw new ObjectDisposedException("WorkerGroup");
     }
     AcquireOneConcurrencyRight();
     ThreadWorker threadWorker = new ThreadWorker(Progress);
     threadWorker.Completed += new EventHandler<ThreadWorkerEventArgs>(HandleThreadWorkerCompletedEvent);
     return new ThreadWorkerWrapper(threadWorker);
 }
Esempio n. 7
0
        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.");
        }
 /// <summary>
 /// Perform a background operation with support for progress bars and cancel.
 /// </summary>
 /// <param name="displayText">A text that may be used as a reference in various messages.</param>
 /// <param name="work">A 'work' delegate, taking a ProgressContext and return a FileOperationStatus. Executed on a background thread. Not the calling/GUI thread.</param>
 /// <param name="complete">A 'complete' delegate, taking the final status. Executed on the original caller thread, typically the GUI thread.</param>
 public void BackgroundWorkWithProgress(Func<ProgressContext, FileOperationStatus> work, Action<FileOperationStatus> complete)
 {
     ProgressContext progress = new ProgressContext();
     ProgressBar progressBar = CreateProgressBar(progress);
     OnProgressBarCreated(new ControlEventArgs(progressBar));
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         progressBar.Value = e.Percent;
     };
     ThreadWorker threadWorker = new ThreadWorker(progress);
     threadWorker.Work += (object sender, ThreadWorkerEventArgs e) =>
     {
         e.Result = work(e.Progress);
     };
     threadWorker.Completing += (object sender, ThreadWorkerEventArgs e) =>
     {
         try
         {
             complete(e.Result);
             progressBar.Parent = null;
         }
         finally
         {
             progressBar.Dispose();
         }
     };
     threadWorker.Completed += (object sender, ThreadWorkerEventArgs e) =>
     {
         IDisposable disposable = sender as IDisposable;
         if (disposable != null)
         {
             disposable.Dispose();
         }
         Interlocked.Decrement(ref _workerCount);
     };
     Interlocked.Increment(ref _workerCount);
     threadWorker.Run();
 }
Esempio n. 9
0
        public static void TestObjectDisposedException()
        {
            ThreadWorker worker = new ThreadWorker(new ProgressContext());
            worker.Work += (object sender, ThreadWorkerEventArgs e) =>
                {
                    e.Result = FileOperationStatus.Success;
                };
            try
            {
                worker.Run();
                worker.Join();
            }
            finally
            {
                worker.Dispose();
            }

            bool hasCompleted = false;
            Assert.Throws<ObjectDisposedException>(() => { worker.Run(); });
            Assert.Throws<ObjectDisposedException>(() => { worker.Join(); });
            Assert.Throws<ObjectDisposedException>(() => { hasCompleted = worker.HasCompleted; });
            Assert.That(!hasCompleted, "Although the thread has completed, the variable should still be false since the attempt to set it is after Dispose().");
            Assert.DoesNotThrow(() => { worker.Dispose(); });
        }
Esempio n. 10
0
        public static void TestPrepare()
        {
            bool wasPrepared = false;
            using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
            {
                worker.Prepare += (object sender, ThreadWorkerEventArgs e) =>
                    {
                        wasPrepared = true;
                    };
                worker.Run();
                worker.Join();
            }

            Assert.That(wasPrepared, Is.True, "The Prepare event should be raised.");
        }
Esempio n. 11
0
 public static void TestHasCompleted()
 {
     using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
     {
         bool wasCompletedInWork = false;
         worker.Work += (object sender, ThreadWorkerEventArgs e) =>
         {
             wasCompletedInWork = worker.HasCompleted;
         };
         bool wasCompletedInCompleted = false;
         worker.Completing += (object sender, ThreadWorkerEventArgs e) =>
         {
             wasCompletedInCompleted = worker.HasCompleted;
         };
         worker.Run();
         worker.Join();
         Assert.That(!wasCompletedInWork, "Completion is not set as true in the work event.");
         Assert.That(!wasCompletedInCompleted, "Completion is not set as true until after the completed event.");
         Assert.That(worker.HasCompleted, "Completion should be set as true when the thread is joined.");
     }
 }
Esempio n. 12
0
        public static void TestErrorSetInWorkCompleted()
        {
            bool errorInWork = false;
            using (ThreadWorker worker = new ThreadWorker(new ProgressContext()))
            {
                worker.Work += (object sender, ThreadWorkerEventArgs e) =>
                {
                    throw new InvalidOperationException();
                };
                worker.Completing += (object sender, ThreadWorkerEventArgs e) =>
                {
                    errorInWork = e.Result == FileOperationStatus.Exception;
                };
                worker.Run();
                worker.Join();
            }

            Assert.That(errorInWork, Is.True, "The operation was interrupted by an exception and should return status as such.");
        }
Esempio n. 13
0
        public static void TestFinishInBackground()
        {
            bool didComplete = false;
            ProgressContext progress = new ProgressContext();
            progress.Progressing += (object sender2, ProgressEventArgs e2) =>
            {
                didComplete = true;
            };

            using (ThreadWorker threadWorker = new ThreadWorker(progress))
            {
                threadWorker.Work += (object sender, ThreadWorkerEventArgs e) =>
                {
                    using (WorkerGroup workerGroup = new WorkerGroup(progress))
                    {
                        IThreadWorker worker = workerGroup.CreateWorker();
                        worker.Work += (object sender2, ThreadWorkerEventArgs e2) =>
                        {
                            e2.Progress.NotifyLevelStart();
                            e2.Progress.NotifyLevelFinished();
                        };
                        worker.Run();
                    }
                };
                threadWorker.Run();
            }

            Assert.That(didComplete, "Execution should continue here, with the flag set indicating that the progress event occurred.");
        }
        public static void TestSimpleEncryptFileOnThreadWorker()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Passphrase = "allan";
            };
            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.EncryptFile(_davidCopperfieldTxtPath, worker);
                worker.Join();
            }
            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.");
                }
            }
        }
Esempio n. 15
0
        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.");
        }
		void CreateWorker() {
			this.worker = new ThreadWorker (this.context);
			//worker.Prepare += delegate { SetProgress(0, "Unlocking ..."); };
			worker.Work += Work;
			worker.Completed += WorkerCompleted;
		}