public async Task TestForEach()
        {
            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                ActiveFile activeFile;
                activeFile = new ActiveFile(New <IDataStore>(_encrypted1AxxPath), New <IDataStore>(_decrypted1TxtPath), new LogOnIdentity("passphrase1"), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);
                activeFile = new ActiveFile(New <IDataStore>(_encrypted2AxxPath), New <IDataStore>(_decrypted2TxtPath), new LogOnIdentity("passphrase2"), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);
                activeFile = new ActiveFile(New <IDataStore>(_encrypted3AxxPath), New <IDataStore>(_decrypted3TxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);

                Assert.That(state.ActiveFiles.Count(), Is.EqualTo(3), "There should be three.");
                int i = 0;
                await state.ForEach((ActiveFile activeFileArgument) =>
                {
                    ++i;
                    return(Task.FromResult(activeFileArgument));
                });

                Assert.That(i, Is.EqualTo(3), "The iteration should have visited three active files.");

                i = 0;
                await state.ForEach((ActiveFile activeFileArgument) =>
                {
                    ++i;
                    return(Task.FromResult(new ActiveFile(activeFileArgument, activeFile.Status | ActiveFileStatus.Error)));
                });

                Assert.That(i, Is.EqualTo(3), "The iteration should have visited three active files.");
            }
        }
Beispiel #2
0
        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.");
            }
        }
Beispiel #3
0
        public virtual async Task <FileOperationContext> OpenAndLaunchApplication(IEnumerable <LogOnIdentity> identities, IDataStore encryptedDataStore, IProgressContext progress)
        {
            if (identities == null)
            {
                throw new ArgumentNullException(nameof(identities));
            }
            if (encryptedDataStore == null)
            {
                throw new ArgumentNullException(nameof(encryptedDataStore));
            }
            if (progress == null)
            {
                throw new ArgumentNullException(nameof(progress));
            }

            if (!encryptedDataStore.IsAvailable)
            {
                if (Resolve.Log.IsWarningEnabled)
                {
                    Resolve.Log.LogWarning("Tried to open non-existing '{0}'.".InvariantFormat(encryptedDataStore.FullName));
                }
                return(new FileOperationContext(encryptedDataStore.FullName, ErrorStatus.FileDoesNotExist));
            }

            ActiveFile activeFile = _fileSystemState.FindActiveFileFromEncryptedPath(encryptedDataStore.FullName);

            if (activeFile == null || !activeFile.DecryptedFileInfo.IsAvailable)
            {
                activeFile = TryDecryptToActiveFile(encryptedDataStore, identities);
            }
            else
            {
                activeFile = CheckKeysForAlreadyDecryptedFile(activeFile, identities, progress);
            }

            if (activeFile == null)
            {
                return(new FileOperationContext(encryptedDataStore.FullName, ErrorStatus.InvalidKey));
            }

            using (FileLock destinationLock = New <FileLocker>().Acquire(activeFile.DecryptedFileInfo))
            {
                if (!activeFile.DecryptedFileInfo.IsAvailable)
                {
                    activeFile = Decrypt(activeFile.Identity, activeFile.EncryptedFileInfo, destinationLock, activeFile, progress);
                }
                _fileSystemState.Add(activeFile);
                await _fileSystemState.Save();

                if (encryptedDataStore.IsWriteProtected || !New <LicensePolicy>().Capabilities.Has(LicenseCapability.EditExistingFiles))
                {
                    activeFile.DecryptedFileInfo.IsWriteProtected = true;
                }

                FileOperationContext status = await LaunchApplicationForDocument(activeFile, destinationLock);

                return(status);
            }
        }
Beispiel #4
0
        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.");
            }
        }
Beispiel #5
0
        public static void DecryptAndOpenFile(IRuntimeFileInfo encryptedDocument, Passphrase passphrase, ProgressContext progress, Action <string, ProgressContext> failure = null)
        {
            string tempPath = Path.GetTempPath();
            string decryptedFileName;

            lastUsedKey = passphrase.DerivedPassphrase;

            if (!TryDecrypt(encryptedDocument, tempPath, lastUsedKey, progress, out decryptedFileName))
            {
                failure("Could not open file", progress);
                return;
            }

            string           fullPathToDecryptedFile = Path.Combine(tempPath, decryptedFileName);
            IRuntimeFileInfo decryptedFile           = OS.Current.FileInfo(fullPathToDecryptedFile);

            NSDictionary   userInfo     = new NSDictionary(Launcher.TargetFileUserInfoKey, decryptedFile.FullName);
            NSNotification notification = NSNotification.FromName(Launcher.FileDecryptedNotification, new NSObject(), userInfo);

            NSNotificationCenter.DefaultCenter.PostNotification(notification);

            ILauncher launcher = OS.Current.Launch(fullPathToDecryptedFile);

            launcher.Exited += (sender, e) => {
                fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext());
            };

            fileSystemState.Add(new ActiveFile(encryptedDocument, decryptedFile, lastUsedKey, ActiveFileStatus.AssumedOpenAndDecrypted, launcher));
            //fileSystemState.Save ();
        }
