public static void InterfaceStatisticsOption_ConvertToByte_Test(bool reorder)
        {
            InterfaceStatisticsOption preOption = new InterfaceStatisticsOption();
            InterfaceStatisticsOption postOption;

            preOption.Comment           = "Test Comment";
            preOption.DeliveredToUser   = 25;
            preOption.EndTime           = new TimestampHelper(new byte[] { 1, 0, 0, 0, 2, 0, 0, 0 }, false);
            preOption.StartTime         = new TimestampHelper(new byte[] { 1, 0, 0, 3, 2, 0, 0, 4 }, false);
            preOption.FilterAccept      = 30;
            preOption.InterfaceDrop     = 35;
            preOption.InterfaceReceived = 40;
            preOption.SystemDrop        = 45;

            byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
            using (MemoryStream stream = new MemoryStream(preOptionByte))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    postOption = InterfaceStatisticsOption.Parse(binaryReader, reorder, null);
                }
            }

            Assert.IsNotNull(postOption);
            Assert.AreEqual(preOption.Comment, postOption.Comment);
            Assert.AreEqual(preOption.DeliveredToUser, postOption.DeliveredToUser);
            Assert.AreEqual(preOption.EndTime.Seconds, postOption.EndTime.Seconds);
            Assert.AreEqual(preOption.EndTime.Microseconds, postOption.EndTime.Microseconds);
            Assert.AreEqual(preOption.StartTime.Seconds, postOption.StartTime.Seconds);
            Assert.AreEqual(preOption.StartTime.Microseconds, postOption.StartTime.Microseconds);
            Assert.AreEqual(preOption.FilterAccept, postOption.FilterAccept);
            Assert.AreEqual(preOption.InterfaceDrop, postOption.InterfaceDrop);
            Assert.AreEqual(preOption.InterfaceReceived, postOption.InterfaceReceived);
            Assert.AreEqual(preOption.SystemDrop, postOption.SystemDrop);
        }
Ejemplo n.º 2
0
        public static InterfaceStatisticsOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");

            InterfaceStatisticsOption             option      = new InterfaceStatisticsOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

            if (optionsList.Any())
            {
                foreach (var item in optionsList)
                {
                    try
                    {
                        switch (item.Key)
                        {
                        case (ushort)InterfaceStatisticsOptionCode.CommentCode:
                            option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.StartTimeCode:
                            if (item.Value.Length == 8)
                            {
                                option.StartTime = new TimestampHelper(item.Value, reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] StartTimeCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.EndTimeCode:
                            if (item.Value.Length == 8)
                            {
                                option.EndTime = new TimestampHelper(item.Value, reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] EndTimeCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.InterfaceReceivedCode:
                            if (item.Value.Length == 8)
                            {
                                option.InterfaceReceived = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] InterfaceReceivedCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.InterfaceDropCode:
                            if (item.Value.Length == 8)
                            {
                                option.InterfaceDrop = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] InterfaceDropCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.FilterAcceptCode:
                            if (item.Value.Length == 8)
                            {
                                option.FilterAccept = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] FilterAcceptCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.SystemDropCode:
                            if (item.Value.Length == 8)
                            {
                                option.SystemDrop = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] SystemDropCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.DeliveredToUserCode:
                            if (item.Value.Length == 8)
                            {
                                option.DeliveredToUser = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceStatisticsOption.Parse] DeliveredToUserCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceStatisticsOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }

            return(option);
        }