Example #1
0
    def test_div(self):
        """Test the Scapy-style chain construction syntax."""

        # Create a bunch of individual packets.
        a = ethernet.ethernet()
        b = ipv4.ipv4()
        c = udpv4.udpv4(sport=1234)
        d = dhcpv4.dhcpv4()

        # Pack them into chains, assert that their properties still hold.
        x = a / b
        self.assertEqual(len(x.packets), 2, "x does not have 2 packets")
        self.assertEqual(x.packets[0].type, 0x0800, "x[0].type is not ETHERTYPE_IP")

        # XXX The link in the existing chain must be broken for these
        # invariants to hold; the semantics of chains have changed therefore
        # these need to be updated.
        # y = b / c
        # self.assertEqual(len(y.packets), 2, "y does not have 2 packets")
        # self.assertEqual(y.packets[0].protocol, 17, \
        #                 "y.packets[0].protocol is not UDP")

        # z = c / d
        # self.assertEqual(len(z.packets), 2, "z does not have 2 packets")
        # self.assertEqual(z.packets[0].sport, 1234, "z.packets[0].sport is not 1234")
        # self.assertEqual(z.packets[0].dport, 67, "z.packets[0].dport is not 67")

        # All together now.
        alpha = ethernet.ethernet() / ipv4.ipv4() / udpv4.udpv4(sport=1234) / dhcpv4.dhcpv4()
        self.assertEqual(len(alpha.packets), 4, "alpha does not have 4 packets")
        self.assertEqual(alpha.packets[0].type, 0x0800, "alpha.packets[0].type is not ETHERTYPE_IP")
        self.assertEqual(alpha.packets[1].protocol, 17, "alpha.packets[1].protocol is not UDP")
        self.assertEqual(alpha.packets[2].sport, 1234, "alpha.packets[2].sport is not 1234")
        self.assertEqual(alpha.packets[2].dport, 67, "alpha.packets[2].dport is not 67")
Example #2
0
    def test_dns_compare(self):
        """Test the underlying __compare__ functionality of the
        packet.  Two packets constructed from the same bytes should be
        equal and two that are not should not be equal."""
        file = pcs.PcapConnector("dns.out")
        packet = file.readpkt()
        ip = packet.data
        assert (ip != None)
        udp1 = udpv4(ip.data.bytes)
        udp2 = udpv4(ip.data.bytes)
        assert (udp1 != None)
        assert (udp2 != None)
        self.assertEqual(udp1, udp2, "packets should be equal but are not")

        udp1.dport = 0xffff
        self.assertNotEqual(udp1, udp2, "packets compare equal but should not\ngot %sexpect %s" % (udp1, udp2))
Example #3
0
    def test_dns_compare(self):
        """Test the underlying __compare__ functionality of the
        packet.  Two packets constructed from the same bytes should be
        equal and two that are not should not be equal."""
        file = pcs.PcapConnector("dns.out")
        packet = file.readpkt()
        ip = packet.data
        assert (ip != None)
        udp1 = udpv4(ip.data.bytes)
        udp2 = udpv4(ip.data.bytes)
        assert (udp1 != None)
        assert (udp2 != None)
        self.assertEqual(udp1, udp2, "packets should be equal but are not")

        udp1.dport = 0xffff
        self.assertNotEqual(
            udp1, udp2,
            "packets compare equal but should not\ngot %sexpect %s" %
            (udp1, udp2))
Example #4
0
    def test_div(self):
        """Test the Scapy-style chain construction syntax."""

        # Create a bunch of individual packets.
        a = ethernet.ethernet()
        b = ipv4.ipv4()
        c = udpv4.udpv4(sport=1234)
        d = dhcpv4.dhcpv4()

        # Pack them into chains, assert that their properties still hold.
        x = a / b
        self.assertEqual(len(x.packets), 2, "x does not have 2 packets")
        self.assertEqual(x.packets[0].type, 0x0800, \
                         "x[0].type is not ETHERTYPE_IP")

        # XXX The link in the existing chain must be broken for these
        # invariants to hold; the semantics of chains have changed therefore
        # these need to be updated.
        #y = b / c
        #self.assertEqual(len(y.packets), 2, "y does not have 2 packets")
        #self.assertEqual(y.packets[0].protocol, 17, \
        #                 "y.packets[0].protocol is not UDP")

        #z = c / d
        #self.assertEqual(len(z.packets), 2, "z does not have 2 packets")
        #self.assertEqual(z.packets[0].sport, 1234, "z.packets[0].sport is not 1234")
        #self.assertEqual(z.packets[0].dport, 67, "z.packets[0].dport is not 67")

        # All together now.
        alpha = ethernet.ethernet() / ipv4.ipv4() / udpv4.udpv4(sport=1234) / \
                dhcpv4.dhcpv4()
        self.assertEqual(len(alpha.packets), 4,
                         "alpha does not have 4 packets")
        self.assertEqual(alpha.packets[0].type, 0x0800, \
                         "alpha.packets[0].type is not ETHERTYPE_IP")
        self.assertEqual(alpha.packets[1].protocol, 17, \
                         "alpha.packets[1].protocol is not UDP")
        self.assertEqual(alpha.packets[2].sport, 1234, \
                         "alpha.packets[2].sport is not 1234")
        self.assertEqual(alpha.packets[2].dport, 67, \
                         "alpha.packets[2].dport is not 67")