Example #1
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(); });
        }
Example #2
0
        public void ThreadWorkerStoresReduceResults()
        {
            var reducerCodeFileName = new Uri("file:///SampleReducer.cs");

            var keys = ReducerTests.CreateTwoKeyFileSet(this.storage);
            TestHelpers.LoadToStorage(@"..\..\SampleReducer.cs", reducerCodeFileName, this.storage);
            var worker = new ThreadWorker(this.storage, 1);

            worker.Reduce(Base64.Encode("k1"), reducerCodeFileName);
            worker.Join();

            var result = default(string);
            var regex = new Regex(string.Format("^" + Core.Properties.Settings.Default.ReduceOutputFileName + "$", @"(?<Key>.+)", "[0-9]+", RegexExtensions.GuidRegexString));
            var uris = this.storage.ListFiles();
            foreach (var uri in uris)
            {
                var fileName = this.storage.GetFileName(uri);
                if (regex.IsMatch(fileName))
                {
                    var key = regex.Match(fileName).Groups["Key"].Value;
                    if (Base64.Decode(key) == "k1")
                    {
                        result = this.storage.Read(fileName);
                    }
                }
            }

            result.ShouldBe("3");
        }
Example #3
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.");
        }
Example #4
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.");
        }
Example #5
0
        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.");
        }
Example #6
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.");
        }
Example #7
0
        public void ThreadWorkerStoresMapResults()
        {
            var fileName = new Uri("file:///file1.txt");
            var fileContent = "whatever am i i";
            var mapperCodeFileName = new Uri("file:///SampleMapper.cs");

            storage.Store(storage.GetFileName(fileName), fileContent);
            TestHelpers.LoadToStorage(@"..\..\SampleMapper.cs", mapperCodeFileName, this.storage);
            var worker = new ThreadWorker(this.storage, 1);

            worker.Map(fileName, mapperCodeFileName);
            worker.Join();

            var fileNo = this.storage.ListFiles().Count();

            fileNo.ShouldBe(6);
        }
Example #8
0
        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.");
        }
Example #9
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.");
        }
Example #10
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.");
        }
Example #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.");
     }
 }
Example #12
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.");
        }
Example #13
0
        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.");
        }
Example #14
0
        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.");
                }
            }
        }