public void TcpExtendesWithDisposedServiceProtocolTest()
        {
            KsiService service         = GetTcpKsiService();
            ulong      aggregationTime = 1455478441;

            IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null);
            IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null);

            service.EndExtend(ar1);

            TcpKsiExtendingServiceProtocol tcp = GetTcpProtocol(service);

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

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

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

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

            ex = Assert.Throws <KsiServiceProtocolException>(delegate
            {
                service.BeginExtend(aggregationTime, null, null);
            });

            Assert.That(ex.Message.StartsWith("TCP KSI service protocol is disposed."), "Unexpected exception message: " + ex.Message);
        }
        public void TcpExtendesWithSocketReuseAndTimeoutTest()
        {
            KsiService service         = GetTcpKsiService();
            ulong      aggregationTime = 1455478441;

            IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null);

            CalendarHashChain cal1    = service.EndExtend(ar1);
            Socket            socket1 = GetExtendingSocket(service);

            Assert.AreEqual(aggregationTime, cal1.AggregationTime, "Unexpected calendar aggregation time");

            Socket socket2 = GetExtendingSocket(service);

            Assert.AreEqual(socket1, socket2, "Sockets should be equal");

            // after 20 sec server will close connection
            Thread.Sleep(23000);

            IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null);

            CalendarHashChain cal2 = service.EndExtend(ar2);

            Assert.AreEqual(aggregationTime, cal2.AggregationTime, "Unexpected calendar aggregation time");

            socket2 = GetExtendingSocket(service);

            Assert.AreNotEqual(socket1, socket2, "Sockets should not be equal");
        }
        public void TcpExtendWithReusedSocketTest()
        {
            KsiService service         = GetTcpKsiService();
            ulong      aggregationTime = 1455478441;

            IAsyncResult ar1 = service.BeginExtend(aggregationTime, null, null);
            IAsyncResult ar2 = service.BeginExtend(aggregationTime, null, null);

            CalendarHashChain cal1 = service.EndExtend(ar1);

            Assert.AreEqual(aggregationTime, cal1.AggregationTime, "Unexpected calendar aggregation time");
            CalendarHashChain cal2 = service.EndExtend(ar2);

            Assert.AreEqual(aggregationTime, cal2.AggregationTime, "Unexpected calendar aggregation time");

            Socket socket1 = GetExtendingSocket(service);

            IAsyncResult ar3 = service.BeginExtend(aggregationTime, null, null);
            IAsyncResult ar4 = service.BeginExtend(aggregationTime, null, null);

            CalendarHashChain cal3 = service.EndExtend(ar3);

            Assert.AreEqual(aggregationTime, cal3.AggregationTime, "Unexpected calendar aggregation time");
            CalendarHashChain cal4 = service.EndExtend(ar4);

            Assert.AreEqual(aggregationTime, cal4.AggregationTime, "Unexpected calendar aggregation time");

            Socket socket2 = GetExtendingSocket(service);

            Assert.AreEqual(socket1, socket2, "Sockets should be equal");
        }
        public void AsyncExtendWithInvalidPassTest(KsiService service)
        {
            ManualResetEvent  waitHandle = new ManualResetEvent(false);
            Exception         ex         = null;
            CalendarHashChain cal        = null;

            service.BeginExtend(1455400000, delegate(IAsyncResult ar)
            {
                try
                {
                    cal = service.EndExtend(ar);
                }
                catch (Exception e)
                {
                    ex = e;
                }
                finally
                {
                    waitHandle.Set();
                }
            }, null);

            Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out.");

            Assert.IsNull(cal, "Calendar hash chain should be null.");
            Assert.IsNotNull(ex, "Exception should not be null.");
            Assert.AreEqual("Server responded with error message. Status: 258; Message: Failed hmac check.", ex.Message);
        }
        public void AsyncExtendTest(KsiService service)
        {
            ManualResetEvent  waitHandle = new ManualResetEvent(false);
            CalendarHashChain cal        = null;

            object testObject     = new object();
            bool   isAsyncCorrect = false;

            service.BeginExtend(1455400000, delegate(IAsyncResult ar)
            {
                try
                {
                    isAsyncCorrect = ar.AsyncState == testObject;
                    cal            = service.EndExtend(ar);
                }
                catch (Exception ex)
                {
                    Assert.Fail("Unexpected exception: " + ex);
                }
                finally
                {
                    waitHandle.Set();
                }
            }, testObject);

            Assert.IsTrue(waitHandle.WaitOne(10000), "Wait handle timed out.");

            Assert.IsNotNull(cal, "Calendar hash chain should not be null.");
            Assert.AreEqual(true, isAsyncCorrect, "Unexpected async state.");
        }
        public void EndExtendWithoutExtendingServiceProtocol()
        {
            KsiService service = new KsiService(null, null, null, null, null, null);

            KsiServiceException ex = Assert.Throws <KsiServiceException>(delegate
            {
                service.EndExtend(new TestAsyncResult());
            });

            Assert.That(ex.Message.StartsWith("Extending service protocol is missing from service"), "Unexpected exception message: " + ex.Message);
        }