コード例 #1
0
        public void TestMakeAxCryptFileName()
        {
            string testFile = _rootPath.PathCombine("Directory", "file.txt");
            string axxFile  = _rootPath.PathCombine("Directory", "file-txt.axx");
            string madeName = AxCryptFile.MakeAxCryptFileName(New <IDataStore>(testFile));

            Assert.That(madeName, Is.EqualTo(axxFile), "The AxCrypt version of the name is unexpected.");
        }
コード例 #2
0
        public void TestInvalidArguments()
        {
            IDataStore       sourceFileInfo      = New <IDataStore>(_testTextPath);
            IDataStore       destinationFileInfo = sourceFileInfo.CreateEncryptedName();
            IAxCryptDocument document            = new V1AxCryptDocument();
            IDataStore       decryptedFileInfo   = New <IDataStore>(Path.Combine(_rootPath, "decrypted test.txt"));

            IAxCryptDocument     nullDocument             = null;
            IDataStore           nullFileInfo             = null;
            LogOnIdentity        nullKey                  = null;
            ProgressContext      nullProgress             = null;
            EncryptionParameters nullEncryptionParameters = null;
            Stream nullStream = null;
            string nullString = null;
            Func <Stream, Task> nullStreamAction = null;
            FileLock            nullFileLock     = null;

            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(nullFileInfo, destinationFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, nullFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, destinationFileInfo, nullEncryptionParameters, AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, destinationFileInfo, new EncryptionParameters(new V1Aes128CryptoFactory().CryptoId, new Passphrase("axcrypt")), AxCryptOptions.EncryptWithCompression, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(nullFileInfo, new MemoryStream(), EncryptionParameters.Empty, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, nullStream, EncryptionParameters.Empty, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, new MemoryStream(), nullEncryptionParameters, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { new AxCryptFile().Encrypt(sourceFileInfo, new MemoryStream(), EncryptionParameters.Empty, AxCryptOptions.None, nullProgress); });

            using (FileLock fileInfoLock = New <FileLocker>().Acquire(New <IDataStore>(_testTextPath)))
            {
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullDocument, fileInfoLock, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, nullFileLock, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
                Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, fileInfoLock, AxCryptOptions.SetFileTimes, nullProgress); }));
            }

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullFileInfo, decryptedFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, nullFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, decryptedFileInfo, nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, decryptedFileInfo, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, nullProgress); }));

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(nullFileInfo, Path.Combine(_rootPath, "Directory"), LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, nullString, LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), LogOnIdentity.Empty, AxCryptOptions.SetFileTimes, nullProgress); }));

            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(nullFileInfo, LogOnIdentity.Empty, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(sourceFileInfo, nullKey, new ProgressContext()); }));
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Document(sourceFileInfo, LogOnIdentity.Empty, nullProgress); }));

            Assert.ThrowsAsync <ArgumentNullException>((async() => { await New <AxCryptFile>().EncryptToFileWithBackupAsync(null, async(Stream stream) => { await Task.Delay(0); }, new ProgressContext()); }));
            using (FileLock fileInfoLock = New <FileLocker>().Acquire(New <IDataStore>(_testTextPath)))
            {
                Assert.ThrowsAsync <ArgumentNullException>((async() => { await New <AxCryptFile>().EncryptToFileWithBackupAsync(fileInfoLock, nullStreamAction, new ProgressContext()); }));
            }

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.MakeAxCryptFileName(nullFileInfo); });
            Assert.Throws <ArgumentNullException>((TestDelegate)(() => { New <AxCryptFile>().Wipe(nullFileLock, new ProgressContext()); }));
        }
