Exemplo n.º 1
0
        public void SetUp()
        {
            // Постарайтесь вынести в SetUp всё неспецифическое конфигурирование так,
            // чтобы в конкретных тестах осталась только специфика теста,
            // без конфигурирования "обычного" сценария работы

            file  = new File("someFile", new byte[] { 1, 2, 3 });
            file2 = new File("someFile2", new byte[] { 1, 2, 3, 4 });
            //var document = new Document(file.Name, file.Content, DateTime.Now, "4.0");
            signedContent  = new byte[] { 1, 7 };
            signedContent2 = new byte[] { 1, 7, 2 };

            cryptographer = A.Fake <ICryptographer>();
            A.CallTo(() => cryptographer.Sign(file.Content, certificate))
            .Returns(signedContent);
            A.CallTo(() => cryptographer.Sign(file2.Content, certificate))
            .Returns(signedContent2);

            sender = A.Fake <ISender>();
            A.CallTo(() => sender.TrySend(signedContent))
            .WithAnyArguments()
            .Returns(true);

            var document  = new Document(file.Name, file.Content, DateTime.Now, "4.0");
            var document2 = new Document(file2.Name, file2.Content, DateTime.Now, "4.0");

            recognizer = A.Fake <IRecognizer>();
            A.CallTo(() => recognizer.TryRecognize(file, out document))
            .Returns(true);
            A.CallTo(() => recognizer.TryRecognize(file2, out document2))
            .Returns(true);

            fileSender = new FileSender(cryptographer, sender, recognizer);
        }
Exemplo n.º 2
0
        public void Send_WhenGoodFormat(string format)
        {
            var document = new Document(file.Name, file.Content, DateTime.Now, format);

            A.CallTo(() => recognizer.TryRecognize(file, out document)).Returns(true);
            A.CallTo(() => cryptographer.Sign(document.Content, certificate)).Returns(signedContent);
            A.CallTo(() => sender.TrySend(signedContent)).Returns(true);

            fileSender.SendFiles(new[] { file }, certificate).SkippedFiles.Should().BeEmpty();
        }
Exemplo n.º 3
0
        public void SetUp()
        {
            // Постарайтесь вынести в SetUp всё неспецифическое конфигурирование так,
            // чтобы в конкретных тестах осталась только специфика теста,
            // без конфигурирования "обычного" сценария работы

            file          = new File("someFile", new byte[] { 1, 2, 3 });
            signedContent = new byte[] { 1, 7 };

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);


            idealDocument = new Document(idealFile.Name, idealFile.Content, idealDate, idealFormat);

            A.CallTo(() => recognizer.TryRecognize(idealFile, out idealDocument))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(idealDocument.Content, certificate))
            .Returns(idealSignedContent);
            A.CallTo(() => sender.TrySend(idealSignedContent))
            .Returns(true);


            fileSender.SendFiles(new[] { idealFile }, certificate)
            .SkippedFiles.Should().BeEmpty();
        }
Exemplo n.º 4
0
        private void PrepareDocument(FileContent content, byte[] signedContent, DateTime created, string format)
        {
            var document = new Document(content.Name, content.Content, created, format);

            A.CallTo(() => recognizer.Recognize(content)).Returns(document);
            A.CallTo(() => cryptographer.Sign(content.Content, certificate)).Returns(signedContent);
        }
Exemplo n.º 5
0
        public void SetUp()
        {
            // Постарайтесь вынести в SetUp всё неспецифическое конфигурирование так,
            // чтобы в конкретных тестах осталась только специфика теста,
            // без конфигурирования "обычного" сценария работы

            file          = new File("someFile", new byte[] { 1, 2, 3 });
            signedContent = new byte[] { 1, 7 };

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);

            document = new Document(file.Name, file.Content, DateTime.Now, "4.0");

            A.CallTo(() => sender.TrySend(signedContent))
            .Returns(true);
            Document _;

            A.CallTo(() => recognizer.TryRecognize(file, out _))
            .Returns(true).AssignsOutAndRefParametersLazily(__ => new[] { document });

            A.CallTo(() => cryptographer.Sign(document.Content, certificate))
            .Returns(signedContent);
        }
