public static void TestArgumentNull() { using (FileSystemState state = new FileSystemState()) { ActiveFile nullActiveFile = null; string nullPath = null; Func<ActiveFile, ActiveFile> nullAction = null; IRuntimeFileInfo nullFileInfo = null; Assert.Throws<ArgumentNullException>(() => { state.Remove(nullActiveFile); }); Assert.Throws<ArgumentNullException>(() => { state.Add(nullActiveFile); }); Assert.Throws<ArgumentNullException>(() => { state.FindEncryptedPath(nullPath); }); Assert.Throws<ArgumentNullException>(() => { state.FindDecryptedPath(nullPath); }); Assert.Throws<ArgumentNullException>(() => { state.ForEach(ChangedEventMode.RaiseAlways, nullAction); }); Assert.Throws<ArgumentNullException>(() => { state.Load(nullFileInfo); }); } }
public static void TestChangedEvent() { using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); bool wasHere; state.Changed += new EventHandler<ActiveFileChangedEventArgs>((object sender, ActiveFileChangedEventArgs e) => { wasHere = true; }); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted, null); wasHere = false; state.Add(activeFile); Assert.That(state.ActiveFiles.Count(), Is.EqualTo(1), "After the Add() the state should have one active file."); Assert.That(wasHere, Is.True, "After the Add(), the changed event should have been raised."); wasHere = false; state.Remove(activeFile); Assert.That(wasHere, Is.True, "After the Remove(), the changed event should have been raised."); Assert.That(state.ActiveFiles.Count(), Is.EqualTo(0), "After the Remove() the state should have no active files."); } }
public static void TestLoadExisting() { ActiveFile activeFile; using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); Assert.That(state, Is.Not.Null, "An instance should always be instantiated."); Assert.That(state.ActiveFiles.Count(), Is.EqualTo(0), "A new state should not have any active files."); activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted, null); state.Add(activeFile); state.Save(); } using (FileSystemState reloadedState = new FileSystemState()) { reloadedState.Load(OS.Current.FileInfo(_mystateXmlPath)); Assert.That(reloadedState, Is.Not.Null, "An instance should always be instantiated."); Assert.That(reloadedState.ActiveFiles.Count(), Is.EqualTo(1), "The reloaded state should have one active file."); Assert.That(reloadedState.ActiveFiles.First().ThumbprintMatch(activeFile.Key), Is.True, "The reloaded thumbprint should match the key."); } }
public static void TestStatusMaskAtLoad() { using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, null); state.Add(activeFile); state.Save(); FileSystemState reloadedState = new FileSystemState(); reloadedState.Load(OS.Current.FileInfo(_mystateXmlPath)); Assert.That(reloadedState, Is.Not.Null, "An instance should always be instantiated."); Assert.That(reloadedState.ActiveFiles.Count(), Is.EqualTo(1), "The reloaded state should have one active file."); Assert.That(reloadedState.ActiveFiles.First().Status, Is.EqualTo(ActiveFileStatus.AssumedOpenAndDecrypted), "When reloading saved state, some statuses should be masked away."); } }
public static void TestInvalidXml() { string badXml = @"<FileSystemState xmlns=""http://www.axantum.com/Serialization/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">"; IRuntimeFileInfo stateInfo = OS.Current.FileInfo(_mystateXmlPath); using (Stream stream = stateInfo.OpenWrite()) { byte[] bytes = Encoding.UTF8.GetBytes(badXml); stream.Write(bytes, 0, bytes.Length); } using (FileSystemState state = new FileSystemState()) { Assert.DoesNotThrow(() => state.Load(stateInfo)); Assert.That(state.ActiveFileCount, Is.EqualTo(0), "After loading damaged state, the count should be zero."); ActiveFile decryptedFile1 = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted, null); state.Add(decryptedFile1); Assert.That(state.ActiveFileCount, Is.EqualTo(1), "After adding a file, the count should be one."); } }
public static void TestForEach() { bool changedEventWasRaised = false; using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); state.Changed += ((object sender, ActiveFileChangedEventArgs e) => { changedEventWasRaised = true; }); ActiveFile activeFile; activeFile = new ActiveFile(OS.Current.FileInfo(_encrypted1AxxPath), OS.Current.FileInfo(_decrypted1TxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, null); state.Add(activeFile); activeFile = new ActiveFile(OS.Current.FileInfo(_encrypted2AxxPath), OS.Current.FileInfo(_decrypted2TxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, null); state.Add(activeFile); activeFile = new ActiveFile(OS.Current.FileInfo(_encrypted3AxxPath), OS.Current.FileInfo(_decrypted3TxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, null); state.Add(activeFile); Assert.That(changedEventWasRaised, Is.True, "The change event should have been raised by the adding of active files."); changedEventWasRaised = false; Assert.That(state.ActiveFiles.Count(), Is.EqualTo(3), "There should be three."); int i = 0; state.ForEach(ChangedEventMode.RaiseOnlyOnModified, (ActiveFile activeFileArgument) => { ++i; return activeFileArgument; }); Assert.That(i, Is.EqualTo(3), "The iteration should have visited three active files."); Assert.That(changedEventWasRaised, Is.False, "No change event should have been raised."); i = 0; state.ForEach(ChangedEventMode.RaiseAlways, (ActiveFile activeFileArgument) => { ++i; return activeFileArgument; }); Assert.That(i, Is.EqualTo(3), "The iteration should have visited three active files."); Assert.That(changedEventWasRaised, Is.True, "The change event should have been raised."); changedEventWasRaised = false; i = 0; state.ForEach(ChangedEventMode.RaiseAlways, (ActiveFile activeFileArgument) => { ++i; return new ActiveFile(activeFileArgument, activeFile.Status | ActiveFileStatus.Error); }); Assert.That(i, Is.EqualTo(3), "The iteration should have visited three active files."); Assert.That(changedEventWasRaised, Is.True, "The change event should have been raised."); } }
public static void TestFindEncryptedAndDecryptedPath() { using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, null); state.Add(activeFile); ActiveFile byDecryptedPath = state.FindDecryptedPath(_decryptedTxtPath); Assert.That(byDecryptedPath, Is.EqualTo(activeFile), "The search should return the same instance."); ActiveFile byEncryptedPath = state.FindEncryptedPath(_encryptedAxxPath); Assert.That(byEncryptedPath, Is.EqualTo(byDecryptedPath), "The search should return the same instance."); ActiveFile notFoundEncrypted = state.FindEncryptedPath(Path.Combine(_rootPath, "notfoundfile.txt")); Assert.That(notFoundEncrypted, Is.Null, "A search that does not succeed should return null."); ActiveFile notFoundDecrypted = state.FindDecryptedPath(Path.Combine(_rootPath, "notfoundfile.txt")); Assert.That(notFoundDecrypted, Is.Null, "A search that does not succeed should return null."); } }
public static void TestDecryptedActiveFiles() { using (FileSystemState state = new FileSystemState()) { state.Load(OS.Current.FileInfo(_mystateXmlPath)); ActiveFile decryptedFile1 = new ActiveFile(OS.Current.FileInfo(_encryptedAxxPath), OS.Current.FileInfo(_decryptedTxtPath), new AesKey(), ActiveFileStatus.AssumedOpenAndDecrypted, null); state.Add(decryptedFile1); ActiveFile decryptedFile2 = new ActiveFile(OS.Current.FileInfo(_encrypted2AxxPath), OS.Current.FileInfo(_decrypted2TxtPath), new AesKey(), ActiveFileStatus.DecryptedIsPendingDelete, null); state.Add(decryptedFile2); ActiveFile notDecryptedFile = new ActiveFile(OS.Current.FileInfo(_encrypted3AxxPath), OS.Current.FileInfo(_decrypted3TxtPath), new AesKey(), ActiveFileStatus.NotDecrypted, null); state.Add(notDecryptedFile); ActiveFile errorFile = new ActiveFile(OS.Current.FileInfo(_encrypted4AxxPath), OS.Current.FileInfo(_decrypted4TxtPath), new AesKey(), ActiveFileStatus.Error, null); state.Add(errorFile); IList<ActiveFile> decryptedFiles = state.DecryptedActiveFiles; Assert.That(decryptedFiles.Count, Is.EqualTo(2), "There should be two decrypted files."); Assert.That(decryptedFiles.Contains(decryptedFile1), "A file marked as AssumedOpenAndDecrypted should be found."); Assert.That(decryptedFiles.Contains(decryptedFile2), "A file marked as DecryptedIsPendingDelete should be found."); Assert.That(decryptedFiles.Contains(notDecryptedFile), Is.Not.True, "A file marked as NotDecrypted should not be found."); } }
private static FileOperationStatus LaunchApplicationForDocument(FileSystemState fileSystemState, ActiveFile destinationActiveFile) { ILauncher process; try { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Starting process for '{0}'".InvariantFormat(destinationActiveFile.DecryptedFileInfo.FullName)); } process = OS.Current.Launch(destinationActiveFile.DecryptedFileInfo.FullName); if (process.WasStarted) { process.Exited += new EventHandler(process_Exited); } else { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Starting process for '{0}' did not start a process, assumed handled by the shell.".InvariantFormat(destinationActiveFile.DecryptedFileInfo.FullName)); } } } catch (Win32Exception w32ex) { if (OS.Log.IsErrorEnabled) { OS.Log.LogError("Could not launch application for '{0}', Win32Exception was '{1}'.".InvariantFormat(destinationActiveFile.DecryptedFileInfo.FullName, w32ex.Message)); } return FileOperationStatus.CannotStartApplication; } if (OS.Log.IsWarningEnabled) { if (process.HasExited) { OS.Log.LogWarning("The process seems to exit immediately for '{0}'".InvariantFormat(destinationActiveFile.DecryptedFileInfo.FullName)); } } if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Launched and opened '{0}'.".InvariantFormat(destinationActiveFile.DecryptedFileInfo.FullName)); } destinationActiveFile = new ActiveFile(destinationActiveFile, ActiveFileStatus.AssumedOpenAndDecrypted, process); fileSystemState.Add(destinationActiveFile); fileSystemState.Save(); return FileOperationStatus.Success; }