コード例 #3
0
        public static void TestInvalidArguments()
        {
            IRuntimeFileInfo sourceFileInfo      = OS.Current.FileInfo(_testTextPath);
            IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName();
            AxCryptDocument  document            = new AxCryptDocument();
            IRuntimeFileInfo decryptedFileInfo   = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted test.txt"));

            AxCryptDocument  nullDocument     = null;
            IRuntimeFileInfo nullFileInfo     = null;
            AesKey           nullKey          = null;
            ProgressContext  nullProgress     = null;
            Passphrase       nullPassphrase   = null;
            Stream           nullStream       = null;
            string           nullString       = null;
            Action <Stream>  nullStreamAction = null;

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, nullPassphrase, AxCryptOptions.EncryptWithCompression, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullStream, new AesKey(), AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), nullKey, AxCryptOptions.None, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullDocument, decryptedFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, nullFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, decryptedFileInfo, AxCryptOptions.SetFileTimes, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullString, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(nullFileInfo, new AesKey(), new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, nullKey, new ProgressContext()); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, new AesKey(), nullProgress); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(null, (Stream stream) => { }, new ProgressContext()); });
            IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_testTextPath);

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(fileInfo, nullStreamAction, new ProgressContext()); });

            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.MakeAxCryptFileName(nullFileInfo); });
            Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Wipe(nullFileInfo, new ProgressContext()); });
        }
コード例 #4
0
        public async Task TestEncryptFileWhenDestinationExists()
        {
            IDataStore sourceInfo = New <IDataStore>(_davidCopperfieldTxtPath);
            IDataStore expectedDestinationInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceInfo));

            using (Stream stream = expectedDestinationInfo.OpenWrite())
            {
            }

            FileOperationsController controller = new FileOperationsController();
            string        destinationPath       = String.Empty;
            LogOnIdentity logOnIdentity         = null;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.LogOnIdentity = new LogOnIdentity("allan");
            };
            controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx");
            };
            Guid cryptoId = Guid.Empty;

            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
                logOnIdentity   = e.LogOnIdentity;
                cryptoId        = e.CryptoId;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

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

            Assert.That(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed.");
            IDataStore destinationInfo = New <IDataStore>(destinationPath);

            Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created.");

            EncryptionParameters encryptionParameters = new EncryptionParameters(cryptoId, logOnIdentity);
            await encryptionParameters.AddAsync(logOnIdentity.PublicKeys);

            Headers           headers = new Headers();
            AxCryptReaderBase reader  = headers.CreateReader(new LookAheadStream(destinationInfo.OpenRead()));

            using (IAxCryptDocument document = AxCryptReaderBase.Document(reader))
            {
                document.Load(logOnIdentity.Passphrase, cryptoId, headers);
                Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given.");
            }
        }
コード例 #5
0
        private Task <bool> EncryptFilePreparationAsync(IDataStore sourceFileInfo)
        {
            _eventArgs.OpenFileFullName = sourceFileInfo.FullName;

            if (String.Compare(Resolve.Portable.Path().GetExtension(sourceFileInfo.FullName), OS.Current.AxCryptExtension, StringComparison.OrdinalIgnoreCase) == 0)
            {
                _eventArgs.Status = new FileOperationContext(sourceFileInfo.FullName, ErrorStatus.FileAlreadyEncrypted);
                return(Task.FromResult(false));
            }

            using (FileLock fileLock = New <FileLocker>().Acquire(sourceFileInfo))
            {
                if (IsLocked(fileLock))
                {
                    return(Task.FromResult(false));
                }

                IDataStore destinationFileInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceFileInfo));
                _eventArgs.SaveFileFullName = destinationFileInfo.FullName;
                _eventArgs.OpenFileFullName = sourceFileInfo.FullName;
                if (destinationFileInfo.IsAvailable)
                {
                    OnQuerySaveFileAs(_eventArgs);
                    if (_eventArgs.Cancel)
                    {
                        _eventArgs.Status = new FileOperationContext(sourceFileInfo.FullName, ErrorStatus.Canceled);
                        return(Task.FromResult(false));
                    }
                }

                if (Resolve.KnownIdentities.DefaultEncryptionIdentity == LogOnIdentity.Empty)
                {
                    OnQueryEncryptionPassphrase(_eventArgs);
                    if (_eventArgs.Cancel)
                    {
                        _eventArgs.Status = new FileOperationContext(sourceFileInfo.FullName, ErrorStatus.Canceled);
                        return(Task.FromResult(false));
                    }
                }
                else
                {
                    _eventArgs.LogOnIdentity = Resolve.KnownIdentities.DefaultEncryptionIdentity;
                }

                OnQuerySharedPublicKeys(_eventArgs);
            }

            return(Task.FromResult(true));
        }