Beispiel #6
0
        public static FileOperationStatus OpenAndLaunchApplication(this FileSystemState fileSystemState, string file, IEnumerable <AesKey> keys, ProgressContext progress)
        {
            if (fileSystemState == null)
            {
                throw new ArgumentNullException("fileSystemState");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }
            if (progress == null)
            {
                throw new ArgumentNullException("progress");
            }

            IRuntimeFileInfo fileInfo = OS.Current.FileInfo(file);

            if (!fileInfo.Exists)
            {
                if (OS.Log.IsWarningEnabled)
                {
                    OS.Log.LogWarning("Tried to open non-existing '{0}'.".InvariantFormat(fileInfo.FullName));
                }
                return(FileOperationStatus.FileDoesNotExist);
            }

            ActiveFile destinationActiveFile = fileSystemState.FindEncryptedPath(fileInfo.FullName);

            if (destinationActiveFile == null || !destinationActiveFile.DecryptedFileInfo.Exists)
            {
                IRuntimeFileInfo destinationFolderInfo = GetTemporaryDestinationFolder(destinationActiveFile);
                destinationActiveFile = TryDecrypt(fileInfo, destinationFolderInfo, keys, progress);
            }
            else
            {
                destinationActiveFile = CheckKeysForAlreadyDecryptedFile(destinationActiveFile, keys, progress);
            }

            if (destinationActiveFile == null)
            {
                return(FileOperationStatus.InvalidKey);
            }

            fileSystemState.Add(destinationActiveFile);
            fileSystemState.Save();

            FileOperationStatus status = LaunchApplicationForDocument(fileSystemState, destinationActiveFile);

            return(status);
        }
        private Task <FileOperationContext> EncryptFileWorkOneAsync(IDataStore file, IProgressContext progress)
        {
            FileOperationsController controller = EncryptFileWorkController(progress);

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                if (e.Status.ErrorStatus == ErrorStatus.Success)
                {
                    IDataStore encryptedInfo = New <IDataStore>(e.SaveFileFullName);
                    IDataStore decryptedInfo = New <IDataStore>(FileOperation.GetTemporaryDestinationName(e.OpenFileFullName));
                    ActiveFile activeFile    = new ActiveFile(encryptedInfo, decryptedInfo, e.LogOnIdentity, ActiveFileStatus.NotDecrypted, e.CryptoId);
                    _fileSystemState.Add(activeFile);
                }
                if (e.Status.ErrorStatus == ErrorStatus.FileAlreadyEncrypted)
                {
                    e.Status = new FileOperationContext(string.Empty, ErrorStatus.Success);
                }
            };
            return(controller.EncryptFileAsync(file));
        }
