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 TestAddRemoveProgressingEvent() { CancelProgressContext progress = new CancelProgressContext(new ProgressContext(TimeSpan.Zero)); int percent = -1; EventHandler <ProgressEventArgs> handler = (object sender, ProgressEventArgs e) => { percent = e.Percent; }; progress.Progressing += handler; progress.AddTotal(200); progress.AddCount(100); Assert.That(percent, Is.EqualTo(50), "When halfway, the percent should be 50."); FakeRuntimeEnvironment.Instance.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); progress.AddCount(50); Assert.That(percent, Is.EqualTo(75), "150 of 200 is 75%."); progress.Progressing -= handler; FakeRuntimeEnvironment.Instance.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); progress.AddCount(50); Assert.That(percent, Is.EqualTo(75), "Since the event handler was removed, percent should stay the same."); }
public static void TestAllItemsConfirmed() { ProgressContext progressContext = new ProgressContext(TimeSpan.Zero); CancelProgressContext cancelContext = new CancelProgressContext(progressContext); Assert.That(progressContext.AllItemsConfirmed, Is.False, "The default value is false."); Assert.That(cancelContext.AllItemsConfirmed, Is.EqualTo(progressContext.AllItemsConfirmed), "The cancel context should have the same value as the progressContext."); cancelContext.AllItemsConfirmed = true; Assert.That(progressContext.AllItemsConfirmed, Is.True, "The progressContext should reflect the setting of the cancelContext."); Assert.That(cancelContext.AllItemsConfirmed, Is.EqualTo(progressContext.AllItemsConfirmed), "The cancel context should have the same value as the progressContext."); }
public static void TestCancelAddCount() { IProgressContext progress = new ProgressContext(); progress.NotifyLevelStart(); progress.AddTotal(100); progress.AddCount(10); progress = new CancelProgressContext(progress); Assert.DoesNotThrow(() => { progress.NotifyLevelStart(); }); Assert.DoesNotThrow(() => { progress.AddTotal(50); }); progress.Cancel = true; Assert.Throws <OperationCanceledException>(() => { progress.AddCount(10); }); Assert.DoesNotThrow(() => { progress.NotifyLevelFinished(); }); }
public void TestWipeWithDelayedUntilDoneCancel() { IDataStore fileInfo = New <IDataStore>(_davidCopperfieldTxtPath); IProgressContext progress = new CancelProgressContext(new ProgressContext(TimeSpan.Zero)); progress.Progressing += (object sender, ProgressEventArgs e) => { ((IProgressContext)sender).Cancel = true; }; using (FileLock fileLock = New <FileLocker>().Acquire(fileInfo)) { Assert.Throws <OperationCanceledException>((TestDelegate)(() => { New <AxCryptFile>().Wipe(fileLock, progress); })); } Assert.That(!fileInfo.IsAvailable, "The file should be completely wiped, even if canceled at start."); }
public static void TestRemoveCount() { ProgressContext progressContext = new ProgressContext(TimeSpan.Zero); CancelProgressContext cancelContext = new CancelProgressContext(progressContext); int percent = 0; cancelContext.Progressing += (sender, e) => { percent = e.Percent; }; cancelContext.AddTotal(100); cancelContext.AddCount(10); Assert.That(percent, Is.EqualTo(10), "10 of 100 is 10%."); cancelContext.RemoveCount(50, 10); FakeRuntimeEnvironment.Instance.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(1000); cancelContext.AddCount(10); Assert.That(percent, Is.EqualTo(20), "10 of 50 is 20%."); }
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); }); } } }