public void MacAddressWithBufferTest()
        {
            Random random = new Random();
            MacAddress address = random.NextMacAddress();

            byte[] buffer = new byte[MacAddress.SizeOf];

            buffer.Write(0, address, Endianity.Big);
            Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
            Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Small));

            int offset = 0;
            buffer.Write(ref offset, address, Endianity.Big);
            Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
            Assert.AreEqual(6, offset);

            offset = 0;
            Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Big));
            Assert.AreEqual(MacAddress.SizeOf, offset);

            buffer.Write(0, address, Endianity.Small);
            Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
            Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Big));

            offset = 0;
            buffer.Write(ref offset, address, Endianity.Small);
            Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
            Assert.AreEqual(6, offset);

            offset = 0;
            Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Small));
            Assert.AreEqual(MacAddress.SizeOf, offset);
        }
Beispiel #2
0
        public void RandomArpTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                MacAddress ethernetSource = random.NextMacAddress();
                EthernetLayer ethernetLayer = new EthernetLayer
                                                  {
                                                      Source = ethernetSource,
                                                  };

                ArpLayer arpLayer = random.NextArpLayer();

                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, arpLayer);

                Assert.IsTrue(packet.IsValid, "IsValid");

                // Ethernet
                Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");

                Assert.AreEqual(ethernetSource, packet.Ethernet.Source, "Ethernet Source");
                Assert.AreEqual(EthernetDatagram.BroadcastAddress, packet.Ethernet.Destination, "Ethernet Destination");
                Assert.AreEqual(EthernetType.Arp, packet.Ethernet.EtherType, "Ethernet EtherType");

                // Arp
                Assert.AreEqual(ArpDatagram.HeaderBaseLength + 2 * arpLayer.SenderHardwareAddress.Count + 2 * arpLayer.SenderProtocolAddress.Count, packet.Ethernet.Arp.Length, "Arp length");
                Assert.AreEqual(ArpHardwareType.Ethernet, packet.Ethernet.Arp.HardwareType, "Arp hardware type");
                Assert.AreEqual(arpLayer, packet.Ethernet.Arp.ExtractLayer(), "ARP Layer");
                Assert.AreNotEqual(arpLayer, random.NextArpLayer(), "ARP Layer");
                Assert.AreEqual(arpLayer.GetHashCode(), packet.Ethernet.Arp.ExtractLayer().GetHashCode(), "ARP Layer");
                ArpLayer differentArpLayer = random.NextArpLayer();
                Assert.AreNotEqual(arpLayer.GetHashCode(), differentArpLayer.GetHashCode(), "ARP Layer");
            }
        }
        public void MacAddressTest()
        {
            Random random = new Random();

            for (int i = 0; i != 1000; ++i)
            {
                MacAddress macAddress = random.NextMacAddress();

                Assert.IsNotNull(macAddress.ToString());
                Assert.AreEqual(macAddress, new MacAddress(macAddress.ToString()));
                Assert.AreNotEqual(macAddress, random.NextMacAddress());
                Assert.IsTrue(macAddress == new MacAddress(macAddress.ToString()));
                Assert.AreEqual(macAddress.GetHashCode(), new MacAddress(macAddress.ToString()).GetHashCode());
                Assert.IsTrue(macAddress != random.NextMacAddress());
                Assert.AreNotEqual(macAddress.GetHashCode(), random.NextMacAddress().GetHashCode());
            }
        }
        public void SetSamplingMethodFirstAfterIntervalTest()
        {
            Random random = new Random();

            MacAddress sourceMac = random.NextMacAddress();
            MacAddress destinationMac = random.NextMacAddress();

            using (PacketCommunicator communicator = OpenLiveDevice())
            {
                communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
                communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
                int numPacketsGot;
                communicator.ReceiveSomePackets(out numPacketsGot, 100, p => { });

                Packet[] packetsToSend = new Packet[11];
                packetsToSend[0] = _random.NextEthernetPacket(60, sourceMac, destinationMac);
                for (int i = 0; i != 10; ++i)
                    packetsToSend[i + 1] = _random.NextEthernetPacket(60 * (i + 2), sourceMac, destinationMac);

                List<Packet> packets = new List<Packet>(6);
                Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6)));
                thread.Start();

                communicator.SendPacket(packetsToSend[0]);
                Thread.Sleep(TimeSpan.FromSeconds(0.7));
                for (int i = 0; i != 10; ++i)
                {
                    communicator.SendPacket(packetsToSend[i + 1]);
                    Thread.Sleep(TimeSpan.FromSeconds(0.55));
                }

                if (!thread.Join(TimeSpan.FromSeconds(10)))
                    thread.Abort();
                Assert.AreEqual(6, packets.Count, packets.Select(p => (p.Timestamp-packets[0].Timestamp).TotalSeconds + "(" + p.Length + ")").SequenceToString(", "));
                Packet packet;
                for (int i = 0; i != 6; ++i)
                    Assert.AreEqual(60 * (i * 2 + 1), packets[i].Length, i.ToString());
                PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
                Assert.IsNull(packet);
            }
        }