public void HASignWithInvalidResultTlvFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregatorConfigResponsePdu)),
                                    1584727637)
            },
                    null, null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                HAAsyncResult asyncResult = (HAAsyncResult)haService.BeginSign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")),
                                                                               null, null);

                // add invalid result
                FieldInfo memberInfo  = typeof(HARequestRunner).GetField("_resultTlvs", BindingFlags.NonPublic | BindingFlags.Instance);
                List <object> results = (List <object>)memberInfo.GetValue(asyncResult.RequestRunner);
                results.Add(new IntegerTag(1, false, false, 1));

                haService.EndSign(asyncResult);
            });

            Assert.That(ex.Message.StartsWith("Could not get request response of type " + typeof(KsiSignature)), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 2
0
        public void HAGetConfigResultsAndRemoveAllTest()
        {
            // A configuration request with 2 successful sub-requests is made.
            // Then a new configuration request is made with 2 unsuccessful sub-requests.
            // Both configuration are removed from cache.
            // ExtenderConfigChanged event handler should get result containing an exception.

            HAKsiService haService = GetHAService(
                new List <PduPayload>()
            {
                GetExtenderConfigResponsePayload(4, new List <string>()
                {
                    "uri-1"
                }, 1136073600, 2136073600)
            },
                new List <PduPayload>()
            {
                GetExtenderConfigResponsePayload(4, new List <string>()
                {
                    "uri-2"
                }, 1136073600, 2136073600)
            });

            haService.GetExtenderConfig();

            // change first service response so that request fails
            ((TestKsiService)haService.ExtendingServices[0]).ExtendingServiceProtocol.RequestResult =
                File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455));

            // change second service response so that request fails
            ((TestKsiService)haService.ExtendingServices[1]).ExtendingServiceProtocol.RequestResult =
                File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455));

            ExtenderConfigChangedEventArgs args = null;
            ManualResetEvent waitHandle         = new ManualResetEvent(false);

            haService.ExtenderConfigChanged += delegate(object sender, ExtenderConfigChangedEventArgs e)
            {
                args = e;
                waitHandle.Set();
            };

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.GetExtenderConfig();
            });

            Assert.That(ex.Message.StartsWith("Could not get extender configuration"), "Unexpected exception message: " + ex.Message);

            waitHandle.WaitOne(1000);

            Assert.IsNotNull(args, "ExtenderConfigChangedEventArgs cannot be null.");
            Assert.IsNull(args.ExtenderConfig, "ExtenderConfigChangedEventArgs.ExtenderConfig cannot have value.");
            Assert.IsNotNull(args.Exception, "ExtenderConfigChangedEventArgs.Exception cannot be null.");
            Assert.AreEqual(haService, args.KsiService, "Unexpected ExtenderConfigChangedEventArgs.KsiService");
            Assert.That(args.Exception.Message.StartsWith("Could not get extender configuration"), "Unexpected exception message: " + args.Exception.Message);
        }
        public void HASignWithInvalidTypeAsyncResultFailTest()
        {
            IKsiService haService = new HAKsiService(null, null, null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                IAsyncResult ar = GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)),
                                                      1584727637).BeginSign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null, null);
                haService.EndSign(ar);
            });

            Assert.That(ex.Message.StartsWith("Invalid asyncResult, could not cast to correct object"), "Unexpected exception message: " + ex.Message);
        }
        public void HASignWithoutServicesFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    null,
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1),
            },
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1));

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Sign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")));
            });

            Assert.That(ex.Message.StartsWith("Sub-services are missing"), "Unexpected exception message: " + ex.Message);
        }
        public void HACreateServiceWith4SubServicesFailTest()
        {
            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                IKsiService haService =
                    new HAKsiService(
                        new List <IKsiService>()
                {
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1),
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 2),
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1043101455),
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1043101455)
                },
                        null, null);
            });

            Assert.That(ex.Message.StartsWith("Cannot use more than 3 signing services"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 6
0
        public void HAExtedWithoutServicesFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 1),
            },
                    null,
                    GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 1)
                    );

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Extend(123);
            });

            Assert.That(ex.Message.StartsWith("Sub-services are missing"), "Unexpected exception message: " + ex.Message);
        }
        public void HAGetSignResponsePayloadWithInvalidAsyncResultFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    null,
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 1043101455)
            },
                    null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                IAsyncResult ar = haService.BeginExtend(1455494400, null, null);
                // use extending async result
                haService.GetSignResponsePayload(ar);
            });

            Assert.That(ex.Message.StartsWith("Invalid async result. Containing invalid request runner"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 8
0
        public void HAExtendAllServicesFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    null,
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 1),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 2),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727637),
            },
                    null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Extend(123);
            });

            Assert.That(ex.Message.StartsWith("All sub-requests failed"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 9
0
        public void HAExtendWithInvalidAsyncResultFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1584727637),
            },
                    null,
                    null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                IAsyncResult ar = haService.BeginSign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")), null, null);
                // send signing async result to extend ending
                haService.EndExtend(ar);
            });

            Assert.That(ex.Message.StartsWith("Invalid async result. Containing invalid request runner"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 10
0
        /// <summary>
        /// End get additional extender configuration data (async)
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns>Extender configuration data</returns>
        public ExtenderConfig EndGetExtenderConfig(IAsyncResult asyncResult)
        {
            HAAsyncResult haAsyncResult          = GetHAAsyncResult(asyncResult);
            HAExtenderConfigRequestRunner runner = GetRequestRunner <HAExtenderConfigRequestRunner>(haAsyncResult);
            ExtenderConfig config = runner.EndGetExtenderConfig(haAsyncResult);

            if (config == null)
            {
                lock (_extenderConfigChangedLock)
                {
                    _currentExtenderConfigList.Clear();
                    _currentExtenderConfig = null;
                }

                HAKsiServiceException ex = new HAKsiServiceException("Could not get extender configuration.", runner.SubServiceErrors);
                Logger.Warn(ex);
                ExtenderConfigChangedEventArgs extenderConfigChangedEventArgs = new ExtenderConfigChangedEventArgs(ex, this);
                ExtenderConfigChanged?.Invoke(this, extenderConfigChangedEventArgs);
                throw ex;
            }

            // if sub-service config request failed then remove corresponding config from cache
            foreach (HAKsiSubServiceException ex in runner.SubServiceErrors)
            {
                if (ex.ThrownBySubService == null)
                {
                    continue;
                }

                lock (_extenderConfigChangedLock)
                {
                    if (_currentExtenderConfigList.ContainsKey(ex.ThrownBySubService))
                    {
                        _currentExtenderConfigList.Remove(ex.ThrownBySubService);
                        RecalculateExtenderConfig();
                    }
                }
            }

            return(config);
        }
        public void HAEndPublicationsFileRequestWithoutServicesFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetPublicationsFileService()
            },
                    new List <IKsiService>()
            {
                GetPublicationsFileService()
            },
                    null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.EndGetPublicationsFile(new TestAsyncResult());
            });

            Assert.That(ex.Message.StartsWith("Publications file service is missing"), "Unexpected exception message: " + ex.Message);
        }
        public void HAAggregatorConfigRequestFailSTest()
        {
            // Test getting aggregator configuration with all 3 sub-services responses failing

            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 2),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 3)
            },
                    null, null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.GetAggregatorConfig();
            });

            Assert.That(ex.Message.StartsWith("Could not get aggregator configuration"), "Unexpected exception message: " + ex.Message);
        }
        public void HASignAllServicesFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 1),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)), 2),
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)), 1043101455)
            },
                    null, null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Sign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")));
            });

            Assert.That(ex.Message.StartsWith("All sub-requests failed"), "Unexpected exception message: " + ex.Message);
            Assert.AreEqual(3, ex.SubServiceExceptions.Count, "Unexpected sub-service exception count");
            Assert.That(ex.SubServiceExceptions[0].Message.StartsWith("Using sub-service failed"),
                        "Unexpected sub-service exception message: " + ex.SubServiceExceptions[0].Message);
        }