Exemplo n.º 6
0
 public IEnumerable <FileSendResult> SendFiles(FileContent[] files, X509Certificate certificate)
 {
     foreach (var file in files)
     {
         string errorMessage = null;
         try
         {
             Document doc = recognizer.Recognize(file);
             if (!IsValidFormatVersion(doc))
             {
                 throw new FormatException("Invalid format version");
             }
             if (!IsValidTimestamp(doc))
             {
                 throw new FormatException("Too old document");
             }
             doc.Content = cryptographer.Sign(doc.Content, certificate);
             sender.Send(doc);
         }
         catch (FormatException e)
         {
             errorMessage = "Can't prepare file to send. " + e.Message;
         }
         catch (InvalidOperationException e)
         {
             errorMessage = "Can't send. " + e.Message;
         }
         yield return(new FileSendResult(file, errorMessage));
     }
 }
Exemplo n.º 7
0
        public void Send_WhenGoodDocument()
        {
            var document = BuildDocument(file);

            A.CallTo(() => recognizer.TryRecognize(file, out document))
            .Returns(true);
            A.CallTo(() => documentChecker.CheckDocument(document))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(document.Content, certificate))
            .Returns(signedContent);
            A.CallTo(() => sender.TrySend(signedContent))
            .Returns(true);

            fileSender.TrySendFile(file, certificate)
            .Should().BeTrue();
        }
Exemplo n.º 8
0
 private Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate)
 {
     return(recognizer.Recognize(file)
            .Then(CheckValidFormatVersion)
            .Then(CheckValidTimestamp)
            .Then(doc => doc.WithContent(cryptographer.Sign(doc.Content, certificate)))
            .RefineError("Can't prepare file to send"));
 }
Exemplo n.º 9
0
        private byte[] GetSigned(Document document)
        {
            var signedContent = GetFileContent();

            A.CallTo(() => cryptographer.Sign(document.Content, certificate))
            .Returns(signedContent);
            return(signedContent);
        }
Exemplo n.º 10
0
        public void SetUp()
        {
            // Постарайтесь вынести в SetUp всё неспецифическое конфигурирование так,
            // чтобы в конкретных тестах осталась только специфика теста,
            // без конфигурирования "обычного" сценария работы

            file          = new File("someFile", new byte[] { 1, 2, 3 });
            signedContent = new byte[] { 1, 7 };

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);

            document = new Document(file.Name, file.Content, DateTime.Now, "4.0");
            A.CallTo(() => recognizer.TryRecognize(file, out document))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(document.Content, certificate))
            .Returns(signedContent);
            A.CallTo(() => sender.TrySend(signedContent))
            .Returns(true);


            invalidFile          = new File("invalidFile", new byte[] { 1, 2, 3 });
            invalidSignedContent = new byte[] { 1, 7 };
            invalidDocument      = new Document(invalidFile.Name, invalidFile.Content, DateTime.Now, "111.222");

            A.CallTo(() => recognizer.TryRecognize(invalidFile, out invalidDocument))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(invalidDocument.Content, certificate))
            .Returns(invalidSignedContent);
            A.CallTo(() => sender.TrySend(invalidSignedContent))
            .Returns(true);


            couldNotSendFile          = new File("invalidFile", new byte[] { 1, 2, 3 });
            couldNotSendSignedContent = new byte[] { 1, 7 };
            couldNotSendDocument      = new Document(couldNotSendFile.Name, couldNotSendFile.Content, DateTime.Now, "4.0");

            A.CallTo(() => recognizer.TryRecognize(couldNotSendFile, out couldNotSendDocument))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(couldNotSendDocument.Content, certificate))
            .Returns(couldNotSendSignedContent);
            A.CallTo(() => sender.TrySend(couldNotSendSignedContent))
            .Returns(false);
        }
Exemplo n.º 11
0
 public Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate)
 {
     return(recognizer.Recognize(file)
            .Then(ValidateFormatIsSupported)
            .Then(ValidateIsNotTooOld)
            .Then(doc => doc.WithContent(cryptographer.Sign(doc.Content, certificate)))
            .RefineError("Can't prepare file to send"));
 }
Exemplo n.º 12
0
        private Document PrepareDocument(FileContent fileToPrepare, byte[] signed, DateTime created, string format)
        {
            var document = new Document(fileToPrepare.Name, fileToPrepare.Content, created, format);

            A.CallTo(() => recognizer.Recognize(fileToPrepare)).Returns(Result.Ok(document));
            A.CallTo(() => cryptographer.Sign(fileToPrepare.Content, certificate)).Returns(signed);
            return(document);
        }
Exemplo n.º 13
0
 private Result <Document> PreparedFileToSend(FileContent file, X509Certificate certificate)
 {
     return(Result
            .Of(() => recognizer.Recognize(file), "Can't recognize")
            .Then(CheckFormatVersion)
            .Then(CheckTimestamp)
            .RefineError("Can't prepare file to send")
            .Then(x => x.WithNewContent(cryptographer.Sign(x.Content, certificate))));
 }
