Esempio n. 1
0
        public void SimpleTest()
        {
            Assert.AreEqual <SerialNumber32>(1, 1);
            Assert.AreNotEqual <SerialNumber32>(1, 2);
            MoreAssert.IsBigger(1, 2);
            MoreAssert.IsSmaller(2, 1);

            SerialNumber32 serialNumber = 1;

            serialNumber = serialNumber.Add(10);
            Assert.AreEqual <SerialNumber32>(11, serialNumber);

            serialNumber = serialNumber.Add(((uint)1 << 31) - 1);
            Assert.AreEqual <SerialNumber32>(2147483658, serialNumber);
            MoreAssert.IsSmaller <SerialNumber32>(1, serialNumber);
            MoreAssert.IsBigger <SerialNumber32>(20, serialNumber);

            serialNumber = serialNumber.Add(((uint)1 << 31) - 1);
            Assert.AreEqual <SerialNumber32>(9, serialNumber);

            Assert.IsTrue(new SerialNumber32(1) < new SerialNumber32(2));
            Assert.IsTrue(new SerialNumber32(2) > new SerialNumber32(1));
            // ReSharper disable EqualExpressionComparison
            Assert.IsFalse(new SerialNumber32(1) < new SerialNumber32(1));
            Assert.IsFalse(new SerialNumber32(1) > new SerialNumber32(1));
            // ReSharper restore EqualExpressionComparison
            Assert.IsTrue(new SerialNumber32(2) != new SerialNumber32(1));
            Assert.IsFalse(new SerialNumber32(1) != new SerialNumber32(0).Add(1));
            Assert.IsTrue(new SerialNumber32(2) == new SerialNumber32(1).Add(1));
            Assert.IsFalse(new SerialNumber32(1).Equals(1.0));

            Assert.AreEqual("1", new SerialNumber32(1).ToString());
        }
Esempio n. 2
0
        public static OfflinePacketDevice GetOfflineDevice(int numPackets, Packet packet, TimeSpan intervalBetweenPackets, string dumpFilename, string readFilename = null)
        {
            if (readFilename == null)
            {
                readFilename = dumpFilename;
            }
            PacketCommunicator communicator;

            using (communicator = LivePacketDeviceTests.OpenLiveDevice())
            {
                using (PacketDumpFile dumpFile = communicator.OpenDump(dumpFilename))
                {
                    int lastPosition = 0;
                    for (int i = 0; i != numPackets; ++i)
                    {
                        if (intervalBetweenPackets != TimeSpan.Zero && i != 0)
                        {
                            DateTime timestamp = packet.Timestamp;
                            timestamp = timestamp.Add(intervalBetweenPackets);
                            packet    = new Packet(packet.Buffer, timestamp, packet.DataLink);
                        }
                        dumpFile.Dump(packet);
                        MoreAssert.IsBigger(lastPosition, dumpFile.Position);
                        lastPosition = dumpFile.Position;
                        dumpFile.Flush();
                    }
                }
            }

            if (readFilename != dumpFilename)
            {
                if (File.Exists(readFilename))
                {
                    File.Delete(readFilename);
                }
                File.Move(dumpFilename, readFilename);
            }

            OfflinePacketDevice device = new OfflinePacketDevice(readFilename);

            Assert.AreEqual(0, device.Addresses.Count);
            Assert.AreEqual(string.Empty, device.Description);
            Assert.AreEqual(DeviceAttributes.None, device.Attributes);
            Assert.AreEqual(readFilename, device.Name);

            return(device);
        }
Esempio n. 3
0
        public void SetBigKernelMinimumBytesToCopyTest()
        {
            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            using (PacketCommunicator communicator = OpenLiveDevice())
            {
                communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
                communicator.SetKernelMinimumBytesToCopy(1024 * 1024);
                Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac);
                for (int i = 0; i != 5; ++i)
                {
                    communicator.SendPacket(expectedPacket);
                    Packet   packet;
                    DateTime start = DateTime.Now;
                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                    DateTime end = DateTime.Now;
                    Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
                    Assert.AreEqual(expectedPacket, packet);
                    MoreAssert.IsBigger(TimeSpan.FromSeconds(0.9), end - start);
                }
            }
        }