Esempio n. 14
0
        public void HAGetExtenderConfigTimeoutTest()
        {
            TestKsiServiceProtocol protocol = new TestKsiServiceProtocol
            {
                RequestResult     = GetExtenderConfigResponsePayload(1, null, 123, 234).Encode(),
                DelayMilliseconds = 3000
            };

            IKsiService haService =
                new HAKsiService(
                    null, new List <IKsiService>()
            {
                GetStaticKsiService(protocol),
            },
                    null, 1000);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.GetExtenderConfig();
            });

            Assert.That(ex.Message.StartsWith("HA service request timed out"), "Unexpected exception message: " + ex.Message);
        }
        public void HASignTimeoutTest()
        {
            TestKsiServiceProtocol protocol = new TestKsiServiceProtocol
            {
                RequestResult     = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637)),
                DelayMilliseconds = 3000
            };

            IKsiService haService =
                new HAKsiService(
                    new List <IKsiService>()
            {
                GetStaticKsiService(protocol, 1584727637),
            },
                    null, null, 1000);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Sign(new DataHash(Base16.Decode("019f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")));
            });

            Assert.That(ex.Message.StartsWith("HA service request timed out"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 16
0
        public void HAExtendTimeoutTest()
        {
            TestKsiServiceProtocol protocol = new TestKsiServiceProtocol
            {
                RequestResult     = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtendResponsePdu_RequestId_1043101455)),
                DelayMilliseconds = 3000
            };

            IKsiService haService =
                new HAKsiService(
                    null, new List <IKsiService>()
            {
                GetStaticKsiService(protocol, 1043101455),
            },
                    null, 1000);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                haService.Extend(123);
            });

            Assert.That(ex.Message.StartsWith("HA service request timed out"), "Unexpected exception message: " + ex.Message);
        }
Esempio n. 17
0
        public void HAExtendWithInvalidResultTlvFailTest()
        {
            IKsiService haService =
                new HAKsiService(
                    null,
                    new List <IKsiService>()
            {
                GetStaticKsiService(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_ExtenderConfigResponsePdu)), 1043101455)
            },
                    null);

            HAKsiServiceException ex = Assert.Throws <HAKsiServiceException>(delegate
            {
                HAAsyncResult ar = (HAAsyncResult)haService.BeginExtend(1455494400, null, null);

                // add invalid result
                FieldInfo memberInfo  = typeof(HARequestRunner).GetField("_resultTlvs", BindingFlags.NonPublic | BindingFlags.Instance);
                List <object> results = (List <object>)memberInfo.GetValue(ar.RequestRunner);
                results.Add(new IntegerTag(1, false, false, 1));
                haService.EndExtend(ar);
            });

            Assert.That(ex.Message.StartsWith("Could not get request response of type " + typeof(CalendarHashChain)), "Unexpected exception message: " + ex.Message);
        }