public void AggregationResponsePduToStringWithResponseTest()
        {
            AggregationResponsePdu tag = new AggregationResponsePdu(new TlvTagBuilder(Constants.AggregationResponsePdu.TagType, false, false,
                                                                                      new ITlvTag[]
            {
                new PduHeader(new TlvTagBuilder(Constants.PduHeader.TagType, false, false,
                                                new ITlvTag[]
                {
                    new StringTag(Constants.PduHeader.LoginIdTagType, false, false, "Test Login Id"),
                    new IntegerTag(Constants.PduHeader.InstanceIdTagType, false, false, 1),
                    new IntegerTag(Constants.PduHeader.MessageIdTagType, false, false, 2)
                }).BuildTag()),
                new AggregationResponsePayload(new TlvTagBuilder(Constants.AggregationResponsePayload.TagType, false, false, new ITlvTag[]
                {
                    new IntegerTag(Constants.PduPayload.RequestIdTagType, false, false, 2),
                    new IntegerTag(Constants.PduPayload.StatusTagType, false, false, 1),
                    new StringTag(Constants.PduPayload.ErrorMessageTagType, false, false, "Test error message."),
                }).BuildTag()),
                new ImprintTag(Constants.Pdu.MacTagType, false, false,
                               new DataHash(HashAlgorithm.Sha2256,
                                            new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 })),
            }).BuildTag());

            AggregationResponsePdu tag2 = new AggregationResponsePdu(new RawTag(tag.Type, tag.NonCritical, tag.Forward, tag.EncodeValue()));

            Assert.AreEqual(tag.ToString(), tag2.ToString());
        }
        private static AggregationResponsePayload GetAggregationResponsePayload(string path)
        {
            byte[] bytes = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, path));

            using (TlvReader reader = new TlvReader(new MemoryStream(bytes)))
            {
                AggregationResponsePdu pdu = new AggregationResponsePdu(reader.ReadTag());
                return(pdu.Payloads[0] as AggregationResponsePayload);
            }
        }
Ejemplo n.º 3
0
        public void PduTest()
        {
            byte[] bytes = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregationResponsePdu_RequestId_1584727637));

            using (TlvReader tlvReader = new TlvReader(new MemoryStream(bytes)))
            {
                AggregationResponsePdu pdu = new AggregationResponsePdu(tlvReader.ReadTag());
                Assert.IsNotNull(pdu.Header, "Unexpected PDU header");
                Assert.AreEqual(3, pdu.GetChildren().Length, "Unexpected childen count.");
            }
        }
Ejemplo n.º 4
0
        public void PduMacValidationTest()
        {
            byte[] bytes = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregatorConfigResponsePdu));

            ImprintTag mac;

            using (TlvReader tlvReader = new TlvReader(new MemoryStream(bytes)))
            {
                mac = new AggregationResponsePdu(tlvReader.ReadTag()).Mac;
            }

            Assert.IsTrue(Pdu.ValidateMac(bytes, mac, Util.EncodeNullTerminatedUtf8String(TestConstants.ServicePass)), "MAC should be valid");
        }
Ejemplo n.º 5
0
        public void PduMacValidationWith0x0IntStaticInvalidTest()
        {
            byte[] bytes = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, Resources.KsiService_AggregatorConfigResponsePdu_0x0_Int));

            AggregationResponsePdu pdu;

            using (TlvReader tlvReader = new TlvReader(new MemoryStream(bytes)))
            {
                pdu = new AggregationResponsePdu(tlvReader.ReadTag());
            }

            // 0x0 value representing an integer is converted to an empty TLV, thus MAC check will fail.
            byte[] pduBytes = pdu.Encode();
            Assert.IsFalse(Pdu.ValidateMac(pduBytes, pdu.Mac, Util.EncodeNullTerminatedUtf8String(TestConstants.ServicePass)), "MAC should be invalid");
        }
        public void AggregationResponsePduToStringWithErrorTest()
        {
            AggregationResponsePdu tag = new AggregationResponsePdu(new TlvTagBuilder(Constants.AggregationResponsePdu.TagType, false, false,
                                                                                      new ITlvTag[]
            {
                new AggregationErrorPayload(new TlvTagBuilder(Constants.ErrorPayload.TagType, false, false, new ITlvTag[]
                {
                    new IntegerTag(Constants.PduPayload.StatusTagType, false, false, 1),
                    new StringTag(Constants.PduPayload.ErrorMessageTagType, false, false, "Test Error message")
                }).BuildTag())
            }).BuildTag());

            AggregationResponsePdu tag2 = new AggregationResponsePdu(new RawTag(tag.Type, tag.NonCritical, tag.Forward, tag.EncodeValue()));

            Assert.AreEqual(tag.ToString(), tag2.ToString());
        }
        private static List <KsiServiceResponsePayloadInfo> GetAggregatorResponsePayloadInfos(RawTag pdu)
        {
            List <KsiServiceResponsePayloadInfo> list = new List <KsiServiceResponsePayloadInfo>();
            IEnumerable <RawTag> children             = GetChildren(pdu.Value);

            bool containsUnknownPayload = false;

            foreach (RawTag child in children)
            {
                switch (child.Type)
                {
                case Constants.AggregationResponsePayload.TagType:
                    RawTag requestIdTag = GetTagByType(child.Value, Constants.PduPayload.RequestIdTagType);
                    if (requestIdTag == null)
                    {
                        throw new KsiServiceProtocolException("Cannot find request id tag from aggregation response payload.");
                    }
                    list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.Aggregation, new IntegerTag(requestIdTag).Value));
                    break;

                case Constants.AggregatorConfigResponsePayload.TagType:
                    list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.AggregatorConfig));
                    break;

                case Constants.ErrorPayload.TagType:
                    list.Add(new KsiServiceResponsePayloadInfo(KsiServiceResponsePayloadType.Error));
                    break;

                case Constants.PduHeader.TagType:
                case Constants.Pdu.MacTagType:
                    break;

                default:
                    containsUnknownPayload = true;
                    break;
                }
            }

            if (containsUnknownPayload)
            {
                // try to parse PDU to check if critical unknown tags are included in which case a parsing exceptions is thrown by AggregationResponsePdu
                AggregationResponsePdu aggregationResponsePdu = new AggregationResponsePdu(pdu);
                Logger.Warn(string.Format("TCP response processor received unexpected response payloads!{0}PDU:{0}{1}", Environment.NewLine, aggregationResponsePdu));
            }

            return(list);
        }
        private static IKsiService GetService(List <PduPayload> payloads, ulong requestId = 1584727637)
        {
            List <ITlvTag> childTags = new List <ITlvTag> {
                new PduHeader(Settings.Default.HttpSigningServiceUser)
            };

            childTags.AddRange(payloads);
            childTags.Add(new ImprintTag(Constants.Pdu.MacTagType, false, false, new DataHash(HashAlgorithm.Sha2256, new byte[32])));

            AggregationResponsePdu pdu = TestUtil.GetCompositeTag <AggregationResponsePdu>(Constants.AggregationResponsePdu.TagType, childTags.ToArray());

            MethodInfo m = pdu.GetType().GetMethod("SetMacValue", BindingFlags.Instance | BindingFlags.NonPublic);

            m.Invoke(pdu, new object[] { HashAlgorithm.Sha2256, Util.EncodeNullTerminatedUtf8String(TestConstants.ServicePass) });

            MemoryStream stream = new MemoryStream();

            using (TlvWriter writer = new TlvWriter(stream))
            {
                writer.WriteTag(pdu);
            }

            return(GetStaticKsiService(stream.ToArray(), requestId));
        }