示例#1
0
        public async Task TestAddEmptyKey()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   key             = new LogOnIdentity(String.Empty);
            await knownIdentities.AddAsync(key);

            await knownIdentities.AddAsync(key);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(0), "No key should be in the collection even if added twice, since it is empty.");
        }
示例#2
0
        public async Task TestAddSameKeyTwice()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   key             = new LogOnIdentity("abc");
            await knownIdentities.AddAsync(key);

            await knownIdentities.AddAsync(key);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(1), "Only one key should be in the collection even if added twice.");
            Assert.That(knownIdentities.Identities.First(), Is.EqualTo(key), "The first and only key should be the one just added.");
        }
示例#3
0
        public async Task TestAddTwoNewKnownIdentities()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   key1            = new LogOnIdentity("key1");
            await knownIdentities.AddAsync(key1);

            LogOnIdentity key2 = new LogOnIdentity("key2");
            await knownIdentities.AddAsync(key2);

            Assert.That(knownIdentities.Identities.First(), Is.EqualTo(key2), "The first key should be the last one added.");
            Assert.That(knownIdentities.Identities.Last(), Is.EqualTo(key1), "The last key should be the first one added.");
        }
示例#4
0
        public async Task TestAddNewKnownKey()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   passphrase      = new LogOnIdentity("a");
            await knownIdentities.AddAsync(passphrase);

            Assert.That(knownIdentities.Identities.First(), Is.EqualTo(passphrase), "The first and only key should be the one just added.");
        }
示例#5
0
        public async Task TestAddKeyForKnownIdentity()
        {
            Resolve.FileSystemState.KnownPassphrases.Add(new Passphrase("a"));
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            await knownIdentities.AddAsync(new LogOnIdentity("a"));

            Assert.That(knownIdentities.DefaultEncryptionIdentity.Equals(LogOnIdentity.Empty), "When adding a key that is for a known identity it should not be set as the default.");
        }
示例#6
0
        public async Task TestClear()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   key1            = new LogOnIdentity("key1");
            await knownIdentities.AddAsync(key1);

            LogOnIdentity key2 = new LogOnIdentity("key2");
            await knownIdentities.AddAsync(key2);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(2), "There should be two keys in the collection.");

            await knownIdentities.SetDefaultEncryptionIdentity(LogOnIdentity.Empty);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(0), "There should be zero keys in the collection after Clear().");

            await knownIdentities.SetDefaultEncryptionIdentity(LogOnIdentity.Empty);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(0), "There should be zero keys in the collection after Clear() with zero keys to start with.");
        }
        private Task <FileOperationContext> DecryptFileWork(IDataStore file, IProgressContext progress)
        {
            ActiveFile activeFile = New <FileSystemState>().FindActiveFileFromEncryptedPath(file.FullName);

            if (activeFile != null && activeFile.Status.HasFlag(ActiveFileStatus.AssumedOpenAndDecrypted))
            {
                return(Task.FromResult(new FileOperationContext(file.FullName, ErrorStatus.FileLocked)));
            }

            FileOperationsController operationsController = new FileOperationsController(progress);

            operationsController.QueryDecryptionPassphrase = HandleQueryDecryptionPassphraseEventAsync;

            operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                FileSelectionEventArgs fileSelectionArgs = new FileSelectionEventArgs(new string[] { e.SaveFileFullName })
                {
                    FileSelectionType = FileSelectionType.SaveAsDecrypted,
                };
                OnSelectingFiles(fileSelectionArgs);
                if (fileSelectionArgs.Cancel)
                {
                    e.Cancel = true;
                    return;
                }
                e.SaveFileFullName = fileSelectionArgs.SelectedFiles[0];
            };

            operationsController.KnownKeyAdded = new AsyncDelegateAction <FileOperationEventArgs>(async(FileOperationEventArgs e) =>
            {
                await _knownIdentities.AddAsync(e.LogOnIdentity);
            });

            operationsController.Completed += async(object sender, FileOperationEventArgs e) =>
            {
                if (e.Status.ErrorStatus == ErrorStatus.Success)
                {
                    await New <ActiveFileAction>().RemoveRecentFiles(new IDataStore[] { New <IDataStore>(e.OpenFileFullName) }, progress);
                }
            };

            return(operationsController.DecryptFileAsync(file));
        }
示例#8
0
        public async Task TestChangedEvent()
        {
            bool            wasChanged          = false;
            SessionNotify   notificationMonitor = new SessionNotify();
            KnownIdentities knownIdentities     = new KnownIdentities(Resolve.FileSystemState, notificationMonitor);

            notificationMonitor.AddCommand((SessionNotification notification) =>
            {
                wasChanged |= notification.NotificationType == SessionNotificationType.KnownKeyChange;
                return(Constant.CompletedTask);
            });
            LogOnIdentity key1 = new LogOnIdentity("abc");
            await knownIdentities.AddAsync(key1);

            Assert.That(wasChanged, Is.True, "A new key should trigger the Changed event.");
            wasChanged = false;
            await knownIdentities.AddAsync(key1);

            Assert.That(wasChanged, Is.False, "Re-adding an existing key should not trigger the Changed event.");
        }
示例#9
0
        public async Task TestSettingNullDefaultEncryptionKey()
        {
            KnownIdentities knownIdentities = new KnownIdentities(Resolve.FileSystemState, Resolve.SessionNotify);
            LogOnIdentity   key1            = new LogOnIdentity("a");
            await knownIdentities.AddAsync(key1);

            LogOnIdentity key2 = new LogOnIdentity("B");
            await knownIdentities.SetDefaultEncryptionIdentity(key2);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(2), "Setting the DefaultEncryptionKey should also add it as a known key.");

            await knownIdentities.SetDefaultEncryptionIdentity(LogOnIdentity.Empty);

            Assert.That(knownIdentities.Identities.Count(), Is.EqualTo(0), "Setting the DefaultEncryptionKey to empty should clear the known keys.");
        }
示例#10
0
        private async Task <LogOnIdentity> AddKnownIdentityFromEventAsync(LogOnEventArgs logOnArgs)
        {
            if (logOnArgs.Cancel || logOnArgs.Passphrase == Passphrase.Empty)
            {
                return(LogOnIdentity.Empty);
            }

            _userSettings.DisplayEncryptPassphrase = logOnArgs.DisplayPassphrase;

            Passphrase    passphrase = logOnArgs.Passphrase;
            LogOnIdentity identity   = await LogOnIdentityFromCredentialsAsync(EmailAddress.Parse(logOnArgs.UserEmail), passphrase);

            if (identity == LogOnIdentity.Empty)
            {
                identity = new LogOnIdentity(passphrase);
                _fileSystemState.KnownPassphrases.Add(passphrase);
                await _fileSystemState.Save();
            }
            await _knownIdentities.AddAsync(identity);

            return(identity);
        }