public void SignHashWithTcpTimeoutAndBufferSizeTest()
        {
            uint requestTimeout = 9000;
            uint bufferSize     = 1024;
            TcpKsiSigningServiceProtocol protocol = new TcpKsiSigningServiceProtocol(
                IPAddress.Parse(Settings.Default.TcpExtendingServiceIp),
                Settings.Default.TcpSigningServicePort,
                requestTimeout,
                bufferSize);
            HttpKsiServiceProtocol publicationsFileProtocol = new HttpKsiServiceProtocol(
                null,
                null,
                Settings.Default.HttpPublicationsFileUrl);
            Ksi ksi = new Ksi(new KsiService(
                                  protocol,
                                  new ServiceCredentials(Settings.Default.HttpSigningServiceUser, Settings.Default.HttpSigningServicePass,
                                                         TestUtil.GetHashAlgorithm(Settings.Default.HttpSigningServiceHmacAlgorithm)),
                                  null, null, publicationsFileProtocol,
                                  new PublicationsFileFactory(
                                      new PkiTrustStoreProvider(new X509Store(StoreName.Root), CryptoTestFactory.CreateCertificateSubjectRdnSelector("[email protected]")))));

            VerificationResult verificationResult = SignHash(ksi);

            Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode, "Signature should verify with key based policy");
        }
        private static Socket GetSigningSocket(TcpKsiSigningServiceProtocol tcp)
        {
            string    fieldName  = "_socket";
            FieldInfo memberInfo = typeof(TcpKsiServiceProtocolBase).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);

            if (memberInfo != null)
            {
                return((Socket)memberInfo.GetValue(tcp));
            }
            throw new Exception("Could not get field info: " + fieldName);
        }
        public void EndSignInvalidAsyncResultTest()
        {
            TcpKsiSigningServiceProtocol protocol = new TcpKsiSigningServiceProtocol(new IPAddress(new byte[] { 127, 0, 0, 1 }), 0);

            KsiServiceProtocolException ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                protocol.EndSign(new TestAsyncResult());
            });

            Assert.That(ex.Message.StartsWith("Invalid IAsyncResult"), "Unexpected exception message: " + ex.Message);
        }
        public void BeginSignWithHashNullTest()
        {
            TcpKsiSigningServiceProtocol protocol = new TcpKsiSigningServiceProtocol(new IPAddress(new byte[] { 127, 0, 0, 1 }), 0);

            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(delegate
            {
                protocol.BeginSign(null, 1, null, null);
            });

            Assert.AreEqual("data", ex.ParamName);
        }
        public void EndSignWithAsyncResultDisposedTest()
        {
            TcpKsiSigningServiceProtocol protocol    = new TcpKsiSigningServiceProtocol(new IPAddress(new byte[] { 127, 0, 0, 1 }), 0);
            KsiServiceAsyncResult        asyncResult = new TcpKsiServiceAsyncResult(KsiServiceRequestType.Sign, new byte[] { 1, 2, 3 }, 12345, null, null);

            asyncResult.Dispose();

            KsiServiceProtocolException ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                protocol.EndSign(asyncResult);
            });

            Assert.That(ex.Message.StartsWith("Provided async result is already disposed. Possibly using the same async result twice when ending request"),
                        "Unexpected exception message: " + ex.Message);
        }
        public void TcpSignHashesWithDisposedServiceProtocolTest()
        {
            KsiService service = GetTcpKsiService();

            IAsyncResult ar1 = service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("1f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null,
                                                 null);
            IAsyncResult ar2 = service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("2f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null,
                                                 null);

            service.EndSign(ar1);

            TcpKsiSigningServiceProtocol tcp = GetTcpProtocol(service);

            Assert.IsNotNull(GetSigningSocket(tcp), "Socket should not be null");
            tcp.Dispose();

            Assert.IsNull(GetSigningSocket(tcp), "Socket should be null");

            KsiServiceProtocolException ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                tcp.Dispose();
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is already disposed."), "Unexpected exception message: " + ex.Message);

            ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                service.EndSign(ar2);
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message);

            ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                service.BeginSign(new DataHash(HashAlgorithm.Sha2256, Base16.Decode("3f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null, null);
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message);
        }