def test_udp_block_single_port_outgoing(self):
        rules = Rules(block_single_port)

        binary_packet = BinaryPacket()

        binary_packet.udp_dest = 52 # The rule shouldn't apply here
        packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_udp_packet(), geoDB=None)
        result = rules.result_for_pkt(packet)
        self.assertEqual(RULE_RESULT_PASS, result)

        binary_packet.udp_dest = 53 # The rule should apply here
        packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_udp_packet(), geoDB=None)
        result = rules.result_for_pkt(packet)
        self.assertEqual(RULE_RESULT_DROP, result)
 def test_dns_outgoing_block_google(self):
     rules = Rules(block_google_rules)
     binary_packet = BinaryPacket()
     binary_packet.dns_question = "www.google.com"
     binary_packet.udp_dest = 53
     packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_dns_packet(), geoDB=None)
     result = rules.result_for_pkt(packet)
     self.assertEqual(RULE_RESULT_DROP, result)
    def test_udp_block_port_range_outgoing(self):
        rules = Rules(block_port_range_rules)

        binary_packet = BinaryPacket()
        port_unblocked_range = range(0, 1000) + range(2001, 3001)
        port_blocked_range = range(1000, 2001)

        for port in port_unblocked_range:
            binary_packet.udp_dest = port # The rule shouldn't apply here
            packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_udp_packet(), geoDB=None)
            result = rules.result_for_pkt(packet)
            self.assertEqual(RULE_RESULT_PASS, result)

        for port in port_blocked_range:
            binary_packet.udp_dest = port # The rule should apply here
            packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_udp_packet(), geoDB=None)
            result = rules.result_for_pkt(packet)
            self.assertEqual(RULE_RESULT_DROP, result)
    def test_dns_outgoing_requires_qclass_one(self):
        rules = Rules(block_google_rules)
        binary_packet = BinaryPacket()
        binary_packet.dns_question = "www.google.com"
        binary_packet.udp_dest = 53

        # If the query type is 1 or 28 it should be okay
        for qclass in range(1, 256):
            binary_packet.dns_qclass = qclass
            packet = Packet(pkt_dir=PKT_DIR_OUTGOING, pkt=binary_packet.get_dns_packet(), geoDB=None)
            result = rules.result_for_pkt(packet)
            if qclass == 1:
                self.assertEqual(RULE_RESULT_DROP, result)
            else:
                # The rule wont apply
                self.assertEqual(RULE_RESULT_PASS, result)