コード例 #6
0
        public static void TestEncryptFileWhenDestinationExists()
        {
            IRuntimeFileInfo sourceInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath);
            IRuntimeFileInfo expectedDestinationInfo = OS.Current.FileInfo(AxCryptFile.MakeAxCryptFileName(sourceInfo));

            using (Stream stream = expectedDestinationInfo.OpenWrite())
            {
            }

            FileOperationsController controller = new FileOperationsController(_fileSystemState);
            string destinationPath = String.Empty;

            controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) =>
            {
                e.Passphrase = "allan";
            };
            controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx");
            };
            controller.Completed += (object sender, FileOperationEventArgs e) =>
            {
                destinationPath = e.SaveFileFullName;
            };

            FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath);

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

            Assert.That(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed.");
            IRuntimeFileInfo destinationInfo = OS.Current.FileInfo(destinationPath);

            Assert.That(destinationInfo.Exists, "After encryption the destination file should be created.");
            using (AxCryptDocument document = new AxCryptDocument())
            {
                using (Stream stream = destinationInfo.OpenRead())
                {
                    document.Load(stream, new Passphrase("allan").DerivedPassphrase);
                    Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given.");
                }
            }
        }
コード例 #7
0
        public async Task TestEncryptFileWhenCanceledDuringQuerySaveAs()
        {
            IDataStore sourceInfo = New <IDataStore>(_davidCopperfieldTxtPath);
            IDataStore expectedDestinationInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceInfo));

            using (Stream stream = expectedDestinationInfo.OpenWrite())
            {
            }

            FileOperationsController controller = new FileOperationsController();

            controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };

            FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath));

            Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Canceled), "The status should indicate cancellation.");
        }
コード例 #8
0
        public static void TestEncryptFileWhenCanceledDuringQuerySaveAs()
        {
            IRuntimeFileInfo sourceInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath);
            IRuntimeFileInfo expectedDestinationInfo = OS.Current.FileInfo(AxCryptFile.MakeAxCryptFileName(sourceInfo));

            using (Stream stream = expectedDestinationInfo.OpenWrite())
            {
            }

            FileOperationsController controller = new FileOperationsController(_fileSystemState);

            controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) =>
            {
                e.Cancel = true;
            };

            FileOperationStatus status = controller.EncryptFile(_davidCopperfieldTxtPath);

            Assert.That(status, Is.EqualTo(FileOperationStatus.Canceled), "The status should indicate cancellation.");
        }
コード例 #9
0
        private bool EncryptFilePreparation(string fullName)
        {
            if (String.Compare(Path.GetExtension(fullName), OS.Current.AxCryptExtension, StringComparison.OrdinalIgnoreCase) == 0)
            {
                _eventArgs.Status = FileOperationStatus.FileAlreadyEncrypted;
                return(false);
            }
            IRuntimeFileInfo sourceFileInfo      = OS.Current.FileInfo(fullName);
            IRuntimeFileInfo destinationFileInfo = OS.Current.FileInfo(AxCryptFile.MakeAxCryptFileName(sourceFileInfo));

            _eventArgs.SaveFileFullName = destinationFileInfo.FullName;
            _eventArgs.OpenFileFullName = fullName;
            if (destinationFileInfo.Exists)
            {
                OnQuerySaveFileAs(_eventArgs);
                if (_eventArgs.Cancel)
                {
                    _eventArgs.Status = FileOperationStatus.Canceled;
                    return(false);
                }
            }

            if (_fileSystemState.KnownKeys.DefaultEncryptionKey == null)
            {
                OnQueryEncryptionPassphrase(_eventArgs);
                if (_eventArgs.Cancel)
                {
                    _eventArgs.Status = FileOperationStatus.Canceled;
                    return(false);
                }
                Passphrase passphrase = new Passphrase(_eventArgs.Passphrase);
                _eventArgs.Key = passphrase.DerivedPassphrase;
            }
            else
            {
                _eventArgs.Key = _fileSystemState.KnownKeys.DefaultEncryptionKey;
            }

            return(true);
        }
コード例 #10
0
        public static string EncryptedFileName(this string fileName)
        {
            string encryptedFileName = AxCryptFile.MakeAxCryptFileName(fileName);

            return(encryptedFileName);
        }