Beispiel #8
0
        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);
        }
        public void TestFindEncryptedPath()
        {
            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                ActiveFile activeFile = new ActiveFile(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);

                ActiveFile byEncryptedPath = state.FindActiveFileFromEncryptedPath(_encryptedAxxPath);
                Assert.That(byEncryptedPath.EncryptedFileInfo.FullName, Is.EqualTo(_encryptedAxxPath), "The search should return the same path.");

                ActiveFile notFoundEncrypted = state.FindActiveFileFromEncryptedPath(Path.Combine(_rootPath, "notfoundfile.txt"));
                Assert.That(notFoundEncrypted, Is.Null, "A search that does not succeed should return null.");
            }
        }
        public void TestDecryptedActiveFiles()
        {
            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                ActiveFile decryptedFile1 = new ActiveFile(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase1"), ActiveFileStatus.AssumedOpenAndDecrypted, new V1Aes128CryptoFactory().CryptoId);
                state.Add(decryptedFile1);

                ActiveFile decryptedFile2 = new ActiveFile(New <IDataStore>(_encrypted2AxxPath), New <IDataStore>(_decrypted2TxtPath), new LogOnIdentity("passphrase2"), ActiveFileStatus.DecryptedIsPendingDelete, new V1Aes128CryptoFactory().CryptoId);
                state.Add(decryptedFile2);

                ActiveFile notDecryptedFile = new ActiveFile(New <IDataStore>(_encrypted3AxxPath), New <IDataStore>(_decrypted3TxtPath), new LogOnIdentity("passphrase3"), ActiveFileStatus.NotDecrypted, new V1Aes128CryptoFactory().CryptoId);
                state.Add(notDecryptedFile);

                ActiveFile errorFile = new ActiveFile(New <IDataStore>(_encrypted4AxxPath), New <IDataStore>(_decrypted4TxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.Error, new V1Aes128CryptoFactory().CryptoId);
                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.");
            }
        }
        public async Task TestStatusMaskAtLoad()
        {
            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                FakeDataStore.AddFile(_encryptedAxxPath, FakeDataStore.TestDate1Utc, FakeDataStore.TestDate2Utc, FakeDataStore.TestDate3Utc, new MemoryStream(Resources.helloworld_key_a_txt));
                ActiveFile activeFile = new ActiveFile(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted | ActiveFileStatus.Error | ActiveFileStatus.IgnoreChange | ActiveFileStatus.NotShareable, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);
                await state.Save();

                FileSystemState reloadedState = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt"));
                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 | ActiveFileStatus.NoProcessKnown), "When reloading saved state, some statuses should be masked away and NoProcessKnown added.");
            }
        }
Beispiel #12
0
        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 void TestArgumentNull()
        {
            using (FileSystemState state = new FileSystemState())
            {
                ActiveFile nullActiveFile = null;
                string     nullPath       = null;
                Func <ActiveFile, Task <ActiveFile> > nullAction = null;
                IDataStore nullFileInfo = null;

                Assert.Throws <ArgumentNullException>(() => { state.RemoveActiveFile(nullActiveFile); });
                Assert.Throws <ArgumentNullException>(() => { state.Add(nullActiveFile); });
                Assert.Throws <ArgumentNullException>(() => { state.FindActiveFileFromEncryptedPath(nullPath); });
                Assert.ThrowsAsync <ArgumentNullException>(async() => { await state.ForEach(nullAction); });
                Assert.Throws <ArgumentNullException>(() => { FileSystemState.Create(nullFileInfo); });
            }
        }
Beispiel #14
0
        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 TestFileContainedByActiveFilesButNotDecrypted()
        {
            IEnumerable <AesKey> keys = new AesKey[] { new Passphrase("a").DerivedPassphrase };

            FileOperationStatus status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext());

            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should succeed.");

            IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_helloWorldAxxPath);
            ActiveFile       destinationActiveFile = _fileSystemState.FindEncryptedPath(fileInfo.FullName);

            destinationActiveFile.DecryptedFileInfo.Delete();
            destinationActiveFile = new ActiveFile(destinationActiveFile, ActiveFileStatus.NotDecrypted);
            _fileSystemState.Add(destinationActiveFile);
            _fileSystemState.Save();

            status = _fileSystemState.OpenAndLaunchApplication(_helloWorldAxxPath, keys, new ProgressContext());
            Assert.That(status, Is.EqualTo(FileOperationStatus.Success), "The launch should once again succeed.");
        }
        public async Task TestWatchedFolderChanged()
        {
            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                FakeDataStore.AddFolder(_rootPath);
                await state.AddWatchedFolderAsync(new WatchedFolder(_rootPath, IdentityPublicTag.Empty));

                Assert.That(state.ActiveFileCount, Is.EqualTo(0));

                FakeDataStore.AddFile(_encryptedAxxPath, new MemoryStream(Resources.helloworld_key_a_txt));
                Assert.That(state.ActiveFileCount, Is.EqualTo(0));

                state.Add(new ActiveFile(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.NotDecrypted, new V1Aes128CryptoFactory().CryptoId));
                Assert.That(state.ActiveFileCount, Is.EqualTo(1));

                New <IDataStore>(_encryptedAxxPath).Delete();
                Assert.That(state.ActiveFileCount, Is.EqualTo(0), "When deleted, the active file count should be zero.");
            }
        }
Beispiel #17
0
        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 async Task TestLoadExistingWithNonExistingFile()
        {
            ActiveFile activeFile;

            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                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(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);
                await state.Save();
            }

            using (FileSystemState reloadedState = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                Assert.That(reloadedState, Is.Not.Null, "An instance should always be instantiated.");
                Assert.That(reloadedState.ActiveFiles.Count(), Is.EqualTo(0), "The reloaded state should not have an active file, since it is non-existing.");
            }
        }