Esempio n. 4
0
 public void MinMaxTests()
 {
     MoreAssert.IsBigger(PacketTimestamp.MinimumPacketTimestamp, PacketTimestamp.MaximumPacketTimestamp);
 }
        protected override bool CompareField(XElement field, Datagram datagram)
        {
            IpV6Datagram ipV6Datagram = (IpV6Datagram)datagram;

            SkipAuthenticationHeaders(ipV6Datagram);
            int optionsIndex = 0;

            switch (field.Name())
            {
            case "ipv6.version":
                field.AssertShowDecimal(ipV6Datagram.Version);
                foreach (XElement subfield in field.Fields())
                {
                    switch (subfield.Name())
                    {
                    case "ip.version":
                        subfield.AssertShowDecimal(ipV6Datagram.Version);
                        break;

                    default:
                        throw new InvalidOperationException(string.Format("Invalid ipv6 version subfield {0}", subfield.Name()));
                    }
                }
                break;

            case "ipv6.class":
                field.AssertShowDecimal(ipV6Datagram.TrafficClass);
                break;

            case "ipv6.flow":
                field.AssertShowDecimal(ipV6Datagram.FlowLabel);
                field.AssertNoFields();
                break;

            case "ipv6.plen":
                field.AssertShowDecimal(ipV6Datagram.PayloadLength);
                field.AssertNoFields();
                break;

            case "ipv6.nxt":
                field.AssertShowDecimal((byte)ipV6Datagram.NextHeader);
                field.AssertNoFields();
                break;

            case "ipv6.hlim":
                field.AssertShowDecimal(ipV6Datagram.HopLimit);
                field.AssertNoFields();
                break;

            case "ipv6.src":
            case "ipv6.src_host":
                field.AssertShow(ipV6Datagram.Source.GetWiresharkString());
                field.AssertNoFields();
                break;

            case "ipv6.src_6to4_gw_ipv4":
            case "ipv6.src_6to4_sla_id":
            case "ipv6.6to4_gw_ipv4":
            case "ipv6.6to4_sla_id":
                field.AssertNoFields();
                break;

            case "ipv6.dst":
            case "ipv6.dst_host":
                field.AssertShow(ipV6Datagram.CurrentDestination.GetWiresharkString());
                field.AssertNoFields();
                break;

            case "ipv6.addr":
            case "ipv6.host":
                Assert.IsTrue(field.Show() == ipV6Datagram.Source.GetWiresharkString() ||
                              field.Show() == ipV6Datagram.CurrentDestination.GetWiresharkString());
                field.AssertNoFields();
                break;

            case "ipv6.hop_opt":
                if (_currentExtensionHeaderIndex >= ipV6Datagram.ExtensionHeaders.Headers.Count)
                {
                    Assert.IsFalse(ipV6Datagram.ExtensionHeaders.IsValid);
                    int maxLength = ipV6Datagram.Length - IpV6Datagram.HeaderLength - ipV6Datagram.ExtensionHeaders.BytesLength;
                    if (field.Fields().Any(subfield => subfield.Name() == "ipv6.opt.length"))
                    {
                        int length = int.Parse(field.Fields().First(subfield => subfield.Name() == "ipv6.opt.length").Show());
                        MoreAssert.IsBigger(maxLength, length);
                    }
                    else
                    {
                        Assert.AreEqual(6, maxLength);
                    }
                }
                else
                {
                    IpV6ExtensionHeaderHopByHopOptions hopByHopOptions =
                        (IpV6ExtensionHeaderHopByHopOptions)ipV6Datagram.ExtensionHeaders[_currentExtensionHeaderIndex];
                    IncrementCurrentExtensionHeaderIndex(ipV6Datagram);
                    CompareOptions(field, ref optionsIndex, ipV6Datagram, hopByHopOptions);
                }
                break;

            case "ipv6.routing_hdr":
                if (!ipV6Datagram.IsValid)
                {
                    return(false);
                }
                IpV6ExtensionHeaderRouting routing = (IpV6ExtensionHeaderRouting)ipV6Datagram.ExtensionHeaders[_currentExtensionHeaderIndex];
                IpV6ExtensionHeaderRoutingProtocolLowPowerAndLossyNetworks routingProtocolLowPowerAndLossyNetworks =
                    routing as IpV6ExtensionHeaderRoutingProtocolLowPowerAndLossyNetworks;
                int routingProtocolLowPowerAndLossyNetworksAddressIndex = 0;
                IncrementCurrentExtensionHeaderIndex(ipV6Datagram);
                int sourceRouteAddressIndex = 0;
                foreach (var headerField in field.Fields())
                {
                    switch (headerField.Name())
                    {
                    case "":
                        headerField.AssertNoFields();
                        ValidateExtensionHeaderUnnamedField(routing, headerField);
                        break;

                    case "ipv6.routing_hdr.type":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal((byte)routing.RoutingType);
                        break;

                    case "ipv6.routing_hdr.left":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal(routing.SegmentsLeft);
                        break;

                    case "ipv6.mipv6_home_address":
                        headerField.AssertNoFields();
                        IpV6ExtensionHeaderRoutingHomeAddress routingHomeAddress = (IpV6ExtensionHeaderRoutingHomeAddress)routing;
                        headerField.AssertShow(routingHomeAddress.HomeAddress.ToString("x"));
                        break;

                    case "ipv6.routing_hdr.addr":
                        headerField.AssertNoFields();
                        IpV6ExtensionHeaderRoutingSourceRoute routingSourceRoute = (IpV6ExtensionHeaderRoutingSourceRoute)routing;
                        headerField.AssertShow(routingSourceRoute.Addresses[sourceRouteAddressIndex++].ToString("x"));
                        break;

                    case "ipv6.routing_hdr.rpl.cmprI":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal(routingProtocolLowPowerAndLossyNetworks.CommonPrefixLengthForNonLastAddresses);
                        break;

                    case "ipv6.routing_hdr.rpl.cmprE":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal(routingProtocolLowPowerAndLossyNetworks.CommonPrefixLengthForLastAddress);
                        break;

                    case "ipv6.routing_hdr.rpl.pad":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal(routingProtocolLowPowerAndLossyNetworks.PadSize);
                        break;

                    case "ipv6.routing_hdr.rpl.reserved":
                        headerField.AssertNoFields();
                        headerField.AssertShowDecimal(0);
                        break;

                    case "ipv6.routing_hdr.rpl.segments":
                        if (headerField.Fields().Any())
                        {
                            headerField.AssertNumFields(1);
                            headerField.Fields().First().AssertName("_ws.expert");
                        }
                        // TODO: Remove this condition when https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=11803 is fixed.
                        if (routingProtocolLowPowerAndLossyNetworks.Addresses.Count > 0)
                        {
                            headerField.AssertShowDecimal(routingProtocolLowPowerAndLossyNetworks.Addresses.Count);
                        }
                        break;

                    case "ipv6.routing_hdr.rpl.address":
                        headerField.AssertNoFields();
                        // TODO: Remove this condition when https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=11803 is fixed.
                        if (routingProtocolLowPowerAndLossyNetworks.Addresses.Count > 0)
                        {
                            IpV6Address actualAddress =
                                new IpV6Address(UInt128.Parse(headerField.Value(), NumberStyles.HexNumber, CultureInfo.InvariantCulture));
                            Assert.AreEqual(routingProtocolLowPowerAndLossyNetworks.Addresses[routingProtocolLowPowerAndLossyNetworksAddressIndex],
                                            actualAddress);
                        }
                        break;

                    case "ipv6.routing_hdr.rpl.full_address":
                        headerField.AssertNoFields();
                        // TODO: Uncomment the following code when https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=10673 is fixed.
//                                // TODO: Remove this condition when https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=11803 is fixed.
//                                if (routingProtocolLowPowerAndLossyNetworks.Addresses.Count > 0)
//                                {
//                                    IpV6Address actualFullAddress = new IpV6Address(headerField.Show());
//
//                                    IpV6Address destinationAddress = ipV6Datagram.CurrentDestination;
//                                    for (int i = 0; i < _currentExtensionHeaderIndex; ++i)
//                                    {
//                                        IpV6ExtensionHeaderRoutingHomeAddress ipV6ExtensionHeaderRoutingHomeAddress =
//                                            ipV6Datagram.ExtensionHeaders[i] as IpV6ExtensionHeaderRoutingHomeAddress;
//                                        if (ipV6ExtensionHeaderRoutingHomeAddress != null)
//                                            destinationAddress = ipV6ExtensionHeaderRoutingHomeAddress.HomeAddress;
//
//                                        IpV6ExtensionHeaderRoutingSourceRoute ipV6ExtensionHeaderRoutingSourceRoute =
//                                            ipV6Datagram.ExtensionHeaders[i] as IpV6ExtensionHeaderRoutingSourceRoute;
//                                        if (ipV6ExtensionHeaderRoutingSourceRoute != null && ipV6ExtensionHeaderRoutingSourceRoute.Addresses.Any())
//                                            destinationAddress = ipV6ExtensionHeaderRoutingSourceRoute.Addresses.Last();
//                                    }
//
//                                    int commonPrefixLength =
//                                        routingProtocolLowPowerAndLossyNetworksAddressIndex == routingProtocolLowPowerAndLossyNetworks.Addresses.Count - 1
//                                            ? routingProtocolLowPowerAndLossyNetworks.CommonPrefixLengthForLastAddress
//                                            : routingProtocolLowPowerAndLossyNetworks.CommonPrefixLengthForNonLastAddresses;
//
//                                    byte[] destinationAddressBytes = new byte[IpV6Address.SizeOf];
//                                    destinationAddressBytes.Write(0, destinationAddress, Endianity.Big);
//
//                                    byte[] routingAddressBytes = new byte[IpV6Address.SizeOf];
//                                    routingAddressBytes.Write(0, routingProtocolLowPowerAndLossyNetworks.Addresses[routingProtocolLowPowerAndLossyNetworksAddressIndex], Endianity.Big);
//
//                                    byte[] fullAddressBytes = destinationAddressBytes.Subsegment(0, commonPrefixLength).Concat(routingAddressBytes.Subsegment(commonPrefixLength, IpV6Address.SizeOf - commonPrefixLength)).ToArray();
//                                    IpV6Address expectedFullAddress = fullAddressBytes.ReadIpV6Address(0, Endianity.Big);
//
//                                    Assert.AreEqual(expectedFullAddress, actualFullAddress);
//                                }

                        ++routingProtocolLowPowerAndLossyNetworksAddressIndex;
                        break;

                    default:
                        throw new InvalidOperationException("Invalid IPv6 routing source route field " + headerField.Name());
                    }
                }
                break;

            case "ipv6.dst_opt":
                if (_currentExtensionHeaderIndex >= ipV6Datagram.ExtensionHeaders.Headers.Count)
                {
                    int expectedExtensionHeaderLength = (int.Parse(field.Fields().Skip(1).First().Value(), NumberStyles.HexNumber) + 1) * 8;
                    int actualMaxPossibleLength       = ipV6Datagram.RealPayloadLength -
                                                        ipV6Datagram.ExtensionHeaders.Take(_currentExtensionHeaderIndex).Sum(
                        extensionHeader => extensionHeader.Length);
                    MoreAssert.IsSmaller(expectedExtensionHeaderLength, actualMaxPossibleLength);
                    return(false);
                }
                IpV6ExtensionHeaderDestinationOptions destinationOptions = (IpV6ExtensionHeaderDestinationOptions)ipV6Datagram.ExtensionHeaders[_currentExtensionHeaderIndex];
                IncrementCurrentExtensionHeaderIndex(ipV6Datagram);
                CompareOptions(field, ref optionsIndex, ipV6Datagram, destinationOptions);
                break;

            case "ipv6.shim6":
                // TODO: Implement Shim6.
                IpV4Protocol nextHeader = _currentExtensionHeaderIndex > 0
                                                  ? ipV6Datagram.ExtensionHeaders[_currentExtensionHeaderIndex - 1].NextHeader.Value
                                                  : ipV6Datagram.NextHeader;
                Assert.IsTrue(nextHeader == IpV4Protocol.Shim6);
                break;

            case "ipv6.unknown_hdr":
                Assert.AreEqual(ipV6Datagram.ExtensionHeaders.Count(), _currentExtensionHeaderIndex);
                // TODO: Fix according to https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9996
                return(false);

            case "ipv6.src_sa_mac":
            case "ipv6.dst_sa_mac":
            case "ipv6.sa_mac":
            case "ipv6.dst_6to4_gw_ipv4":
            case "ipv6.dst_6to4_sla_id":
                // TODO: Understand how these are calculated.
                break;

            case "":
                switch (field.Show())
                {
                case "Fragmentation Header":
                    if (_currentExtensionHeaderIndex >= ipV6Datagram.ExtensionHeaders.Headers.Count && !ipV6Datagram.IsValid)
                    {
                        return(false);
                    }
                    IpV6ExtensionHeaderFragmentData fragmentData =
                        (IpV6ExtensionHeaderFragmentData)ipV6Datagram.ExtensionHeaders[_currentExtensionHeaderIndex];
                    IncrementCurrentExtensionHeaderIndex(ipV6Datagram);
                    foreach (var headerField in field.Fields())
                    {
                        switch (headerField.Name())
                        {
                        case "ipv6.fragment.nxt":
                            headerField.AssertValue((byte)fragmentData.NextHeader.Value);
                            break;

                        case "ipv6.fragment.offset":
                            headerField.AssertShowDecimal(fragmentData.FragmentOffset);
                            break;

                        case "ipv6.fragment.more":
                            headerField.AssertShowDecimal(fragmentData.MoreFragments);
                            break;

                        case "ipv6.fragment.id":
                            headerField.AssertShowDecimal(fragmentData.Identification);
                            break;

                        case "ipv6.fragment.reserved_octet":
                        case "ipv6.fragment.reserved_bits":
                            headerField.AssertShowDecimal(0);
                            break;

                        default:
                            throw new InvalidOperationException("Invalid ipv6 fragmentation field " + headerField.Name());
                        }
                    }
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Invalid ipv6 field {0}", field.Show()));
                }
                break;

            default:
                throw new InvalidOperationException(string.Format("Invalid ipv6 field {0}", field.Name()));
            }

            return(true);
        }