Exemplo n.º 14
0
        public Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate)
        {
            var doc = recognizer.Recognize(file)
                      .Then(d => IsValidFormatVersion(d))
                      .Then(d => IsValidTimestamp(d));

            return(!doc.IsSuccess
                ? doc
                : doc.Value.WithContent(cryptographer.Sign(doc.Value.Content, certificate)));
        }
Exemplo n.º 15
0
        private void SetDefaultConfiguration()
        {
            document = new Document(file.Name, file.Content, DateTime.Now, Format1);

            A.CallTo(() => recognizer.TryRecognize(file, out document))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(document.Content, certificate))
            .Returns(signedContent);
            A.CallTo(() => sender.TrySend(signedContent))
            .Returns(true);
        }
Exemplo n.º 16
0
        public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend()
        {
            var badFile     = new File("badName", new byte[] { 1 });
            var badDocument = new Document(badFile.Name, badFile.Content, DateTime.Now, "4.0");

            A.CallTo(() => recognizer.TryRecognize(badFile, out badDocument))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(badFile.Content, certificate))
            .Returns(badFile.Content);
            A.CallTo(() => sender.TrySend(badFile.Content))
            .Returns(false);

            fileSender.SendFiles(new[] { file, badFile }, certificate)
            .SkippedFiles.Should().BeEquivalentTo(badFile);
        }
Exemplo n.º 17
0
        public bool TrySendFile(File file, X509Certificate certificate)
        {
            Document document;

            if (!recognizer.TryRecognize(file, out document))
            {
                return(false);
            }
            if (!documentChecker.CheckDocument(document))
            {
                return(false);
            }
            var signedContent = cryptographer.Sign(document.Content, certificate);

            return(sender.TrySend(signedContent));
        }
Exemplo n.º 18
0
        private static Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate,
                                                           IRecognizer recognizer, ICryptographer cryptographer, Func <DateTime> now)
        {
            var doc = recognizer.Recognize(file);

            if (!IsValidFormatVersion(doc))
            {
                return(Result.Fail <Document>($"Can't prepare file to send. Invalid format version {doc.Format}"));
            }
            if (!IsValidTimestamp(doc, now))
            {
                return(Result.Fail <Document>($"Can't prepare file to send. Too old document {doc.CreationDate}"));
            }
            doc = doc.SetContent(cryptographer.Sign(doc.Content, certificate));
            return(doc);
        }
Exemplo n.º 19
0
        private Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate)
        {
            var recognizedFile = recognizer.Recognize(file);
            var result         = new Result <Document>(null, recognizedFile);

            if (!IsValidFormatVersion(result.Value))
            {
                result = new Result <Document>("Invalid format version", recognizedFile);
            }
            if (result.IsSuccess && !IsValidTimestamp(result.Value))
            {
                result = new Result <Document>("Too old document", recognizedFile);
            }
            result = result.Then(_ => result.Value.ChangeContent(cryptographer.Sign(result.Value.Content, certificate)));
            return(result.RefineError("Can't prepare file to send"));
        }
Exemplo n.º 20
0
        public void SetUp()
        {
            random = new Random();

            file          = new File("someFile", new byte[] { 1, 2, 3 });
            signedContent = new byte[] { 1, 7 };

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);

            A.CallTo(() => cryptographer.Sign(A <byte[]> .Ignored, certificate))
            .Returns(GenerateFileContent());
            A.CallTo(() => sender.TrySend(A <byte[]> .Ignored))
            .Returns(true);
        }
Exemplo n.º 21
0
        private Result <Document> PrepareFileToSend(FileContent file, X509Certificate certificate)
        {
            var      before = "Can't prepare file to send. ";
            Document doc    = recognizer.Recognize(file);

            if (!IsValidFormatVersion(doc))
            {
                return(Result.Fail <Document>(before + "Invalid format version"));
            }
            //throw new FormatException(before + "Invalid format version");
            if (!IsValidTimestamp(doc))
            {
                return(Result.Fail <Document>(before + "Too old document"));
            }
            doc = doc.ChangeContent(cryptographer.Sign(doc.Content, certificate));
            return(Result.Ok(doc));
        }
Exemplo n.º 22
0
        public void SetUp()
        {
            fileName = "temp.file";
            now      = DateTime.Now;
            random   = new Random();

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);

            A.CallTo(() => cryptographer.Sign(A <byte[]> ._, certificate))
            .Returns(GetFileContent());

            A.CallTo(() => sender.TrySend(A <byte[]> ._))
            .Returns(true);
        }
