Esempio n. 1
0
        public void RandomVLanTaggedFrameTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None);
                VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                int payloadLength = random.Next(1500);
                PayloadLayer payloadLayer = new PayloadLayer
                {
                    Data = random.NextDatagram(payloadLength),
                };
                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer, payloadLayer);

                ethernetLayer.EtherType = EthernetType.VLanTaggedFrame;

                // Test output.
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer());
                Assert.AreEqual(vLanTaggedFrameLayer.GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreNotEqual(random.NextVLanTaggedFrameLayer().GetHashCode(), packet.Ethernet.VLanTaggedFrame.ExtractLayer().GetHashCode());
                Assert.AreEqual(vLanTaggedFrameLayer.TagControlInformation, packet.Ethernet.VLanTaggedFrame.TagControlInformation);
                Assert.AreEqual(payloadLayer.Data, packet.Ethernet.VLanTaggedFrame.Payload);
            }
        }
        public void DontAutoSetEthernetDestinationTest()
        {
            Random random = new Random();
            EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None);
            ethernetLayer.Destination = MacAddress.Zero;
            VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
            Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer);

            ethernetLayer.EtherType = EthernetType.VLanTaggedFrame;

            // Test equality.
            Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer());
            Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer());
        }
Esempio n. 3
0
        public void AutoSetEtherTypeTest()
        {
            Random random = new Random();
            EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None);
            VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer(EthernetType.None);
            IpV4Layer ipV4Layer = random.NextIpV4Layer();
            Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer, ipV4Layer);

            ethernetLayer.EtherType = EthernetType.VLanTaggedFrame;
            vLanTaggedFrameLayer.EtherType = EthernetType.IpV4;

            // Test equality.
            Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer());
            Assert.AreEqual(EthernetType.IpV4, packet.Ethernet.VLanTaggedFrame.EtherType);
            Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer());
            ipV4Layer.HeaderChecksum = packet.Ethernet.VLanTaggedFrame.IpV4.HeaderChecksum;
            Assert.AreEqual(ipV4Layer, packet.Ethernet.VLanTaggedFrame.IpV4.ExtractLayer());
        }
        private static void CreateRandomEthernetPayload(Random random, EthernetBaseLayer ethernetBaseLayer, List<ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            ethernetBaseLayer.EtherType = EthernetType.None;
            switch (random.NextInt(0, 7))
            {
                case 0: // VLanTaggedFrame.
                case 1:
                    VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                    layers.Add(vLanTaggedFrameLayer);
                    CreateRandomEthernetPayload(random, vLanTaggedFrameLayer, layers);
                    return;

                case 2: // ARP.
                    EthernetLayer ethernetLayer = (ethernetBaseLayer as EthernetLayer);
                    if (ethernetLayer != null)
                        ethernetLayer.Destination = MacAddress.Zero;
                    layers.Add(random.NextArpLayer());
                    return;

                case 3: // IPv4.
                case 4:
                    IpV4Layer ipV4Layer = random.NextIpV4Layer();
                    layers.Add(ipV4Layer);
                    CreateRandomIpPayload(random, ipV4Layer, layers);
                    return;

                case 5: // IPv6
                case 6:
                    IpV6Layer ipV6Layer = random.NextIpV6Layer(random.NextBool(20));
                    layers.Add(ipV6Layer);
                    CreateRandomIpPayload(random, ipV6Layer, layers);
                    return;

                default:
                    throw new InvalidOperationException("Invalid value.");
            }
        }