import unittest
from python_utils.common import exceptions
from python_utils.net import pcap_funcs
from python_utils.tests.utils.test_utils import run_unit_test


class PCAPFuncsTest(unittest.TestCase):
    def test_start_cap_no_iface(self):
        pcap = pcap_funcs.Pcap('blarg')
        self.assertRaises(exceptions.SubprocessFailedException,
                          pcap.start_capture)


run_unit_test(PCAPFuncsTest)
             0x0a, 0x00, 0x02, 0x02, 0x00, 0x16, 0xd1, 0xf4,
             0x52, 0x1a, 0x58, 0x7c, 0x58, 0x25, 0x2e, 0x9b,
             0x50, 0x18, 0x9f, 0xb0, 0x18, 0x5f, 0x00, 0x00,
             0x00, 0x00, 0x00]

        packet = pcap_packet.PCAPPacket(full_eii_packet_data, '13:00')
        self.assertRaises(exceptions.PacketParsingException, packet.parse,
                          [pcap_packet.PCAPSLL])

        pmap = packet.layer_data
        emap = packet.extra_data

        self.assertEqual(1, len(emap['parse_errors.ip']))
        self.assertRegexpMatches(emap['parse_errors.ip'][0],
                                 'field must be at least')

        self.assertEqual('13:00', packet.timestamp)
        self.assertTrue('ethernet' in pmap)

        self.assertEqual(pcap_packet.PCAPSLL, type(pmap['ethernet']))

        ether_pmap = pmap['ethernet']
        """ :type: pcap_packet.PCAPEthernet """
        self.assertEqual('08:00:27:c6:25:01', ether_pmap.source_mac)
        self.assertEqual('00:00:00:00:00:00', ether_pmap.dest_mac)
        self.assertEqual(pcap_packet.PCAPIP4,
                         pmap['ethernet'].next_parse_recommendation)


run_unit_test(PCAPPacketTest)
Exemplo n.º 3
0
        self.assertFalse(
            string_utils.check_string_for_tag("foo foo baz", "foo", 1))
        self.assertFalse(
            string_utils.check_string_for_tag("foo foo baz", "foo", 3,
                                              exact=False))
        self.assertFalse(
            string_utils.check_string_for_tag("foo bar baz", "bar", 2,
                                              exact=False))
        self.assertFalse(
            string_utils.check_string_for_tag("foo barfoo barfoo",
                                              "foo barfoo", 2,
                                              exact=False))

        self.assertTrue(
            string_utils.check_string_for_tag("foo bar baz", "bamf", 0))
        self.assertTrue(
            string_utils.check_string_for_tag("foo bar baz", "bamf", 0,
                                              exact=False))
        self.assertTrue(
            string_utils.check_string_for_tag("foo bar baz", "foo", 0,
                                              exact=False))

        self.assertFalse(
            string_utils.check_string_for_tag("foo bar baz", "foo", 0))
        self.assertFalse(
            string_utils.check_string_for_tag("foo bar baz", "bar", 0))
        self.assertFalse(
            string_utils.check_string_for_tag("foo bar baz", "baz", 0))

run_unit_test(UtilsTest)
Exemplo n.º 4
0
                pcap_rules.NotEqual('ip[0] & 0xf', '5'),
                pcap_rules.LessThanEqual('len', '1500')
            ])
        ])

        expected_str = \
            '( ' \
            '( not ( ether multicast ) ) and ' \
            '( ' \
            '( ether src host bar ) or ' \
            '( net 192.168.0.0 mask 255.255.255.0 ) ' \
            ') and ' \
            '( len > 80 ) ' \
            ') and ' \
            '( dst port 80 ) and ' \
            '( ' \
            '( src portrange 30000-50000 ) or ' \
            '( ip proto \\tcp ) or ' \
            '( not ( ether broadcast ) ) ' \
            ') and ' \
            '( ' \
            '( ether[0] & 1 != 0 ) and ' \
            '( ip[0] & 0xf != 5 ) and ' \
            '( len <= 1500 ) ' \
            ')'

        self.assertEqual(expected_str, complex_rule.to_str())


run_unit_test(PCAPTest)
Exemplo n.º 5
0
        cli = LinuxCLI()
        root_pids = cli.get_process_pids("root")
        pids = cli.get_running_pids()
        ppids = cli.get_parent_pids("1")

        self.assertTrue(len(root_pids) > 0)
        self.assertTrue(len(pids) > 0)
        self.assertTrue(len(ppids) > 0)

    def test_grep_count(self):
        cli = LinuxCLI()
        try:
            cli.write_to_file('test-grep-count',
                              'foo\nfoo\nbar\nfoo bar\nfoo foo\n')
            count = cli.grep_count('cat test-grep-count', 'foo')
            self.assertEqual(4, count)
            count2 = cli.grep_count('cat test-grep-count', 'baz')
            self.assertEqual(0, count2)
            count3 = cli.grep_count(['cat', 'test-grep-count'], 'foo')
            self.assertEqual(4, count3)
            count4 = cli.grep_count(['cat', 'test-grep-count'], 'baz')
            self.assertEqual(0, count4)
        finally:
            cli.rm('test-grep-count')

    def tearDown(self):
        LinuxCLI().rm('tmp-test')


run_unit_test(CLITest)
Exemplo n.º 6
0
        self.assertEqual('fooout', final_result)

        cl = MPThreadTestFuncs()
        thr2 = mp_thread.MPThread(cl.func_class_set, bar='foo')
        thr2.start()
        midthread_result = thr2.get_data()
        self.assertEqual('foo', midthread_result)
        final_result = thr2.join()
        self.assertEqual('fooout', final_result)

        thr3 = mp_thread.MPThread(cl.func_class_static_set, bar='foo')
        thr3.start()
        midthread_result = thr3.get_data()
        self.assertEqual('foo', midthread_result)
        final_result = thr3.join()
        self.assertEqual('fooout', final_result)

        self._thread = mp_thread.MPThread(self.selfThread, bar='foo')
        self._thread.start()
        midthread_result = self._thread.get_data()
        self.assertEqual('foo', midthread_result)
        final_result = self._thread.join()
        self.assertEqual('fooout', final_result)

    def selfThread(self, bar=None):
        self._thread.put_data('foo')
        return bar + 'out'


run_unit_test(MPThreadTest)