Exemplo n.º 23
0
        public void SetUp()
        {
            fileName    = "some.txt";
            now         = DateTime.Now;
            certificate = new X509Certificate();

            recognizer = A.Fake <IRecognizer>();

            cryptographer = A.Fake <ICryptographer>();
            A.CallTo(() => cryptographer.Sign(A <byte[]> ._, certificate))
            .ReturnsLazily(GetNewBytes);

            sender = A.Fake <ISender>();
            A.CallTo(() => sender.TrySend(A <byte[]> ._))
            .Returns(true);

            fileSender = new FileSender(cryptographer, sender, recognizer);
        }
Exemplo n.º 24
0
        public void IndependentlySend_WhenSeveralFiles()
        {
            var file1     = new File("someFile1", new byte[] { 1, 2, 4 });
            var file2     = new File("someFile2", new byte[] { 1, 2, 5 });
            var document1 = new Document(file1.Name, file1.Content, DateTime.Now, "5.0");
            var document2 = new Document(file2.Name, file2.Content, DateTime.Now, "4.0");

            A.CallTo(() => recognizer.TryRecognize(file, out document))
            .Returns(false);
            A.CallTo(() => recognizer.TryRecognize(file1, out document1))
            .Returns(true);
            A.CallTo(() => recognizer.TryRecognize(file2, out document2))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(document2.Content, certificate))
            .Returns(signedContent);

            fileSender.SendFiles(new[] { file, file1, file2 }, certificate)
            .SkippedFiles.Should().BeEquivalentTo(file, file1);
        }
Exemplo n.º 25
0
        public void SetUp()
        {
            // Тут мы задаем некоторые известны для всех тестов данные
            // и умолчательные поведения сервисов-заглушек.
            // Наша цель — сделать так, чтобы в конкретных тестах осталась только их специфика,
            // а конфигурирование "обычного" поведения не надо было повторять от теста к тесту
            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);

            var signedContent = Guid.NewGuid().ToByteArray();

            A.CallTo(() => cryptographer.Sign(null, null))
            .WithAnyArguments()
            .Returns(signedContent);
            A.CallTo(() => sender.TrySend(signedContent))
            .Returns(true);
        }
Exemplo n.º 26
0
        private Result <Document> PrepareDocument(FileContent file, X509Certificate certificate)
        {
            var    document     = recognizer.Recognize(file);
            string errorMessage = null;

            if (!IsValidFormatVersion(document))
            {
                errorMessage = $"Can't prepare file to send. This format is not provided {document.Format}";
            }
            if (!IsValidTimestamp(document))
            {
                errorMessage = $"Can't send. This file is too old. Data of creation: {document.Created}";
            }
            if (errorMessage == null)
            {
                return(new Result <Document>(null, document.ChangeContent(cryptographer.Sign(document.Content, certificate))));
            }
            return(new Result <Document>(errorMessage));
        }
Exemplo n.º 27
0
        public void SetUp()
        {
            // Постарайтесь вынести в SetUp всё неспецифическое конфигурирование так,
            // чтобы в конкретных тестах осталась только специфика теста,
            // без конфигурирования "обычного" сценария работы

            file          = new File("someFile", new byte[] { 1, 2, 3 });
            signedContent = new byte[] { 1, 7 };

            cryptographer = A.Fake <ICryptographer>();
            sender        = A.Fake <ISender>();
            recognizer    = A.Fake <IRecognizer>();
            fileSender    = new FileSender(cryptographer, sender, recognizer);
            var goodDocument = new Document(file.Name, file.Content, DateTime.Now, "4.0");

            A.CallTo(() => recognizer.TryRecognize(A <File> .Ignored, out goodDocument))
            .Returns(true);
            A.CallTo(() => cryptographer.Sign(A <byte[]> .Ignored, A <X509Certificate> .Ignored))
            .Returns(signedContent);
            A.CallTo(() => sender.TrySend(A <byte[]> .Ignored))
            .Returns(true);
        }
Exemplo n.º 28
0
 public static Document Sign(this Document doc,
                             ICryptographer cryptographer,
                             X509Certificate certificate)
 {
     return(new Document(doc.Name, cryptographer.Sign(doc.Content, certificate), doc.Created, doc.Format));
 }
Exemplo n.º 29
0
 private Result <Document> SignDocument(Document doc, ICryptographer cryptographer,
                                        X509Certificate certificate)
 {
     return(Result.Of <Document>(() =>
                                 new Document(doc.Name, cryptographer.Sign(doc.Content, certificate), doc.Created, doc.Format)));
 }