Beispiel #19
0
        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.");
            }
        }
Beispiel #20
0
        public static FileOperationStatus OpenAndLaunchApplication(this FileSystemState fileSystemState, string file, AxCryptDocument document, ProgressContext progress)
        {
            if (fileSystemState == null)
            {
                throw new ArgumentNullException("fileSystemState");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
            if (progress == null)
            {
                throw new ArgumentNullException("progress");
            }

            IRuntimeFileInfo fileInfo = OS.Current.FileInfo(file);

            ActiveFile destinationActiveFile = fileSystemState.FindEncryptedPath(fileInfo.FullName);

            if (destinationActiveFile == null || !destinationActiveFile.DecryptedFileInfo.Exists)
            {
                IRuntimeFileInfo destinationFolderInfo = GetTemporaryDestinationFolder(destinationActiveFile);
                destinationActiveFile = DecryptActiveFileDocument(fileInfo, destinationFolderInfo, document, progress);
            }
            else
            {
                destinationActiveFile = new ActiveFile(destinationActiveFile, document.DocumentHeaders.KeyEncryptingKey);
            }

            fileSystemState.Add(destinationActiveFile);
            fileSystemState.Save();

            FileOperationStatus status = LaunchApplicationForDocument(fileSystemState, destinationActiveFile);

            return(status);
        }
        public void TestInvalidJson()
        {
            string badJson = @"{abc:1{}";

            IDataStore stateInfo = Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt");

            using (Stream stream = stateInfo.OpenWrite())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(badJson);
                stream.Write(bytes, 0, bytes.Length);
            }

            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                Assert.That(state.ActiveFileCount, Is.EqualTo(0), "After loading damaged state, the count should be zero.");

                ActiveFile decryptedFile1 = new ActiveFile(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted, new V1Aes128CryptoFactory().CryptoId);
                state.Add(decryptedFile1);

                Assert.That(state.ActiveFileCount, Is.EqualTo(1), "After adding a file, the count should be one.");
            }
        }
Beispiel #22
0
        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 async Task TestLoadExistingWithExistingFile()
        {
            ActiveFile activeFile;

            using (FileSystemState state = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                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(New <IDataStore>(_encryptedAxxPath), New <IDataStore>(_decryptedTxtPath), new LogOnIdentity("passphrase"), ActiveFileStatus.AssumedOpenAndDecrypted, new V1Aes128CryptoFactory().CryptoId);
                state.Add(activeFile);
                await state.Save();
            }

            FakeDataStore.AddFile(_encryptedAxxPath, FakeDataStore.TestDate1Utc, FakeDataStore.TestDate2Utc, FakeDataStore.TestDate3Utc, Stream.Null);

            using (FileSystemState reloadedState = FileSystemState.Create(Resolve.WorkFolder.FileInfo.FileItemInfo("mystate.txt")))
            {
                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.Identity.Passphrase), Is.True, "The reloaded thumbprint should  match the key.");
            }
        }
Beispiel #24
0
        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.");
            }
        }
Beispiel #25
0
        public static void TestCheckActiveFilesIsNotLocked()
        {
            DateTime utcNow       = OS.Current.UtcNow;
            DateTime utcYesterday = utcNow.AddDays(-1);

            FakeRuntimeFileInfo.AddFile(_encryptedFile1, utcNow, utcNow, utcNow, Stream.Null);
            FakeRuntimeFileInfo.AddFile(_decryptedFile1, utcYesterday, utcYesterday, utcYesterday, Stream.Null);

            ActiveFile activeFile = new ActiveFile(OS.Current.FileInfo(_encryptedFile1), OS.Current.FileInfo(_decryptedFile1), new AesKey(), ActiveFileStatus.NotDecrypted, null);

            SetupAssembly.FakeRuntimeEnvironment.TimeFunction = (() => { return(utcNow.AddMinutes(10)); });
            bool changedWasRaised = false;

            _fileSystemState.Add(activeFile);
            _fileSystemState.Changed += ((object sender, ActiveFileChangedEventArgs e) =>
            {
                changedWasRaised = true;
            });
            _fileSystemState.CheckActiveFiles(ChangedEventMode.RaiseOnlyOnModified, new ProgressContext());
            Assert.That(changedWasRaised, Is.True, "The file should be detected as decrypted being created.");
        }