Best Python code snippet using autotest_python
net_utils_unittest.py
Source:net_utils_unittest.py
...770 def test_ethernet_mac_binary_to_string(self):771 mac_str = net_utils.ethernet.mac_binary_to_string(772 '\x00\x01\x02\x03\x04\x05')773 self.assertEqual(mac_str, '00:01:02:03:04:05')774 def test_ethernet_pack(self):775 dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')776 src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')777 protocol = 2030778 payload = 'some payload'779 frame = struct.pack("!6s6sH", dst, src, protocol) + payload780 self.assertEquals(net_utils.ethernet.pack(dst, src,protocol, payload),781 frame)782 def test_ethernet_unpack(self):783 dst = net_utils.ethernet.mac_string_to_binary('00:01:02:03:04:05')784 src = net_utils.ethernet.mac_string_to_binary('16:17:18:19:1A:1B')785 protocol = 2030786 payload = 'some payload'787 frame = net_utils.ethernet.pack(dst, src, protocol, payload)788 uframe = net_utils.ethernet.unpack(frame)...
test_network_types.py
Source:test_network_types.py
1"""Test Python-openflow network types."""2import unittest3from pyof.foundation.basic_types import BinaryData4from pyof.foundation.exceptions import UnpackException5from pyof.foundation.network_types import ARP, VLAN, Ethernet, GenericTLV, IPv46class TestARP(unittest.TestCase):7 """Test ARP packets, without Ethernet headers."""8 def test_arp_pack(self):9 """Test pack method of ARP class."""10 arp = ARP(oper=1, sha='00:15:af:d5:38:98', spa='172.16.0.10',11 tpa='172.16.10.20')12 packed = arp.pack()13 expected = b'\x00\x01\x08\x00\x06\x04\x00\x01\x00\x15\xaf\xd58\x98\xac'14 expected += b'\x10\x00\n\x00\x00\x00\x00\x00\x00\xac\x10\n\x14'15 self.assertEqual(packed, expected)16 def test_arp_unpack(self):17 """Test unpack method of ARP class."""18 raw = b'\x00\x01\x08\x00\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n'19 raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n'20 expected = ARP(oper=2, sha='00:1f:3a:3e:9a:cf', spa='172.16.10.20',21 tha='00:15:af:d5:38:98', tpa='172.16.0.10')22 unpacked = ARP()23 unpacked.unpack(raw)24 self.assertEqual(unpacked, expected)25 def test_unpack_invalid_htype(self):26 """Raise UnpackException when L2 protocol is not Ethernet."""27 raw = b'\x01\x23\x08\x00\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n'28 raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n'29 arp = ARP()30 with self.assertRaises(UnpackException):31 arp.unpack(raw)32 def test_unpack_invalid_ptype(self):33 """Raise UnpackException when L3 protocol is not IPv4."""34 raw = b'\x00\x01\x08\x90\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n'35 raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n'36 arp = ARP()37 with self.assertRaises(UnpackException):38 arp.unpack(raw)39class TestNetworkTypes(unittest.TestCase):40 """Reproduce bugs found."""41 def test_GenTLV_value_unpack(self):42 """Value attribute should be the same after unpacking."""43 value = BinaryData(b'test')44 tlv = GenericTLV(value=value)45 tlv_unpacked = GenericTLV()46 tlv_unpacked.unpack(tlv.pack())47 self.assertEqual(tlv.value.value, tlv_unpacked.value.value)48class TestEthernet(unittest.TestCase):49 """Test Ethernet frames."""50 def test_Ethernet_pack(self):51 """Test pack method of Ethernet class without VLAN tag."""52 ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf',53 source='00:15:af:d5:38:98', ether_type=0x800,54 data=b'testdata')55 packed = ethernet.pack()56 expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58\x98\x08\x00testdata'57 self.assertEqual(packed, expected)58 def test_Ethernet_unpack(self):59 """Test pack method of Ethernet class without VLAN tag."""60 raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata'61 expected = Ethernet(destination='00:15:af:d5:38:98',62 source='00:1f:3a:3e:9a:cf', ether_type=0x800,63 data=b'testdata')64 expected.pack()65 unpacked = Ethernet()66 unpacked.unpack(raw)67 self.assertEqual(unpacked, expected)68 def test_Tagged_Ethernet_pack(self):69 """Test pack method of Ethernet class including VLAN tag."""70 ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf',71 source='00:15:af:d5:38:98', vlans=[VLAN(vid=200)],72 ether_type=0x800, data=b'testdata')73 packed = ethernet.pack()74 expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58'75 expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata'76 self.assertEqual(packed, expected)77 def test_Tagged_Ethernet_unpack(self):78 """Test pack method of Ethernet class including VLAN tag."""79 raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>'80 raw += b'\x9a\xcf\x81\x00!^\x08\x00testdata'81 expected = Ethernet(destination='00:15:af:d5:38:98',82 source='00:1f:3a:3e:9a:cf', vlans=[VLAN(pcp=1,83 vid=350)],84 ether_type=0x800, data=b'testdata')85 expected.pack()86 unpacked = Ethernet()87 unpacked.unpack(raw)88 self.assertEqual(unpacked, expected)89class TestVLAN(unittest.TestCase):90 """Test VLAN headers."""91 def test_VLAN_pack(self):92 """Test pack method of VLAN class."""93 vlan = VLAN(pcp=3, vid=20)94 packed = vlan.pack()95 expected = b'\x81\x00`\x14'96 self.assertEqual(packed, expected)97 def test_VLAN_unpack(self):98 """Test unpack method of VLAN class."""99 raw = b'\x81\x00\xa0{'100 expected = VLAN(pcp=5, vid=123)101 unpacked = VLAN()102 unpacked.unpack(raw)103 self.assertEqual(unpacked, expected)104 def test_unpack_wrong_tpid(self):105 """Raise UnpackException if the tpid is not VLAN_TPID."""106 raw = b'\x12\x34\xa0{'107 vlan = VLAN()108 with self.assertRaises(UnpackException):109 vlan.unpack(raw)110class TestIPv4(unittest.TestCase):111 """Test IPv4 packets."""112 def test_IPv4_pack(self):113 """Test pack/unpack of IPv4 class."""114 packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",115 destination="172.16.10.30", options=b'1000',116 data=b'testdata')117 packed = packet.pack()118 expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02'119 expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata'120 self.assertEqual(packed, expected)121 def test_IPv4_unpack(self):122 """Test unpack of IPv4 binary packet."""123 raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W'124 raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata'125 expected = IPv4(dscp=20, ttl=128, protocol=6, source="10.154.78.129",126 destination="192.168.199.204", options=b'1000',127 data=b'somemoredata')128 expected.pack()129 unpacked = IPv4()130 unpacked.unpack(raw)131 self.assertEqual(unpacked, expected)132 def test_IPv4_size(self):133 """Test Header size for IPv4 packet."""134 packet = IPv4()135 packet.pack()136 self.assertEqual(20, packet.get_size())137 self.assertEqual(20, packet.length)138 self.assertEqual(20, packet.ihl * 4)139 def test_IPv4_checksum(self):140 """Test if the IPv4 checksum is being calculated correclty."""141 packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",142 destination="172.16.10.30", options=b'1000',143 data=b'testdata')144 packet.pack()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!