Beispiel #1
0
        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.");
        }
Beispiel #2
0
        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.");
        }
Beispiel #3
0
        public static void TestDecryptWithCancelDuringQueryDecryptionPassphrase()
        {
            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.QueryDecryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };
            FileOperationStatus status = controller.DecryptFile(_helloWorldAxxPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation.");
        }
Beispiel #4
0
        private void DecryptFile(string file, IThreadWorker worker, ProgressContext progress)
        {
            FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress);

            operationsController.QueryDecryptionPassphrase += HandleQueryDecryptionPassphraseEvent;

            operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                string extension = Path.GetExtension(e.SaveFileFullName);
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.AddExtension     = !String.IsNullOrEmpty(extension);
                    sfd.CheckPathExists  = true;
                    sfd.DefaultExt       = extension;
                    sfd.Filter           = Resources.DecryptedSaveAsFileDialogFilterPattern.InvariantFormat(extension);
                    sfd.InitialDirectory = Path.GetDirectoryName(file);
                    sfd.FileName         = Path.GetFileName(e.SaveFileFullName);
                    sfd.OverwritePrompt  = true;
                    sfd.RestoreDirectory = true;
                    sfd.Title            = Resources.DecryptedSaveAsFileDialogTitle;
                    DialogResult result = sfd.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        e.Cancel = true;
                        return;
                    }
                    e.SaveFileFullName = sfd.FileName;
                }
                return;
            };

            operationsController.KnownKeyAdded += (object sender, FileOperationEventArgs e) =>
            {
                persistentState.Current.KnownKeys.Add(e.Key);
            };

            operationsController.Completed += (object sender, FileOperationEventArgs e) =>
            {
                if (CheckStatusAndShowMessage(e.Status, e.OpenFileFullName))
                {
                    persistentState.Current.RemoveRecentFiles(new string[] { e.OpenFileFullName }, progress);
                }
            };

            operationsController.DecryptFile(file, worker);
        }
Beispiel #5
0
        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.");
        }
Beispiel #6
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.");
        }
Beispiel #7
0
        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.");
        }
Beispiel #8
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.");
        }
Beispiel #9
0
        public static void TestDecryptWithAlternativeDestinationName()
        {
            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.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "Other Hello World.txt");
            };
            string destinationPath = String.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };
            FileOperationStatus status = controller.DecryptFile(_helloWorldAxxPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The status should indicate success.");

            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);
            string           fileContent;

            using (Stream stream = destinationInfo.OpenRead())
            {
                fileContent = new StreamReader(stream).ReadToEnd();
            }
            Assert.That(fileContent.Contains("Hello"), "A file named 'Other Hello World.txt' should contain that text when decrypted.");
        }