<commit_before>from unittest import *
from sniffles.ruletrafficgenerator import *
from sniffles.vendor_mac_list import VENDOR_MAC_OUI


class TestRuleTrafficGenerator(TestCase):
    def test_build_random_ethernet_header(self):
        random.seed()
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        self.assertIn(myehdr.get_d_mac()[0:3], VENDOR_MAC_OUI)
        self.assertIn(myehdr.get_s_mac()[0:3], VENDOR_MAC_OUI)
        myehdrstr1 = str(myehdr)
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        myehdrstr2 = str(myehdr)
        self.assertEqual(myehdrstr1, myehdrstr2)
        self.assertEqual(((myehdr.get_ether_type() >> 8) & 0xff), 0x08)
        self.assertEqual((myehdr.get_ether_type() & 0xff), 0x00)
        myehdr.clear_globals()
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        self.assertNotEqual(myehdrstr1, str(myehdr))

    def test_build_ethernet_header_dist(self):
        for i in range(0, 1000):
            myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                                   ETHERNET_HDR_GEN_DISTRIBUTION,
                                   'examples/mac_definition_file.txt')
            self.assertEqual(''.join(['%02x' % i
                                      for i in myehdr.get_d_mac()[0:2]]),
                             '0080')

            self.assertEqual(''.join(['%02x' % i
                                      for i in myehdr.get_s_mac()[0:2]]),
                             '0080')
            mystr1 = str(myehdr)
            myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                                   ETHERNET_HDR_GEN_DISTRIBUTION,
                                   'examples/mac_definition_file.txt')
            self.assertEqual(mystr1, str(myehdr))
            myehdr.clear_globals()
            myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                                   ETHERNET_HDR_GEN_DISTRIBUTION,
                                   'examples/mac_definition_file.txt')
            self.assertNotEqual(mystr1, str(myehdr))

    def test_build_ip_header(self):
        myipv4a = IPV4(None, None)
        myipv4b = IPV4(myipv4a.get_sip(), myipv4a.get_dip())<commit_msg>Add a unit test for get_dist_mac_oui<commit_after>from unittest import *
from sniffles.ruletrafficgenerator import *
from sniffles.vendor_mac_list import VENDOR_MAC_OUI


class TestRuleTrafficGenerator(TestCase):
    def test_build_random_ethernet_header(self):
        random.seed()
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        self.assertIn(myehdr.get_d_mac()[0:3], VENDOR_MAC_OUI)
        self.assertIn(myehdr.get_s_mac()[0:3], VENDOR_MAC_OUI)
        myehdrstr1 = str(myehdr)
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        myehdrstr2 = str(myehdr)
        self.assertEqual(myehdrstr1, myehdrstr2)
        self.assertEqual(((myehdr.get_ether_type() >> 8) & 0xff), 0x08)
        self.assertEqual((myehdr.get_ether_type() & 0xff), 0x00)
        myehdr.clear_globals()
        myehdr = EthernetFrame('10.0.0.1', '10.1.1.1', ETHERNET_HDR_GEN_RANDOM)
        self.assertNotEqual(myehdrstr1, str(myehdr))

    def test_build_ethernet_header_dist(self):
        myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                               ETHERNET_HDR_GEN_DISTRIBUTION,
                               'examples/mac_definition_file.txt')
        self.assertEqual(''.join(['%02x' % i
                                  for i in myehdr.get_d_mac()[0:2]]),
                         '0080')

        self.assertEqual(''.join(['%02x' % i
                                  for i in myehdr.get_s_mac()[0:2]]),
                         '0080')
        mystr1 = str(myehdr)
        myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                               ETHERNET_HDR_GEN_DISTRIBUTION,
                               'examples/mac_definition_file.txt')
        self.assertEqual(mystr1, str(myehdr))
        myehdr.clear_globals()
        myehdr = EthernetFrame('10.2.2.2', '10.3.3.3',
                               ETHERNET_HDR_GEN_DISTRIBUTION,
                               'examples/mac_definition_file.txt')
        self.assertNotEqual(mystr1, str(myehdr))

    def test_get_dist_mac_oui_with_empty_dist(self):
        ef = EthernetFrame('10.2.2.2', '10.3.3.3',
                           ETHERNET_HDR_GEN_DISTRIBUTION)
        with self.assertRaises(KeyError):
            ef.get_dist_mac_oui()

    def test_build_ip_header(self):
        myipv4a = IPV4(None, None)
        myipv4b = IPV4(myipv4a.get_sip(), myipv4a.get_dip())
{'fraction_non_alphanumeric': 0.12111468381564845, 'fraction_numerical': 0.04737406216505895, 'mean_word_length': 2.004507405022537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 26, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1779435', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1653, 'n_words': 204}