<commit_before>using System;
using System.Collections.Generic;
using Core.Interfaces;
using Core.Model;
using Core.SystemWrappers;
using Crypto.Generators;
using Crypto.Mappers;
using Crypto.Providers;
using Moq;
using NUnit.Framework;
using SimpleInjector;
using Ui.Console.Provider;

namespace Ui.Console.Test.Integration
{
    [TestFixture]
    public class CreateSignatureTest
    {
        private string privateKey;
        private byte[] fileContent;
        private Mock<FileWrapper> file;
        private Dictionary<string, string> fileOutput;

        [SetUp]
        public void SetupCreateSignatureTest()
        {
            var random = new SecureRandomGenerator();
            fileContent = random.NextBytes(10000);

            file = new Mock<FileWrapper>();
            fileOutput = new Dictionary<string, string>();

            file = new Mock<FileWrapper>();
            file.Setup(f => f.WriteAllText(It.IsAny<string>(), It.IsAny<string>()))
                .Callback<string, string>((path, content) =>
                {
                    fileOutput.Add(path, content);
                });

            Container container = ContainerProvider.GetContainer();
            container.Register<FileWrapper>(() => file.Object);
        }

        [TearDown]
        public void TeardownCreateSignatureTest()
        {
            ContainerProvider.ClearContainer();
        }

        [TestFixture]
        public class CreateSignatureWithRsaKey : CreateSignatureTest
        {
            private IAsymmetricKeyPair keyPair;

            [SetUp]
            public void Setup()
            {
                var rsaKeyPairGenerator = new RsaKeyPairGenerator(new SecureRandomGenerator());
                var rsaKeyProvider = new RsaKeyProvider(rsaKeyPairGenerator);
                var asymmetricKeyProvider = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), rsaKeyProvider);
                var pkcs8Formatter = new Pkcs8FormattingProvider(asymmetricKeyProvider);

                keyPair = rsaKeyProvider.CreateKeyPair(2048);
                var rsaKey = keyPair.PrivateKey;
                privateKey = pkcs8Formatter.GetAsPem(rsaKey);

                file.Setup(f => f.ReadAllText("private.pem"))
                    .Returns(privateKey);

                file.Setup(f => f.ReadAllBytes("foo.file"))
                    .Returns(fileContent);

                Certifier.Main(new[] {"-c", "signature", "--privatekey", "private.pem", "-f", "foo.file"});
            }

            [Test]
            public void ShouldWriteBase64EncodedSignatureToFile()
            {
                string signature = fileOutput["foo.file.signature"];

                Assert.IsNotEmpty(signature);
                Assert.DoesNotThrow(() => Convert.FromBase64String(signature));
            }

            [Test]
            public void ShouldCreateValidSignature()
            {
                Container container = ContainerProvider.GetContainer();
                var signatureProvider = container.GetInstance<SignatureProvider>();

                string encodedSignature = fileOutput["foo.file.signature"];
                var signature = new Signature
                {
                    Content = Convert.FromBase64String(encodedSignature),
                    SignedData = fileContent
                };

                Assert.IsTrue(signatureProvider.VerifySignature(keyPair.PublicKey, signature));
            }
        }
    }
}<commit_msg>Added integration tests for error cases in signature creation.<commit_after>using System;
using System.Collections.Generic;
using Core.Interfaces;
using Core.Model;
using Core.SystemWrappers;
using Crypto.Generators;
using Crypto.Mappers;
using Crypto.Providers;
using Moq;
using NUnit.Framework;
using SimpleInjector;
using Ui.Console.Provider;

namespace Ui.Console.Test.Integration
{
    [TestFixture]
    public class CreateSignatureTest
    {
        private string privateKey;
        private byte[] fileContent;
        private Mock<FileWrapper> file;
        private Dictionary<string, string> fileOutput;

        [SetUp]
        public void SetupCreateSignatureTest()
        {
            var random = new SecureRandomGenerator();
            fileContent = random.NextBytes(10000);

            file = new Mock<FileWrapper>();
            fileOutput = new Dictionary<string, string>();

            file = new Mock<FileWrapper>();
            file.Setup(f => f.WriteAllText(It.IsAny<string>(), It.IsAny<string>()))
                .Callback<string, string>((path, content) =>
                {
                    fileOutput.Add(path, content);
                });

            Container container = ContainerProvider.GetContainer();
            container.Register<FileWrapper>(() => file.Object);
        }

        [TearDown]
        public void TeardownCreateSignatureTest()
        {
            ContainerProvider.ClearContainer();
        }

        protected IAsymmetricKeyPair SetupWithRsaKey()
        {
            var rsaKeyPairGenerator = new RsaKeyPairGenerator(new SecureRandomGenerator());
            var rsaKeyProvider = new RsaKeyProvider(rsaKeyPairGenerator);
            var asymmetricKeyProvider = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), rsaKeyProvider);
            var pkcs8Formatter = new Pkcs8FormattingProvider(asymmetricKeyProvider);

            var keyPair = rsaKeyProvider.CreateKeyPair(2048);
            var rsaKey = keyPair.PrivateKey;
            privateKey = pkcs8Formatter.GetAsPem(rsaKey);

            file.Setup(f => f.ReadAllText("private.pem"))
                .Returns(privateKey);

            file.Setup(f => f.ReadAllBytes("foo.file"))
                .Returns(fileContent);

            return keyPair;
        }

        [TestFixture]
        public class CreateWithRsaSignature : CreateSignatureTest
        {
            private IAsymmetricKeyPair keyPair;

            [SetUp]
            public void SetupCreateRsaSignature()
            {
                keyPair = SetupWithRsaKey();
            }

            [Test]
            public void ShouldWriteBase64EncodedSignatureToFile()
            {
                Certifier.Main(new[] {"-c", "signature", "--privatekey", "private.pem", "-f", "foo.file"});

                string signature = fileOutput["foo.file.signature"];

                Assert.IsNotEmpty(signature);
                Assert.DoesNotThrow(() => Convert.FromBase64String(signature));
            }

            [Test]
            public void ShouldCreateValidSignature()
            {
                Certifier.Main(new[] {"-c", "signature", "--privatekey", "private.pem", "-f", "foo.file"});

                Container container = ContainerProvider.GetContainer();
                var signatureProvider = container.GetInstance<SignatureProvider>();

                string encodedSignature = fileOutput["foo.file.signature"];
                var signature = new Signature
                {
                    Content = Convert.FromBase64String(encodedSignature),
                    SignedData = fileContent
                };

                Assert.IsTrue(signatureProvider.VerifySignature(keyPair.PublicKey, signature));
            }
        }

        [TestFixture]
        public class ErrorCases : CreateSignatureTest
        {
            [SetUp]
            public void SetupErrorCases()
            {
                SetupWithRsaKey();
            }

            [Test]
            public void ShouldIndicateMissingPrivateKey()
            {
                var exception = Assert.Throws<ArgumentException>(() =>
                {
                    Certifier.Main(new []{"-c", "signature", "-f", "foo.file"});
                });

                Assert.AreEqual("Private key file or path is required.", exception.Message);
            }

            [Test]
            public void ShouldIndicateMissingTargetFile()
            {
                var exception = Assert.Throws<ArgumentException>(() =>
                {
                    Certifier.Main(new []{"-c", "signature", "--privatekey", "private.pem"});
                });

                Assert.AreEqual("Target file is required.", exception.Message);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09799392097264438, 'fraction_numerical': 0.004376899696048632, 'mean_word_length': 1.9087694483734088, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4434893', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1962, 'n_words': 400}